[clang] [analyzer] Fix [[clang::suppress]] for tricky template specializations (PR #178441)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 08:22:45 PST 2026
================
@@ -165,6 +165,57 @@ bool BugSuppression::isSuppressed(const BugReport &R) {
isSuppressed(UniqueingLocation, DeclWithIssue, {});
}
+// For template specializations, returns the primary template definition or
+// partial specialization that was used to instantiate the specialization.
+// This ensures suppression attributes on templates apply to their
+// specializations.
+//
+// For example, given:
+// template <typename T> class [[clang::suppress]] Wrapper { ... };
+// Wrapper<int> w; // instantiates ClassTemplateSpecializationDecl
+//
+// When analyzing code in Wrapper<int>, this function maps the specialization
+// back to the primary template definition, allowing us to find the suppression
+// attribute.
+//
+// The function handles two cases:
+// 1. Instantiation from a class template - searches redeclarations to find
+// the definition (not just a forward declaration).
+// 2. Instantiation from a partial specialization - returns it directly.
+//
+// For non-template-specialization decls, returns the input unchanged.
+static const Decl *
+preferTemplateDefinitionForTemplateSpecializations(const Decl *D) {
+ const auto *SpecializationDecl = dyn_cast<ClassTemplateSpecializationDecl>(D);
+ if (!SpecializationDecl)
+ return D;
+
+ auto InstantiatedFrom = SpecializationDecl->getInstantiatedFrom();
+ if (!InstantiatedFrom)
+ return D;
+
+ // This might be a class template.
+ if (const auto *Tmpl = InstantiatedFrom.dyn_cast<ClassTemplateDecl *>()) {
+ // Interestingly, the source template might be a forward declaration, so we
+ // need to find the definition redeclaration.
+ for (const auto *Redecl : Tmpl->redecls()) {
+ if (cast<ClassTemplateDecl>(Redecl)->isThisDeclarationADefinition()) {
+ return Redecl;
+ }
+ }
+ assert(false && "A redecl must be a definition");
----------------
steakhal wrote:
Applied in abfe124140d29f3d30bc40a9cf155a4b4158ac21
https://github.com/llvm/llvm-project/pull/178441
More information about the cfe-commits
mailing list