Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple message comments in nested calls #304

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/common/generators/plantuml/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void generator<C, D>::generate_plantuml_directives(
directive.replace(std::get<1>(alias_match),
std::get<2>(alias_match), element_opt.value().alias());
else {
LOG_ERROR("Cannot find clang-uml alias for element {}",
LOG_WARN("Cannot find clang-uml alias for element {}",
full_name.to_string());
directive.replace(std::get<1>(alias_match),
std::get<2>(alias_match), "UNKNOWN_ALIAS");
Expand All @@ -363,24 +363,24 @@ void generator<C, D>::generate_plantuml_directives(
ostr << directive << '\n';
}
catch (const clanguml::error::uml_alias_missing &e) {
LOG_ERROR("Failed to render PlantUML directive due to unresolvable "
"alias: {}",
LOG_WARN("Failed to render PlantUML directive due to unresolvable "
"alias: {}",
e.what());
}
catch (const inja::json::parse_error &e) {
LOG_ERROR("Failed to parse Jinja template: {}", d);
LOG_WARN("Failed to parse Jinja template: {}", d);
}
catch (const inja::json::exception &e) {
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
LOG_WARN("Failed to render PlantUML directive: \n{}\n due to: {}",
d, e.what());
}
catch (const std::regex_error &e) {
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to "
"std::regex_error: {}",
LOG_WARN("Failed to render PlantUML directive: \n{}\n due to "
"std::regex_error: {}",
d, e.what());
}
catch (const std::exception &e) {
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
LOG_WARN("Failed to render PlantUML directive: \n{}\n due to: {}",
d, e.what());
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/common/visitor/ast_id_mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ namespace clanguml::common::visitor {

void ast_id_mapper::add(int64_t ast_id, eid_t global_id)
{
id_map_.emplace(ast_id, global_id);
id_map_[ast_id] = global_id;
}

std::optional<eid_t> ast_id_mapper::get_global_id(eid_t ast_id)
{
assert(!ast_id.is_global());

if (ast_id.is_global())
return {};

Expand All @@ -38,4 +36,15 @@ std::optional<eid_t> ast_id_mapper::get_global_id(eid_t ast_id)
return id_map_.at(ast_id.ast_local_value());
}

eid_t ast_id_mapper::resolve_or(eid_t id)
{
if (id.is_global())
return id;

if (id_map_.count(id.ast_local_value()) == 0)
return id;

return id_map_.at(id.ast_local_value());
}

} // namespace clanguml::common::visitor
2 changes: 2 additions & 0 deletions src/common/visitor/ast_id_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class ast_id_mapper {
*/
std::optional<eid_t> get_global_id(eid_t ast_id);

eid_t resolve_or(eid_t id);

private:
std::map</* Clang AST translation unit local id */ int64_t,
/* clang-uml global id */ eid_t>
Expand Down
4 changes: 2 additions & 2 deletions src/common/visitor/translation_unit_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ template <typename ConfigT, typename DiagramT> class translation_unit_visitor {
if (comment == nullptr)
return {};

auto [it, inserted] = processed_comments_.emplace(comment);
auto [it, inserted] = processed_comments().emplace(comment);

if (!inserted)
return {};
Expand All @@ -282,7 +282,7 @@ template <typename ConfigT, typename DiagramT> class translation_unit_visitor {
// TODO: Refactor to use standard block comments processable by
// clang comments
const auto &[decorators, stripped_comment] =
decorators::parse(comment->getFormattedText(source_manager_, de));
decorators::parse(comment->getFormattedText(source_manager(), de));

e.add_decorators(decorators);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void generator::generate_call(const message &m, nlohmann::json &parent) const
msg["from"]["activity_id"] = std::to_string(from.value().id().value());
msg["to"]["activity_id"] = std::to_string(to.value().id().value());
if (const auto &cmt = m.comment(); cmt.has_value())
msg["comment"] = cmt.value();
msg["comment"] = cmt.value().at("comment");

if (from.value().type_name() == "method") {
const auto &class_participant =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ void generator::generate_message_comment(
if (comment_generated_from_note_decorators)
return;

if (const auto &cmt = m.comment();
config().generate_message_comments() && cmt.has_value()) {
if (const auto &cmt = m.comment(); config().generate_message_comments() &&
cmt.has_value() &&
generated_comment_ids_.emplace(cmt.value().at("id")).second) {

ostr << indent(1) << "note over " << generate_alias(from.value())
<< ": ";

auto formatted_message = util::format_message_comment(
cmt.value(), config().message_comment_width());
cmt.value().at("comment"), config().message_comment_width());

util::replace_all(formatted_message, "\n", "<br/>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class generator : public common_generator<diagram_config, diagram_model> {
select_method_arguments_render_mode() const;

mutable std::set<eid_t> generated_participants_;
mutable std::set<unsigned int> generated_comment_ids_;
mutable std::vector<model::message> already_generated_in_static_context_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ void generator::generate_message_comment(
if (!from)
return;

// First generate message comments from \note directives in comments
bool comment_generated_from_note_decorators{false};
for (const auto &decorator : m.decorators()) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
Expand All @@ -322,11 +323,14 @@ void generator::generate_message_comment(
if (!config().generate_message_comments())
return;

if (const auto &comment = m.comment(); comment) {
// Now generate message notes from raw comments if enabled
if (const auto &comment = m.comment(); comment &&
generated_comment_ids_.emplace(comment.value().at("id")).second) {

ostr << "note over " << generate_alias(from.value()) << '\n';

ostr << util::format_message_comment(
comment.value(), config().message_comment_width())
ostr << util::format_message_comment(comment.value().at("comment"),
config().message_comment_width())
<< '\n';

ostr << "end note" << '\n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class generator : public common_generator<diagram_config, diagram_model> {
select_method_arguments_render_mode() const;

mutable std::set<eid_t> generated_participants_;
mutable std::set<unsigned int> generated_comment_ids_;
mutable std::vector<model::message> already_generated_in_static_context_;
};

Expand Down
7 changes: 6 additions & 1 deletion src/sequence_diagram/model/diagram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,17 @@ void diagram::print() const
else {
const auto &to_participant = *participants_.at(message.to());

std::string message_comment{"None"};
if (const auto &cmt = message.comment(); cmt.has_value()) {
message_comment = cmt.value().at("comment");
}

LOG_TRACE(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}, type={}, comment={}",
from_participant.full_name(false), from_participant.id(),
to_participant.full_name(false), to_participant.id(),
message.message_name(), to_string(message.type()),
message.comment().value_or("None"));
message_comment);
}
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/sequence_diagram/model/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool message::operator==(const message &other) const noexcept
return from_ == other.from_ && to_ == other.to_ && type_ == other.type_ &&
scope_ == other.scope_ && message_name_ == other.message_name_ &&
return_type_ == other.return_type_ &&
condition_text_ == other.condition_text_;
condition_text_ == other.condition_text_ && comment_ == other.comment_;
}

void message::set_type(common::model::message_t t) { type_ = t; }
Expand All @@ -57,15 +57,38 @@ 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_; }
const std::optional<common::model::comment_t> &message::comment() const
{
return comment_;
}

void message::set_comment(
std::optional<std::pair<unsigned int, std::string>> comment)
{
if (comment.has_value()) {
set_comment(comment.value().first, comment.value().second);
}
}

void message::set_comment(unsigned int id, std::string comment)
{
if (comment.empty())
return;

common::model::comment_t c;
c["id"] = id;
c["comment"] = comment;

set_comment(std::move(c));
}

void message::set_comment(std::string c)
void message::set_comment(common::model::comment_t c)
{
if (!c.empty())
comment_ = std::move(c);
}

void message::set_comment(const std::optional<std::string> &c)
void message::set_comment(const std::optional<common::model::comment_t> &c)
{
if (c)
set_comment(c.value());
Expand Down
13 changes: 9 additions & 4 deletions src/sequence_diagram/model/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ class message : public common::model::diagram_element {
*/
const std::string &return_type() const;

const std::optional<std::string> &comment() const;
const std::optional<common::model::comment_t> &comment() const;

void set_comment(std::string c);
void set_comment(
std::optional<std::pair<unsigned int, std::string>> comment);

void set_comment(const std::optional<std::string> &c);
void set_comment(unsigned int id, std::string comment);

void set_comment(common::model::comment_t c);

void set_comment(const std::optional<common::model::comment_t> &c);

/**
* @brief Set message scope
Expand Down Expand Up @@ -177,7 +182,7 @@ class message : public common::model::diagram_element {

std::optional<std::string> condition_text_;

std::optional<std::string> comment_;
std::optional<common::model::comment_t> comment_;

bool in_static_declaration_context_{false};
};
Expand Down
Loading
Loading