Debian Codegraph @

gcc/testsuite/gcc.dg/c23-enum-7.c

raw
1 /* Test C23 enumerations with fixed underlying type. Invalid code. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c23 -pedantic-errors" } */
4
5 /* An enum type specifier may only be used when the enum is defined, or in a
6 declaration of the form "enum name enum-type-specifier;". */
7 extern enum e1 : int; /* { dg-error "storage class specifier in empty declaration with 'enum' underlying type" } */
8 _Thread_local enum e2 : short; /* { dg-error "'_Thread_local' in empty declaration with 'enum' underlying type" } */
9 const enum e3 : long; /* { dg-error "type qualifier in empty declaration with 'enum' underlying type" } */
10 alignas (8) enum e4 : long; /* { dg-error "'alignas' in empty declaration with 'enum' underlying type" } */
11 inline enum e5 : unsigned; /* { dg-error "'inline' in empty declaration" } */
12 _Noreturn enum e6 : unsigned; /* { dg-error "'_Noreturn' in empty declaration" } */
13 auto enum e7 : unsigned; /* { dg-error "'auto' in file-scope empty declaration" } */
14 register enum e8 : unsigned; /* { dg-error "'register' in file-scope empty declaration" } */
15
16 /* When the enum is defined, some extra declaration specifiers are permitted,
17 but diagnosed as useless. */
18 extern enum e9 : int { E9 }; /* { dg-warning "useless storage class specifier in empty declaration" } */
19 _Thread_local enum e10 : short { E10 }; /* { dg-warning "useless '_Thread_local' in empty declaration" } */
20 const enum e11 : long { E11 }; /* { dg-warning "useless type qualifier in empty declaration" } */
21 alignas (8) enum e12 : long { E12 }; /* { dg-warning "useless '_Alignas' in empty declaration" } */
22
23 /* Nothing else may be declared with an enum type specifier for an enum not
24 being defined in that declaration. */
25 enum e13 : short x13; /* { dg-error "'enum' underlying type may not be specified here" } */
26 enum e14 : short f14 (); /* { dg-error "'enum' underlying type may not be specified here" } */
27 typeof (enum e15 : long) x15; /* { dg-error "'enum' underlying type may not be specified here" } */
28 int f16 (enum e16 : char p); /* { dg-error "'enum' underlying type may not be specified here" } */
29 int f17 (enum e17 : char); /* { dg-error "'enum' underlying type may not be specified here" } */
30 struct s18 { enum e18 : int x; }; /* { dg-error "'enum' underlying type may not be specified here" } */
31
32 /* But those are OK if the enum content is defined. */
33 enum e19 : short { E19 } x19;
34 enum e20 : long { E20 } f20 ();
35 typeof (enum e21 : long { E21 }) x21;
36 int f22 (enum e22 : long long { E22 } p);
37 int f23 (enum e23 : long long { E23 } p);
38 struct s24 { enum e24 : int { E24 } x; };
39
40 /* Incompatible kinds of tags in the same scope are errors. */
41 struct s25;
42 enum s25 : int; /* { dg-error "wrong kind of tag" } */
43 struct s26;
44 enum s26 : int { E26 }; /* { dg-error "wrong kind of tag" } */
45 struct s27 { int x; };
46 enum s27 : int; /* { dg-error "wrong kind of tag" } */
47 struct s28 { int x; };
48 enum s28 : int { E28 }; /* { dg-error "wrong kind of tag" } */
49 union u29;
50 enum u29 : int; /* { dg-error "wrong kind of tag" } */
51 union u30;
52 enum u30 : int { E30 }; /* { dg-error "wrong kind of tag" } */
53 union u31 { int x; };
54 enum u31 : int; /* { dg-error "wrong kind of tag" } */
55 union u32 { int x; };
56 enum u32 : int { E32 }; /* { dg-error "wrong kind of tag" } */
57
58 /* If an enum has a fixed underlying type, that must be given when defining the
59 enum. */
60 enum e33 : short;
61 enum e33 { E33 }; /* { dg-error "'enum' declared with but defined without fixed underlying type" } */
62
63 /* An enum defined without a fixed underlying type cannot then be declared with
64 one. */
65 enum e34 { E34A = -__INT_MAX__, E34B = __INT_MAX__ };
66 enum e34 : int; /* { dg-error "'enum' declared both with and without fixed underlying type" } */
67
68 /* An enum with a fixed underlying type cannot be declared with an incompatible
69 fixed underlying type. */
70 enum e35 : int;
71 enum e35 : unsigned int; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
72 enum e36 : int;
73 enum e36 : unsigned int { E36 }; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
74 enum e37 : unsigned int { E37 };
75 enum e37 : int; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
76
77 /* Enumeration constants must fit in the fixed underlying type. */
78 enum e38 : unsigned char { E38 = (unsigned long long)((unsigned char) -1) + 1 }; /* { dg-error "enumerator value outside the range of underlying type" } */
79 enum e39 : unsigned int { E39 = -1 }; /* { dg-error "enumerator value outside the range of underlying type" } */
80 enum e40 : int { E40 = __INT_MAX__, E40A }; /* { dg-error "overflow in enumeration values" } */
81 enum e41 : unsigned int { E41 = (unsigned int) -1, E41A }; /* { dg-error "overflow in enumeration values" } */
82 enum e42 : bool { E42 = 2 }; /* { dg-error "enumerator value outside the range of underlying type" } */
83 enum e43 : bool { E43 = 1, E43A }; /* { dg-error "overflow in enumeration values" } */
84
85 /* The underlying type must be an integer type, not itself an enum (or
86 bit-precise) type. */
87 enum e44 : double; /* { dg-error "invalid 'enum' underlying type" } */
88 typedef int T;
89 enum e45 : T;
90 typedef int *TP;
91 enum e46 : TP; /* { dg-error "invalid 'enum' underlying type" } */
92 enum e47 : enum e45; /* { dg-error "invalid 'enum' underlying type" } */
93 enum e48 : const; /* { dg-error "no 'enum' underlying type specified" } */
94 /* 'restrict' is not valid on integer types. */
95 enum e49 : int restrict; /* { dg-error "invalid use of 'restrict'" } */