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