Added support for ternary conditional operator in sequence diagrams
This commit is contained in:
@@ -106,6 +106,10 @@ std::string to_string(message_t r)
|
||||
return "case";
|
||||
case message_t::kSwitchEnd:
|
||||
return "end switch";
|
||||
case message_t::kConditional:
|
||||
return "conditional";
|
||||
case message_t::kConditionalEnd:
|
||||
return "end conditional";
|
||||
default:
|
||||
assert(false);
|
||||
return "";
|
||||
|
||||
@@ -59,6 +59,8 @@ enum class message_t {
|
||||
kSwitch,
|
||||
kCase,
|
||||
kSwitchEnd,
|
||||
kConditional,
|
||||
kConditionalEnd,
|
||||
kNone
|
||||
};
|
||||
|
||||
|
||||
@@ -208,6 +208,15 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
|
||||
else if (m.type() == message_t::kSwitchEnd) {
|
||||
ostr << "end\n";
|
||||
}
|
||||
else if (m.type() == message_t::kConditional) {
|
||||
ostr << "alt\n";
|
||||
}
|
||||
else if (m.type() == message_t::kElse) {
|
||||
ostr << "else\n";
|
||||
}
|
||||
else if (m.type() == message_t::kConditionalEnd) {
|
||||
ostr << "end\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -348,6 +348,52 @@ void diagram::add_default_stmt(
|
||||
}
|
||||
}
|
||||
|
||||
void diagram::add_conditional_stmt(
|
||||
const 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)});
|
||||
}
|
||||
|
||||
get_activity(current_caller_id)
|
||||
.add_message({message_t::kConditional, current_caller_id});
|
||||
}
|
||||
|
||||
void diagram::add_conditional_elsestmt(
|
||||
const common::model::diagram_element::id_t current_caller_id)
|
||||
{
|
||||
using clanguml::common::model::message_t;
|
||||
|
||||
get_activity(current_caller_id)
|
||||
.add_message({message_t::kElse, current_caller_id});
|
||||
}
|
||||
|
||||
void diagram::end_conditional_stmt(
|
||||
const common::model::diagram_element::id_t current_caller_id)
|
||||
{
|
||||
using clanguml::common::model::message_t;
|
||||
|
||||
message m{message_t::kConditionalEnd, current_caller_id};
|
||||
|
||||
if (sequences_.find(current_caller_id) != sequences_.end()) {
|
||||
auto ¤t_messages = get_activity(current_caller_id).messages();
|
||||
|
||||
if (current_messages.at(current_messages.size() - 1).type() ==
|
||||
message_t::kElse &&
|
||||
current_messages.at(current_messages.size() - 2).type() ==
|
||||
message_t::kConditional) {
|
||||
current_messages.pop_back();
|
||||
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; }
|
||||
|
||||
@@ -112,6 +112,13 @@ public:
|
||||
void add_default_stmt(
|
||||
common::model::diagram_element::id_t current_caller_id);
|
||||
|
||||
void add_conditional_stmt(
|
||||
common::model::diagram_element::id_t current_caller_id);
|
||||
void add_conditional_elsestmt(
|
||||
common::model::diagram_element::id_t current_caller_id);
|
||||
void end_conditional_stmt(
|
||||
common::model::diagram_element::id_t current_caller_id);
|
||||
|
||||
bool started() const;
|
||||
void started(bool s);
|
||||
|
||||
|
||||
@@ -283,6 +283,27 @@ void call_expression_context::leave_switchstmt()
|
||||
switch_stmt_stack_.pop();
|
||||
}
|
||||
|
||||
clang::ConditionalOperator *
|
||||
call_expression_context::current_conditionaloperator() const
|
||||
{
|
||||
if (conditional_operator_stack_.empty())
|
||||
return nullptr;
|
||||
|
||||
return conditional_operator_stack_.top();
|
||||
}
|
||||
|
||||
void call_expression_context::enter_conditionaloperator(
|
||||
clang::ConditionalOperator *stmt)
|
||||
{
|
||||
conditional_operator_stack_.push(stmt);
|
||||
}
|
||||
|
||||
void call_expression_context::leave_conditionaloperator()
|
||||
{
|
||||
if (conditional_operator_stack_.empty())
|
||||
conditional_operator_stack_.pop();
|
||||
}
|
||||
|
||||
bool call_expression_context::is_expr_in_current_control_statement_condition(
|
||||
const clang::Stmt *stmt) const
|
||||
{
|
||||
@@ -334,6 +355,13 @@ bool call_expression_context::is_expr_in_current_control_statement_condition(
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_conditionaloperator()) {
|
||||
if (common::is_subexpr_of(
|
||||
current_conditionaloperator()->getCond(), stmt)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -88,6 +88,10 @@ struct call_expression_context {
|
||||
void enter_switchstmt(clang::SwitchStmt *stmt);
|
||||
void leave_switchstmt();
|
||||
|
||||
clang::ConditionalOperator *current_conditionaloperator() const;
|
||||
void enter_conditionaloperator(clang::ConditionalOperator *stmt);
|
||||
void leave_conditionaloperator();
|
||||
|
||||
bool is_expr_in_current_control_statement_condition(
|
||||
const clang::Stmt *stmt) const;
|
||||
|
||||
@@ -110,6 +114,7 @@ private:
|
||||
std::stack<clang::Stmt *> loop_stmt_stack_;
|
||||
std::stack<clang::Stmt *> try_stmt_stack_;
|
||||
std::stack<clang::SwitchStmt *> switch_stmt_stack_;
|
||||
std::stack<clang::ConditionalOperator *> conditional_operator_stack_;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -807,6 +807,37 @@ bool translation_unit_visitor::TraverseDefaultStmt(clang::DefaultStmt *stmt)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool translation_unit_visitor::TraverseConditionalOperator(
|
||||
clang::ConditionalOperator *stmt)
|
||||
{
|
||||
const auto current_caller_id = context().caller_id();
|
||||
|
||||
if (current_caller_id) {
|
||||
context().enter_conditionaloperator(stmt);
|
||||
diagram().add_conditional_stmt(current_caller_id);
|
||||
}
|
||||
|
||||
RecursiveASTVisitor<translation_unit_visitor>::TraverseStmt(
|
||||
stmt->getCond());
|
||||
|
||||
RecursiveASTVisitor<translation_unit_visitor>::TraverseStmt(
|
||||
stmt->getTrueExpr());
|
||||
|
||||
if (current_caller_id) {
|
||||
diagram().add_conditional_elsestmt(current_caller_id);
|
||||
}
|
||||
|
||||
RecursiveASTVisitor<translation_unit_visitor>::TraverseStmt(
|
||||
stmt->getFalseExpr());
|
||||
|
||||
if (current_caller_id) {
|
||||
context().leave_conditionaloperator();
|
||||
diagram().end_conditional_stmt(current_caller_id);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
|
||||
{
|
||||
using clanguml::common::model::message_scope_t;
|
||||
|
||||
@@ -86,6 +86,8 @@ public:
|
||||
|
||||
bool TraverseDefaultStmt(clang::DefaultStmt *stmt);
|
||||
|
||||
bool TraverseConditionalOperator(clang::ConditionalOperator *stmt);
|
||||
|
||||
clanguml::sequence_diagram::model::diagram &diagram();
|
||||
|
||||
const clanguml::sequence_diagram::model::diagram &diagram() const;
|
||||
|
||||
Reference in New Issue
Block a user