Improved handling of message call comments (#264)

This commit is contained in:
Bartek Kryza
2024-04-30 00:06:42 +02:00
parent 6c6575bc7f
commit 4293a6cc79
11 changed files with 267 additions and 41 deletions

View File

@@ -265,23 +265,29 @@ public:
* @param comment clang::RawComment pointer
* @param de Reference to clang::DiagnosticsEngine
* @param element Reference to element to be updated
* @return Comment with uml directives stripped from it
*/
void process_comment(const clang::RawComment *comment,
clang::DiagnosticsEngine &de,
[[maybe_unused]] std::string process_comment(
const clang::RawComment *comment, clang::DiagnosticsEngine &de,
clanguml::common::model::decorated_element &e)
{
if (comment != nullptr) {
auto [it, inserted] = processed_comments_.emplace(comment);
if (comment == nullptr)
return {};
if (!inserted)
return;
auto [it, inserted] = processed_comments_.emplace(comment);
// Process clang-uml decorators in the comments
// TODO: Refactor to use standard block comments processable by
// clang comments
e.add_decorators(decorators::parse(
comment->getFormattedText(source_manager_, de)));
}
if (!inserted)
return {};
// Process clang-uml decorators in the comments
// TODO: Refactor to use standard block comments processable by
// clang comments
const auto &[decorators, stripped_comment] =
decorators::parse(comment->getFormattedText(source_manager_, de));
e.add_decorators(decorators);
return stripped_comment;
}
/**
@@ -332,6 +338,12 @@ public:
*/
const ConfigT &config() const { return config_; }
protected:
std::set<const clang::RawComment *> &processed_comments()
{
return processed_comments_;
}
private:
// Reference to the output diagram model
DiagramT &diagram_;

View File

@@ -187,10 +187,12 @@ std::shared_ptr<decorator> call::from_string(std::string_view c)
return res;
}
std::vector<std::shared_ptr<decorator>> parse(
std::pair<std::vector<std::shared_ptr<decorator>>, std::string> parse(
std::string documentation_block, const std::string &clanguml_tag)
{
std::vector<std::shared_ptr<decorator>> res;
std::string stripped_comment;
const std::string begin_tag{"@" + clanguml_tag};
const auto begin_tag_size = begin_tag.size();
@@ -202,12 +204,20 @@ std::vector<std::shared_ptr<decorator>> parse(
const std::string_view block_view{documentation_block};
auto pos = block_view.find("@" + clanguml_tag + "{");
if (pos == std::string::npos) {
// This comment had no uml directives
return {{}, util::trim(documentation_block)};
}
size_t last_end_pos{0};
while (pos < documentation_block.size()) {
auto c_begin = pos + begin_tag_size;
auto c_end = documentation_block.find('}', c_begin);
auto c_end = block_view.find('}', c_begin);
if (c_end == std::string::npos)
return res;
if (c_end == std::string::npos) {
return {res, util::trim(stripped_comment)};
}
auto com =
decorator::from_string(block_view.substr(c_begin + 1, c_end - 2));
@@ -215,10 +225,15 @@ std::vector<std::shared_ptr<decorator>> parse(
if (com)
res.emplace_back(std::move(com));
const auto in_between_length = pos - last_end_pos;
stripped_comment += block_view.substr(last_end_pos, in_between_length);
last_end_pos = pos + (c_end - c_begin + begin_tag_size + 1);
pos = block_view.find("@" + clanguml_tag + "{", c_end);
}
return res;
return {res, util::trim(stripped_comment)};
};
} // namespace clanguml::decorators

View File

@@ -156,9 +156,10 @@ struct call : public decorator {
*
* @param documentation_block Documentation block extracted from source code
* @param clanguml_tag Name of the clanguml tag (default `uml`)
* @return List of clang-uml decorators extracted from comment
* @return Pair of: a list of clang-uml decorators extracted from comment and
* a comment stripped of any uml directives
*/
std::vector<std::shared_ptr<decorator>> parse(
std::pair<std::vector<std::shared_ptr<decorator>>, std::string> parse(
std::string documentation_block, const std::string &clanguml_tag = "uml");
} // namespace decorators

View File

@@ -59,7 +59,11 @@ 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(std::string c)
{
if (!c.empty())
comment_ = std::move(c);
}
void message::set_comment(const std::optional<std::string> &c)
{

View File

@@ -1047,24 +1047,28 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
set_source_location(*expr, m);
process_comment(clanguml::common::get_expression_raw_comment(
source_manager(), *context().get_ast_context(), expr),
context().get_ast_context()->getDiagnostics(), m);
const auto *raw_expr_comment = clanguml::common::get_expression_raw_comment(
source_manager(), *context().get_ast_context(), expr);
const auto stripped_comment = process_comment(
raw_expr_comment, context().get_ast_context()->getDiagnostics(), m);
if (m.skip())
return true;
auto generated_message_from_comment = generate_message_from_comment(m);
if (!generated_message_from_comment && !should_include(expr))
if (!generated_message_from_comment && !should_include(expr)) {
processed_comments().erase(raw_expr_comment);
return true;
}
if (context().is_expr_in_current_control_statement_condition(expr)) {
m.set_message_scope(common::model::message_scope_t::kCondition);
}
if (generated_message_from_comment) {
// Do nothing
LOG_DBG(
"Message for this call expression is taken from comment directive");
}
//
// Call to an overloaded operator
@@ -1148,11 +1152,7 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
// Add message to diagram
if (m.from() > 0 && m.to() > 0) {
if (!generated_message_from_comment) {
auto expr_comment = get_expression_comment(source_manager(),
*context().get_ast_context(), context().caller_id(), expr);
m.set_comment(expr_comment);
}
m.set_comment(stripped_comment);
if (diagram().sequences().find(m.from()) ==
diagram().sequences().end()) {
@@ -2167,10 +2167,17 @@ std::optional<std::string> translation_unit_visitor::get_expression_comment(
if (raw_comment == nullptr)
return {};
if (!processed_comments_.emplace(caller_id, raw_comment).second) {
if (!processed_comments_by_caller_id_.emplace(caller_id, raw_comment)
.second) {
return {};
}
return raw_comment->getFormattedText(sm, sm.getDiagnostics());
const auto &[decorators, stripped_comment] = decorators::parse(
raw_comment->getFormattedText(sm, sm.getDiagnostics()));
if (stripped_comment.empty())
return {};
return stripped_comment;
}
} // namespace clanguml::sequence_diagram::visitor

View File

@@ -511,7 +511,7 @@ private:
already_visited_in_static_declaration_{};
mutable std::set<std::pair<int64_t, const clang::RawComment *>>
processed_comments_;
processed_comments_by_caller_id_;
template_builder_t template_builder_;
};