Added handling of comment decorators (skip, note) in sequence diagram comments (#194)

This commit is contained in:
Bartek Kryza
2023-10-21 17:07:28 +02:00
parent ddbfffbf23
commit 7f595b1c54
19 changed files with 403 additions and 45 deletions

View File

@@ -849,9 +849,8 @@ bool parse_source_location(const std::string &location_str, std::string &file,
return true;
}
std::optional<std::string> get_expression_comment(
const clang::SourceManager &sm, const clang::ASTContext &context,
const clang::Stmt *stmt)
clang::RawComment *get_expression_raw_comment(const clang::SourceManager &sm,
const clang::ASTContext &context, const clang::Stmt *stmt)
{
// First get the first line of the expression
auto expr_begin = stmt->getSourceRange().getBegin();
@@ -859,17 +858,13 @@ std::optional<std::string> get_expression_comment(
if (!context.Comments.empty())
for (const auto [offset, raw_comment] :
*context.Comments.getCommentsInFile(sm.getMainFileID())) {
auto comment =
raw_comment->getFormattedText(sm, sm.getDiagnostics());
*context.Comments.getCommentsInFile(sm.getFileID(expr_begin))) {
const auto comment_end_line = sm.getSpellingLineNumber(
raw_comment->getSourceRange().getEnd());
if (expr_begin_line == comment_end_line ||
expr_begin_line == comment_end_line + 1)
return comment;
return raw_comment;
}
return {};

View File

@@ -282,8 +282,15 @@ clang::QualType dereference(clang::QualType type);
std::pair<clang::QualType, std::deque<common::model::context>>
consume_type_context(clang::QualType type);
std::optional<std::string> get_expression_comment(
const clang::SourceManager &sm, const clang::ASTContext &context,
const clang::Stmt *stmt);
/**
* @brief Extract a comment before or next to a statement
*
* @param sm clang::SourceManager reference
* @param context clang::ASTContext reference
* @param stmt Pointer to the current clang::Stmt
* @return Pointer to a clang::RawComment* or nullptr
*/
clang::RawComment *get_expression_raw_comment(const clang::SourceManager &sm,
const clang::ASTContext &context, const clang::Stmt *stmt);
} // namespace clanguml::common

View File

@@ -59,7 +59,6 @@ clang::SourceManager &translation_unit_visitor::source_manager() const
void translation_unit_visitor::process_comment(
const clang::NamedDecl &decl, clanguml::common::model::decorated_element &e)
{
assert(comment_visitor_.get() != nullptr);
comment_visitor_->visit(decl, e);
@@ -67,12 +66,23 @@ void translation_unit_visitor::process_comment(
const auto *comment =
decl.getASTContext().getRawCommentForDeclNoCache(&decl);
process_comment(comment, decl.getASTContext().getDiagnostics(), e);
}
void translation_unit_visitor::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 (!inserted)
return;
// 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_, decl.getASTContext().getDiagnostics())));
e.add_decorators(
decorators::parse(comment->getFormattedText(source_manager_, de)));
}
}

View File

@@ -102,7 +102,7 @@ public:
protected:
/**
* @brief Set source location in diagram element
* @brief Process comment directives in comment attached to a declaration
*
* @param decl Reference to @ref clang::NamedDecl
* @param element Reference to element to be updated
@@ -110,6 +110,17 @@ protected:
void process_comment(const clang::NamedDecl &decl,
clanguml::common::model::decorated_element &e);
/**
* @brief Process comment directives in raw comment
*
* @param comment clang::RawComment pointer
* @param de Reference to clang::DiagnosticsEngine
* @param element Reference to element to be updated
*/
void process_comment(const clang::RawComment *comment,
clang::DiagnosticsEngine &de,
clanguml::common::model::decorated_element &e);
private:
clang::SourceManager &source_manager_;
@@ -118,5 +129,7 @@ private:
std::filesystem::path relative_to_path_;
std::filesystem::path translation_unit_path_;
std::set<const clang::RawComment *> processed_comments_;
};
} // namespace clanguml::common::visitor