Author: Vlad Serebrennikov Date: 2022-03-16T15:48:18+08:00 New Revision: a603f566dbe04d8a9eb0ada67b585965945f7e6d
URL: https://github.com/llvm/llvm-project/commit/a603f566dbe04d8a9eb0ada67b585965945f7e6d DIFF: https://github.com/llvm/llvm-project/commit/a603f566dbe04d8a9eb0ada67b585965945f7e6d.diff LOG: [clang] CWG 2354: prohibit alignas for enums Reviewed By: aaron.ballman, erichkeane Differential Revision: https://reviews.llvm.org/D121723 Added: clang/test/CXX/drs/dr2354.cpp Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaDeclAttr.cpp clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p5.cpp clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p6.cpp clang/www/cxx_dr_status.html Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index e17b018937fb4..0806cc4170753 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3022,8 +3022,9 @@ def err_aligned_attribute_argument_not_int : Error< def err_align_value_attribute_argument_not_int : Error< "'align_value' attribute requires integer constant">; def err_alignas_attribute_wrong_decl_type : Error< - "%0 attribute cannot be applied to a %select{function parameter|" - "variable with 'register' storage class|'catch' variable|bit-field}1">; + "%0 attribute cannot be applied to %select{a function parameter|" + "a variable with 'register' storage class|a 'catch' variable|a bit-field|" + "an enumeration}1">; def err_alignas_missing_on_definition : Error< "%0 must be specified on definition if it is specified on any declaration">; def note_alignas_on_declaration : Note<"declared with %0 attribute here">; diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 3034abdf04028..70d6721a8b1c0 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -4290,6 +4290,9 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, // declared with the register storage class specifier. An // alignment-specifier may also be applied to the declaration of a class // or enumeration type. + // CWG 2354: + // CWG agreed to remove permission for alignas to be applied to + // enumerations. // C11 6.7.5/2: // An alignment attribute shall not be specified in a declaration of // a typedef, or a bit-field, or a function, or a parameter, or an @@ -4305,6 +4308,9 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, } else if (const auto *FD = dyn_cast<FieldDecl>(D)) { if (FD->isBitField()) DiagKind = 3; + } else if (const auto *ED = dyn_cast<EnumDecl>(D)) { + if (ED->getLangOpts().CPlusPlus) + DiagKind = 4; } else if (!isa<TagDecl>(D)) { Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr << (TmpAttr.isC11() ? ExpectedVariableOrField diff --git a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p5.cpp b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p5.cpp index e713d72dc58fa..45d5ebf68ba8f 100644 --- a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p5.cpp +++ b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p5.cpp @@ -11,13 +11,6 @@ alignas(1) int n7 alignas(2), // expected-error {{less than minimum alignment}} alignas(8) int n9 alignas(2); // ok, overaligned alignas(1) extern int n10; // expected-error {{less than minimum alignment}} -enum alignas(1) E1 {}; // expected-error {{requested alignment is less than minimum alignment of 4 for type 'E1'}} -enum alignas(1) E2 : char {}; // ok -enum alignas(4) E3 { e3 = 0 }; // ok -enum alignas(4) E4 { e4 = 1ull << 33 }; // expected-error {{requested alignment is less than minimum alignment of 8 for type 'E4'}} -enum alignas(8) E5 {}; -static_assert(alignof(E5) == 8, ""); - typedef __attribute__((aligned(16))) int IntAlign16; enum E6 : IntAlign16 {}; static_assert(alignof(E6) == 4, ""); @@ -62,15 +55,6 @@ template struct X<2, 2, short>; template struct X<16, 8, S1>; template struct X<4, 4, S1>; // expected-note {{instantiation}} -template<int N, typename T> -struct Y { - enum alignas(N) E : T {}; // expected-error {{requested alignment is less than minimum}} -}; -template struct Y<1, char>; -template struct Y<2, char>; -template struct Y<1, short>; // expected-note {{instantiation}} -template struct Y<2, short>; - template<int N, typename T> void f() { alignas(N) T v; // expected-error {{requested alignment is less than minimum}} diff --git a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p6.cpp b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p6.cpp index e788577480770..5d93336341f6e 100644 --- a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p6.cpp +++ b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.align/p6.cpp @@ -27,21 +27,6 @@ int n8; // expected-error {{'alignas' must be specified on definition if it is s int n9; // expected-error {{'alignas' must be specified on definition if it is specified on any declaration}} alignas(4) extern int n9; // expected-note {{declared with 'alignas' attribute here}} - -enum alignas(2) E : char; // expected-note {{declared with 'alignas' attribute here}} -enum E : char {}; // expected-error {{'alignas' must be specified on definition if it is specified on any declaration}} - -enum alignas(4) F : char; // expected-note {{previous declaration is here}} -enum alignas(2) F : char; // expected-error {{redeclaration has diff erent alignment requirement (2 vs 4)}} - -enum G : char; -enum alignas(8) G : char {}; -enum G : char; - -enum H : char {}; // expected-error {{'alignas' must be specified on definition if it is specified on any declaration}} -enum alignas(1) H : char; // expected-note {{declared with 'alignas' attribute here}} - - struct S; struct alignas(16) S; // expected-note {{declared with 'alignas' attribute here}} struct S; @@ -73,14 +58,5 @@ alignas(O) alignas(P) char X<M, N, O, P>::Buffer[32]; // expected-error {{redecl char *x1848 = X<1,8,4,8>::Buffer; // ok char *x1248 = X<1,2,4,8>::Buffer; // expected-note {{in instantiation of}} -template<int M, int N, int O, int P> struct Y { - enum alignas(M) alignas(N) E : char; -}; -template<int M, int N, int O, int P> -enum alignas(O) alignas(P) Y<M,N,O,P>::E : char { e }; -int y1848 = Y<1,8,4,8>::e; -// FIXME: We should reject this. -int y1248 = Y<1,2,4,8>::e; - // Don't crash here. alignas(4) struct Incomplete incomplete; // expected-error {{incomplete type}} expected-note {{forward declaration}} diff --git a/clang/test/CXX/drs/dr2354.cpp b/clang/test/CXX/drs/dr2354.cpp new file mode 100644 index 0000000000000..3efb0ba555669 --- /dev/null +++ b/clang/test/CXX/drs/dr2354.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -x c++ -verify %s + +// dr2354: 15 + +namespace DR2354 { + +enum alignas(64) A {}; // expected-error {{'alignas' attribute cannot be applied to an enumeration}} +enum struct alignas(64) B {}; // expected-error {{'alignas' attribute cannot be applied to an enumeration}} + +} // namespace DR2354 diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html index 62ccfc518d456..4734e9fd89253 100755 --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -13938,7 +13938,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2> <td><a href="https://wg21.link/cwg2354">2354</a></td> <td>CD5</td> <td>Extended alignment and object representation</td> - <td class="none" align="center">Unknown</td> + <td class="unreleased" align="center">Clang 15</td> </tr> <tr class="open" id="2355"> <td><a href="https://wg21.link/cwg2355">2355</a></td> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits