xref: /llvm-project/clang/test/Sema/enum.c (revision 25b1896686177673dcd548489e92c7880d576948)
1 // RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify=expected,pre-c23 -pedantic
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fsyntax-only -std=c23 -verify=expected -pedantic
3 enum e {A,
4         B = 42LL << 32,        // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}
5       C = -4, D = 12456 };
6 
7 enum f { a = -2147483648, b = 2147483647 }; // ok.
8 
9 enum g {  // too negative
10    c = -2147483649,         // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}
11    d = 2147483647 };
12 enum h { e = -2147483648, // too pos
13    f = 2147483648,           // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}
14   i = 0xFFFF0000 // pre-c23-warning {{too large}}
15 };
16 
17 // minll maxull
18 enum x                      // expected-warning {{enumeration values exceed range of largest integer}}
19 { y = -9223372036854775807LL-1,  // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}
20 z = 9223372036854775808ULL };    // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}
21 
22 int test(void) {
23   return sizeof(enum e) ;
24 }
25 
26 enum gccForwardEnumExtension ve; // expected-warning{{ISO C forbids forward references to 'enum' types}} \
27 // expected-error{{tentative definition has type 'enum gccForwardEnumExtension' that is never completed}} \
28 // expected-note{{forward declaration of 'enum gccForwardEnumExtension'}}
29 
30 int test2(int i)
31 {
32   ve + i; // expected-error{{invalid operands to binary expression}}
33 }
34 
35 // PR2020
36 union u0;    // expected-note {{previous use is here}}
37 enum u0 { U0A }; // expected-error {{use of 'u0' with tag type that does not match previous declaration}}
38 
39 extern enum some_undefined_enum ve2; // expected-warning {{ISO C forbids forward references to 'enum' types}}
40 
41 void test4(void) {
42   for (; ve2;) // expected-error {{statement requires expression of scalar type}}
43     ;
44   (_Bool)ve2;  // expected-error {{arithmetic or pointer type is required}}
45 
46   for (; ;ve2) // expected-warning {{expression result unused}}
47     ;
48   (void)ve2;
49   ve2;         // expected-warning {{expression result unused}}
50 }
51 
52 // PR2416
53 enum someenum {};  // expected-error {{use of empty enum}}
54 
55 enum e0 { // expected-note {{previous definition is here}}
56   E0 = sizeof(enum e0 { E1 }), // expected-error {{nested redefinition}}
57 };
58 
59 // PR3173
60 enum { PR3173A, PR3173B = PR3173A+50 };
61 
62 // PR2753
63 void foo(void) {
64   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
65   enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
66 }
67 
68 typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}
69 
70 
71 enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}
72   NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}
73 };
74 
75 /// PR3688
76 struct s1 {
77   enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}
78 };
79 
80 enum e1 { YES, NO };
81 
82 static enum e1 badfunc(struct s1 *q) {
83   return q->bar();
84 }
85 
86 
87 // Make sure we don't a.k.a. anonymous enums.
88 typedef enum {
89   an_enumerator = 20
90 } an_enum;
91 char * s = (an_enum) an_enumerator; // expected-error {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'an_enum'}}
92 
93 // PR4515
94 enum PR4515 {PR4515a=1u,PR4515b=(PR4515a-2)/2};
95 int CheckPR4515[PR4515b==0?1:-1];
96 
97 // PR7911
98 extern enum PR7911T PR7911V; // expected-warning{{ISO C forbids forward references to 'enum' types}}
99 void PR7911F(void) {
100   switch (PR7911V) // expected-error {{statement requires expression of integer type}}
101     ;
102 }
103 
104 char test5[__has_feature(enumerator_attributes) ? 1 : -1];
105 
106 // PR8694
107 void PR8694(int* e) // expected-note {{passing argument to parameter 'e' here}}
108 {
109 }
110 
111 void crash(enum E* e) // expected-warning {{declaration of 'enum E' will not be visible outside of this function}} \
112                       // expected-warning {{ISO C forbids forward references to 'enum' types}}
113 {
114         PR8694(e); // expected-warning {{incompatible pointer types passing 'enum E *' to parameter of type 'int *'}}
115 }
116 
117 typedef enum { NegativeShort = (short)-1 } NegativeShortEnum;
118 int NegativeShortTest[NegativeShort == -1 ? 1 : -1];
119 
120 // PR24610
121 enum Color { Red, Green, Blue }; // expected-note{{previous use is here}}
122 typedef struct Color NewColor; // expected-error {{use of 'Color' with tag type that does not match previous declaration}}
123 
124 // Enumerations with a fixed underlying type.
125 // https://github.com/llvm/llvm-project/issues/116880
126 #if __STDC_VERSION__ >= 202311L
127   static_assert(__has_feature(c_fixed_enum));
128   static_assert(__has_extension(c_fixed_enum)); // Matches behavior for c_alignas, etc
129 #else
130   _Static_assert(__has_extension(c_fixed_enum), "");
131   _Static_assert(!__has_feature(c_fixed_enum), "");
132 #if __STDC_VERSION__ < 201112L
133   // expected-warning@-3 {{'_Static_assert' is a C11 extension}}
134   // expected-warning@-3 {{'_Static_assert' is a C11 extension}}
135 #endif
136 #endif
137 typedef enum : unsigned char { Pink, Black, Cyan } Color; // pre-c23-warning {{enumeration types with a fixed underlying type are a C23 extension}}
138 
139 // PR28903
140 // In C it is valid to define tags inside enums.
141 struct PR28903 {
142   enum {
143     PR28903_A = (enum {
144       PR28903_B,
145       PR28903_C = PR28903_B
146     })0
147   };
148   int makeStructNonEmpty;
149 };
150 
151 static int EnumRedecl; // expected-note 2 {{previous definition is here}}
152 struct S {
153   enum {
154     EnumRedecl = 4 // expected-error {{redefinition of 'EnumRedecl'}}
155   } e;
156 };
157 
158 union U {
159   enum {
160     EnumRedecl = 5 // expected-error {{redefinition of 'EnumRedecl'}}
161   } e;
162 };
163 
164 enum PR15071 {
165   PR15071_One // expected-note {{previous definition is here}}
166 };
167 
168 struct EnumRedeclStruct {
169   enum {
170     PR15071_One // expected-error {{redefinition of enumerator 'PR15071_One'}}
171   } e;
172 };
173 
174 enum struct GH42372_1 { // expected-error {{expected identifier or '{'}}
175   One
176 };
177 
178 // Because class is not a keyword in C, this looks like a forward declaration.
179 // expected-error@+4 {{expected ';' after top level declarator}}
180 // expected-error@+3 {{tentative definition has type 'enum class' that is never completed}}
181 // expected-warning@+2 {{ISO C forbids forward references to 'enum' types}}
182 // expected-note@+1 {{forward declaration of 'enum class'}}
183 enum class GH42372_2 {
184   One
185 };
186 
187 enum IncOverflow {
188   V2 = __INT_MAX__,
189   V3 // pre-c23-warning {{incremented enumerator value which exceeds the range of 'int' is a C23 extension}}
190 };
191 
192 #if __STDC_VERSION__ >= 202311L
193 // FIXME: GCC picks __uint128_t as the underlying type for the enumeration
194 // value and Clang picks unsigned long long.
195 enum GH59352 { // expected-warning {{enumeration values exceed range of largest integer}}
196  BigVal = 66666666666666666666wb
197 };
198 _Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}
199                                                      expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}
200                                                    */
201 _Static_assert(
202     _Generic(BigVal,                             // expected-error {{static assertion failed}}
203     _BitInt(67) : 0,
204     __INTMAX_TYPE__ : 0,
205     __UINTMAX_TYPE__ : 0,
206     long long : 0,
207     unsigned long long : 0,
208     __int128_t : 0,
209     __uint128_t : 1
210     )
211 );
212 
213 #include <limits.h>
214 
215 void fooinc23() {
216   enum E1 {
217     V1 = INT_MAX
218   } e1;
219 
220   enum E2 {
221     V2 = INT_MAX,
222     V3
223   } e2;
224 
225   enum E3 {
226     V4 = INT_MAX,
227     V5 = LONG_MIN
228   } e3;
229 
230   enum E4 {
231     V6 = 1u,
232     V7 = 2wb
233   } e4;
234 
235   _Static_assert(_Generic(V1, int : 1));
236   _Static_assert(_Generic(V2, int : 0, unsigned int : 1));
237   _Static_assert(_Generic(V3, int : 0, unsigned int : 1));
238   _Static_assert(_Generic(V4, int : 0, signed long : 1));
239   _Static_assert(_Generic(V5, int : 0, signed long : 1));
240   _Static_assert(_Generic(V6, int : 1));
241   _Static_assert(_Generic(V7, int : 1));
242   _Static_assert(_Generic((enum E4){}, unsigned int : 1));
243 
244 }
245 
246 #endif // __STDC_VERSION__ >= 202311L
247