switch.c revision 30ab37122300a5f6664b8ae2d0b43b4396eb6bcb
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2void f (int z) {
3  while (z) {
4    default: z--;            // expected-error {{statement not in switch}}
5  }
6}
7
8void 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}}
13   ;
14
15  case 100 ... 99: ;         // expected-warning {{empty case range}}
16
17  case 43: ;                 // expected-note {{previous case}}
18  case 43 ... 45:  ;         // expected-error {{duplicate case value}}
19
20  case 100 ... 20000:;       // expected-note {{previous case}}
21  case 15000 ... 40000000:;  // expected-error {{duplicate case value}}
22  }
23}
24
25void test3(void) {
26  // empty switch;
27  switch (0);
28}
29
30extern int g();
31
32void test4()
33{
34  switch (1) {
35  case 0 && g():
36  case 1 || g():
37    break;
38  }
39
40  switch(1)  {
41  case g(): // expected-error {{expression is not an integer constant expression}}
42  case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
43    break;
44  }
45
46  switch (1) {
47  case 0 && g() ... 1 || g():
48    break;
49  }
50
51  switch (1) {
52  case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
53    break;
54  }
55
56  switch (1) {
57  case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
58    break;
59  }
60}
61
62void test5(int z) {
63  switch(z) {
64    default:  // expected-note {{previous case defined here}}
65    default:  // expected-error {{multiple default labels in one switch}}
66      break;
67  }
68}
69
70void test6() {
71  const char ch = 'a';
72  switch(ch) {
73    case 1234:  // expected-warning {{overflow converting case value}}
74      break;
75  }
76}
77
78// PR5606
79int f0(int var) { // expected-note{{'var' declared here}}
80  switch (va) { // expected-error{{use of undeclared identifier 'va'}}
81  case 1:
82    break;
83  case 2:
84    return 1;
85  }
86  return 2;
87}
88
89void test7() {
90  enum {
91    A = 1,
92    B
93  } a;
94  switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
95    case A:
96      break;
97  }
98  switch(a) {
99    case B:
100    case A:
101      break;
102  }
103  switch(a) {
104    case A:
105    case B:
106    case 3: // expected-warning{{case value not in enumerated type ''}}
107      break;
108  }
109  switch(a) {
110    case A:
111    case B:
112    case 3 ... //expected-warning{{case value not in enumerated type ''}}
113        4: //expected-warning{{case value not in enumerated type ''}}
114      break;
115  }
116  switch(a) {
117    case 1 ... 2:
118      break;
119  }
120  switch(a) {
121    case 0 ... 2: //expected-warning{{case value not in enumerated type ''}}
122      break;
123  }
124  switch(a) {
125    case 1 ... 3: //expected-warning{{case value not in enumerated type ''}}
126      break;
127  }
128  switch(a) {
129    case 0 ...  //expected-warning{{case value not in enumerated type ''}}
130      3:  //expected-warning{{case value not in enumerated type ''}}
131      break;
132  }
133
134}
135
136void test8() {
137  enum {
138    A,
139    B,
140    C = 1
141  } a;
142  switch(a) {
143    case A:
144    case B:
145     break;
146  }
147  switch(a) {
148    case A:
149    case C:
150      break;
151  }
152  switch(a) { //expected-warning{{enumeration value 'B' not handled in switch}}
153    case A:
154      break;
155  }
156}
157
158void test9() {
159  enum {
160    A = 3,
161    C = 1
162  } a;
163  switch(a) {
164    case 0: //expected-warning{{case value not in enumerated type ''}}
165    case 1:
166    case 2: //expected-warning{{case value not in enumerated type ''}}
167    case 3:
168    case 4: //expected-warning{{case value not in enumerated type ''}}
169      break;
170  }
171}
172
173void test10() {
174  enum {
175    A = 10,
176    C = 2,
177    B = 4,
178    D = 12
179  } a;
180  switch(a) {
181    case 0 ...  //expected-warning{{case value not in enumerated type ''}}
182	    1:  //expected-warning{{case value not in enumerated type ''}}
183    case 2 ... 4:
184    case 5 ...  //expected-warning{{case value not in enumerated type ''}}
185	      9:  //expected-warning{{case value not in enumerated type ''}}
186    case 10 ... 12:
187    case 13 ...  //expected-warning{{case value not in enumerated type ''}}
188              16: //expected-warning{{case value not in enumerated type ''}}
189      break;
190  }
191}
192
193void test11() {
194  enum {
195    A = -1,
196    B,
197    C
198  } a;
199  switch(a) { //expected-warning{{enumeration value 'A' not handled in switch}}
200    case B:
201    case C:
202      break;
203  }
204
205  switch(a) {
206    case B:
207    case C:
208      break;
209
210    default:
211      break;
212  }
213}
214
215void test12() {
216  enum {
217    A = -1,
218    B = 4294967286
219  } a;
220  switch(a) {
221    case A:
222    case B:
223      break;
224  }
225}
226
227// <rdar://problem/7643909>
228typedef enum {
229    val1,
230    val2,
231    val3
232} my_type_t;
233
234int test13(my_type_t t) {
235  switch(t) { // expected-warning{{enumeration value 'val3' not handled in switch}}
236  case val1:
237    return 1;
238  case val2:
239    return 2;
240  }
241  return -1;
242}
243