https://github.com/carlosgalvezp created https://github.com/llvm/llvm-project/pull/178223
…ations Solves a use case listed in #64087. >From 3feeb1796e1261dcb1f4dc2a59de67b9bbb6ef25 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/include/clang/AST/CommentSema.h | 1 + clang/lib/AST/CommentSema.cpp | 25 +++++++++++++++++++++++++ clang/test/Sema/warn-documentation.cpp | 10 ++++++++++ 3 files changed, 36 insertions(+) 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
