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