Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / switch.c
blob69b34f96820d37b0f8aee6bbc494088933abb220
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default -triple x86_64-linux-gnu %s
2 void f (int z) {
3 while (z) {
4 default: z--; // expected-error {{statement not in switch}}
5 }
8 void foo(int X) {
9 switch (X) {
10 case 42: ; // expected-note {{previous case}}
11 case 5000000000LL: // expected-warning {{overflow}}
12 case 42: // expected-error {{duplicate case value '42'}}
15 case 100 ... 99: ; // expected-warning {{empty case range}}
17 case 43: ; // expected-note {{previous case}}
18 case 43 ... 45: ; // expected-error {{duplicate case value}}
20 case 100 ... 20000:; // expected-note {{previous case}}
21 case 15000 ... 40000000:; // expected-error {{duplicate case value}}
25 void test3(void) {
26 // empty switch;
27 switch (0); // expected-warning {{no case matching constant switch condition '0'}} \
28 // expected-warning {{switch statement has empty body}} \
29 // expected-note{{put the semicolon on a separate line to silence this warning}}
32 extern int g(void);
34 void test4(void)
36 int cond;
37 switch (cond) {
38 case 0 && g():
39 case 1 || g():
40 break;
43 switch(cond) {
44 case g(): // expected-error {{expression is not an integer constant expression}}
45 case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
46 break;
49 switch (cond) {
50 case 0 && g() ... 1 || g():
51 break;
54 switch (cond) {
55 case g() // expected-error {{expression is not an integer constant expression}}
56 && 0:
57 break;
60 switch (cond) {
61 case 0 ...
62 g() // expected-error {{expression is not an integer constant expression}}
63 || 1:
64 break;
68 void test5(int z) {
69 switch(z) {
70 default: // expected-note {{previous case defined here}}
71 default: // expected-error {{multiple default labels in one switch}}
72 break;
76 void test6(void) {
77 char ch = 'a';
78 switch(ch) {
79 case 1234: // expected-warning {{overflow converting case value}}
80 break;
84 // PR5606
85 int f0(int var) {
86 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
87 case 1:
88 break;
89 case 2:
90 return 1;
92 return 2;
95 void test7(void) {
96 enum {
97 A = 1,
99 } a;
100 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
101 case A:
102 break;
104 switch(a) {
105 case B:
106 case A:
107 break;
109 switch(a) {
110 case A:
111 case B:
112 case 3: // expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
113 break;
115 switch(a) {
116 case A:
117 case B:
118 case 3 ... //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
119 4: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
120 break;
122 switch(a) {
123 case 1 ... 2:
124 break;
126 switch(a) {
127 case 0 ... 2: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
128 break;
130 switch(a) {
131 case 1 ... 3: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
132 break;
134 switch(a) {
135 case 0 ... //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
136 3: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
137 break;
142 void test8(void) {
143 enum {
146 C = 1
147 } a;
148 switch(a) {
149 case A:
150 case B:
151 break;
153 switch(a) {
154 case A:
155 case C:
156 break;
158 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
159 case A:
160 break;
164 void test9(void) {
165 enum {
166 A = 3,
167 C = 1
168 } a;
169 switch(a) {
170 case 0: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
171 case 1:
172 case 2: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
173 case 3:
174 case 4: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
175 break;
179 void test10(void) {
180 enum {
181 A = 10,
182 C = 2,
183 B = 4,
184 D = 12
185 } a;
186 switch(a) {
187 case 0 ... //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
188 1: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
189 case 2 ... 4:
190 case 5 ... //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
191 9: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
192 case 10 ... 12:
193 case 13 ... //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
194 16: //expected-warning{{case value not in enumerated type 'enum (unnamed enum}}
195 break;
199 void test11(void) {
200 enum {
201 A = -1,
204 } a;
205 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
206 case B:
207 case C:
208 break;
211 switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}}
212 case B:
213 case C:
214 break;
216 default:
217 break;
221 void test12(void) {
222 enum {
223 A = -1,
224 B = 4294967286
225 } a;
226 switch(a) {
227 case A:
228 case B:
229 break;
233 typedef enum {
234 val1,
235 val2,
236 val3
237 } my_type_t;
239 int test13(my_type_t t) {
240 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
241 case val1:
242 return 1;
243 case val2:
244 return 2;
246 return -1;
249 enum {
250 EC0 = 0xFFFF0000,
251 EC1 = 0xFFFF0001,
254 int test14(int a) {
255 switch(a) {
256 case EC0: return 0;
257 case EC1: return 1;
259 return 0;
262 void f1(unsigned x) {
263 switch (x) {
264 case -1: break;
265 default: break;
269 void test15(void) {
270 int i = 0;
271 switch (1) { // expected-warning {{no case matching constant switch condition '1'}}
272 case 0: i = 0; break;
273 case 2: i++; break;
277 void test16(void) {
278 const char c = '5';
279 switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
280 case '6': return;
284 struct bitfield_member {
285 unsigned bf : 1;
288 // PR7359
289 void test17(int x) {
290 switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
291 case 0: return;
294 switch ((int) (x <= 17)) {
295 case 0: return;
298 struct bitfield_member bm;
299 switch (bm.bf) { // no warning
300 case 0:
301 case 1:
302 return;
306 int test18(void) {
307 enum { A, B } a;
308 switch (a) {
309 case A: return 0;
310 case B: return 1;
311 case 7: return 1; // expected-warning {{case value not in enumerated type}}
312 default: return 2; // expected-warning {{default label in switch which covers all enumeration values}}
316 typedef enum {
317 kOne = 1,
318 } Ints;
320 void rdar110822110(Ints i)
322 switch (i) {
323 case kOne:
324 break;
325 case 2: // expected-warning {{case value not in enumerated type 'Ints'}}
326 break;
327 default: // expected-warning {{default label in switch which covers all enumeration values}}
328 break;
332 // PR9243
333 #define TEST19MACRO 5
334 void test19(int i) {
335 enum {
336 kTest19Enum1 = 7,
337 kTest19Enum2 = kTest19Enum1
339 const int a = 3;
340 switch (i) {
341 case 5: // expected-note {{previous case}}
342 case TEST19MACRO: // expected-error {{duplicate case value '5'}}
344 case 7: // expected-note {{previous case}}
345 case kTest19Enum1: // expected-error {{duplicate case value: '7' and 'kTest19Enum1' both equal '7'}} \
346 // expected-note {{previous case}}
347 case kTest19Enum1: // expected-error {{duplicate case value 'kTest19Enum1'}} \
348 // expected-note {{previous case}}
349 case kTest19Enum2: // expected-error {{duplicate case value: 'kTest19Enum1' and 'kTest19Enum2' both equal '7'}} \
350 // expected-note {{previous case}}
351 case (int)kTest19Enum2: //expected-error {{duplicate case value 'kTest19Enum2'}}
353 case 3: // expected-note {{previous case}}
354 case a: // expected-error {{duplicate case value: '3' and 'a' both equal '3'}} \
355 // expected-note {{previous case}}
356 case a: // expected-error {{duplicate case value 'a'}}
357 break;
361 // Allow the warning 'case value not in enumerated type' to be silenced with
362 // the following pattern.
364 // If 'case' expression refers to a static const variable of the correct enum
365 // type, then we count this as a sufficient declaration of intent by the user,
366 // so we silence the warning.
367 enum ExtendedEnum1 {
368 EE1_a,
369 EE1_b
372 enum ExtendedEnum1_unrelated { EE1_misc };
374 static const enum ExtendedEnum1 EE1_c = 100;
375 static const enum ExtendedEnum1_unrelated EE1_d = 101;
377 void switch_on_ExtendedEnum1(enum ExtendedEnum1 e) {
378 switch(e) {
379 case EE1_a: break;
380 case EE1_b: break;
381 case EE1_c: break; // no-warning
382 case EE1_d: break; // expected-warning {{case value not in enumerated type 'enum ExtendedEnum1'}}
383 // expected-warning@-1 {{comparison of different enumeration types in switch statement ('enum ExtendedEnum1' and 'enum ExtendedEnum1_unrelated')}}
387 void PR11778(char c, int n, long long ll) {
388 // Do not reject this; we don't have duplicate case values because we
389 // check for duplicates in the promoted type.
390 switch (c) case 1: case 257: ; // expected-warning {{overflow}}
392 switch (n) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
393 switch ((int)ll) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
394 switch ((long long)n) case 0x100000001LL: case 1: ;
395 switch (ll) case 0x100000001LL: case 1: ;