1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc // <rdar://problem/7971948> 4*f4a2713aSLionel Sambuc struct A {}; 5*f4a2713aSLionel Sambuc struct B { 6*f4a2713aSLionel Sambuc void foo(int b) { 7*f4a2713aSLionel Sambuc switch (a) { // expected-error{{use of undeclared identifier 'a'}} 8*f4a2713aSLionel Sambuc default: 9*f4a2713aSLionel Sambuc return; 10*f4a2713aSLionel Sambuc } 11*f4a2713aSLionel Sambuc 12*f4a2713aSLionel Sambuc switch (b) { 13*f4a2713aSLionel Sambuc case 17 // expected-error{{expected ':' after 'case'}} 14*f4a2713aSLionel Sambuc break; 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel Sambuc default // expected-error{{expected ':' after 'default'}} 17*f4a2713aSLionel Sambuc return; 18*f4a2713aSLionel Sambuc } 19*f4a2713aSLionel Sambuc } 20*f4a2713aSLionel Sambuc 21*f4a2713aSLionel Sambuc void test2() { 22*f4a2713aSLionel Sambuc enum X { Xa, Xb } x; 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc switch (x) { // expected-warning {{enumeration value 'Xb' not handled in switch}} 25*f4a2713aSLionel Sambuc case Xa; // expected-error {{expected ':' after 'case'}} 26*f4a2713aSLionel Sambuc break; 27*f4a2713aSLionel Sambuc } 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel Sambuc switch (x) { 30*f4a2713aSLionel Sambuc default; // expected-error {{expected ':' after 'default'}} 31*f4a2713aSLionel Sambuc break; 32*f4a2713aSLionel Sambuc } 33*f4a2713aSLionel Sambuc } 34*f4a2713aSLionel Sambuc 35*f4a2713aSLionel Sambuc int test3(int i) { 36*f4a2713aSLionel Sambuc switch (i) { 37*f4a2713aSLionel Sambuc case 1: return 0; 38*f4a2713aSLionel Sambuc 2: return 1; // expected-error {{expected 'case' keyword before expression}} 39*f4a2713aSLionel Sambuc default: return 5; 40*f4a2713aSLionel Sambuc } 41*f4a2713aSLionel Sambuc } 42*f4a2713aSLionel Sambuc }; 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel Sambuc int test4(int i) { 45*f4a2713aSLionel Sambuc switch (i) 46*f4a2713aSLionel Sambuc 1: return -1; // expected-error {{expected 'case' keyword before expression}} 47*f4a2713aSLionel Sambuc return 0; 48*f4a2713aSLionel Sambuc } 49*f4a2713aSLionel Sambuc 50*f4a2713aSLionel Sambuc int test5(int i) { 51*f4a2713aSLionel Sambuc switch (i) { 52*f4a2713aSLionel Sambuc case 1: case 2: case 3: return 1; 53*f4a2713aSLionel Sambuc { 54*f4a2713aSLionel Sambuc 4:5:6:7: return 2; // expected-error 4{{expected 'case' keyword before expression}} 55*f4a2713aSLionel Sambuc } 56*f4a2713aSLionel Sambuc default: return -1; 57*f4a2713aSLionel Sambuc } 58*f4a2713aSLionel Sambuc } 59*f4a2713aSLionel Sambuc 60*f4a2713aSLionel Sambuc int test6(int i) { 61*f4a2713aSLionel Sambuc switch (i) { 62*f4a2713aSLionel Sambuc case 1: 63*f4a2713aSLionel Sambuc case 4: 64*f4a2713aSLionel Sambuc // This class provides extra single colon tokens. Make sure no 65*f4a2713aSLionel Sambuc // errors are seen here. 66*f4a2713aSLionel Sambuc class foo{ 67*f4a2713aSLionel Sambuc public: 68*f4a2713aSLionel Sambuc protected: 69*f4a2713aSLionel Sambuc private: 70*f4a2713aSLionel Sambuc }; 71*f4a2713aSLionel Sambuc case 2: 72*f4a2713aSLionel Sambuc 5: // expected-error {{expected 'case' keyword before expression}} 73*f4a2713aSLionel Sambuc default: return 1; 74*f4a2713aSLionel Sambuc } 75*f4a2713aSLionel Sambuc } 76*f4a2713aSLionel Sambuc 77*f4a2713aSLionel Sambuc int test7(int i) { 78*f4a2713aSLionel Sambuc switch (i) { 79*f4a2713aSLionel Sambuc case false ? 1 : 2: 80*f4a2713aSLionel Sambuc true ? 1 : 2: // expected-error {{expected 'case' keyword before expression}} 81*f4a2713aSLionel Sambuc case 10: 82*f4a2713aSLionel Sambuc 14 ? 3 : 4; // expected-warning {{expression result unused}} 83*f4a2713aSLionel Sambuc default: 84*f4a2713aSLionel Sambuc return 1; 85*f4a2713aSLionel Sambuc } 86*f4a2713aSLionel Sambuc } 87*f4a2713aSLionel Sambuc 88*f4a2713aSLionel Sambuc enum foo { A, B, C}; 89*f4a2713aSLionel Sambuc int test8( foo x ) { 90*f4a2713aSLionel Sambuc switch (x) { 91*f4a2713aSLionel Sambuc A: return 0; // FIXME: give a warning for unused labels that could also be 92*f4a2713aSLionel Sambuc // a case expression. 93*f4a2713aSLionel Sambuc default: return 1; 94*f4a2713aSLionel Sambuc } 95*f4a2713aSLionel Sambuc } 96*f4a2713aSLionel Sambuc 97*f4a2713aSLionel Sambuc // Stress test to make sure Clang doesn't crash. 98*f4a2713aSLionel Sambuc void test9(int x) { // expected-note {{'x' declared here}} 99*f4a2713aSLionel Sambuc switch(x) { 100*f4a2713aSLionel Sambuc case 1: return; 101*f4a2713aSLionel Sambuc 2: case; // expected-error {{expected 'case' keyword before expression}} \ 102*f4a2713aSLionel Sambuc expected-error {{expected expression}} 103*f4a2713aSLionel Sambuc 4:5:6: return; // expected-error 3{{expected 'case' keyword before expression}} 104*f4a2713aSLionel Sambuc 7: :x; // expected-error {{expected 'case' keyword before expression}} \ 105*f4a2713aSLionel Sambuc expected-error {{expected expression}} 106*f4a2713aSLionel Sambuc 8:: x; // expected-error {{expected ';' after expression}} \ 107*f4a2713aSLionel Sambuc expected-error {{no member named 'x' in the global namespace; did you mean simply 'x'?}} \ 108*f4a2713aSLionel Sambuc expected-warning 2 {{expression result unused}} 109*f4a2713aSLionel Sambuc 9:: :y; // expected-error {{expected ';' after expression}} \ 110*f4a2713aSLionel Sambuc expected-error {{expected unqualified-id}} \ 111*f4a2713aSLionel Sambuc expected-warning {{expression result unused}} 112*f4a2713aSLionel Sambuc :; // expected-error {{expected expression}} 113*f4a2713aSLionel Sambuc ::; // expected-error {{expected unqualified-id}} 114*f4a2713aSLionel Sambuc } 115*f4a2713aSLionel Sambuc } 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel Sambuc void test10(int x) { 118*f4a2713aSLionel Sambuc switch (x) { 119*f4a2713aSLionel Sambuc case 1: { 120*f4a2713aSLionel Sambuc struct Inner { 121*f4a2713aSLionel Sambuc void g(int y) { 122*f4a2713aSLionel Sambuc 2: y++; // expected-error {{expected ';' after expression}} \ 123*f4a2713aSLionel Sambuc // expected-warning {{expression result unused}} 124*f4a2713aSLionel Sambuc } 125*f4a2713aSLionel Sambuc }; 126*f4a2713aSLionel Sambuc break; 127*f4a2713aSLionel Sambuc } 128*f4a2713aSLionel Sambuc } 129*f4a2713aSLionel Sambuc } 130*f4a2713aSLionel Sambuc 131*f4a2713aSLionel Sambuc template<typename T> 132*f4a2713aSLionel Sambuc struct test11 { 133*f4a2713aSLionel Sambuc enum { E }; 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc void f(int x) { 136*f4a2713aSLionel Sambuc switch (x) { 137*f4a2713aSLionel Sambuc E: break; // FIXME: give a 'case' fix-it for unused labels that 138*f4a2713aSLionel Sambuc // could also be an expression an a case label. 139*f4a2713aSLionel Sambuc E+1: break; // expected-error {{expected 'case' keyword before expression}} 140*f4a2713aSLionel Sambuc } 141*f4a2713aSLionel Sambuc } 142*f4a2713aSLionel Sambuc }; 143*f4a2713aSLionel Sambuc 144*f4a2713aSLionel Sambuc void test12(int x) { 145*f4a2713aSLionel Sambuc switch (x) { 146*f4a2713aSLionel Sambuc 0: // expected-error {{expected 'case' keyword before expression}} 147*f4a2713aSLionel Sambuc while (x) { 148*f4a2713aSLionel Sambuc 1: // expected-error {{expected 'case' keyword before expression}} 149*f4a2713aSLionel Sambuc for (;x;) { 150*f4a2713aSLionel Sambuc 2: // expected-error {{expected 'case' keyword before expression}} 151*f4a2713aSLionel Sambuc if (x > 0) { 152*f4a2713aSLionel Sambuc 3: // expected-error {{expected 'case' keyword before expression}} 153*f4a2713aSLionel Sambuc --x; 154*f4a2713aSLionel Sambuc } 155*f4a2713aSLionel Sambuc } 156*f4a2713aSLionel Sambuc } 157*f4a2713aSLionel Sambuc } 158*f4a2713aSLionel Sambuc } 159*f4a2713aSLionel Sambuc 160*f4a2713aSLionel Sambuc void missing_statement_case(int x) { 161*f4a2713aSLionel Sambuc switch (x) { 162*f4a2713aSLionel Sambuc case 1: 163*f4a2713aSLionel Sambuc case 0: // expected-error {{label at end of compound statement: expected statement}} 164*f4a2713aSLionel Sambuc } 165*f4a2713aSLionel Sambuc } 166*f4a2713aSLionel Sambuc 167*f4a2713aSLionel Sambuc void missing_statement_default(int x) { 168*f4a2713aSLionel Sambuc switch (x) { 169*f4a2713aSLionel Sambuc case 0: 170*f4a2713aSLionel Sambuc default: // expected-error {{label at end of compound statement: expected statement}} 171*f4a2713aSLionel Sambuc } 172*f4a2713aSLionel Sambuc } 173