Clang Project

clang_source_code/test/Parser/cxx0x-ambig.cpp
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.
6namespace 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.
34namespace 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
85namespace 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
109namespace ellipsis {
110  template<typename...T>
111  struct S {
112    void e(S::S()); // expected-error {{is a constructor name}}
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  struct CtorSink {
137    template <typename ...T> constexpr CtorSink(T &&...t) { }
138    constexpr operator int() const { return 42; }
139  };
140
141  template <unsigned ...N> struct UnsignedTmplArgSink;
142
143  template <typename ...T>
144  void foo(int x, T ...t) {
145    // Have a variety of cases where the syntax is technically unambiguous, but hinges on careful treatment of ellipses.
146    CtorSink(t ...), x; // ok, expression; expected-warning 2{{expression result unused}}
147
148    int x0(CtorSink(t ...)); // ok, declares object x0
149    int *p0 = &x0;
150    (void)p0;
151
152    CtorSink x1(int(t) ..., int(x)); // ok, declares object x1
153    CtorSink *p1 = &x1;
154    (void)p1;
155
156    UnsignedTmplArgSink<T(CtorSink(t ...)) ...> *t0; // ok
157    UnsignedTmplArgSink<((T *)0, 42u) ...> **t0p = &t0;
158  }
159
160  template void foo(int, int, int); // expected-note {{in instantiation of function template specialization 'ellipsis::foo<int, int>' requested here}}
161}
162
163namespace braced_init_list {
164  struct X {
165    void foo() {}
166  };
167
168  void (*pf1)() {};
169  void (X::*pmf1)() {&X::foo};
170  void (X::*pmf2)() = {&X::foo};
171
172  void test() {
173    void (*pf2)() {};
174    void (X::*pmf3)() {&X::foo};
175    void (X::*pmf4)() = {&X::foo};
176  }
177}
178