1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 // <rdar://problem/7971948>
4 struct A {};
5 struct B {
fooB6   void foo(int b) {
7     switch (a) { // expected-error{{use of undeclared identifier 'a'}}
8     default:
9       return;
10     }
11 
12     switch (b) {
13     case 17 // expected-error{{expected ':' after 'case'}}
14       break;
15 
16     default // expected-error{{expected ':' after 'default'}}
17       return;
18     }
19   }
20 
test2B21   void test2() {
22     enum X { Xa, Xb } x;
23 
24     switch (x) { // expected-warning {{enumeration value 'Xb' not handled in switch}}
25     case Xa; // expected-error {{expected ':' after 'case'}}
26       break;
27     }
28 
29     switch (x) {
30     default; // expected-error {{expected ':' after 'default'}}
31       break;
32     }
33   }
34 
test3B35   int test3(int i) {
36     switch (i) {
37       case 1: return 0;
38       2: return 1;  // expected-error {{expected 'case' keyword before expression}}
39       default: return 5;
40     }
41   }
42 };
43 
test4(int i)44 int test4(int i) {
45   switch (i)
46     1: return -1;  // expected-error {{expected 'case' keyword before expression}}
47   return 0;
48 }
49 
test5(int i)50 int test5(int i) {
51   switch (i) {
52     case 1: case 2: case 3: return 1;
53     {
54     4:5:6:7: return 2;  // expected-error 4{{expected 'case' keyword before expression}}
55     }
56     default: return -1;
57   }
58 }
59 
test6(int i)60 int test6(int i) {
61   switch (i) {
62     case 1:
63     case 4:
64       // This class provides extra single colon tokens.  Make sure no
65       // errors are seen here.
66       class foo{
67         public:
68         protected:
69         private:
70       };
71     case 2:
72     5:  // expected-error {{expected 'case' keyword before expression}}
73     default: return 1;
74   }
75 }
76 
test7(int i)77 int test7(int i) {
78   switch (i) {
79     case false ? 1 : 2:
80     true ? 1 : 2:  // expected-error {{expected 'case' keyword before expression}}
81     case 10:
82       14 ? 3 : 4;  // expected-warning {{expression result unused}}
83     default:
84       return 1;
85   }
86 }
87 
88 enum foo { A, B, C};
test8(foo x)89 int test8( foo x ) {
90   switch (x) {
91     A: return 0;  // FIXME: give a warning for unused labels that could also be
92                   // a case expression.
93     default: return 1;
94   }
95 }
96 
97 // Stress test to make sure Clang doesn't crash.
test9(int x)98 void test9(int x) { // expected-note {{'x' declared here}}
99   switch(x) {
100     case 1: return;
101     2: case; // expected-error {{expected 'case' keyword before expression}} \
102                 expected-error {{expected expression}}
103     4:5:6: return; // expected-error 3{{expected 'case' keyword before expression}}
104     7: :x; // expected-error {{expected 'case' keyword before expression}} \
105               expected-error {{expected expression}}
106     8:: x; // expected-error {{expected ';' after expression}} \
107               expected-error {{no member named 'x' in the global namespace; did you mean simply 'x'?}} \
108               expected-warning 2 {{expression result unused}}
109     9:: :y; // expected-error {{expected ';' after expression}} \
110                expected-error {{expected unqualified-id}} \
111                expected-warning {{expression result unused}}
112     :; // expected-error {{expected expression}}
113     ::; // expected-error {{expected unqualified-id}}
114   }
115 }
116 
test10(int x)117 void test10(int x) {
118   switch (x) {
119     case 1: {
120       struct Inner {
121         void g(int y) {
122           2: y++;  // expected-error {{expected ';' after expression}} \
123                    // expected-warning {{expression result unused}}
124         }
125       };
126       break;
127     }
128   }
129 }
130 
131 template<typename T>
132 struct test11 {
133   enum { E };
134 
ftest11135   void f(int x) {
136     switch (x) {
137       E: break;    // FIXME: give a 'case' fix-it for unused labels that
138                    // could also be an expression an a case label.
139       E+1: break;  // expected-error {{expected 'case' keyword before expression}}
140     }
141   }
142 };
143 
test12(int x)144 void test12(int x) {
145   switch (x) {
146     0:  // expected-error {{expected 'case' keyword before expression}}
147     while (x) {
148       1:  // expected-error {{expected 'case' keyword before expression}}
149       for (;x;) {
150         2:  // expected-error {{expected 'case' keyword before expression}}
151         if (x > 0) {
152           3:  // expected-error {{expected 'case' keyword before expression}}
153           --x;
154         }
155       }
156     }
157   }
158 }
159 
missing_statement_case(int x)160 void missing_statement_case(int x) {
161   switch (x) {
162     case 1:
163     case 0: // expected-error {{label at end of compound statement: expected statement}}
164   }
165 }
166 
missing_statement_default(int x)167 void missing_statement_default(int x) {
168   switch (x) {
169     case 0:
170     default: // expected-error {{label at end of compound statement: expected statement}}
171   }
172 }
173 
pr19022_1()174 void pr19022_1() {
175   switch (int x)  // expected-error {{variable declaration in condition must have an initializer}}
176   case v: ;  // expected-error {{use of undeclared identifier 'v'}}
177 }
178 
pr19022_1a(int x)179 void pr19022_1a(int x) {
180   switch(x) {
181   case 1  // expected-error{{expected ':' after 'case'}} \
182           // expected-error{{label at end of compound statement: expected statement}}
183   }
184 }
185 
186 void pr19022_1b(int x) {
187   switch(x) {
188   case v  // expected-error{{use of undeclared identifier 'v'}}
189   }
190  }
191 
192 void pr19022_2() {
193   switch (int x)  // expected-error {{variable declaration in condition must have an initializer}}
194   case v1: case v2: ;  // expected-error {{use of undeclared identifier 'v1'}} \
195                        // expected-error {{use of undeclared identifier 'v2'}}
196 }
197 
198 void pr19022_3(int x) {
199   switch (x)
200   case 1: case v2: ;  // expected-error {{use of undeclared identifier 'v2'}}
201 }
202 
203 int pr19022_4(int x) {
204   switch(x) {
205   case 1  // expected-error{{expected ':' after 'case'}} expected-note{{previous case defined here}}
206   case 1 : return x;  // expected-error{{duplicate case value '1'}}
207   }
208 }
209 
210 void pr19022_5(int x) {
211   switch(x) {
212   case 1: case
213   }  // expected-error{{expected expression}}
214 }
215 
216 namespace pr19022 {
217 int baz5() {}
218 bool bar0() {
219   switch (int foo0)  //expected-error{{variable declaration in condition must have an initializer}}
220   case bar5: ;  // expected-error{{use of undeclared identifier 'bar5'}}
221 }
222 }
223 
224 namespace pr21841 {
225 void fn1() {
226   switch (0)
227     switch (0  // expected-note{{to match this '('}}
228     {  // expected-error{{expected ')'}}
229     }
230 } // expected-error{{expected statement}}
231 }
232