[clang] [clang] associate newline and function decl. trailing Doxygen comments (PR #198534)
Aydın Mercan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 02:00:37 PDT 2026
https://github.com/aydinmercan updated https://github.com/llvm/llvm-project/pull/198534
>From 842ea11f48900453fb4ae9034da74621074ea314 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ayd=C4=B1n=20Mercan?= <aydin at mercan.dev>
Date: Thu, 30 Jul 2026 12:00:11 +0300
Subject: [PATCH] [clang] associate newline and function decl. trailing Doxygen
comments
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.
---
clang/lib/AST/ASTContext.cpp | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 02a3f88431f58..8b5970690a65e 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -272,15 +272,20 @@ RawComment *ASTContext::getRawCommentNoCacheImpl(
if ((CommentBehindDecl->isDocumentation() ||
LangOpts.CommentOpts.ParseAllComments) &&
CommentBehindDecl->isTrailingComment() &&
- (IsMacro || (D && (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) ||
- isa<VarDecl>(D) || isa<ObjCMethodDecl>(D) ||
- isa<ObjCPropertyDecl>(D))))) {
+ (IsMacro ||
+ (D && (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) ||
+ isa<VarDecl>(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(LocDecomp.first, LocDecomp.second) ==
- Comments.getCommentBeginLine(CommentBehindDecl, LocDecomp.first,
- OffsetCommentBehindDecl->first)) {
+ // on the same or the next line, and in the same file as the declaration.
+ auto LocLineNumber =
+ SourceMgr.getLineNumber(LocDecomp.first, LocDecomp.second);
+ auto CommentBeginLine = Comments.getCommentBeginLine(
+ CommentBehindDecl, LocDecomp.first, OffsetCommentBehindDecl->first);
+ if (LocLineNumber == CommentBeginLine ||
+ (LocLineNumber + 1 == CommentBeginLine &&
+ CommentBehindDecl->isDocumentation())) {
return CommentBehindDecl;
}
}
More information about the cfe-commits
mailing list