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

Yurong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 17 06:40:41 PDT 2023


yronglin added a comment.

In D150528#4343102 <https://reviews.llvm.org/D150528#4343102>, @aaron.ballman wrote:

> In D150528#4342646 <https://reviews.llvm.org/D150528#4342646>, @yronglin wrote:
>
>> In D150528#4342600 <https://reviews.llvm.org/D150528#4342600>, @aaron.ballman wrote:
>>
>>> In D150528#4342535 <https://reviews.llvm.org/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 <typename Ty> 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 <typename T> class alignas(T) Bar {};
  template <unsigned N> class alignas(N) FooBar {};
  
  int main() {
    Bar<int> 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 <<invalid sloc>> <invalid sloc>
  |-TypedefDecl 0x14b043a70 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
  | `-BuiltinType 0x14b0437d0 '__int128'
  |-TypedefDecl 0x14b043ae0 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
  | `-BuiltinType 0x14b0437f0 'unsigned __int128'
  |-TypedefDecl 0x14b043e58 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
  | `-RecordType 0x14b043bd0 '__NSConstantString_tag'
  |   `-CXXRecord 0x14b043b38 '__NSConstantString_tag'
  |-TypedefDecl 0x14b043ef0 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
  | `-PointerType 0x14b043eb0 'char *'
  |   `-BuiltinType 0x14b0432b0 'char'
  |-TypedefDecl 0x14b087858 <<invalid sloc>> <invalid sloc> 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 <col:7> alignas 'void *'
  | `-CXXRecordDecl 0x14b087a68 <col:1, col:23> col:23 implicit class Foo
  |-VarDecl 0x14b087be8 <line:2:1, col:38> col:22 align32array 'char[128]'
  | `-AlignedAttr 0x14b087c50 <col:1> _Alignas 'void *'
  |-ClassTemplateDecl 0x14b087e80 <line:4:1, col:45> col:40 Bar
  | |-TemplateTypeParmDecl 0x14b087cf8 <col:11, col:20> col:20 referenced typename depth 0 index 0 T
  | |-CXXRecordDecl 0x14b087df0 <col:23, col:45> 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 <col:29> alignas 'T'
  | | `-CXXRecordDecl 0x14b088130 <col:23, col:40> col:40 implicit class Bar
  | `-ClassTemplateSpecializationDecl 0x14b09e200 <col:1, col:45> 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 <col:29> alignas 'int':'int'
  |   |-CXXRecordDecl 0x14b09e520 <col:23, col:40> col:40 implicit class Bar
  |   |-CXXConstructorDecl 0x14b09e5e0 <col:40> col:40 implicit used constexpr Bar 'void () noexcept' inline default trivial
  |   | `-CompoundStmt 0x14b09eb48 <col:40>
  |   |-CXXConstructorDecl 0x14b09e778 <col:40> col:40 implicit constexpr Bar 'void (const Bar<int> &)' inline default trivial noexcept-unevaluated 0x14b09e778
  |   | `-ParmVarDecl 0x14b09e898 <col:40> col:40 'const Bar<int> &'
  |   `-CXXConstructorDecl 0x14b09e978 <col:40> col:40 implicit constexpr Bar 'void (Bar<int> &&)' inline default trivial noexcept-unevaluated 0x14b09e978
  |     `-ParmVarDecl 0x14b09ea98 <col:40> col:40 'Bar<int> &&'
  |-ClassTemplateDecl 0x14b088310 <line:5:1, col:48> col:40 FooBar
  | |-NonTypeTemplateParmDecl 0x14b0881f0 <col:11, col:20> col:20 referenced 'unsigned int' depth 0 index 0 N
  | |-CXXRecordDecl 0x14b088280 <col:23, col:48> col:40 class FooBar 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 0x14b088580 <col:29> alignas
  | | | `-DeclRefExpr 0x14b088260 <col:37> 'unsigned int' NonTypeTemplateParm 0x14b0881f0 'N' 'unsigned int'
  | | `-CXXRecordDecl 0x14b0885e0 <col:23, col:40> col:40 implicit class FooBar
  | `-ClassTemplateSpecializationDecl 0x14b09ed10 <col:1, col:48> col:40 class FooBar 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 integral 4
  |   |-AlignedAttr 0x14b09ef90 <col:29> alignas
  |   | `-ConstantExpr 0x14b09ef70 <col:37> 'unsigned int'
  |   |   |-value: Int 4
  |   |   `-SubstNonTypeTemplateParmExpr 0x14b09ef48 <col:37> 'unsigned int'
  |   |     |-NonTypeTemplateParmDecl 0x14b0881f0 <col:11, col:20> col:20 referenced 'unsigned int' depth 0 index 0 N
  |   |     `-IntegerLiteral 0x14b09ef28 <col:37> 'unsigned int' 4
  |   |-CXXRecordDecl 0x14b09f058 <col:23, col:40> col:40 implicit class FooBar
  |   |-CXXConstructorDecl 0x14980fc00 <col:40> col:40 implicit used constexpr FooBar 'void () noexcept' inline default trivial
  |   | `-CompoundStmt 0x149810108 <col:40>
  |   |-CXXConstructorDecl 0x14980fd68 <col:40> col:40 implicit constexpr FooBar 'void (const FooBar<4> &)' inline default trivial noexcept-unevaluated 0x14980fd68
  |   | `-ParmVarDecl 0x14980fe88 <col:40> col:40 'const FooBar<4> &'
  |   `-CXXConstructorDecl 0x14980ff68 <col:40> col:40 implicit constexpr FooBar 'void (FooBar<4> &&)' inline default trivial noexcept-unevaluated 0x14980ff68
  |     `-ParmVarDecl 0x149810088 <col:40> col:40 'FooBar<4> &&'
  `-FunctionDecl 0x14b0886e0 <line:7:1, line:11:1> line:7:5 main 'int ()'
    `-CompoundStmt 0x1498101e8 <col:12, line:11:1>
      |-DeclStmt 0x14b09eca0 <line:8:3, col:15>
      | `-VarDecl 0x14b09e3b0 <col:3, col:12> col:12 bar 'Bar<int>':'Bar<int>' callinit
      |   `-CXXConstructExpr 0x14b09ec78 <col:12> 'Bar<int>':'Bar<int>' 'void () noexcept'
      |-DeclStmt 0x1498101a0 <line:9:3, col:19>
      | `-VarDecl 0x14b09eec0 <col:3, col:13> col:13 fooBar 'FooBar<4>':'FooBar<4>' callinit
      |   `-CXXConstructExpr 0x149810178 <col:13> 'FooBar<4>':'FooBar<4>' 'void () noexcept'
      `-ReturnStmt 0x1498101d8 <line:10:3, col:10>
        `-IntegerLiteral 0x1498101b8 <col:10> 'int' 0

This AST dose not have a `UnaryExprOrTypeTraitExpr` in `alignas`, What do you think?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150528



More information about the cfe-commits mailing list