[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Parser / switch-recovery.cpp
blobbaf703cd03aed7ba2d974b0a6c8147427d0886e8
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
3 struct A {};
4 struct B {
5 void foo(int b) {
6 switch (a) { // expected-error{{use of undeclared identifier 'a'}}
7 default:
8 return;
11 switch (b) {
12 case 17 // expected-error{{expected ':' after 'case'}}
13 break;
15 default // expected-error{{expected ':' after 'default'}}
16 return;
20 void test2() {
21 enum X { Xa, Xb } x;
23 switch (x) { // expected-warning {{enumeration value 'Xb' not handled in switch}}
24 case Xa; // expected-error {{expected ':' after 'case'}}
25 break;
28 switch (x) {
29 default; // expected-error {{expected ':' after 'default'}}
30 break;
34 int test3(int i) {
35 switch (i) {
36 case 1: return 0;
37 2: return 1; // expected-error {{expected 'case' keyword before expression}}
38 default: return 5;
43 int test4(int i) {
44 switch (i)
45 1: return -1; // expected-error {{expected 'case' keyword before expression}}
46 return 0;
49 int test5(int i) {
50 switch (i) {
51 case 1: case 2: case 3: return 1;
53 4:5:6:7: return 2; // expected-error 4{{expected 'case' keyword before expression}}
55 default: return -1;
59 int test6(int i) {
60 switch (i) {
61 case 1:
62 case 4:
63 // This class provides extra single colon tokens. Make sure no
64 // errors are seen here.
65 class foo{
66 public:
67 protected:
68 private:
70 case 2:
71 5: // expected-error {{expected 'case' keyword before expression}}
72 default: return 1;
76 int test7(int i) {
77 switch (i) {
78 case false ? 1 : 2:
79 true ? 1 : 2: // expected-error {{expected 'case' keyword before expression}}
80 case 10:
81 14 ? 3 : 4; // expected-warning {{expression result unused}}
82 default:
83 return 1;
87 enum foo { A, B, C};
88 int test8( foo x ) {
89 switch (x) {
90 A: return 0; // FIXME: give a warning for unused labels that could also be
91 // a case expression.
92 default: return 1;
96 // Stress test to make sure Clang doesn't crash.
97 void test9(int x) { // expected-note {{'x' declared here}}
98 switch(x) {
99 case 1: return;
100 2: case; // expected-error {{expected 'case' keyword before expression}} \
101 expected-error {{expected expression}}
102 4:5:6: return; // expected-error 3{{expected 'case' keyword before expression}}
103 7: :x; // expected-error {{expected 'case' keyword before expression}} \
104 expected-error {{expected expression}}
105 8:: x; // expected-error {{expected ';' after expression}} \
106 expected-error {{no member named 'x' in the global namespace; did you mean simply 'x'?}} \
107 expected-warning {{expression result unused}}
108 9:: :y; // expected-error {{expected ';' after expression}} \
109 expected-error {{expected unqualified-id}} \
110 expected-warning {{expression result unused}}
111 :; // expected-error {{expected expression}}
112 ::; // expected-error {{expected unqualified-id}}
116 void test10(int x) {
117 switch (x) {
118 case 1: {
119 struct Inner {
120 void g(int y) {
121 2: y++; // expected-error {{expected ';' after expression}} \
122 // expected-warning {{expression result unused}}
125 break;
130 template<typename T>
131 struct test11 {
132 enum { E };
134 void f(int x) {
135 switch (x) {
136 E: break; // FIXME: give a 'case' fix-it for unused labels that
137 // could also be an expression an a case label.
138 E+1: break; // expected-error {{expected 'case' keyword before expression}}
143 void test12(int x) {
144 switch (x) {
145 0: // expected-error {{expected 'case' keyword before expression}}
146 while (x) {
147 1: // expected-error {{expected 'case' keyword before expression}}
148 for (;x;) {
149 2: // expected-error {{expected 'case' keyword before expression}}
150 if (x > 0) {
151 3: // expected-error {{expected 'case' keyword before expression}}
152 --x;
159 void missing_statement_case(int x) {
160 switch (x) {
161 case 1:
162 case 0:
163 } // expected-warning {{label at end of compound statement is a C++23 extension}}
166 void missing_statement_default(int x) {
167 switch (x) {
168 case 0:
169 default:
170 } // expected-warning {{label at end of compound statement is a C++23 extension}}
173 void pr19022_1() {
174 switch (int x) // expected-error {{variable declaration in condition must have an initializer}}
175 case v: ; // expected-error {{use of undeclared identifier 'v'}}
178 void pr19022_1a(int x) {
179 switch(x) {
180 case 1 // expected-error{{expected ':' after 'case'}}
181 } // expected-warning {{label at end of compound statement is a C++23 extension}}
184 void pr19022_1b(int x) {
185 switch(x) {
186 case v // expected-error{{use of undeclared identifier 'v'}} \
187 // expected-error{{expected ':' after 'case'}}
188 } // expected-error{{expected statement}}
191 void pr19022_2() {
192 switch (int x) // expected-error {{variable declaration in condition must have an initializer}}
193 case v1: case v2: ; // expected-error {{use of undeclared identifier 'v1'}} \
194 // expected-error {{use of undeclared identifier 'v2'}}
197 void pr19022_3(int x) {
198 switch (x)
199 case 1: case v2: ; // expected-error {{use of undeclared identifier 'v2'}}
202 int pr19022_4(int x) {
203 switch(x) {
204 case 1 // expected-error{{expected ':' after 'case'}} expected-note{{previous case defined here}}
205 case 1 : return x; // expected-error{{duplicate case value '1'}}
209 void pr19022_5(int x) {
210 switch(x) {
211 case 1: case // expected-error{{expected ':' after 'case'}}
212 } // expected-error{{expected expression}} \
213 // expected-warning {{label at end of compound statement is a C++23 extension}}
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'}}
224 namespace pr21841 {
225 void fn1() {
226 switch (0)
227 switch (0 // expected-note{{to match this '('}}
228 { // expected-error{{expected ')'}}
230 } // expected-error{{expected statement}}