[clang] [Comment] Fix @method/@methodgroup/@callback false positives (PR #220787)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 2 19:11:18 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akira Hatanaka (ahatanak)
<details>
<summary>Changes</summary>
76af74004e8b (#<!-- -->181769) refactored the DiagSelect computation in checkFunctionDeclVerbatimLine to use named enum constants instead of magic numbers. The checks for whether the comment was attached to an Objective-C method, a method group, or a pointer to a function declaration were removed for the @<!-- -->method, @<!-- -->methodgroup, and @<!-- -->callback cases, which caused warn_doc_function_method_decl_mismatch to be emitted even when the comments were attached to matching declarations.
Restore the dropped checks.
rdar://184264978
---
Full diff: https://github.com/llvm/llvm-project/pull/220787.diff
3 Files Affected:
- (modified) clang/lib/AST/CommentSema.cpp (+6-3)
- (modified) clang/test/Sema/warn-documentation.cpp (+6)
- (modified) clang/test/Sema/warn-documentation.m (+12)
``````````diff
diff --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp
index bc7884f1ffd52..fba37d5a60c4f 100644
--- a/clang/lib/AST/CommentSema.cpp
+++ b/clang/lib/AST/CommentSema.cpp
@@ -111,13 +111,16 @@ void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
DiagSelect = diag::CallableKind::FunctionGroup;
break;
case CommandTraits::KCI_method:
- DiagSelect = diag::CallableKind::Method;
+ if (!isObjCMethodDecl())
+ DiagSelect = diag::CallableKind::Method;
break;
case CommandTraits::KCI_methodgroup:
- DiagSelect = diag::CallableKind::MethodGroup;
+ if (!isObjCMethodDecl())
+ DiagSelect = diag::CallableKind::MethodGroup;
break;
case CommandTraits::KCI_callback:
- DiagSelect = diag::CallableKind::Callback;
+ if (!isFunctionPointerVarDecl())
+ DiagSelect = diag::CallableKind::Callback;
break;
}
if (DiagSelect)
diff --git a/clang/test/Sema/warn-documentation.cpp b/clang/test/Sema/warn-documentation.cpp
index e64f68a8797ee..ae891957e07f4 100644
--- a/clang/test/Sema/warn-documentation.cpp
+++ b/clang/test/Sema/warn-documentation.cpp
@@ -834,6 +834,12 @@ unsigned test_function(Base64Flags inFlags);
typedef unsigned int BaseFlags;
unsigned (*test_callback)(BaseFlags inFlags);
+// @callback should not warn when the comment is directly attached to a
+// pointer to function declaration.
+/*! @callback test_callback_attached
+*/
+unsigned (*test_callback_attached)(BaseFlags inFlags);
+
// expected-warning at +1 {{'\endverbatim' command does not terminate a verbatim text block}}
/// \endverbatim
int test_verbatim_1();
diff --git a/clang/test/Sema/warn-documentation.m b/clang/test/Sema/warn-documentation.m
index 30c8bb754c2cd..4b8758bab5c70 100644
--- a/clang/test/Sema/warn-documentation.m
+++ b/clang/test/Sema/warn-documentation.m
@@ -135,6 +135,18 @@ @interface rdar12379114
- (id)initWithTimeout:(NSTimeInterval)timeout;
@end
+// @method and @methodgroup should not warn when the comment is directly
+// attached to an Objective-C method declaration.
+ at interface TestMethodAndMethodgroupAttached
+/*!
+ @methodgroup Creating a request
+*/
+/*!
+ @method doSomething
+*/
+- (id)doSomething;
+ at end
+
// expected-warning at +2 {{'@protocol' command should not be used in a comment attached to a non-protocol declaration}}
/*!
@protocol PROTO
``````````
</details>
https://github.com/llvm/llvm-project/pull/220787
More information about the cfe-commits
mailing list