Skip to content

[clang] associate newline and function decl. trailing Doxygen comments#198534

Open
aydinmercan wants to merge 2 commits into
llvm:mainfrom
aydinmercan:more-correct-trailing-doxygen-comments
Open

[clang] associate newline and function decl. trailing Doxygen comments#198534
aydinmercan wants to merge 2 commits into
llvm:mainfrom
aydinmercan:more-correct-trailing-doxygen-comments

Conversation

@aydinmercan
Copy link
Copy Markdown

The clang AST now correctly matches trailing Doxygen comments for function declarations and also comments that come after the declaration with the same < trailing directive.

The clang AST now correctly matches trailing Doxygen comments for
function declarations and also comments that come after the declaration
with the same `<` trailing directive.
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 19, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Aydın Mercan (aydinmercan)

Changes

The clang AST now correctly matches trailing Doxygen comments for function declarations and also comments that come after the declaration with the same &lt; trailing directive.


Full diff: https://github.com/llvm/llvm-project/pull/198534.diff

1 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+12-5)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index bc4771aec77d1..fc2048239ba55 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -243,13 +243,20 @@ RawComment *ASTContext::getRawCommentForDeclNoCacheImpl(
          LangOpts.CommentOpts.ParseAllComments) &&
         CommentBehindDecl->isTrailingComment() &&
         (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) ||
-         isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) {
+         isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D) ||
+         isa<FunctionDecl>(D))) {
 
-      // Check that Doxygen trailing comment comes after the declaration, starts
-      // on the same line and in the same file as the declaration.
-      if (SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) ==
+      const auto DeclLineNumber =
+          SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second);
+
+      const auto DeclOfCommentLine =
           Comments.getCommentBeginLine(CommentBehindDecl, DeclLocDecomp.first,
-                                       OffsetCommentBehindDecl->first)) {
+                                       OffsetCommentBehindDecl->first);
+
+      // Check that Doxygen trailing comment comes after the declaration, starts
+      // on the same or next line and in the same file as the declaration.
+      if (DeclLineNumber == std::clamp(DeclLineNumber, DeclOfCommentLine,
+                                       DeclOfCommentLine + 1)) {
         return CommentBehindDecl;
       }
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant