[clang] [Sema] Propagate preferred_name attribute to existing specializations (PR #186741)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 15 23:59:47 PDT 2026
https://github.com/Anupamvashistha2002 created https://github.com/llvm/llvm-project/pull/186741
Fixes #106358
This PR ensures the `preferred_name` attribute is propagated to specializations
that were instantiated before the attribute was seen on the primary template.
The fix iterates through the redeclaration chain of matching specializations
and applies a cloned attribute to each, ensuring consistent diagnostic
output (the 'aka' message) regardless of which declaration node is accessed.
Validated with new test: `clang/test/SemaCXX/gh106358.cpp`
>From 5fa5fc775dd98e1371f51cd4bace293125e15caf Mon Sep 17 00:00:00 2001
From: Anupam Vashistha <anupamvashistha44 at gmail.com>
Date: Mon, 16 Mar 2026 12:13:16 +0530
Subject: [PATCH] [Sema] Propagate preferred_name attribute to existing
specializations
---
clang/lib/Sema/SemaDeclAttr.cpp | 16 +++++++++++++++-
clang/test/SemaCXX/gh106358.cpp | 18 ++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/gh106358.cpp
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 3abc69d0e4b96..0f1c867acdcfe 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1222,7 +1222,21 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
}
if (Template && declaresSameEntity(Template, CTD)) {
- D->addAttr(::new (S.Context) PreferredNameAttr(S.Context, AL, TSI));
+ auto *PNA = ::new (S.Context) PreferredNameAttr(S.Context, AL, TSI);
+ D->addAttr(PNA);
+
+ for(auto *Spec : CTD->specializations()) {
+
+ const TypeDecl *TD = static_cast<const TypeDecl *>(Spec);
+ if (S.Context.hasSameType(S.Context.getTypeDeclType(TD), TSI->getType())) {
+
+ {
+ for (auto *R : Spec->redecls()) {
+ if (!R->hasAttr<PreferredNameAttr>())
+ R->addAttr(PNA->clone(S.Context));
+ }
+ }
+ }
return;
}
}
diff --git a/clang/test/SemaCXX/gh106358.cpp b/clang/test/SemaCXX/gh106358.cpp
new file mode 100644
index 0000000000000..82d6378ad12eb
--- /dev/null
+++ b/clang/test/SemaCXX/gh106358.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// GH106358: Test that preferred_name propagates to specializations
+// instantiated before the attribute is seen.
+
+template<typename T>
+void foo(T arg) {} // expected-note {{candidate function template not viable: no known conversion from 'int' to 'my_string' (aka 'my_basic_string<char>') for 1st argument}}
+
+template<typename T> struct my_basic_string;
+using my_string = my_basic_string<char>;
+
+// This attribute application now correctly propagates to the existing specialization
+template<typename T>
+struct __attribute__((__preferred_name__(my_string))) my_basic_string {};
+
+int main() {
+ foo<my_string>(123); // expected-error {{no matching function for call to 'foo'}}
+}
More information about the cfe-commits
mailing list