Driver: tweak handling of '--analyze' to invoke
[clang.git] / test / Sema / switch.c
blob96a6eb6e9632d01e45b58711646508671573f0e5
1 // RUN: %clang_cc1 -fsyntax-only -verify %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}}
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'}}
30 extern int g();
32 void test4()
34 int cond;
35 switch (cond) {
36 case 0 && g():
37 case 1 || g():
38 break;
41 switch(cond) {
42 case g(): // expected-error {{expression is not an integer constant expression}}
43 case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
44 break;
47 switch (cond) {
48 case 0 && g() ... 1 || g():
49 break;
52 switch (cond) {
53 case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
54 break;
57 switch (cond) {
58 case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
59 break;
63 void test5(int z) {
64 switch(z) {
65 default: // expected-note {{previous case defined here}}
66 default: // expected-error {{multiple default labels in one switch}}
67 break;
71 void test6() {
72 char ch = 'a';
73 switch(ch) {
74 case 1234: // expected-warning {{overflow converting case value}}
75 break;
79 // PR5606
80 int f0(int var) {
81 switch (va) { // expected-error{{use of undeclared identifier 'va'}}
82 case 1:
83 break;
84 case 2:
85 return 1;
87 return 2;
90 void test7() {
91 enum {
92 A = 1,
94 } a;
95 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
96 case A:
97 break;
99 switch(a) {
100 case B:
101 case A:
102 break;
104 switch(a) {
105 case A:
106 case B:
107 case 3: // expected-warning{{case value not in enumerated type ''}}
108 break;
110 switch(a) {
111 case A:
112 case B:
113 case 3 ... //expected-warning{{case value not in enumerated type ''}}
114 4: //expected-warning{{case value not in enumerated type ''}}
115 break;
117 switch(a) {
118 case 1 ... 2:
119 break;
121 switch(a) {
122 case 0 ... 2: //expected-warning{{case value not in enumerated type ''}}
123 break;
125 switch(a) {
126 case 1 ... 3: //expected-warning{{case value not in enumerated type ''}}
127 break;
129 switch(a) {
130 case 0 ... //expected-warning{{case value not in enumerated type ''}}
131 3: //expected-warning{{case value not in enumerated type ''}}
132 break;
137 void test8() {
138 enum {
141 C = 1
142 } a;
143 switch(a) {
144 case A:
145 case B:
146 break;
148 switch(a) {
149 case A:
150 case C:
151 break;
153 switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
154 case A:
155 break;
159 void test9() {
160 enum {
161 A = 3,
162 C = 1
163 } a;
164 switch(a) {
165 case 0: //expected-warning{{case value not in enumerated type ''}}
166 case 1:
167 case 2: //expected-warning{{case value not in enumerated type ''}}
168 case 3:
169 case 4: //expected-warning{{case value not in enumerated type ''}}
170 break;
174 void test10() {
175 enum {
176 A = 10,
177 C = 2,
178 B = 4,
179 D = 12
180 } a;
181 switch(a) {
182 case 0 ... //expected-warning{{case value not in enumerated type ''}}
183 1: //expected-warning{{case value not in enumerated type ''}}
184 case 2 ... 4:
185 case 5 ... //expected-warning{{case value not in enumerated type ''}}
186 9: //expected-warning{{case value not in enumerated type ''}}
187 case 10 ... 12:
188 case 13 ... //expected-warning{{case value not in enumerated type ''}}
189 16: //expected-warning{{case value not in enumerated type ''}}
190 break;
194 void test11() {
195 enum {
196 A = -1,
199 } a;
200 switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
201 case B:
202 case C:
203 break;
206 switch(a) {
207 case B:
208 case C:
209 break;
211 default:
212 break;
216 void test12() {
217 enum {
218 A = -1,
219 B = 4294967286
220 } a;
221 switch(a) {
222 case A:
223 case B:
224 break;
228 // <rdar://problem/7643909>
229 typedef enum {
230 val1,
231 val2,
232 val3
233 } my_type_t;
235 int test13(my_type_t t) {
236 switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
237 case val1:
238 return 1;
239 case val2:
240 return 2;
242 return -1;
245 // <rdar://problem/7658121>
246 enum {
247 EC0 = 0xFFFF0000,
248 EC1 = 0xFFFF0001,
251 int test14(int a) {
252 switch(a) {
253 case EC0: return 0;
254 case EC1: return 1;
256 return 0;
259 void f1(unsigned x) {
260 switch (x) {
261 case -1: break;
262 default: break;
266 void test15() {
267 int i = 0;
268 switch (1) { // expected-warning {{no case matching constant switch condition '1'}}
269 case 0: i = 0; break;
270 case 2: i++; break;
274 void test16() {
275 const char c = '5';
276 switch (c) { // expected-warning {{no case matching constant switch condition '53'}}
277 case '6': return;
281 // PR7359
282 void test17(int x) {
283 switch (x >= 17) { // expected-warning {{switch condition has boolean value}}
284 case 0: return;
287 switch ((int) (x <= 17)) {
288 case 0: return;