[clang] [clang] Fix false positive with -Wdocumentation and explicit instanti… (PR #178223)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 27 07:06:46 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Carlos Galvez (carlosgalvezp)
<details>
<summary>Changes</summary>
…ations
Solves a use case listed in #<!-- -->64087.
---
Full diff: https://github.com/llvm/llvm-project/pull/178223.diff
3 Files Affected:
- (modified) clang/include/clang/AST/CommentSema.h (+1)
- (modified) clang/lib/AST/CommentSema.cpp (+25)
- (modified) clang/test/Sema/warn-documentation.cpp (+10)
``````````diff
diff --git a/clang/include/clang/AST/CommentSema.h b/clang/include/clang/AST/CommentSema.h
index 3169e2b0d86b9..03a54319b0cd5 100644
--- a/clang/include/clang/AST/CommentSema.h
+++ b/clang/include/clang/AST/CommentSema.h
@@ -211,6 +211,7 @@ class Sema {
bool isObjCMethodDecl();
bool isObjCPropertyDecl();
bool isTemplateOrSpecialization();
+ bool isExplicitInstantiation();
bool isRecordLikeDecl();
bool isClassOrStructDecl();
/// \return \c true if the declaration that this comment is attached to
diff --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp
index d5ba240cb2bde..2e96b3529377b 100644
--- a/clang/lib/AST/CommentSema.cpp
+++ b/clang/lib/AST/CommentSema.cpp
@@ -288,6 +288,12 @@ TParamCommandComment *Sema::actOnTParamCommandStart(
new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID,
CommandMarker);
+ if (isExplicitInstantiation()) {
+ // Do not warnt on explicit instantiations, since the documentation
+ // comments are on the primary template.
+ return Command;
+ }
+
if (!isTemplateOrSpecialization())
Diag(Command->getLocation(),
diag::warn_doc_tparam_not_attached_to_a_template_decl)
@@ -308,6 +314,12 @@ void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command,
Comment::Argument{SourceRange(ArgLocBegin, ArgLocEnd), Arg};
Command->setArgs(ArrayRef(A, 1));
+ if (isExplicitInstantiation()) {
+ // Do not warnt on explicit instantiations, since the documentation
+ // comments are on the primary template.
+ return;
+ }
+
if (!isTemplateOrSpecialization()) {
// We already warned that this \\tparam is not attached to a template decl.
return;
@@ -856,6 +868,19 @@ bool Sema::isTemplateOrSpecialization() {
return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate;
}
+bool Sema::isExplicitInstantiation() {
+ if (!ThisDeclInfo)
+ return false;
+ if (!ThisDeclInfo->IsFilled)
+ inspectThisDecl();
+ if (const auto *FD = dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl)) {
+ TemplateSpecializationKind TSK = FD->getTemplateSpecializationKind();
+ return (TSK == TSK_ExplicitInstantiationDeclaration) ||
+ (TSK == TSK_ExplicitInstantiationDefinition);
+ }
+ return false;
+}
+
bool Sema::isRecordLikeDecl() {
if (!ThisDeclInfo)
return false;
diff --git a/clang/test/Sema/warn-documentation.cpp b/clang/test/Sema/warn-documentation.cpp
index 0d1faa1b562fe..626a6bf92af95 100644
--- a/clang/test/Sema/warn-documentation.cpp
+++ b/clang/test/Sema/warn-documentation.cpp
@@ -1536,3 +1536,13 @@ template <class T, class = void> constexpr auto var = T{};
template <typename T> constexpr auto var<T> = T{};
} // namespace GH144775
#endif
+
+namespace GH64087
+{
+/// @brief Primary template
+/// @tparam T type
+template <typename T>
+class Foo{};
+
+template class Foo<float>;
+} // namespace GH64087
``````````
</details>
https://github.com/llvm/llvm-project/pull/178223
More information about the cfe-commits
mailing list