Added generate_message_comments configuration option

This commit is contained in:
Bartek Kryza
2023-10-15 19:59:06 +02:00
parent c4d3d61770
commit 03e8c867f4
14 changed files with 127 additions and 0 deletions

View File

@@ -83,6 +83,8 @@ void generator::generate_call(const message &m, std::ostream &ostr) const
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << from_alias << " "
<< common::generators::plantuml::to_plantuml(message_t::kCall) << " ";
@@ -178,6 +180,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kIf) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "alt";
if (const auto &text = m.condition_text(); text.has_value())
ostr << " " << text.value();
@@ -199,6 +202,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kWhile) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "loop";
if (const auto &text = m.condition_text(); text.has_value())
ostr << " " << text.value();
@@ -209,6 +213,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kFor) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "loop";
if (const auto &text = m.condition_text(); text.has_value())
ostr << " " << text.value();
@@ -219,6 +224,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kDo) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "loop";
if (const auto &text = m.condition_text(); text.has_value())
ostr << " " << text.value();
@@ -229,6 +235,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kTry) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "group try\n";
}
else if (m.type() == message_t::kCatch) {
@@ -241,6 +248,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kSwitch) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "group switch\n";
}
else if (m.type() == message_t::kCase) {
@@ -252,6 +260,7 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
else if (m.type() == message_t::kConditional) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
ostr << "alt";
if (const auto &text = m.condition_text(); text.has_value())
ostr << " " << text.value();
@@ -267,6 +276,33 @@ void generator::generate_activity(const activity &a, std::ostream &ostr,
}
}
void generator::generate_message_comment(
std::ostream &ostr, const model::message &m) const
{
if (!config().generate_message_comments() || !m.comment())
return;
const auto &from = model().get_participant<model::participant>(m.from());
if (!from)
return;
if (m.type() == message_t::kCall) {
const auto &to = model().get_participant<model::participant>(m.to());
if (!to)
return;
ostr << "note over " << generate_alias(from.value()) << ", "
<< generate_alias(to.value()) << '\n';
}
else {
ostr << "note over " << generate_alias(from.value()) << '\n';
}
ostr << m.comment().value() << '\n';
ostr << "end note" << '\n';
}
void generator::generate_participant(
std::ostream &ostr, const std::string &name) const
{

View File

@@ -147,6 +147,9 @@ private:
model::function::message_render_mode
select_method_arguments_render_mode() const;
void generate_message_comment(
std::ostream &ostr, const model::message &m) const;
mutable std::set<common::id_t> generated_participants_;
mutable std::vector<model::message> already_generated_in_static_context_;
};

View File

@@ -58,6 +58,16 @@ void message::set_return_type(std::string t) { return_type_ = std::move(t); }
const std::string &message::return_type() const { return return_type_; }
const std::optional<std::string> &message::comment() const { return comment_; }
void message::set_comment(std::string c) { comment_ = std::move(c); }
void message::set_comment(const std::optional<std::string> &c)
{
if (c)
set_comment(c.value());
}
void message::set_message_scope(common::model::message_scope_t scope)
{
scope_ = scope;

View File

@@ -119,6 +119,12 @@ public:
*/
const std::string &return_type() const;
const std::optional<std::string> &comment() const;
void set_comment(std::string c);
void set_comment(const std::optional<std::string> &c);
/**
* @brief Set message scope
*
@@ -172,6 +178,8 @@ private:
std::optional<std::string> condition_text_;
std::optional<std::string> comment_;
bool in_static_declaration_context_{false};
};

View File

@@ -613,6 +613,8 @@ bool translation_unit_visitor::TraverseIfStmt(clang::IfStmt *stmt)
message m{message_t::kElseIf, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
else {
@@ -621,6 +623,8 @@ bool translation_unit_visitor::TraverseIfStmt(clang::IfStmt *stmt)
message m{message_t::kIf, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
}
@@ -655,6 +659,8 @@ bool translation_unit_visitor::TraverseWhileStmt(clang::WhileStmt *stmt)
message m{message_t::kWhile, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
RecursiveASTVisitor<translation_unit_visitor>::TraverseWhileStmt(stmt);
@@ -685,6 +691,8 @@ bool translation_unit_visitor::TraverseDoStmt(clang::DoStmt *stmt)
message m{message_t::kDo, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -716,6 +724,10 @@ bool translation_unit_visitor::TraverseForStmt(clang::ForStmt *stmt)
message m{message_t::kFor, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -742,6 +754,8 @@ bool translation_unit_visitor::TraverseCXXTryStmt(clang::CXXTryStmt *stmt)
context().enter_trystmt(stmt);
message m{message_t::kTry, current_caller_id};
set_source_location(*stmt, m);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -800,6 +814,8 @@ bool translation_unit_visitor::TraverseCXXForRangeStmt(
message m{message_t::kFor, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -825,6 +841,8 @@ bool translation_unit_visitor::TraverseSwitchStmt(clang::SwitchStmt *stmt)
context().enter_switchstmt(stmt);
model::message m{message_t::kSwitch, current_caller_id};
set_source_location(*stmt, m);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -891,6 +909,8 @@ bool translation_unit_visitor::TraverseConditionalOperator(
model::message m{message_t::kConditional, current_caller_id};
set_source_location(*stmt, m);
m.condition_text(condition_text);
m.set_comment(clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), stmt));
diagram().add_block_message(std::move(m));
}
@@ -1015,6 +1035,10 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
}
if (m.from() > 0 && m.to() > 0) {
auto expr_comment = clanguml::common::get_expression_comment(
source_manager(), *context().get_ast_context(), expr);
m.set_comment(expr_comment);
if (diagram().sequences().find(m.from()) ==
diagram().sequences().end()) {
activity a{m.from()};