https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/178223

>From 83650ae4f7acd4f68303834999fdb5e962b4e226 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= <[email protected]>
Date: Mon, 26 Jan 2026 15:21:28 +0000
Subject: [PATCH] [clang] Fix false positive with -Wdocumentation and explicit
 instantiations

Solves a use case listed in #64087.
---
 clang/docs/ReleaseNotes.rst            |  3 +++
 clang/include/clang/AST/CommentSema.h  |  1 +
 clang/lib/AST/CommentSema.cpp          | 25 +++++++++++++++++++++++++
 clang/test/Sema/warn-documentation.cpp | 10 ++++++++++
 4 files changed, 39 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a5340a31d4fe9..d58d6575c3978 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -136,6 +136,9 @@ Attribute Changes in Clang
 
 Improvements to Clang's diagnostics
 -----------------------------------
+- Fixed bug in ``-Wdocumentation`` so that it correctly handles explicit
+  template instantiations (#64087).
+
 - Added ``-Wlifetime-safety`` to enable lifetime safety analysis,
   a CFG-based intra-procedural analysis that detects use-after-free and related
   temporal safety bugs. See the
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..7ab6c7c261a04 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 warn 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 warn 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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to