Debian Codegraph @

clang/test/Sema/switch.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default -triple x86_64-linux-gnu %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum -Wcovered-switch-default -triple x86_64-linux-gnu %s -fexperimental-new-constant-interpreter
3 void f (int z) {
4 while (z) {
5 default: z--; // expected-error {{statement not in switch}}
6 }
7 }
8
9 void foo(int X) {
10 switch (X) {
11 case 42: ; // expected-note {{previous case}}
12 case 5000000000LL: // expected-warning {{overflow}}
13 case 42: // expected-error {{duplicate case value '42'}}
14 ;
15
16 case 100 ... 99: ; // expected-warning {{empty case range}}
17
18 case 43: ; // expected-note {{previous case}}
19 case 43 ... 45: ; // expected-error {{duplicate case value}}
20
21 case 100 ... 20000:; // expected-note {{previous case}}
22 case 15000 ... 40000000:; // expected-error {{duplicate case value}}
23 }
24 }
25
26 void test3(void) {
27 // empty switch;
28 switch (0); // expected-warning {{no case matching constant switch condition '0'}} \
29 // expected-warning {{switch statement has empty body}} \
30 // expected-note{{put the semicolon on a separate line to silence this warning}}
31 }
32
33 extern int g(void);
34
35 void test4(void)
36 {
37 int cond;
38 switch (cond) {
39 case 0 && g():
40 case 1 || g():
41 break;
42 }
43
44 switch(cond) {
45 case g(): // expected-error {{expression is not an integer constant expression}}
46 case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
47 break;
48 }
49
50 switch (cond) {
51 case 0 && g() ... 1 || g():
52 break;
53 }
54
55 switch (cond) {
56 case g() // expected-error {{expression is not an integer constant expression}}
57 && 0:
58 break;
59 }
60
61 switch (cond) {
62 case 0 ...
63 g() // expected-error {{expression is not an integer constant expression}}
64 || 1:
65 break;
66 }
67 }
68
69 void test5(int z) {
70 switch(z) {
71 default: // expected-note {{previous case defined here}}
72 default: // expected-error {{multiple default labels in one switch}}
73 break;
74 }
75 }
76
77 void test6(void) {
78 char ch = 'a';
79 switch(ch) {
80 case 1234: // expected-warning {{overflow converting case value}}
81 break;
82 }
83 }
84
85 // PR5606
86 int f0(int var) {
87 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
88 case 1:
89 break;
90 case 2:
91 return 1;
92 }
93 return 2;
94 }
95
96 void test7(void) {
97 enum {
98 A = 1,
99 B
100 } a;
101 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
102 case A:
103 break;
104 }
105 switch(a) {
106 case B:
107 case A:
108 break;
109 }
110 switch(a) {
111 case A:
112 case B:
113 case 3: // expected-warning{{case value not in enumerated type 'enum (unnamed}}
114 break;
115 }
116 switch(a) {
117 case A:
118 case B:
119 case 3 ... //expected-warning{{case value not in enumerated type 'enum (unnamed}}
120 4: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
121 break;
122 }
123 switch(a) {
124 case 1 ... 2:
125 break;
126 }
127 switch(a) {
128 case 0 ... 2: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
129 break;
130 }
131 switch(a) {
132 case 1 ... 3: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
133 break;
134 }
135 switch(a) {
136 case 0 ... //expected-warning{{case value not in enumerated type 'enum (unnamed}}
137 3: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
138 break;
139 }
140
141 }
142
143 void test8(void) {
144 enum {
145 A,
146 B,
147 C = 1
148 } a;
149 switch(a) {
150 case A:
151 case B:
152 break;
153 }
154 switch(a) {
155 case A:
156 case C:
157 break;
158 }
159 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
160 case A:
161 break;
162 }
163 }
164
165 void test9(void) {
166 enum {
167 A = 3,
168 C = 1
169 } a;
170 switch(a) {
171 case 0: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
172 case 1:
173 case 2: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
174 case 3:
175 case 4: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
176 break;
177 }
178 }
179
180 void test10(void) {
181 enum {
182 A = 10,
183 C = 2,
184 B = 4,
185 D = 12
186 } a;
187 switch(a) {
188 case 0 ... //expected-warning{{case value not in enumerated type 'enum (unnamed}}
189 1: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
190 case 2 ... 4:
191 case 5 ... //expected-warning{{case value not in enumerated type 'enum (unnamed}}
192 9: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
193 case 10 ... 12:
194 case 13 ... //expected-warning{{case value not in enumerated type 'enum (unnamed}}
195 16: //expected-warning{{case value not in enumerated type 'enum (unnamed}}
196 break;
197 }
198 }
199
200 void test11(void) {
201 enum {
202 A = -1,
203 B,
204 C
205 } a;
206 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
207 case B:
208 case C:
209 break;
210 }
211
212 switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}}
213 case B:
214 case C:
215 break;
216
217 default:
218 break;
219 }
220 }
221
222 void test12(void) {
223 enum {
224 A = -1,
225 B = 4294967286
226 } a;
227 switch(a) {
228 case A:
229 case B:
230 break;
231 }
232 }
233
234 typedef enum {
235 val1,
236 val2,
237 val3
238 } my_type_t;
239
240 int test13(my_type_t t) {
241 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
242 case val1:
243 return 1;
244 case val2:
245 return 2;
246 }
247 return -1;
248 }
249
250 enum {
251 EC0 = 0xFFFF0000,
252 EC1 = 0xFFFF0001,
253 };
254
255 int test14(int a) {
256 switch(a) {
257 case EC0: return 0;
258 case EC1: return 1;
259 }
260 return 0;
261 }
262
263 void f1(unsigned x) {
264 switch (x) {
265 case -1: break;
266 default: break;
267 }
268 }
269
270 void test15(void) {
271 int i = 0;
272 switch (1) { // expected-warning {{no case matching constant switch condition '1'}}
273 case 0: i = 0; break;
274 case 2: i++; break;
275 }
276 }
277
278 void test16(void) {
279 const char c = '5';
280 switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
281 case '6': return;
282 }
283 }
284
285 struct bitfield_member {
286 unsigned bf : 1;
287 };
288
289 // PR7359
290 void test17(int x) {
291 switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
292 case 0: return;
293 }
294
295 switch ((int) (x <= 17)) {
296 case 0: return;
297 }
298
299 struct bitfield_member bm;
300 switch (bm.bf) { // no warning
301 case 0:
302 case 1:
303 return;
304 }
305 }
306
307 int test18(void) {
308 enum { A, B } a;
309 switch (a) {
310 case A: return 0;
311 case B: return 1;
312 case 7: return 1; // expected-warning {{case value not in enumerated type}}
313 default: return 2; // expected-warning {{default label in switch which covers all enumeration values}}
314 }
315 }
316
317 typedef enum {
318 kOne = 1,
319 } Ints;
320
321 void rdar110822110(Ints i)
322 {
323 switch (i) {
324 case kOne:
325 break;
326 case 2: // expected-warning {{case value not in enumerated type 'Ints'}}
327 break;
328 default: // expected-warning {{default label in switch which covers all enumeration values}}
329 break;
330 }
331 }
332
333 // PR9243
334 #define TEST19MACRO 5
335 void test19(int i) {
336 enum {
337 kTest19Enum1 = 7,
338 kTest19Enum2 = kTest19Enum1
339 };
340 const int a = 3;
341 switch (i) {
342 case 5: // expected-note {{previous case}}
343 case TEST19MACRO: // expected-error {{duplicate case value '5'}}
344
345 case 7: // expected-note {{previous case}}
346 case kTest19Enum1: // expected-error {{duplicate case value: '7' and 'kTest19Enum1' both equal '7'}} \
347 // expected-note {{previous case}}
348 case kTest19Enum1: // expected-error {{duplicate case value 'kTest19Enum1'}} \
349 // expected-note {{previous case}}
350 case kTest19Enum2: // expected-error {{duplicate case value: 'kTest19Enum1' and 'kTest19Enum2' both equal '7'}} \
351 // expected-note {{previous case}}
352 case (int)kTest19Enum2: //expected-error {{duplicate case value 'kTest19Enum2'}}
353
354 case 3: // expected-note {{previous case}}
355 case a: // expected-error {{duplicate case value: '3' and 'a' both equal '3'}} \
356 // expected-note {{previous case}}
357 case a: // expected-error {{duplicate case value 'a'}}
358 break;
359 }
360 }
361
362 // Allow the warning 'case value not in enumerated type' to be silenced with
363 // the following pattern.
364 //
365 // If 'case' expression refers to a static const variable of the correct enum
366 // type, then we count this as a sufficient declaration of intent by the user,
367 // so we silence the warning.
368 enum ExtendedEnum1 {
369 EE1_a,
370 EE1_b
371 };
372
373 enum ExtendedEnum1_unrelated { EE1_misc };
374
375 static const enum ExtendedEnum1 EE1_c = 100;
376 static const enum ExtendedEnum1_unrelated EE1_d = 101;
377
378 void switch_on_ExtendedEnum1(enum ExtendedEnum1 e) {
379 switch(e) {
380 case EE1_a: break;
381 case EE1_b: break;
382 case EE1_c: break; // no-warning
383 case EE1_d: break; // expected-warning {{case value not in enumerated type 'enum ExtendedEnum1'}}
384 // expected-warning@-1 {{comparison of different enumeration types in switch statement ('enum ExtendedEnum1' and 'enum ExtendedEnum1_unrelated')}}
385 }
386 }
387
388 void PR11778(char c, int n, long long ll) {
389 // Do not reject this; we don't have duplicate case values because we
390 // check for duplicates in the promoted type.
391 switch (c) case 1: case 257: ; // expected-warning {{overflow}}
392
393 switch (n) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
394 switch ((int)ll) case 0x100000001LL: case 1: ; // expected-warning {{overflow}} expected-error {{duplicate}} expected-note {{previous}}
395 switch ((long long)n) case 0x100000001LL: case 1: ;
396 switch (ll) case 0x100000001LL: case 1: ;
397 }