[clang] CWG 2354: prohibit alignas for enums

Reviewed By: aaron.ballman, erichkeane

Differential Revision: https://reviews.llvm.org/D121723
This commit is contained in:
Vlad Serebrennikov 2022-03-16 15:46:44 +08:00 committed by Chuanqi Xu
parent 989f1c72e0
commit a603f566db
6 changed files with 20 additions and 43 deletions

View File

@ -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">;

View File

@ -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

View File

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

View File

@ -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 different 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}}

View File

@ -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

View File

@ -13938,7 +13938,7 @@ and <I>POD class</I></td>
<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>