Added support for call comment directive to inject calls in comments (Fixes #196)

This commit is contained in:
Bartek Kryza
2023-10-21 21:47:04 +02:00
parent 4f43253c0d
commit 0d237fec0a
8 changed files with 92 additions and 18 deletions

View File

@@ -856,7 +856,8 @@ clang::RawComment *get_expression_raw_comment(const clang::SourceManager &sm,
auto expr_begin = stmt->getSourceRange().getBegin();
const auto expr_begin_line = sm.getSpellingLineNumber(expr_begin);
if (!context.Comments.empty())
if (!context.Comments.empty() &&
context.Comments.getCommentsInFile(sm.getFileID(expr_begin)) != nullptr)
for (const auto [offset, raw_comment] :
*context.Comments.getCommentsInFile(sm.getFileID(expr_begin))) {
const auto comment_end_line = sm.getSpellingLineNumber(

View File

@@ -47,6 +47,9 @@ std::shared_ptr<decorator> decorator::from_string(std::string_view c)
if (c.find(association::label) == 0) {
return association::from_string(c);
}
if (c.find(call::label) == 0) {
return call::from_string(c);
}
return {};
}
@@ -173,6 +176,17 @@ std::shared_ptr<decorator> association::from_string(std::string_view c)
return res;
}
std::shared_ptr<decorator> call::from_string(std::string_view c)
{
auto res = std::make_shared<call>();
auto toks = res->tokenize(call::label, c);
res->diagrams = toks.diagrams;
res->callee = util::trim(toks.text);
return res;
}
std::vector<std::shared_ptr<decorator>> parse(
std::string documentation_block, const std::string &clanguml_tag)
{

View File

@@ -140,6 +140,17 @@ struct association : public relationship {
static std::shared_ptr<decorator> from_string(std::string_view c);
};
/**
* @brief Represents a call message in sequence diagram
*/
struct call : public decorator {
static inline const std::string label{"call"};
std::string callee;
static std::shared_ptr<decorator> from_string(std::string_view c);
};
/**
* @brief Parse a documentation block and extract all clang-uml decorators
*

View File

@@ -75,6 +75,10 @@ clang::ASTContext *call_expression_context::get_ast_context() const
return &current_function_decl_->getASTContext();
}
if (current_method_decl_ != nullptr) {
return &current_function_decl_->getASTContext();
}
return nullptr;
}

View File

@@ -947,13 +947,9 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
using clanguml::sequence_diagram::model::activity;
using clanguml::sequence_diagram::model::message;
if (!should_include(expr))
if (!context().valid() || context().get_ast_context() == nullptr)
return true;
LOG_TRACE("Visiting call expression at {} [caller_id = {}]",
expr->getBeginLoc().printToString(source_manager()),
context().caller_id());
message m{message_t::kCall, context().caller_id()};
m.in_static_declaration_context(within_static_variable_declaration_ > 0);
@@ -967,6 +963,25 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
if (m.skip())
return true;
auto generated_message_from_comment{false};
for (const auto &decorator : m.decorators()) {
auto call_decorator =
std::dynamic_pointer_cast<decorators::call>(decorator);
if (call_decorator &&
call_decorator->applies_to_diagram(config().name)) {
m.set_to(common::to_id(call_decorator->callee));
generated_message_from_comment = true;
break;
}
}
if (!generated_message_from_comment && !should_include(expr))
return true;
LOG_TRACE("Visiting call expression at {} [caller_id = {}]",
expr->getBeginLoc().printToString(source_manager()),
context().caller_id());
// If we're currently inside a lambda expression, set it's id as
// message source rather then enclosing context
// Unless the lambda is declared in a function or method call
@@ -978,17 +993,17 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
m.set_message_scope(common::model::message_scope_t::kCondition);
}
if (generated_message_from_comment) { }
//
// Call to an overloaded operator
//
if (const auto *operator_call_expr =
clang::dyn_cast_or_null<clang::CXXOperatorCallExpr>(expr);
operator_call_expr != nullptr) {
else if (const auto *operator_call_expr =
clang::dyn_cast_or_null<clang::CXXOperatorCallExpr>(expr);
operator_call_expr != nullptr) {
if (!process_operator_call_expression(m, operator_call_expr))
return true;
}
//
// Call to a class method
//
@@ -1042,9 +1057,11 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
}
if (m.from() > 0 && m.to() > 0) {
auto expr_comment = get_expression_comment(source_manager(),
*context().get_ast_context(), context().caller_id(), expr);
m.set_comment(expr_comment);
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);
}
if (diagram().sequences().find(m.from()) ==
diagram().sequences().end()) {