blob: ea9be590244d22d26469d9a2676b7bb5a87d44ac [file] [log] [blame]
David Blaikiee1d82de2012-01-24 04:56:25 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-enum %s
Chris Lattner8a87e572007-07-23 17:05:23 +00002void f (int z) {
3 while (z) {
Chris Lattner0471f5b2007-08-23 18:29:20 +00004 default: z--; // expected-error {{statement not in switch}}
Chris Lattner8a87e572007-07-23 17:05:23 +00005 }
6}
7
Chris Lattnerf4021e72007-08-23 05:46:52 +00008void foo(int X) {
9 switch (X) {
Chris Lattner5f4a6822008-11-23 23:12:31 +000010 case 42: ; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000011 case 5000000000LL: // expected-warning {{overflow}}
12 case 42: // expected-error {{duplicate case value}}
Chris Lattnerf4021e72007-08-23 05:46:52 +000013 ;
Chris Lattner6efc4d32007-08-23 17:48:14 +000014
Chris Lattner0471f5b2007-08-23 18:29:20 +000015 case 100 ... 99: ; // expected-warning {{empty case range}}
16
Chris Lattner5f4a6822008-11-23 23:12:31 +000017 case 43: ; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000018 case 43 ... 45: ; // expected-error {{duplicate case value}}
19
Chris Lattner5f4a6822008-11-23 23:12:31 +000020 case 100 ... 20000:; // expected-note {{previous case}}
Chris Lattner0471f5b2007-08-23 18:29:20 +000021 case 15000 ... 40000000:; // expected-error {{duplicate case value}}
Chris Lattnerf4021e72007-08-23 05:46:52 +000022 }
23}
24
Chris Lattnerf3348502007-08-23 14:29:07 +000025void test3(void) {
Chris Lattner0471f5b2007-08-23 18:29:20 +000026 // empty switch;
John McCall0fb97082010-05-18 03:19:21 +000027 switch (0); // expected-warning {{no case matching constant switch condition '0'}}
Chris Lattnerf3348502007-08-23 14:29:07 +000028}
29
Anders Carlsson51fe9962008-11-22 21:04:56 +000030extern int g();
31
32void test4()
33{
John McCall0fb97082010-05-18 03:19:21 +000034 int cond;
35 switch (cond) {
Anders Carlsson51fe9962008-11-22 21:04:56 +000036 case 0 && g():
37 case 1 || g():
38 break;
39 }
40
John McCall0fb97082010-05-18 03:19:21 +000041 switch(cond) {
Anders Carlssond3a61d52008-12-01 02:13:02 +000042 case g(): // expected-error {{expression is not an integer constant expression}}
43 case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
Anders Carlsson51fe9962008-11-22 21:04:56 +000044 break;
45 }
46
John McCall0fb97082010-05-18 03:19:21 +000047 switch (cond) {
Anders Carlsson51fe9962008-11-22 21:04:56 +000048 case 0 && g() ... 1 || g():
49 break;
50 }
Anders Carlsson6dde0d52008-11-22 21:50:49 +000051
John McCall0fb97082010-05-18 03:19:21 +000052 switch (cond) {
Richard Smith282e7e62012-02-04 09:53:13 +000053 case g() // expected-error {{expression is not an integer constant expression}}
54 && 0:
Anders Carlsson6dde0d52008-11-22 21:50:49 +000055 break;
56 }
57
John McCall0fb97082010-05-18 03:19:21 +000058 switch (cond) {
Richard Smith282e7e62012-02-04 09:53:13 +000059 case 0 ...
60 g() // expected-error {{expression is not an integer constant expression}}
61 || 1:
Anders Carlsson6dde0d52008-11-22 21:50:49 +000062 break;
63 }
Anders Carlsson51fe9962008-11-22 21:04:56 +000064}
65
Chris Lattner5f4a6822008-11-23 23:12:31 +000066void test5(int z) {
67 switch(z) {
68 default: // expected-note {{previous case defined here}}
69 default: // expected-error {{multiple default labels in one switch}}
70 break;
71 }
72}
73
Chris Lattner5f048812009-10-16 16:45:22 +000074void test6() {
John McCall0fb97082010-05-18 03:19:21 +000075 char ch = 'a';
Chris Lattner5f048812009-10-16 16:45:22 +000076 switch(ch) {
77 case 1234: // expected-warning {{overflow converting case value}}
78 break;
79 }
80}
Douglas Gregorbe724ba2009-11-25 06:20:02 +000081
82// PR5606
Douglas Gregore24b5752010-10-14 20:34:08 +000083int f0(int var) {
Douglas Gregorbe724ba2009-11-25 06:20:02 +000084 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
85 case 1:
86 break;
87 case 2:
88 return 1;
89 }
90 return 2;
91}
Douglas Gregorba915af2010-02-08 22:24:16 +000092
93void test7() {
94 enum {
95 A = 1,
96 B
97 } a;
98 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
99 case A:
100 break;
101 }
102 switch(a) {
103 case B:
104 case A:
105 break;
106 }
107 switch(a) {
108 case A:
109 case B:
110 case 3: // expected-warning{{case value not in enumerated type ''}}
111 break;
112 }
113 switch(a) {
114 case A:
115 case B:
116 case 3 ... //expected-warning{{case value not in enumerated type ''}}
117 4: //expected-warning{{case value not in enumerated type ''}}
118 break;
119 }
120 switch(a) {
121 case 1 ... 2:
122 break;
123 }
124 switch(a) {
125 case 0 ... 2: //expected-warning{{case value not in enumerated type ''}}
126 break;
127 }
128 switch(a) {
129 case 1 ... 3: //expected-warning{{case value not in enumerated type ''}}
130 break;
131 }
132 switch(a) {
133 case 0 ... //expected-warning{{case value not in enumerated type ''}}
134 3: //expected-warning{{case value not in enumerated type ''}}
135 break;
136 }
137
138}
139
140void test8() {
141 enum {
142 A,
143 B,
144 C = 1
145 } a;
146 switch(a) {
147 case A:
148 case B:
149 break;
150 }
151 switch(a) {
152 case A:
153 case C:
154 break;
155 }
156 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
157 case A:
158 break;
159 }
160}
161
162void test9() {
163 enum {
164 A = 3,
165 C = 1
166 } a;
167 switch(a) {
168 case 0: //expected-warning{{case value not in enumerated type ''}}
169 case 1:
170 case 2: //expected-warning{{case value not in enumerated type ''}}
171 case 3:
172 case 4: //expected-warning{{case value not in enumerated type ''}}
173 break;
174 }
175}
176
177void test10() {
178 enum {
179 A = 10,
180 C = 2,
181 B = 4,
182 D = 12
183 } a;
184 switch(a) {
185 case 0 ... //expected-warning{{case value not in enumerated type ''}}
186 1: //expected-warning{{case value not in enumerated type ''}}
187 case 2 ... 4:
188 case 5 ... //expected-warning{{case value not in enumerated type ''}}
189 9: //expected-warning{{case value not in enumerated type ''}}
190 case 10 ... 12:
191 case 13 ... //expected-warning{{case value not in enumerated type ''}}
192 16: //expected-warning{{case value not in enumerated type ''}}
193 break;
194 }
195}
196
197void test11() {
198 enum {
199 A = -1,
200 B,
201 C
202 } a;
203 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
204 case B:
205 case C:
206 break;
207 }
208
David Blaikiee1d82de2012-01-24 04:56:25 +0000209 switch(a) { //expected-warning{{enumeration value 'A' not explicitly handled in switch}}
Douglas Gregorba915af2010-02-08 22:24:16 +0000210 case B:
211 case C:
212 break;
213
214 default:
215 break;
216 }
217}
218
219void test12() {
220 enum {
221 A = -1,
222 B = 4294967286
223 } a;
224 switch(a) {
225 case A:
226 case B:
227 break;
228 }
229}
Douglas Gregor30ab3712010-02-17 23:29:11 +0000230
231// <rdar://problem/7643909>
232typedef enum {
233 val1,
234 val2,
235 val3
236} my_type_t;
237
238int test13(my_type_t t) {
239 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
240 case val1:
241 return 1;
242 case val2:
243 return 2;
244 }
245 return -1;
246}
Douglas Gregor2853eac2010-02-18 00:56:01 +0000247
248// <rdar://problem/7658121>
249enum {
250 EC0 = 0xFFFF0000,
251 EC1 = 0xFFFF0001,
252};
253
254int test14(int a) {
255 switch(a) {
256 case EC0: return 0;
257 case EC1: return 1;
258 }
259 return 0;
260}
Douglas Gregorf9f627d2010-03-01 01:04:55 +0000261
262void f1(unsigned x) {
263 switch (x) {
264 case -1: break;
265 default: break;
266 }
267}
John McCall0fb97082010-05-18 03:19:21 +0000268
269void test15() {
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;
274 }
275}
276
277void test16() {
278 const char c = '5';
279 switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
280 case '6': return;
281 }
282}
John McCall6907fbe2010-06-12 01:56:02 +0000283
284// PR7359
285void test17(int x) {
286 switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
287 case 0: return;
288 }
289
290 switch ((int) (x <= 17)) {
291 case 0: return;
292 }
293}
David Blaikie31ceb612012-01-21 18:12:07 +0000294
295int test18() {
296 enum { A, B } a;
297 switch (a) {
298 case A: return 0;
299 case B: return 1;
David Blaikie93667502012-01-22 02:31:55 +0000300 case 7: return 1; // expected-warning {{case value not in enumerated type}}
David Blaikie2dd52e32012-01-24 05:34:08 +0000301 default: return 2; // expected-warning {{default label in switch which covers all enumeration values}}
David Blaikie93667502012-01-22 02:31:55 +0000302 }
303}