blob: 4e39e0f0c2d5d527eaca226c9da696da2745109f [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %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) {
Chris Lattner90a8f272010-07-13 19:41:32 +000053 case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \
54 expected-warning {{use of logical && with constant operand}}
Anders Carlsson6dde0d52008-11-22 21:50:49 +000055 break;
56 }
57
John McCall0fb97082010-05-18 03:19:21 +000058 switch (cond) {
Chris Lattner90a8f272010-07-13 19:41:32 +000059 case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \\
60 expected-warning {{use of logical || with constant operand}}
Anders Carlsson6dde0d52008-11-22 21:50:49 +000061 break;
62 }
Anders Carlsson51fe9962008-11-22 21:04:56 +000063}
64
Chris Lattner5f4a6822008-11-23 23:12:31 +000065void test5(int z) {
66 switch(z) {
67 default: // expected-note {{previous case defined here}}
68 default: // expected-error {{multiple default labels in one switch}}
69 break;
70 }
71}
72
Chris Lattner5f048812009-10-16 16:45:22 +000073void test6() {
John McCall0fb97082010-05-18 03:19:21 +000074 char ch = 'a';
Chris Lattner5f048812009-10-16 16:45:22 +000075 switch(ch) {
76 case 1234: // expected-warning {{overflow converting case value}}
77 break;
78 }
79}
Douglas Gregorbe724ba2009-11-25 06:20:02 +000080
81// PR5606
Douglas Gregor539c5c32010-01-07 00:31:29 +000082int f0(int var) { // expected-note{{'var' declared here}}
Douglas Gregorbe724ba2009-11-25 06:20:02 +000083 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
84 case 1:
85 break;
86 case 2:
87 return 1;
88 }
89 return 2;
90}
Douglas Gregorba915af2010-02-08 22:24:16 +000091
92void test7() {
93 enum {
94 A = 1,
95 B
96 } a;
97 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
98 case A:
99 break;
100 }
101 switch(a) {
102 case B:
103 case A:
104 break;
105 }
106 switch(a) {
107 case A:
108 case B:
109 case 3: // expected-warning{{case value not in enumerated type ''}}
110 break;
111 }
112 switch(a) {
113 case A:
114 case B:
115 case 3 ... //expected-warning{{case value not in enumerated type ''}}
116 4: //expected-warning{{case value not in enumerated type ''}}
117 break;
118 }
119 switch(a) {
120 case 1 ... 2:
121 break;
122 }
123 switch(a) {
124 case 0 ... 2: //expected-warning{{case value not in enumerated type ''}}
125 break;
126 }
127 switch(a) {
128 case 1 ... 3: //expected-warning{{case value not in enumerated type ''}}
129 break;
130 }
131 switch(a) {
132 case 0 ... //expected-warning{{case value not in enumerated type ''}}
133 3: //expected-warning{{case value not in enumerated type ''}}
134 break;
135 }
136
137}
138
139void test8() {
140 enum {
141 A,
142 B,
143 C = 1
144 } a;
145 switch(a) {
146 case A:
147 case B:
148 break;
149 }
150 switch(a) {
151 case A:
152 case C:
153 break;
154 }
155 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
156 case A:
157 break;
158 }
159}
160
161void test9() {
162 enum {
163 A = 3,
164 C = 1
165 } a;
166 switch(a) {
167 case 0: //expected-warning{{case value not in enumerated type ''}}
168 case 1:
169 case 2: //expected-warning{{case value not in enumerated type ''}}
170 case 3:
171 case 4: //expected-warning{{case value not in enumerated type ''}}
172 break;
173 }
174}
175
176void test10() {
177 enum {
178 A = 10,
179 C = 2,
180 B = 4,
181 D = 12
182 } a;
183 switch(a) {
184 case 0 ... //expected-warning{{case value not in enumerated type ''}}
185 1: //expected-warning{{case value not in enumerated type ''}}
186 case 2 ... 4:
187 case 5 ... //expected-warning{{case value not in enumerated type ''}}
188 9: //expected-warning{{case value not in enumerated type ''}}
189 case 10 ... 12:
190 case 13 ... //expected-warning{{case value not in enumerated type ''}}
191 16: //expected-warning{{case value not in enumerated type ''}}
192 break;
193 }
194}
195
196void test11() {
197 enum {
198 A = -1,
199 B,
200 C
201 } a;
202 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
203 case B:
204 case C:
205 break;
206 }
207
208 switch(a) {
209 case B:
210 case C:
211 break;
212
213 default:
214 break;
215 }
216}
217
218void test12() {
219 enum {
220 A = -1,
221 B = 4294967286
222 } a;
223 switch(a) {
224 case A:
225 case B:
226 break;
227 }
228}
Douglas Gregor30ab3712010-02-17 23:29:11 +0000229
230// <rdar://problem/7643909>
231typedef enum {
232 val1,
233 val2,
234 val3
235} my_type_t;
236
237int test13(my_type_t t) {
238 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
239 case val1:
240 return 1;
241 case val2:
242 return 2;
243 }
244 return -1;
245}
Douglas Gregor2853eac2010-02-18 00:56:01 +0000246
247// <rdar://problem/7658121>
248enum {
249 EC0 = 0xFFFF0000,
250 EC1 = 0xFFFF0001,
251};
252
253int test14(int a) {
254 switch(a) {
255 case EC0: return 0;
256 case EC1: return 1;
257 }
258 return 0;
259}
Douglas Gregorf9f627d2010-03-01 01:04:55 +0000260
261void f1(unsigned x) {
262 switch (x) {
263 case -1: break;
264 default: break;
265 }
266}
John McCall0fb97082010-05-18 03:19:21 +0000267
268void test15() {
269 int i = 0;
270 switch (1) { // expected-warning {{no case matching constant switch condition '1'}}
271 case 0: i = 0; break;
272 case 2: i++; break;
273 }
274}
275
276void test16() {
277 const char c = '5';
278 switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
279 case '6': return;
280 }
281}
John McCall6907fbe2010-06-12 01:56:02 +0000282
283// PR7359
284void test17(int x) {
285 switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
286 case 0: return;
287 }
288
289 switch ((int) (x <= 17)) {
290 case 0: return;
291 }
292}