[clang] [clang] Fix init_priority attribute by delaying type checks after the type is deduced (PR #182208)
Guillot Tony via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 18:14:37 PST 2026
https://github.com/to268 created https://github.com/llvm/llvm-project/pull/182208
This PR fixes the way we are dealing with type checks for the `init_priority`
attribute, by delaying these checks after we are deducing the type.
It is a follow up from the list of affected attributes in PR #164440.
>From 0f1f0cc6976c457f27fc3890da6dc2998af53c99 Mon Sep 17 00:00:00 2001
From: Tony Guillot <tony.guillot at protonmail.com>
Date: Thu, 19 Feb 2026 02:46:41 +0100
Subject: [PATCH] Fixed init_priority attribute when used with auto
---
clang/docs/ReleaseNotes.rst | 1 +
clang/include/clang/Basic/Attr.td | 1 +
clang/include/clang/Sema/Sema.h | 1 +
clang/lib/Sema/SemaDeclAttr.cpp | 19 +++++++++++--------
clang/test/Sema/type-dependent-attrs.cpp | 11 +++++++++++
clang/test/SemaCXX/init-priority-attr.cpp | 6 ++++++
6 files changed, 31 insertions(+), 8 deletions(-)
create mode 100644 clang/test/Sema/type-dependent-attrs.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c648e8b0ec6fa..67d01fae0c2f0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -275,6 +275,7 @@ Bug Fixes to Compiler Builtins
Bug Fixes to Attribute Support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed a behavioral discrepancy between deleted functions and private members when checking the ``enable_if`` attribute. (#GH175895)
+- Fixed ``init_priority`` attribute by delaying type checks until after the type is deduced.
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 2621d178d99e8..a18d02f12d6d8 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -3275,6 +3275,7 @@ def InitPriority : InheritableAttr, TargetSpecificAttr<TargetSupportsInitPriorit
let Args = [UnsignedArgument<"Priority">];
let Subjects = SubjectList<[Var], ErrorDiag>;
let Documentation = [InitPriorityDocs];
+ let IsTypeDependent = 1;
}
def Section : InheritableAttr {
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9424b80d5cdb6..35ea799296805 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -15577,6 +15577,7 @@ class Sema final : public SemaBase {
ActOnEffectExpression(Expr *CondExpr, StringRef AttributeName);
void ActOnCleanupAttr(Decl *D, const Attr *A);
+ void ActOnInitPriorityAttr(Decl *D, const Attr *A);
private:
/// The implementation of RequireCompleteType
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 5dbff18fff7a9..e69c49b87a31e 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -3834,14 +3834,6 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
AL.setInvalid();
return;
}
- QualType T = cast<VarDecl>(D)->getType();
- if (S.Context.getAsArrayType(T))
- T = S.Context.getBaseElementType(T);
- if (!T->isRecordType()) {
- S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
- AL.setInvalid();
- return;
- }
Expr *E = AL.getArgAsExpr(0);
uint32_t prioritynum;
@@ -8660,3 +8652,14 @@ void Sema::ActOnCleanupAttr(Decl *D, const Attr *A) {
return;
}
}
+
+void Sema::ActOnInitPriorityAttr(Decl *D, const Attr *A) {
+ QualType T = cast<VarDecl>(D)->getType();
+ if (this->Context.getAsArrayType(T))
+ T = this->Context.getBaseElementType(T);
+ if (!T->isRecordType()) {
+ this->Diag(A->getLoc(), diag::err_init_priority_object_attr);
+ D->dropAttr<InitPriorityAttr>();
+ return;
+ }
+}
diff --git a/clang/test/Sema/type-dependent-attrs.cpp b/clang/test/Sema/type-dependent-attrs.cpp
new file mode 100644
index 0000000000000..366b130c62c94
--- /dev/null
+++ b/clang/test/Sema/type-dependent-attrs.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace InitPriorityAttribute {
+ struct S1 {} s1;
+ struct S2 {} s2; // #S2_INIT_PRIORITY
+ [[gnu::init_priority(1000)]] auto auto_var = s1;
+ [[gnu::init_priority(1000)]] S1 struct_var = s1;
+ [[gnu::init_priority(1000)]] S2 invalid_var = s1; // expected-error {{no viable conversion from 'struct S1' to 'S2'}} \
+ expected-note@#S2_INIT_PRIORITY {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'struct S1' to 'const S2 &' for 1st argument}} \
+ expected-note@#S2_INIT_PRIORITY {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'struct S1' to 'S2 &&' for 1st argument}}
+}
diff --git a/clang/test/SemaCXX/init-priority-attr.cpp b/clang/test/SemaCXX/init-priority-attr.cpp
index 8151bf7aecb95..c5fab7b42502e 100644
--- a/clang/test/SemaCXX/init-priority-attr.cpp
+++ b/clang/test/SemaCXX/init-priority-attr.cpp
@@ -65,3 +65,9 @@ int main() {
Two foo __attribute__((init_priority(1001))); // expected-error {{can only use 'init_priority' attribute on file-scope definitions of objects of class type}}
// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
}
+
+struct S1 {} s1;
+[[gnu::init_priority(1001)]] auto auto_var = s1;
+// unknown-warning at -1 {{unknown attribute 'gnu::init_priority' ignored}}
+[[gnu::init_priority(1001)]] S1 struct_var = s1;
+// unknown-warning at -1 {{unknown attribute 'gnu::init_priority' ignored}}
More information about the cfe-commits
mailing list