llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Guillot Tony (to268)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/182208.diff


6 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/include/clang/Basic/Attr.td (+1) 
- (modified) clang/include/clang/Sema/Sema.h (+1) 
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+11-8) 
- (added) clang/test/Sema/type-dependent-attrs.cpp (+11) 
- (modified) clang/test/SemaCXX/init-priority-attr.cpp (+6) 


``````````diff
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@-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}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/182208
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to