[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-21 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 524108.
yronglin added a comment.

Test asm dump.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/ParsedAttr.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/AttrImpl.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/AST/ast-dump-attr.cpp
  clang/test/Sema/aix-attr-aligned-vector-warn.c
  clang/test/Sema/aix-attr-aligned-vector-warn.cpp
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/cxx11-attr-print.cpp
  clang/test/SemaCXX/sizeless-1.cpp
  clang/utils/TableGen/ClangAttrEmitter.cpp

Index: clang/utils/TableGen/ClangAttrEmitter.cpp
===
--- clang/utils/TableGen/ClangAttrEmitter.cpp
+++ clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -508,6 +508,16 @@
   OS << "assert(!is" << getLowerName() << "Expr);\n";
   OS << "return " << getLowerName() << "Type;\n";
   OS << "  }";
+
+  OS << "  std::optional getCached" << getUpperName()
+ << "Value() const {\n";
+  OS << "return " << getLowerName() << "Cache;\n";
+  OS << "  }";
+
+  OS << "  void setCached" << getUpperName()
+ << "Value(unsigned AlignVal) {\n";
+  OS << "" << getLowerName() << "Cache = AlignVal;\n";
+  OS << "  }";
 }
 
 void writeAccessorDefinitions(raw_ostream ) const override {
@@ -530,21 +540,6 @@
   OS << "  return " << getLowerName()
  << "Type->getType()->containsErrors();\n";
   OS << "}\n";
-
-  // FIXME: Do not do the calculation here
-  // FIXME: Handle types correctly
-  // A null pointer means maximum alignment
-  OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
- << "(ASTContext ) const {\n";
-  OS << "  assert(!is" << getUpperName() << "Dependent());\n";
-  OS << "  if (is" << getLowerName() << "Expr)\n";
-  OS << "return " << getLowerName() << "Expr ? " << getLowerName()
- << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
- << " * Ctx.getCharWidth() : "
- << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
-  OS << "  else\n";
-  OS << "return 0; // FIXME\n";
-  OS << "}\n";
 }
 
 void writeASTVisitorTraversal(raw_ostream ) const override {
@@ -601,7 +596,8 @@
   OS << "union {\n";
   OS << "Expr *" << getLowerName() << "Expr;\n";
   OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
-  OS << "};";
+  OS << "};\n";
+  OS << "std::optional " << getLowerName() << "Cache;\n";
 }
 
 void writePCHReadArgs(raw_ostream ) const override {
@@ -628,14 +624,17 @@
 }
 
 std::string getIsOmitted() const override {
-  return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str()
- + "Expr";
+  return "!((is" + getLowerName().str() + "Expr && " +
+ getLowerName().str() + "Expr) || (!is" + getLowerName().str() +
+ "Expr && " + getLowerName().str() + "Type))";
 }
 
 void writeValue(raw_ostream ) const override {
   OS << "\";\n";
-  OS << "" << getLowerName()
- << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (is" << getLowerName() << "Expr && " << getLowerName() << "Expr)";
+  OS << "  " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (!is" << getLowerName() << "Expr && " << getLowerName() << "Type)";
+  OS << "  " << getLowerName() << "Type->getType().print(OS, Policy);\n";
   OS << "OS << \"";
 }
 
Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/cxx11-attr-print.cpp
===
--- clang/test/SemaCXX/cxx11-attr-print.cpp
+++ clang/test/SemaCXX/cxx11-attr-print.cpp

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-21 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 524107.
yronglin added a comment.

Remove extra include.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/ParsedAttr.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/AttrImpl.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/aix-attr-aligned-vector-warn.c
  clang/test/Sema/aix-attr-aligned-vector-warn.cpp
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/cxx11-attr-print.cpp
  clang/test/SemaCXX/sizeless-1.cpp
  clang/utils/TableGen/ClangAttrEmitter.cpp

Index: clang/utils/TableGen/ClangAttrEmitter.cpp
===
--- clang/utils/TableGen/ClangAttrEmitter.cpp
+++ clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -508,6 +508,16 @@
   OS << "assert(!is" << getLowerName() << "Expr);\n";
   OS << "return " << getLowerName() << "Type;\n";
   OS << "  }";
+
+  OS << "  std::optional getCached" << getUpperName()
+ << "Value() const {\n";
+  OS << "return " << getLowerName() << "Cache;\n";
+  OS << "  }";
+
+  OS << "  void setCached" << getUpperName()
+ << "Value(unsigned AlignVal) {\n";
+  OS << "" << getLowerName() << "Cache = AlignVal;\n";
+  OS << "  }";
 }
 
 void writeAccessorDefinitions(raw_ostream ) const override {
@@ -530,21 +540,6 @@
   OS << "  return " << getLowerName()
  << "Type->getType()->containsErrors();\n";
   OS << "}\n";
-
-  // FIXME: Do not do the calculation here
-  // FIXME: Handle types correctly
-  // A null pointer means maximum alignment
-  OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
- << "(ASTContext ) const {\n";
-  OS << "  assert(!is" << getUpperName() << "Dependent());\n";
-  OS << "  if (is" << getLowerName() << "Expr)\n";
-  OS << "return " << getLowerName() << "Expr ? " << getLowerName()
- << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
- << " * Ctx.getCharWidth() : "
- << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
-  OS << "  else\n";
-  OS << "return 0; // FIXME\n";
-  OS << "}\n";
 }
 
 void writeASTVisitorTraversal(raw_ostream ) const override {
@@ -601,7 +596,8 @@
   OS << "union {\n";
   OS << "Expr *" << getLowerName() << "Expr;\n";
   OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
-  OS << "};";
+  OS << "};\n";
+  OS << "std::optional " << getLowerName() << "Cache;\n";
 }
 
 void writePCHReadArgs(raw_ostream ) const override {
@@ -628,14 +624,17 @@
 }
 
 std::string getIsOmitted() const override {
-  return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str()
- + "Expr";
+  return "!((is" + getLowerName().str() + "Expr && " +
+ getLowerName().str() + "Expr) || (!is" + getLowerName().str() +
+ "Expr && " + getLowerName().str() + "Type))";
 }
 
 void writeValue(raw_ostream ) const override {
   OS << "\";\n";
-  OS << "" << getLowerName()
- << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (is" << getLowerName() << "Expr && " << getLowerName() << "Expr)";
+  OS << "  " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (!is" << getLowerName() << "Expr && " << getLowerName() << "Type)";
+  OS << "  " << getLowerName() << "Type->getType().print(OS, Policy);\n";
   OS << "OS << \"";
 }
 
Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/cxx11-attr-print.cpp
===
--- clang/test/SemaCXX/cxx11-attr-print.cpp
+++ clang/test/SemaCXX/cxx11-attr-print.cpp
@@ -28,7 +28,7 @@
 // CHECK: 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-21 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 524106.
yronglin added a comment.

Improve the AST fidelity of ``alignas`` and ``_Alignas`` attribute.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/ParsedAttr.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/AttrImpl.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/aix-attr-aligned-vector-warn.c
  clang/test/Sema/aix-attr-aligned-vector-warn.cpp
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/cxx11-attr-print.cpp
  clang/test/SemaCXX/sizeless-1.cpp
  clang/utils/TableGen/ClangAttrEmitter.cpp

Index: clang/utils/TableGen/ClangAttrEmitter.cpp
===
--- clang/utils/TableGen/ClangAttrEmitter.cpp
+++ clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -508,6 +508,16 @@
   OS << "assert(!is" << getLowerName() << "Expr);\n";
   OS << "return " << getLowerName() << "Type;\n";
   OS << "  }";
+
+  OS << "  std::optional getCached" << getUpperName()
+ << "Value() const {\n";
+  OS << "return " << getLowerName() << "Cache;\n";
+  OS << "  }";
+
+  OS << "  void setCached" << getUpperName()
+ << "Value(unsigned AlignVal) {\n";
+  OS << "" << getLowerName() << "Cache = AlignVal;\n";
+  OS << "  }";
 }
 
 void writeAccessorDefinitions(raw_ostream ) const override {
@@ -530,21 +540,6 @@
   OS << "  return " << getLowerName()
  << "Type->getType()->containsErrors();\n";
   OS << "}\n";
-
-  // FIXME: Do not do the calculation here
-  // FIXME: Handle types correctly
-  // A null pointer means maximum alignment
-  OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
- << "(ASTContext ) const {\n";
-  OS << "  assert(!is" << getUpperName() << "Dependent());\n";
-  OS << "  if (is" << getLowerName() << "Expr)\n";
-  OS << "return " << getLowerName() << "Expr ? " << getLowerName()
- << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
- << " * Ctx.getCharWidth() : "
- << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
-  OS << "  else\n";
-  OS << "return 0; // FIXME\n";
-  OS << "}\n";
 }
 
 void writeASTVisitorTraversal(raw_ostream ) const override {
@@ -601,7 +596,8 @@
   OS << "union {\n";
   OS << "Expr *" << getLowerName() << "Expr;\n";
   OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
-  OS << "};";
+  OS << "};\n";
+  OS << "std::optional " << getLowerName() << "Cache;\n";
 }
 
 void writePCHReadArgs(raw_ostream ) const override {
@@ -628,14 +624,17 @@
 }
 
 std::string getIsOmitted() const override {
-  return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str()
- + "Expr";
+  return "!((is" + getLowerName().str() + "Expr && " +
+ getLowerName().str() + "Expr) || (!is" + getLowerName().str() +
+ "Expr && " + getLowerName().str() + "Type))";
 }
 
 void writeValue(raw_ostream ) const override {
   OS << "\";\n";
-  OS << "" << getLowerName()
- << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (is" << getLowerName() << "Expr && " << getLowerName() << "Expr)";
+  OS << "  " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
+  OS << "if (!is" << getLowerName() << "Expr && " << getLowerName() << "Type)";
+  OS << "  " << getLowerName() << "Type->getType().print(OS, Policy);\n";
   OS << "OS << \"";
 }
 
Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/cxx11-attr-print.cpp
===
--- clang/test/SemaCXX/cxx11-attr-print.cpp
+++ 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-17 Thread Yurong via Phabricator via cfe-commits
yronglin added a comment.

In D150528#4349729 , @aaron.ballman 
wrote:

> In D150528#4349711 , @yronglin 
> wrote:
>
>> Sorry for the late reply and thanks for your tips! @aaron.ballman  I 
>> followed your advice to update this patch, and below AST generated  by clang:
>>
>>   class alignas(void *) Foo {};
>>   _Alignas(void*) char align32array[128];
>>   
>>   template  class alignas(T) Bar {};
>>   template  class alignas(N) FooBar {};
>>   
>>   int main() {
>> Bar bar;
>> FooBar<4> fooBar;
>> return 0;
>>   }
>>
>>
>>
>>   ➜  test ../rel/bin/clang++ -cc1 -triple x86_64-pc-linux -ast-dump 
>> ./alignas.cpp -std=c++11
>>alignas _Alignas alignas alignasTranslationUnitDecl 0x14b043208 <> sloc>> 
>>   |-TypedefDecl 0x14b043a70 <>  implicit 
>> __int128_t '__int128'
>>   | `-BuiltinType 0x14b0437d0 '__int128'
>>   |-TypedefDecl 0x14b043ae0 <>  implicit 
>> __uint128_t 'unsigned __int128'
>>   | `-BuiltinType 0x14b0437f0 'unsigned __int128'
>>   |-TypedefDecl 0x14b043e58 <>  implicit 
>> __NSConstantString '__NSConstantString_tag'
>>   | `-RecordType 0x14b043bd0 '__NSConstantString_tag'
>>   |   `-CXXRecord 0x14b043b38 '__NSConstantString_tag'
>>   |-TypedefDecl 0x14b043ef0 <>  implicit 
>> __builtin_ms_va_list 'char *'
>>   | `-PointerType 0x14b043eb0 'char *'
>>   |   `-BuiltinType 0x14b0432b0 'char'
>>   |-TypedefDecl 0x14b087858 <>  implicit 
>> __builtin_va_list '__va_list_tag[1]'
>>   | `-ConstantArrayType 0x14b087800 '__va_list_tag[1]' 1 
>>   |   `-RecordType 0x14b043fe0 '__va_list_tag'
>>   | `-CXXRecord 0x14b043f48 '__va_list_tag'
>>   |-CXXRecordDecl 0x14b0878e8 <./alignas.cpp:1:1, col:28> col:23 class Foo 
>> definition
>>   | |-DefinitionData pass_in_registers empty aggregate standard_layout 
>> trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor 
>> can_const_default_init
>>   | | |-DefaultConstructor exists trivial constexpr needs_implicit 
>> defaulted_is_constexpr
>>   | | |-CopyConstructor simple trivial has_const_param needs_implicit 
>> implicit_has_const_param
>>   | | |-MoveConstructor exists simple trivial needs_implicit
>>   | | |-CopyAssignment simple trivial has_const_param needs_implicit 
>> implicit_has_const_param
>>   | | |-MoveAssignment exists simple trivial needs_implicit
>>   | | `-Destructor simple irrelevant trivial needs_implicit
>>   | |-AlignedAttr 0x14b087a08  alignas 'void *'
>>   | `-CXXRecordDecl 0x14b087a68  col:23 implicit class Foo
>>   |-VarDecl 0x14b087be8  col:22 align32array 'char[128]'
>>   | `-AlignedAttr 0x14b087c50  _Alignas 'void *'
>>   |-ClassTemplateDecl 0x14b087e80  col:40 Bar
>>   | |-TemplateTypeParmDecl 0x14b087cf8  col:20 referenced 
>> typename depth 0 index 0 T
>>   | |-CXXRecordDecl 0x14b087df0  col:40 class Bar definition
>>   | | |-DefinitionData empty aggregate standard_layout trivially_copyable 
>> pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
>>   | | | |-DefaultConstructor exists trivial constexpr needs_implicit 
>> defaulted_is_constexpr
>>   | | | |-CopyConstructor simple trivial has_const_param needs_implicit 
>> implicit_has_const_param
>>   | | | |-MoveConstructor exists simple trivial needs_implicit
>>   | | | |-CopyAssignment simple trivial has_const_param needs_implicit 
>> implicit_has_const_param
>>   | | | |-MoveAssignment exists simple trivial needs_implicit
>>   | | | `-Destructor simple irrelevant trivial needs_implicit
>>   | | |-AlignedAttr 0x14b0880d0  alignas 'T'
>>   | | `-CXXRecordDecl 0x14b088130  col:40 implicit class Bar
>>   | `-ClassTemplateSpecializationDecl 0x14b09e200  col:40 
>> class Bar definition
>>   |   |-DefinitionData pass_in_registers empty aggregate standard_layout 
>> trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor 
>> can_const_default_init
>>   |   | |-DefaultConstructor exists trivial constexpr defaulted_is_constexpr
>>   |   | |-CopyConstructor simple trivial has_const_param 
>> implicit_has_const_param
>>   |   | |-MoveConstructor exists simple trivial
>>   |   | |-CopyAssignment simple trivial has_const_param needs_implicit 
>> implicit_has_const_param
>>   |   | |-MoveAssignment exists simple trivial needs_implicit
>>   |   | `-Destructor simple irrelevant trivial needs_implicit
>>   |   |-TemplateArgument type 'int'
>>   |   | `-BuiltinType 0x14b043310 'int'
>>   |   |-AlignedAttr 0x14b09e458  alignas 'int':'int'
>>   |   |-CXXRecordDecl 0x14b09e520  col:40 implicit class Bar
>>   |   |-CXXConstructorDecl 0x14b09e5e0  col:40 implicit used 
>> constexpr Bar 'void () noexcept' inline default trivial
>>   |   | `-CompoundStmt 0x14b09eb48 
>>   |   |-CXXConstructorDecl 0x14b09e778  col:40 implicit constexpr 
>> Bar 'void (const Bar &)' inline default trivial noexcept-unevaluated 
>> 0x14b09e778
>>   |   | `-ParmVarDecl 0x14b09e898  col:40 'const Bar &'
>>   |   `-CXXConstructorDecl 0x14b09e978  col:40 implicit constexpr 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-17 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D150528#4349711 , @yronglin wrote:

> Sorry for the late reply and thanks for your tips! @aaron.ballman  I followed 
> your advice to update this patch, and below AST generated  by clang:
>
>   class alignas(void *) Foo {};
>   _Alignas(void*) char align32array[128];
>   
>   template  class alignas(T) Bar {};
>   template  class alignas(N) FooBar {};
>   
>   int main() {
> Bar bar;
> FooBar<4> fooBar;
> return 0;
>   }
>
>
>
>   ➜  test ../rel/bin/clang++ -cc1 -triple x86_64-pc-linux -ast-dump 
> ./alignas.cpp -std=c++11
>alignas _Alignas alignas alignasTranslationUnitDecl 0x14b043208 < sloc>> 
>   |-TypedefDecl 0x14b043a70 <>  implicit 
> __int128_t '__int128'
>   | `-BuiltinType 0x14b0437d0 '__int128'
>   |-TypedefDecl 0x14b043ae0 <>  implicit 
> __uint128_t 'unsigned __int128'
>   | `-BuiltinType 0x14b0437f0 'unsigned __int128'
>   |-TypedefDecl 0x14b043e58 <>  implicit 
> __NSConstantString '__NSConstantString_tag'
>   | `-RecordType 0x14b043bd0 '__NSConstantString_tag'
>   |   `-CXXRecord 0x14b043b38 '__NSConstantString_tag'
>   |-TypedefDecl 0x14b043ef0 <>  implicit 
> __builtin_ms_va_list 'char *'
>   | `-PointerType 0x14b043eb0 'char *'
>   |   `-BuiltinType 0x14b0432b0 'char'
>   |-TypedefDecl 0x14b087858 <>  implicit 
> __builtin_va_list '__va_list_tag[1]'
>   | `-ConstantArrayType 0x14b087800 '__va_list_tag[1]' 1 
>   |   `-RecordType 0x14b043fe0 '__va_list_tag'
>   | `-CXXRecord 0x14b043f48 '__va_list_tag'
>   |-CXXRecordDecl 0x14b0878e8 <./alignas.cpp:1:1, col:28> col:23 class Foo 
> definition
>   | |-DefinitionData pass_in_registers empty aggregate standard_layout 
> trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor 
> can_const_default_init
>   | | |-DefaultConstructor exists trivial constexpr needs_implicit 
> defaulted_is_constexpr
>   | | |-CopyConstructor simple trivial has_const_param needs_implicit 
> implicit_has_const_param
>   | | |-MoveConstructor exists simple trivial needs_implicit
>   | | |-CopyAssignment simple trivial has_const_param needs_implicit 
> implicit_has_const_param
>   | | |-MoveAssignment exists simple trivial needs_implicit
>   | | `-Destructor simple irrelevant trivial needs_implicit
>   | |-AlignedAttr 0x14b087a08  alignas 'void *'
>   | `-CXXRecordDecl 0x14b087a68  col:23 implicit class Foo
>   |-VarDecl 0x14b087be8  col:22 align32array 'char[128]'
>   | `-AlignedAttr 0x14b087c50  _Alignas 'void *'
>   |-ClassTemplateDecl 0x14b087e80  col:40 Bar
>   | |-TemplateTypeParmDecl 0x14b087cf8  col:20 referenced 
> typename depth 0 index 0 T
>   | |-CXXRecordDecl 0x14b087df0  col:40 class Bar definition
>   | | |-DefinitionData empty aggregate standard_layout trivially_copyable pod 
> trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
>   | | | |-DefaultConstructor exists trivial constexpr needs_implicit 
> defaulted_is_constexpr
>   | | | |-CopyConstructor simple trivial has_const_param needs_implicit 
> implicit_has_const_param
>   | | | |-MoveConstructor exists simple trivial needs_implicit
>   | | | |-CopyAssignment simple trivial has_const_param needs_implicit 
> implicit_has_const_param
>   | | | |-MoveAssignment exists simple trivial needs_implicit
>   | | | `-Destructor simple irrelevant trivial needs_implicit
>   | | |-AlignedAttr 0x14b0880d0  alignas 'T'
>   | | `-CXXRecordDecl 0x14b088130  col:40 implicit class Bar
>   | `-ClassTemplateSpecializationDecl 0x14b09e200  col:40 
> class Bar definition
>   |   |-DefinitionData pass_in_registers empty aggregate standard_layout 
> trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor 
> can_const_default_init
>   |   | |-DefaultConstructor exists trivial constexpr defaulted_is_constexpr
>   |   | |-CopyConstructor simple trivial has_const_param 
> implicit_has_const_param
>   |   | |-MoveConstructor exists simple trivial
>   |   | |-CopyAssignment simple trivial has_const_param needs_implicit 
> implicit_has_const_param
>   |   | |-MoveAssignment exists simple trivial needs_implicit
>   |   | `-Destructor simple irrelevant trivial needs_implicit
>   |   |-TemplateArgument type 'int'
>   |   | `-BuiltinType 0x14b043310 'int'
>   |   |-AlignedAttr 0x14b09e458  alignas 'int':'int'
>   |   |-CXXRecordDecl 0x14b09e520  col:40 implicit class Bar
>   |   |-CXXConstructorDecl 0x14b09e5e0  col:40 implicit used 
> constexpr Bar 'void () noexcept' inline default trivial
>   |   | `-CompoundStmt 0x14b09eb48 
>   |   |-CXXConstructorDecl 0x14b09e778  col:40 implicit constexpr Bar 
> 'void (const Bar &)' inline default trivial noexcept-unevaluated 
> 0x14b09e778
>   |   | `-ParmVarDecl 0x14b09e898  col:40 'const Bar &'
>   |   `-CXXConstructorDecl 0x14b09e978  col:40 implicit constexpr Bar 
> 'void (Bar &&)' inline default trivial noexcept-unevaluated 0x14b09e978
>   | `-ParmVarDecl 0x14b09ea98  col:40 'Bar &&'
>   |-ClassTemplateDecl 0x14b088310  col:40 FooBar
>   

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-17 Thread Yurong via Phabricator via cfe-commits
yronglin added a comment.

In D150528#4343102 , @aaron.ballman 
wrote:

> In D150528#4342646 , @yronglin 
> wrote:
>
>> In D150528#4342600 , 
>> @aaron.ballman wrote:
>>
>>> In D150528#4342535 , @yronglin 
>>> wrote:
>>>
 Thanks a lot for your review @aaron.ballman ! BTW, does the most good 
 approach is that we just add a new `ParsedAttribute` with `TypeSourceInfo` 
 but not an `UnaryExprOrTypeTraitExpr`, and only do a sema check same as 
 `alignof(type-id)`?
>>>
>>> Because you've already got a `ParsedAttr`, I think you'd use this 
>>> constructor: 
>>> https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Sema/ParsedAttr.h#L274
>>>  and move the call to `GetTypeFromParser()` until later, but it should 
>>> still be modelled as a `UnaryExprOrTypeTraitExpr`.
>>
>> Many thanks for the tips, I think the next step is to build 
>> `UnaryExprOrTypeTraitExpr ` in this place 
>> (https://github.com/llvm/llvm-project/blob/9715af434579022b5ef31781be40b722d7e63bee/clang/lib/Sema/SemaDeclAttr.cpp#L4335-L4358)
>>  and then calculate the alignment? If this approach is correct, I'm glad to 
>> investigate it.
>
> Hmm, not quite. Roping in @erichkeane in case he's got other ideas, but I 
> think this will be more involved. What I think needs to happen is:
>
> - We need to add another argument to `Aligned` in Attr.td so that the 
> attribute takes either an expression argument or a type argument.
> - We need to update SemaDeclAttr.cpp and other places accordingly
> - We need to add an overload of `Sema::AddAlignedAttr()` that takes a 
> `ParsedType` and call this overload when given `alignas(type)`
> - We need to add code to TreeTransform.h to instantiate a dependent type so 
> we can support `template  alignas(Ty) int foo = 12;`
>
> Your approach of creating a `UnaryExprOrTypeTraitExpr` would still end up 
> having us model as `alignas(alignof(type))` because the argument to the 
> `alignas` attribute would be the type trait.

Sorry for the late reply and thanks for your tips! @aaron.ballman  I followed 
your advice to update this patch, and below AST generated  by clang:

  class alignas(void *) Foo {};
  _Alignas(void*) char align32array[128];
  
  template  class alignas(T) Bar {};
  template  class alignas(N) FooBar {};
  
  int main() {
Bar bar;
FooBar<4> fooBar;
return 0;
  }



  ➜  test ../rel/bin/clang++ -cc1 -triple x86_64-pc-linux -ast-dump 
./alignas.cpp -std=c++11
   alignas _Alignas alignas alignasTranslationUnitDecl 0x14b043208 <> 
  |-TypedefDecl 0x14b043a70 <>  implicit __int128_t 
'__int128'
  | `-BuiltinType 0x14b0437d0 '__int128'
  |-TypedefDecl 0x14b043ae0 <>  implicit 
__uint128_t 'unsigned __int128'
  | `-BuiltinType 0x14b0437f0 'unsigned __int128'
  |-TypedefDecl 0x14b043e58 <>  implicit 
__NSConstantString '__NSConstantString_tag'
  | `-RecordType 0x14b043bd0 '__NSConstantString_tag'
  |   `-CXXRecord 0x14b043b38 '__NSConstantString_tag'
  |-TypedefDecl 0x14b043ef0 <>  implicit 
__builtin_ms_va_list 'char *'
  | `-PointerType 0x14b043eb0 'char *'
  |   `-BuiltinType 0x14b0432b0 'char'
  |-TypedefDecl 0x14b087858 <>  implicit 
__builtin_va_list '__va_list_tag[1]'
  | `-ConstantArrayType 0x14b087800 '__va_list_tag[1]' 1 
  |   `-RecordType 0x14b043fe0 '__va_list_tag'
  | `-CXXRecord 0x14b043f48 '__va_list_tag'
  |-CXXRecordDecl 0x14b0878e8 <./alignas.cpp:1:1, col:28> col:23 class Foo 
definition
  | |-DefinitionData pass_in_registers empty aggregate standard_layout 
trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor 
can_const_default_init
  | | |-DefaultConstructor exists trivial constexpr needs_implicit 
defaulted_is_constexpr
  | | |-CopyConstructor simple trivial has_const_param needs_implicit 
implicit_has_const_param
  | | |-MoveConstructor exists simple trivial needs_implicit
  | | |-CopyAssignment simple trivial has_const_param needs_implicit 
implicit_has_const_param
  | | |-MoveAssignment exists simple trivial needs_implicit
  | | `-Destructor simple irrelevant trivial needs_implicit
  | |-AlignedAttr 0x14b087a08  alignas 'void *'
  | `-CXXRecordDecl 0x14b087a68  col:23 implicit class Foo
  |-VarDecl 0x14b087be8  col:22 align32array 'char[128]'
  | `-AlignedAttr 0x14b087c50  _Alignas 'void *'
  |-ClassTemplateDecl 0x14b087e80  col:40 Bar
  | |-TemplateTypeParmDecl 0x14b087cf8  col:20 referenced 
typename depth 0 index 0 T
  | |-CXXRecordDecl 0x14b087df0  col:40 class Bar definition
  | | |-DefinitionData empty aggregate standard_layout trivially_copyable pod 
trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
  | | | |-DefaultConstructor exists trivial constexpr needs_implicit 
defaulted_is_constexpr
  | | | |-CopyConstructor simple trivial has_const_param needs_implicit 
implicit_has_const_param
  | | | |-MoveConstructor exists simple 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a subscriber: erichkeane.
aaron.ballman added a comment.

In D150528#4342646 , @yronglin wrote:

> In D150528#4342600 , @aaron.ballman 
> wrote:
>
>> In D150528#4342535 , @yronglin 
>> wrote:
>>
>>> Thanks a lot for your review @aaron.ballman ! BTW, does the most good 
>>> approach is that we just add a new `ParsedAttribute` with `TypeSourceInfo` 
>>> but not an `UnaryExprOrTypeTraitExpr`, and only do a sema check same as 
>>> `alignof(type-id)`?
>>
>> Because you've already got a `ParsedAttr`, I think you'd use this 
>> constructor: 
>> https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Sema/ParsedAttr.h#L274
>>  and move the call to `GetTypeFromParser()` until later, but it should still 
>> be modelled as a `UnaryExprOrTypeTraitExpr`.
>
> Many thanks for the tips, I think the next step is to build 
> `UnaryExprOrTypeTraitExpr ` in this place 
> (https://github.com/llvm/llvm-project/blob/9715af434579022b5ef31781be40b722d7e63bee/clang/lib/Sema/SemaDeclAttr.cpp#L4335-L4358)
>  and then calculate the alignment? If this approach is correct, I'm glad to 
> investigate it.

Hmm, not quite. Roping in @erichkeane in case he's got other ideas, but I think 
this will be more involved. What I think needs to happen is:

- We need to add another argument to `Aligned` in Attr.td so that the attribute 
takes either an expression argument or a type argument.
- We need to update SemaDeclAttr.cpp and other places accordingly
- We need to add an overload of `Sema::AddAlignedAttr()` that takes a 
`ParsedType` and call this overload when given `alignas(type)`
- We need to add code to TreeTransform.h to instantiate a dependent type so we 
can support `template  alignas(Ty) int foo = 12;`

Your approach of creating a `UnaryExprOrTypeTraitExpr` would still end up 
having us model as `alignas(alignof(type))` because the argument to the 
`alignas` attribute would be the type trait.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Yurong via Phabricator via cfe-commits
yronglin marked 2 inline comments as done.
yronglin added a comment.

In D150528#4342600 , @aaron.ballman 
wrote:

> In D150528#4342535 , @yronglin 
> wrote:
>
>> Thanks a lot for your review @aaron.ballman ! BTW, does the most good 
>> approach is that we just add a new `ParsedAttribute` with `TypeSourceInfo` 
>> but not an `UnaryExprOrTypeTraitExpr`, and only do a sema check same as 
>> `alignof(type-id)`?
>
> Because you've already got a `ParsedAttr`, I think you'd use this 
> constructor: 
> https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Sema/ParsedAttr.h#L274
>  and move the call to `GetTypeFromParser()` until later, but it should still 
> be modelled as a `UnaryExprOrTypeTraitExpr`.

Many thanks for the tips, I think the next step is to build 
`UnaryExprOrTypeTraitExpr ` in this place 
(https://github.com/llvm/llvm-project/blob/9715af434579022b5ef31781be40b722d7e63bee/clang/lib/Sema/SemaDeclAttr.cpp#L4335-L4358)
 and then calculate the alignment? If this approach is correct, I'm glad to 
investigate it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D150528#4342535 , @yronglin wrote:

> Thanks a lot for your review @aaron.ballman ! BTW, does the most good 
> approach is that we just add a new `ParsedAttribute` with `TypeSourceInfo` 
> but not an `UnaryExprOrTypeTraitExpr`, and only do a sema check same as 
> `alignof(type-id)`?

Because you've already got a `ParsedAttr`, I think you'd use this constructor: 
https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Sema/ParsedAttr.h#L274
 and move the call to `GetTypeFromParser()` until later, but it should still be 
modelled as a `UnaryExprOrTypeTraitExpr`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Yurong via Phabricator via cfe-commits
yronglin marked 3 inline comments as done.
yronglin added a comment.

Thanks a lot for your review @aaron.ballman ! BTW, does the most good approach 
is that we just add a new `ParsedAttribute` with `TypeSourceInfo` but not an 
`UnaryExprOrTypeTraitExpr`?




Comment at: clang/docs/ReleaseNotes.rst:279-281
+- Clang now correctly diagnoses when the argument to alignas is an incomplete 
type.
+  (`#55175: `_,
+  Incorrect mention of 'alignof' in a diagnostic about 'alignas').

aaron.ballman wrote:
> 
+1, and add _Alignas



Comment at: clang/lib/Sema/SemaExpr.cpp:4714
+/// [dcl.align] An alignment-specifier of the form alignas(type-id) has the 
same
+/// effect as alignas(​alignof(type-id)).
+ExprResult Sema::ActOnAlignasTypeArgument(StringRef KWName, ParsedType Ty,

aaron.ballman wrote:
> It looks like a zero-width joiner was added here around the `!` that should 
> be removed.
Thanks, removed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 522201.
yronglin added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/sizeless-1.cpp

Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/builtin-align-cxx.cpp
===
--- clang/test/SemaCXX/builtin-align-cxx.cpp
+++ clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -238,3 +238,6 @@
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
+
+// Check the diagnostic message
+_Alignas(void) char align_void_array[1]; // expected-error {{invalid application of '_Alignas' to an incomplete type 'void'}}
Index: clang/test/SemaCXX/attr-cxx0x.cpp
===
--- clang/test/SemaCXX/attr-cxx0x.cpp
+++ clang/test/SemaCXX/attr-cxx0x.cpp
@@ -50,3 +50,6 @@
 void func(void);
 
 alignas(4) auto PR19252 = 0;
+
+// Check the diagnostic message
+class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}
Index: clang/test/Sema/sizeless-1.c
===
--- clang/test/Sema/sizeless-1.c
+++ clang/test/Sema/sizeless-1.c
@@ -64,7 +64,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -2674,7 +2674,8 @@
  SourceLocation OpLoc,
  UnaryExprOrTypeTrait ExprKind,
  SourceRange R) {
-return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
+return getSema().CreateUnaryExprOrTypeTraitExpr(
+TInfo, OpLoc, ExprKind, getTraitSpelling(ExprKind), R);
   }
 
   /// Build a new sizeof, alignof or vec step expression with an
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4373,7 +4373,8 @@
 bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
 SourceLocation OpLoc,
 SourceRange ExprRange,
-UnaryExprOrTypeTrait ExprKind) {
+UnaryExprOrTypeTrait ExprKind,
+StringRef KWName) {
   if (ExprType->isDependentType())
 return false;
 
@@ -4403,12 +4404,11 @@
 
   if (RequireCompleteSizedType(
   OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type,
-  getTraitSpelling(ExprKind), ExprRange))
+  KWName, ExprRange))
 return true;
 
   if (ExprType->isFunctionType()) {
-Diag(OpLoc, 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-15 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

Thank you for working on this! Just a few minor things.




Comment at: clang/docs/ReleaseNotes.rst:279-281
+- Clang now correctly diagnoses when the argument to alignas is an incomplete 
type.
+  (`#55175: `_,
+  Incorrect mention of 'alignof' in a diagnostic about 'alignas').





Comment at: clang/lib/Parse/ParseDecl.cpp:3032
+  ExprResult ArgExpr =
+  ParseAlignArgument(KWName->getName(), T.getOpenLocation(), EllipsisLoc);
   if (ArgExpr.isInvalid()) {





Comment at: clang/lib/Sema/SemaExpr.cpp:4714
+/// [dcl.align] An alignment-specifier of the form alignas(type-id) has the 
same
+/// effect as alignas(​alignof(type-id)).
+ExprResult Sema::ActOnAlignasTypeArgument(StringRef KWName, ParsedType Ty,

It looks like a zero-width joiner was added here around the `!` that should be 
removed.



Comment at: clang/lib/Sema/SemaExpr.cpp:4720
+  );
+  return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, UETT_AlignOf, KWName, R);
+}




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-14 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 522012.
yronglin added a comment.

Format


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/sizeless-1.cpp

Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/builtin-align-cxx.cpp
===
--- clang/test/SemaCXX/builtin-align-cxx.cpp
+++ clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -238,3 +238,6 @@
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
+
+// Check the diagnostic message
+_Alignas(void) char align_void_array[1]; // expected-error {{invalid application of '_Alignas' to an incomplete type 'void'}}
Index: clang/test/SemaCXX/attr-cxx0x.cpp
===
--- clang/test/SemaCXX/attr-cxx0x.cpp
+++ clang/test/SemaCXX/attr-cxx0x.cpp
@@ -50,3 +50,6 @@
 void func(void);
 
 alignas(4) auto PR19252 = 0;
+
+// Check the diagnostic message
+class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}
Index: clang/test/Sema/sizeless-1.c
===
--- clang/test/Sema/sizeless-1.c
+++ clang/test/Sema/sizeless-1.c
@@ -64,7 +64,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -2674,7 +2674,8 @@
  SourceLocation OpLoc,
  UnaryExprOrTypeTrait ExprKind,
  SourceRange R) {
-return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
+return getSema().CreateUnaryExprOrTypeTraitExpr(
+TInfo, OpLoc, ExprKind, getTraitSpelling(ExprKind), R);
   }
 
   /// Build a new sizeof, alignof or vec step expression with an
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4373,7 +4373,8 @@
 bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
 SourceLocation OpLoc,
 SourceRange ExprRange,
-UnaryExprOrTypeTrait ExprKind) {
+UnaryExprOrTypeTrait ExprKind,
+StringRef KWName) {
   if (ExprType->isDependentType())
 return false;
 
@@ -4403,12 +4404,11 @@
 
   if (RequireCompleteSizedType(
   OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type,
-  getTraitSpelling(ExprKind), ExprRange))
+  KWName, ExprRange))
 return true;
 
   if (ExprType->isFunctionType()) {
-Diag(OpLoc, 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-14 Thread Yurong via Phabricator via cfe-commits
yronglin updated this revision to Diff 522008.
yronglin added a comment.

Update ReleaseNotes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150528/new/

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/sizeless-1.cpp

Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/builtin-align-cxx.cpp
===
--- clang/test/SemaCXX/builtin-align-cxx.cpp
+++ clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -238,3 +238,6 @@
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
+
+// Check the diagnostic message
+_Alignas(void) char align_void_array[1]; // expected-error {{invalid application of '_Alignas' to an incomplete type 'void'}}
Index: clang/test/SemaCXX/attr-cxx0x.cpp
===
--- clang/test/SemaCXX/attr-cxx0x.cpp
+++ clang/test/SemaCXX/attr-cxx0x.cpp
@@ -50,3 +50,6 @@
 void func(void);
 
 alignas(4) auto PR19252 = 0;
+
+// Check the diagnostic message
+class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}
Index: clang/test/Sema/sizeless-1.c
===
--- clang/test/Sema/sizeless-1.c
+++ clang/test/Sema/sizeless-1.c
@@ -64,7 +64,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -2674,7 +2674,8 @@
  SourceLocation OpLoc,
  UnaryExprOrTypeTrait ExprKind,
  SourceRange R) {
-return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
+return getSema().CreateUnaryExprOrTypeTraitExpr(
+TInfo, OpLoc, ExprKind, getTraitSpelling(ExprKind), R);
   }
 
   /// Build a new sizeof, alignof or vec step expression with an
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4373,7 +4373,7 @@
 bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
 SourceLocation OpLoc,
 SourceRange ExprRange,
-UnaryExprOrTypeTrait ExprKind) {
+UnaryExprOrTypeTrait ExprKind, StringRef KWName) {
   if (ExprType->isDependentType())
 return false;
 
@@ -4403,12 +4403,12 @@
 
   if (RequireCompleteSizedType(
   OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type,
-  getTraitSpelling(ExprKind), ExprRange))
+  KWName, ExprRange))
 return true;
 
   if (ExprType->isFunctionType()) {
 Diag(OpLoc, diag::err_sizeof_alignof_function_type)
-<< 

[PATCH] D150528: [Clang] Fix the diagnoses when the argument to alignas is an incomplete type

2023-05-14 Thread Yurong via Phabricator via cfe-commits
yronglin created this revision.
Herald added a project: All.
yronglin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Signed-off-by: yronglin 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150528

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/Sema/sizeless-1.c
  clang/test/SemaCXX/attr-cxx0x.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/sizeless-1.cpp

Index: clang/test/SemaCXX/sizeless-1.cpp
===
--- clang/test/SemaCXX/sizeless-1.cpp
+++ clang/test/SemaCXX/sizeless-1.cpp
@@ -73,7 +73,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/test/SemaCXX/builtin-align-cxx.cpp
===
--- clang/test/SemaCXX/builtin-align-cxx.cpp
+++ clang/test/SemaCXX/builtin-align-cxx.cpp
@@ -238,3 +238,6 @@
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
 static_assert(!__builtin_is_aligned(static_cast(7), static_cast(4)), "");
+
+// Check the diagnostic message
+_Alignas(void) char align_void_array[1]; // expected-error {{invalid application of '_Alignas' to an incomplete type 'void'}}
Index: clang/test/SemaCXX/attr-cxx0x.cpp
===
--- clang/test/SemaCXX/attr-cxx0x.cpp
+++ clang/test/SemaCXX/attr-cxx0x.cpp
@@ -50,3 +50,6 @@
 void func(void);
 
 alignas(4) auto PR19252 = 0;
+
+// Check the diagnostic message
+class alignas(void) AlignasVoid {}; // expected-error {{invalid application of 'alignas' to an incomplete type 'void'}}
Index: clang/test/Sema/sizeless-1.c
===
--- clang/test/Sema/sizeless-1.c
+++ clang/test/Sema/sizeless-1.c
@@ -64,7 +64,7 @@
   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
   svint8_t _Alignas(int) aligned_int8_3;   // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
 
-  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
+  int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
 
   // Using pointers to sizeless data isn't wrong here, but because the
   // type is incomplete, it doesn't provide any alignment guarantees.
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -2674,7 +2674,8 @@
  SourceLocation OpLoc,
  UnaryExprOrTypeTrait ExprKind,
  SourceRange R) {
-return getSema().CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, R);
+return getSema().CreateUnaryExprOrTypeTraitExpr(
+TInfo, OpLoc, ExprKind, getTraitSpelling(ExprKind), R);
   }
 
   /// Build a new sizeof, alignof or vec step expression with an
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4373,7 +4373,7 @@
 bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
 SourceLocation OpLoc,
 SourceRange ExprRange,
-UnaryExprOrTypeTrait ExprKind) {
+UnaryExprOrTypeTrait ExprKind, StringRef KWName) {
   if (ExprType->isDependentType())
 return false;
 
@@ -4403,12 +4403,12 @@
 
   if (RequireCompleteSizedType(
   OpLoc, ExprType, diag::err_sizeof_alignof_incomplete_or_sizeless_type,
-  getTraitSpelling(ExprKind), ExprRange))
+  KWName, ExprRange))
 return true;
 
   if (ExprType->isFunctionType()) {
 Diag(OpLoc,