Debian Codegraph @

clang/test/Sema/enum.c

raw
1 // RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify -pedantic
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fsyntax-only -std=c23 -verify -pedantic
3 enum e {A,
4 B = 42LL << 32, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
5 C = -4, D = 12456 };
6
7 enum f { a = -2147483648, b = 2147483647 }; // ok.
8
9 enum g { // too negative
10 c = -2147483649, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
11 d = 2147483647 };
12 enum h { e = -2147483648, // too pos
13 f = 2147483648, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
14 i = 0xFFFF0000 // expected-warning {{too large}}
15 };
16
17 // minll maxull
18 enum x // expected-warning {{enumeration values exceed range of largest integer}}
19 { y = -9223372036854775807LL-1, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
20 z = 9223372036854775808ULL }; // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
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 // PR28903
125 // In C it is valid to define tags inside enums.
126 struct PR28903 {
127 enum {
128 PR28903_A = (enum {
129 PR28903_B,
130 PR28903_C = PR28903_B
131 })0
132 };
133 int makeStructNonEmpty;
134 };
135
136 static int EnumRedecl; // expected-note 2 {{previous definition is here}}
137 struct S {
138 enum {
139 EnumRedecl = 4 // expected-error {{redefinition of 'EnumRedecl'}}
140 } e;
141 };
142
143 union U {
144 enum {
145 EnumRedecl = 5 // expected-error {{redefinition of 'EnumRedecl'}}
146 } e;
147 };
148
149 enum PR15071 {
150 PR15071_One // expected-note {{previous definition is here}}
151 };
152
153 struct EnumRedeclStruct {
154 enum {
155 PR15071_One // expected-error {{redefinition of enumerator 'PR15071_One'}}
156 } e;
157 };
158
159 enum struct GH42372_1 { // expected-error {{expected identifier or '{'}}
160 One
161 };
162
163 // Because class is not a keyword in C, this looks like a forward declaration.
164 // expected-error@+4 {{expected ';' after top level declarator}}
165 // expected-error@+3 {{tentative definition has type 'enum class' that is never completed}}
166 // expected-warning@+2 {{ISO C forbids forward references to 'enum' types}}
167 // expected-note@+1 {{forward declaration of 'enum class'}}
168 enum class GH42372_2 {
169 One
170 };
171
172 #if __STDC_VERSION__ >= 202311L
173 // FIXME: GCC picks __uint128_t as the underlying type for the enumeration
174 // value and Clang picks unsigned long long.
175 // FIXME: Clang does not yet implement WG14 N3029, so the warning about
176 // restricting enumerator values to 'int' is not correct.
177 enum GH59352 { // expected-warning {{enumeration values exceed range of largest integer}}
178 BigVal = 66666666666666666666wb // expected-warning {{ISO C restricts enumerator values to range of 'int' (66666666666666666666 is too large)}}
179 };
180 _Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}
181 expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}
182 */
183 _Static_assert(
184 _Generic(BigVal, // expected-error {{static assertion failed}}
185 _BitInt(67) : 0,
186 __INTMAX_TYPE__ : 0,
187 __UINTMAX_TYPE__ : 0,
188 long long : 0,
189 unsigned long long : 0,
190 __int128_t : 0,
191 __uint128_t : 1
192 )
193 );
194 #endif // __STDC_VERSION__ >= 202311L