Author: Guillot Tony Date: 2026-02-20T11:00:03-05:00 New Revision: b6d465ceaac4207bef79a0b8418b4477f3bf1597
URL: https://github.com/llvm/llvm-project/commit/b6d465ceaac4207bef79a0b8418b4477f3bf1597 DIFF: https://github.com/llvm/llvm-project/commit/b6d465ceaac4207bef79a0b8418b4477f3bf1597.diff LOG: [clang] Fix init_priority attribute by delaying type checks after the type is deduced (#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. Added: clang/test/Sema/type-dependent-attrs.cpp Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/Attr.td clang/include/clang/Sema/Sema.h clang/lib/Sema/SemaDeclAttr.cpp clang/test/SemaCXX/init-priority-attr.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 8d5d704c1766a..371fb1625cb05 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -289,6 +289,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 aec13e703ef6f..a5641e2e008cd 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -3288,6 +3288,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 41e5218f501e3..386cc49024311 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -15610,6 +15610,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 c97356d580192..3abc69d0e4b96 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; @@ -8676,3 +8668,13 @@ 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>(); + } +} diff --git a/clang/test/Sema/type-dependent-attrs.cpp b/clang/test/Sema/type-dependent-attrs.cpp new file mode 100644 index 0000000000000..ca97174084d17 --- /dev/null +++ b/clang/test/Sema/type-dependent-attrs.cpp @@ -0,0 +1,22 @@ +// 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}} + + template<typename T> + struct ST { // #ST_INIT_PRIORITY + T inner; + }; + + [[gnu::init_priority(2000)]] auto auto_t_var = ST<int>{0}; + [[gnu::init_priority(2000)]] ST<int> struct_t_var = ST<int>{0}; + [[gnu::init_priority(2000)]] ST<float> invalid_t_var = ST<int>{0}; // expected-error {{no viable conversion from 'ST<int>' to 'ST<float>'}} \ + expected-note@#ST_INIT_PRIORITY {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'ST<int>' to 'const ST<float> &' for 1st argument}} \ + expected-note@#ST_INIT_PRIORITY {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'ST<int>' to 'ST<float> &&' 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@-1 {{unknown attribute 'init_priority' ignored}} } + +struct S1 {} s1; +[[gnu::init_priority(1001)]] auto auto_var = s1; +// unknown-warning@-1 {{unknown attribute 'gnu::init_priority' ignored}} +[[gnu::init_priority(1001)]] S1 struct_var = s1; +// unknown-warning@-1 {{unknown attribute 'gnu::init_priority' ignored}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
