Home | History | Annotate | Download | only in Parser
      1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
      2 
      3 // New exciting ambiguities in C++11
      4 
      5 // final 'context sensitive' mess.
      6 namespace final {
      7   struct S { int n; };
      8   struct T { int n; };
      9   namespace N {
     10     int n;
     11     // These declare variables named final..
     12     extern struct S final;
     13     extern struct S final [[]];
     14     extern struct S final, foo;
     15     struct S final = S();
     16 
     17     // This defines a class, not a variable, even though it would successfully
     18     // parse as a variable but not as a class. DR1318's wording suggests that
     19     // this disambiguation is only performed on an ambiguity, but that was not
     20     // the intent.
     21     struct S final { // expected-note {{here}}
     22       int(n) // expected-error {{expected ';'}}
     23     };
     24     // This too.
     25     struct T final : S {}; // expected-error {{base 'S' is marked 'final'}}
     26     struct T bar : S {}; // expected-error {{expected ';' after top level declarator}} expected-error {{expected unqualified-id}}
     27   }
     28   // _Alignas isn't allowed in the places where alignas is. We used to
     29   // assert on this.
     30   struct U final _Alignas(4) {}; // expected-error 3{{}} expected-note {{}}
     31 }
     32 
     33 // enum versus bitfield mess.
     34 namespace bitfield {
     35   enum E {};
     36 
     37   struct T {
     38     constexpr T() {}
     39     constexpr T(int) {}
     40     constexpr T(T, T, T, T) {}
     41     constexpr T operator=(T) const { return *this; }
     42     constexpr operator int() const { return 4; }
     43   };
     44   constexpr T a, b, c, d;
     45 
     46   struct S1 {
     47     enum E : T ( a = 1, b = 2, c = 3, 4 ); // ok, declares a bitfield
     48   };
     49   // This could be a bit-field.
     50   struct S2 {
     51     enum E : T { a = 1, b = 2, c = 3, 4 }; // expected-error {{non-integral type}} expected-error {{expected identifier}}
     52   };
     53   struct S3 {
     54     enum E : int { a = 1, b = 2, c = 3, d }; // ok, defines an enum
     55   };
     56   // Ambiguous.
     57   struct S4 {
     58     enum E : int { a = 1 }; // ok, defines an enum
     59   };
     60   // This could be a bit-field, but would be ill-formed due to the anonymous
     61   // member being initialized.
     62   struct S5 {
     63     enum E : int { a = 1 } { b = 2 }; // expected-error {{expected ';' after enum}} expected-error {{expected member name}}
     64   };
     65   // This could be a bit-field.
     66   struct S6 {
     67     enum E : int { 1 }; // expected-error {{expected identifier}}
     68   };
     69 
     70   struct U {
     71     constexpr operator T() const { return T(); } // expected-note 2{{candidate}}
     72   };
     73   // This could be a bit-field.
     74   struct S7 {
     75     enum E : int { a = U() }; // expected-error {{no viable conversion}}
     76   };
     77   // This could be a bit-field, and does not conform to the grammar of an
     78   // enum definition, because 'id(U())' is not a constant-expression.
     79   constexpr const U &id(const U &u) { return u; }
     80   struct S8 {
     81     enum E : int { a = id(U()) }; // expected-error {{no viable conversion}}
     82   };
     83 }
     84 
     85 namespace trailing_return {
     86   typedef int n;
     87   int a;
     88 
     89   struct S {
     90     S(int);
     91     S *operator()(...) const;
     92     int n;
     93   };
     94 
     95   namespace N {
     96     void f() {
     97       // This parses as a function declaration, but DR1223 makes the presence of
     98       // 'auto' be used for disambiguation.
     99       S(a)()->n; // ok, expression; expected-warning{{expression result unused}}
    100       S(a)(int())->n; // ok, expression; expected-warning{{expression result unused}}
    101       auto(a)()->n; // ok, function declaration
    102       auto(b)(int())->n; // ok, function declaration
    103       using T = decltype(a);
    104       using T = auto() -> n;
    105     }
    106   }
    107 }
    108 
    109 namespace ellipsis {
    110   template<typename...T>
    111   struct S {
    112     void e(S::S());
    113     void f(S(...args[sizeof(T)])); // expected-note {{here}} expected-note {{here}}
    114     void f(S(...args)[sizeof(T)]); // expected-error {{redeclared}}
    115     void f(S ...args[sizeof(T)]); // expected-error {{redeclared}}
    116     void g(S(...[sizeof(T)])); // expected-note {{here}} expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
    117     void g(S(...)[sizeof(T)]); // expected-error {{function cannot return array type}}
    118     void g(S ...[sizeof(T)]); // expected-error {{redeclared}}
    119     void h(T(...)); // function type, expected-error {{unexpanded parameter pack}}
    120     void h(T...); // pack expansion, ok
    121     void i(int(T...)); // expected-note {{here}}
    122     void i(int(T...a)); // expected-error {{redeclared}}
    123     void i(int(T, ...)); // function type, expected-error {{unexpanded parameter pack}}
    124     void i(int(T, ...a)); // expected-error {{expected ')'}} expected-note {{to match}} expected-error {{unexpanded parameter pack}}
    125     void j(int(int...)); // function type, ok
    126     void j(int(int...a)); // expected-error {{does not contain any unexpanded parameter packs}}
    127     void j(T(int...)); // expected-error {{unexpanded parameter pack}}
    128     void j(T(T...)); // expected-error {{unexpanded parameter pack}}
    129     void k(int(...)(T)); // expected-error {{cannot return function type}}
    130     void k(int ...(T));
    131     void l(int(&...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
    132     void l(int(*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
    133     void l(int(S<int>::*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
    134   };
    135 }
    136 
    137 namespace braced_init_list {
    138   struct X {
    139     void foo() {}
    140   };
    141 
    142   void (*pf1)() {};
    143   void (X::*pmf1)() {&X::foo};
    144   void (X::*pmf2)() = {&X::foo};
    145 
    146   void test() {
    147     void (*pf2)() {};
    148     void (X::*pmf3)() {&X::foo};
    149     void (X::*pmf4)() = {&X::foo};
    150   }
    151 }
    152