Added support for switch statements in sequence diagrams

This commit is contained in:
Bartek Kryza
2022-12-13 21:09:00 +01:00
parent 2d72d98234
commit bd61a1540e
16 changed files with 373 additions and 33 deletions

View File

@@ -144,6 +144,51 @@ std::string to_string(const clang::RecordType &type,
return to_string(type.desugar(), ctx, try_canonical);
}
std::string to_string(const clang::Expr *expr)
{
clang::LangOptions lang_options;
std::string result;
llvm::raw_string_ostream ostream(result);
expr->printPretty(ostream, NULL, clang::PrintingPolicy(lang_options));
return result;
}
std::string to_string(const clang::Stmt *stmt)
{
clang::LangOptions lang_options;
std::string result;
llvm::raw_string_ostream ostream(result);
stmt->printPretty(ostream, NULL, clang::PrintingPolicy(lang_options));
return result;
}
std::string to_string(const clang::FunctionTemplateDecl *decl)
{
std::vector<std::string> template_parameters;
// Handle template function
for (const auto *parameter : *decl->getTemplateParameters()) {
if (clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parameter)) {
const auto *template_type_parameter =
clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parameter);
std::string template_parameter{
template_type_parameter->getNameAsString()};
if (template_type_parameter->isParameterPack())
template_parameter += "...";
template_parameters.emplace_back(std::move(template_parameter));
}
else {
// TODO
}
}
return fmt::format("{}<{}>({})", decl->getQualifiedNameAsString(),
fmt::join(template_parameters, ","), "");
}
std::string get_source_text_raw(
clang::SourceRange range, const clang::SourceManager &sm)
{

View File

@@ -62,6 +62,12 @@ std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx,
std::string to_string(const clang::RecordType &type,
const clang::ASTContext &ctx, bool try_canonical = true);
std::string to_string(const clang::Expr *expr);
std::string to_string(const clang::Stmt *stmt);
std::string to_string(const clang::FunctionTemplateDecl *decl);
std::string get_source_text_raw(
clang::SourceRange range, const clang::SourceManager &sm);

View File

@@ -100,6 +100,12 @@ std::string to_string(message_t r)
return "catch";
case message_t::kTryEnd:
return "end try";
case message_t::kSwitch:
return "switch";
case message_t::kCase:
return "case";
case message_t::kSwitchEnd:
return "end switch";
default:
assert(false);
return "";

View File

@@ -56,6 +56,9 @@ enum class message_t {
kTry,
kCatch,
kTryEnd,
kSwitch,
kCase,
kSwitchEnd,
kNone
};

View File

@@ -199,6 +199,15 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
else if (m.type() == message_t::kTryEnd) {
ostr << "end\n";
}
else if (m.type() == message_t::kSwitch) {
ostr << "group switch\n";
}
else if (m.type() == message_t::kCase) {
ostr << "else " << m.message_name() << '\n';
}
else if (m.type() == message_t::kSwitchEnd) {
ostr << "end\n";
}
}
}

View File

@@ -273,6 +273,81 @@ void diagram::add_catch_stmt(
get_activity(current_caller_id).add_message(std::move(m));
}
void diagram::add_switch_stmt(
common::model::diagram_element::id_t current_caller_id)
{
using clanguml::common::model::message_t;
if (sequences_.find(current_caller_id) == sequences_.end()) {
activity a{current_caller_id};
sequences_.insert({current_caller_id, std::move(a)});
}
message m{message_t::kSwitch, current_caller_id};
get_activity(current_caller_id).add_message(std::move(m));
}
void diagram::end_switch_stmt(
common::model::diagram_element::id_t current_caller_id)
{
using clanguml::common::model::message_t;
message m{message_t::kTryEnd, current_caller_id};
if (sequences_.find(current_caller_id) != sequences_.end()) {
auto &current_messages = get_activity(current_caller_id).messages();
if (current_messages.back().type() == message_t::kSwitch) {
current_messages.pop_back();
}
else {
current_messages.emplace_back(std::move(m));
}
}
}
void diagram::add_case_stmt(
common::model::diagram_element::id_t current_caller_id,
const std::string &case_label)
{
using clanguml::common::model::message_t;
message m{message_t::kCase, current_caller_id};
m.set_message_name(case_label);
if (sequences_.find(current_caller_id) != sequences_.end()) {
auto &current_messages = get_activity(current_caller_id).messages();
if (current_messages.back().type() == message_t::kCase) {
// Do nothing - fallthroughs not supported yet...
}
else {
current_messages.emplace_back(std::move(m));
}
}
}
void diagram::add_default_stmt(
common::model::diagram_element::id_t current_caller_id)
{
using clanguml::common::model::message_t;
message m{message_t::kCase, current_caller_id};
m.set_message_name("default");
if (sequences_.find(current_caller_id) != sequences_.end()) {
auto &current_messages = get_activity(current_caller_id).messages();
if (current_messages.back().type() == message_t::kCase) {
current_messages.pop_back();
}
else {
current_messages.emplace_back(std::move(m));
}
}
}
bool diagram::started() const { return started_; }
void diagram::started(bool s) { started_ = s; }

View File

@@ -93,14 +93,24 @@ public:
void end_loop_stmt(common::model::diagram_element::id_t current_caller_id,
common::model::message_t type);
void add_while_stmt(common::model::diagram_element::id_t i);
void end_while_stmt(common::model::diagram_element::id_t i);
void add_while_stmt(common::model::diagram_element::id_t current_caller_id);
void end_while_stmt(common::model::diagram_element::id_t current_caller_id);
void add_do_stmt(common::model::diagram_element::id_t i);
void end_do_stmt(common::model::diagram_element::id_t i);
void add_do_stmt(common::model::diagram_element::id_t current_caller_id);
void end_do_stmt(common::model::diagram_element::id_t current_caller_id);
void add_for_stmt(common::model::diagram_element::id_t i);
void end_for_stmt(common::model::diagram_element::id_t i);
void add_for_stmt(common::model::diagram_element::id_t current_caller_id);
void end_for_stmt(common::model::diagram_element::id_t current_caller_id);
void add_switch_stmt(
common::model::diagram_element::id_t current_caller_id);
void end_switch_stmt(
common::model::diagram_element::id_t current_caller_id);
void add_case_stmt(common::model::diagram_element::id_t current_caller_id);
void add_case_stmt(common::model::diagram_element::id_t current_caller_id,
const std::string &case_label);
void add_default_stmt(
common::model::diagram_element::id_t current_caller_id);
bool started() const;
void started(bool s);

View File

@@ -264,6 +264,25 @@ void call_expression_context::leave_trystmt()
try_stmt_stack_.pop();
}
clang::SwitchStmt *call_expression_context::current_switchstmt() const
{
if (switch_stmt_stack_.empty())
return nullptr;
return switch_stmt_stack_.top();
}
void call_expression_context::enter_switchstmt(clang::SwitchStmt *stmt)
{
switch_stmt_stack_.push(stmt);
}
void call_expression_context::leave_switchstmt()
{
if (switch_stmt_stack_.empty())
switch_stmt_stack_.pop();
}
bool call_expression_context::is_expr_in_current_control_statement_condition(
const clang::Stmt *stmt) const
{

View File

@@ -84,6 +84,10 @@ struct call_expression_context {
void enter_trystmt(clang::Stmt *stmt);
void leave_trystmt();
clang::SwitchStmt *current_switchstmt() const;
void enter_switchstmt(clang::SwitchStmt *stmt);
void leave_switchstmt();
bool is_expr_in_current_control_statement_condition(
const clang::Stmt *stmt) const;
@@ -105,6 +109,7 @@ private:
std::stack<clang::Stmt *> loop_stmt_stack_;
std::stack<clang::Stmt *> try_stmt_stack_;
std::stack<clang::SwitchStmt *> switch_stmt_stack_;
};
}

View File

@@ -23,31 +23,6 @@
namespace clanguml::sequence_diagram::visitor {
std::string to_string(const clang::FunctionTemplateDecl *decl)
{
std::vector<std::string> template_parameters;
// Handle template function
for (const auto *parameter : *decl->getTemplateParameters()) {
if (clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parameter)) {
const auto *template_type_parameter =
clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parameter);
std::string template_parameter{
template_type_parameter->getNameAsString()};
if (template_type_parameter->isParameterPack())
template_parameter += "...";
template_parameters.emplace_back(std::move(template_parameter));
}
else {
// TODO
}
}
return fmt::format("{}<{}>({})", decl->getQualifiedNameAsString(),
fmt::join(template_parameters, ","), "");
}
translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm,
clanguml::sequence_diagram::model::diagram &diagram,
const clanguml::config::sequence_diagram &config)
@@ -318,8 +293,9 @@ bool translation_unit_visitor::VisitCXXMethodDecl(clang::CXXMethodDecl *m)
m_ptr->is_static(m->isStatic());
for (const auto *param : m->parameters()) {
m_ptr->add_parameter(simplify_system_template(
common::to_string(param->getType(), m->getASTContext(), false)));
m_ptr->add_parameter(config().using_namespace().relative(
simplify_system_template(common::to_string(
param->getType(), m->getASTContext(), false))));
}
set_source_location(*m, *m_ptr);
@@ -777,6 +753,52 @@ bool translation_unit_visitor::TraverseCXXForRangeStmt(
return true;
}
bool translation_unit_visitor::TraverseSwitchStmt(clang::SwitchStmt *stmt)
{
const auto current_caller_id = context().caller_id();
if (current_caller_id) {
context().enter_switchstmt(stmt);
diagram().add_switch_stmt(current_caller_id);
}
RecursiveASTVisitor<translation_unit_visitor>::TraverseSwitchStmt(stmt);
if (current_caller_id) {
context().leave_switchstmt();
diagram().end_switch_stmt(current_caller_id);
}
return true;
}
bool translation_unit_visitor::TraverseCaseStmt(clang::CaseStmt *stmt)
{
const auto current_caller_id = context().caller_id();
if (current_caller_id) {
diagram().add_case_stmt(
current_caller_id, common::to_string(stmt->getLHS()));
}
RecursiveASTVisitor<translation_unit_visitor>::TraverseCaseStmt(stmt);
return true;
}
bool translation_unit_visitor::TraverseDefaultStmt(clang::DefaultStmt *stmt)
{
const auto current_caller_id = context().caller_id();
if (current_caller_id) {
diagram().add_default_stmt(current_caller_id);
}
RecursiveASTVisitor<translation_unit_visitor>::TraverseDefaultStmt(stmt);
return true;
}
bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
{
using clanguml::common::model::message_scope_t;

View File

@@ -80,6 +80,12 @@ public:
bool TraverseCXXCatchStmt(clang::CXXCatchStmt *stmt);
bool TraverseSwitchStmt(clang::SwitchStmt *stmt);
bool TraverseCaseStmt(clang::CaseStmt *stmt);
bool TraverseDefaultStmt(clang::DefaultStmt *stmt);
clanguml::sequence_diagram::model::diagram &diagram();
const clanguml::sequence_diagram::model::diagram &diagram() const;