Home | History | Annotate | Download | only in Parser
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 
      3 // <rdar://problem/7971948>
      4 struct A {};
      5 struct B {
      6   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 
     21   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 
     35   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 
     44 int test4(int i) {
     45   switch (i)
     46     1: return -1;  // expected-error {{expected 'case' keyword before expression}}
     47   return 0;
     48 }
     49 
     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 
     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 
     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;
     83     default:
     84       return 1;
     85   }
     86 }
     87 
     88 enum foo { A, B, C};
     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.
     98 void test9(int x) {
     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}} \
    108               expected-warning {{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 
    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 
    135   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 
    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 
    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 
    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