summaryrefslogtreecommitdiffstats
path: root/test/Parser/cxx-class.cpp
blob: fe9c1ac95b44272a0a471c5f5287c8048620057f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions %s
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions -std=c++98 %s
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions -std=c++11 %s

class C;
class C {
public:
protected:
  typedef int A,B;
  static int sf(), u;

  struct S {};
  enum {}; // expected-warning{{declaration does not declare anything}}
  int; // expected-warning {{declaration does not declare anything}}
  int : 1, : 2;

public:
  void m0() {}; // ok, one extra ';' is permitted
  void m1() {}
  ; // ok, one extra ';' is permitted
  void m() {
    int l = 2;
  };; // expected-warning{{extra ';' after member function definition}}

  template<typename T> void mt(T) { }
  ;
  ; // expected-warning{{extra ';' inside a class}}

  virtual int vf() const volatile = 0;

  virtual int vf0() = 0l; // expected-error {{does not look like a pure-specifier}}
  virtual int vf1() = 1; // expected-error {{does not look like a pure-specifier}}
  virtual int vf2() = 00; // expected-error {{does not look like a pure-specifier}}
  virtual int vf3() = 0x0; // expected-error {{does not look like a pure-specifier}}
  virtual int vf4() = 0.0; // expected-error {{does not look like a pure-specifier}}
  virtual int vf5(){0}; // expected-error +{{}} expected-warning {{unused}}
  virtual int vf5a(){0;}; // function definition, expected-warning {{unused}}
  virtual int vf6()(0); // expected-error +{{}} expected-note +{{}}
  virtual int vf7() = { 0 }; // expected-error {{does not look like a pure-specifier}}
  
private:
  int x,f(),y,g();
  inline int h();
  static const int sci = 10;
  mutable int mi;
};
void glo()
{
  struct local {};
}

// PR3177
typedef union {
  __extension__ union {
    int a;
    float b;
  } y;
} bug3177;

// check that we don't consume the token after the access specifier 
// when it's not a colon
class D {
public // expected-error{{expected ':'}}
  int i;
};

// consume the token after the access specifier if it's a semicolon 
// that was meant to be a colon
class E {
public; // expected-error{{expected ':'}}
  int i;
};

class F {
    int F1 { return 1; }
#if __cplusplus <= 199711L
    // expected-error@-2 {{function definition does not declare parameters}}
#else
    // expected-error@-4 {{expected expression}}
    // expected-error@-5 {{expected}}
    // expected-note@-6 {{to match this '{'}}
    // expected-error@-7 {{expected ';' after class}}
#endif

    void F2 {}
#if __cplusplus <= 199711L
    // expected-error@-2 {{function definition does not declare parameters}}
#else
    // expected-error@-4 {{variable has incomplete type 'void'}}
    // expected-error@-5 {{expected ';' after top level declarator}}
#endif

    typedef int F3() { return 0; } // expected-error{{function definition declared 'typedef'}}
    typedef void F4() {} // expected-error{{function definition declared 'typedef'}}
};
#if __cplusplus >= 201103L
// expected-error@-2 {{extraneous closing brace}}
#endif

namespace ctor_error {
  class Foo {};
  // By [class.qual]p2, this is a constructor declaration.
  Foo::Foo (F) = F(); // expected-error{{does not match any declaration in 'ctor_error::Foo'}}

  class Ctor { // expected-note{{not complete until the closing '}'}}
    Ctor(f)(int); // ok
    Ctor(g(int)); // ok
    Ctor(x[5]); // expected-error{{incomplete type}}

    Ctor(UnknownType *); // expected-error{{unknown type name 'UnknownType'}}
    void operator+(UnknownType*); // expected-error{{unknown type name 'UnknownType'}}
  };

  Ctor::Ctor (x) = { 0 }; // \
    // expected-error{{qualified reference to 'Ctor' is a constructor name}}

  Ctor::Ctor(UnknownType *) {} // \
    // expected-error{{unknown type name 'UnknownType'}}
  void Ctor::operator+(UnknownType*) {} // \
    // expected-error{{unknown type name 'UnknownType'}}
}

namespace nns_decl {
  struct A {
    struct B;
  };
  namespace N {
    union C;
  }
  struct A::B; // expected-error {{forward declaration of struct cannot have a nested name specifier}}
  union N::C; // expected-error {{forward declaration of union cannot have a nested name specifier}}
}

// PR13775: Don't assert here.
namespace PR13775 {
  class bar
  {
   public:
    void foo ();
    void baz ();
  };
  void bar::foo ()
  {
    baz x(); // expected-error 3{{}}
  }
}

class pr16989 {
  void tpl_mem(int *) {
    return;
    class C2 {
      void f();
    };
    void C2::f() {} // expected-error{{function definition is not allowed here}}
  };
};

namespace CtorErrors {
  struct A {
    A(NonExistent); // expected-error {{unknown type name 'NonExistent'}}
  };
  struct B {
    B(NonExistent) : n(0) {} // expected-error {{unknown type name 'NonExistent'}}
    int n;
  };
  struct C {
    C(NonExistent) try {} catch (...) {} // expected-error {{unknown type name 'NonExistent'}}
  };
  struct D {
    D(NonExistent) {} // expected-error {{unknown type name 'NonExistent'}}
  };
}

namespace DtorErrors {
  struct A { ~A(); int n; } a;
  ~A::A() { n = 0; } // expected-error {{'~' in destructor name should be after nested name specifier}} expected-note {{previous}}
  A::~A() {} // expected-error {{redefinition}}

  struct B { ~B(); } *b;
  DtorErrors::~B::B() {} // expected-error {{'~' in destructor name should be after nested name specifier}}

  void f() {
    a.~A::A(); // expected-error {{'~' in destructor name should be after nested name specifier}}
    b->~DtorErrors::~B::B(); // expected-error {{'~' in destructor name should be after nested name specifier}}
  }

  struct C; // expected-note {{forward decl}}
  ~C::C() {} // expected-error {{incomplete}} expected-error {{'~' in destructor name should be after nested name specifier}}

  struct D { struct X {}; ~D() throw(X); };
  ~D::D() throw(X) {} // expected-error {{'~' in destructor name should be after nested name specifier}}

  ~Undeclared::Undeclared() {} // expected-error {{use of undeclared identifier 'Undeclared'}} expected-error {{'~' in destructor name should be after nested name specifier}}
  ~Undeclared:: {} // expected-error {{expected identifier}} expected-error {{'~' in destructor name should be after nested name specifier}}

  struct S {
    // For another struct's destructor, emit the same diagnostic like for
    // A::~A() in addition to the "~ in the wrong place" one.
    ~A::A() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{non-friend class member '~A' cannot have a qualified name}}
    A::~A() {} // expected-error {{non-friend class member '~A' cannot have a qualified name}}

    // An inline destructor with a redundant class name should also get the
    // same diagnostic as S::~S.
    ~S::S() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{extra qualification on member '~S'}}

    // This just shouldn't crash.
    int I; // expected-note {{declared here}}
    ~I::I() {} // expected-error {{'I' is not a class, namespace, or enumeration}} expected-error {{'~' in destructor name should be after nested name specifier}}
  };

  struct T {};
  T t1 = t1.T::~T<int>; // expected-error {{destructor name 'T' does not refer to a template}} expected-error {{expected '(' for function-style cast or type construction}} expected-error {{expected expression}}
  // Emit the same diagnostic as for the previous case, plus something about ~.
  T t2 = t2.~T::T<int>; // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{destructor name 'T' does not refer to a template}} expected-error {{expected '(' for function-style cast or type construction}} expected-error {{expected expression}}
}

namespace BadFriend {
  struct A {
    friend int : 3; // expected-error {{friends can only be classes or functions}}
    friend void f() = 123; // expected-error {{illegal initializer}}
    friend virtual void f(); // expected-error {{'virtual' is invalid in friend declarations}}
    friend void f() final; // expected-error {{'final' is invalid in friend declarations}}
    friend void f() override; // expected-error {{'override' is invalid in friend declarations}}
  };
}

class PR20760_a {
  int a = ); // expected-error {{expected expression}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

  int b = }; // expected-error {{expected expression}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

  int c = ]; // expected-error {{expected expression}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

};
class PR20760_b {
  int d = d); // expected-error {{expected ';'}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

  int e = d]; // expected-error {{expected ';'}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

  int f = d // expected-error {{expected ';'}}
#if __cplusplus <= 199711L
  // expected-warning@-2 {{in-class initialization of non-static data member is a C++11 extension}}
#endif

};

namespace PR20887 {
class X1 { a::operator=; }; // expected-error {{undeclared identifier 'a'}}
class X2 { a::a; }; // expected-error {{undeclared identifier 'a'}}
}

class BadExceptionSpec {
  void f() throw(int; // expected-error {{expected ')'}} expected-note {{to match}}
  void g() throw(
      int(
          ; // expected-error {{unexpected ';' before ')'}}
          ));
};

namespace PR41192 {
extern struct A a;
struct A {} ::PR41192::a; // ok, no missing ';' here  expected-warning {{extra qualification}}

#if __cplusplus >= 201103L
struct C;
struct D { static C c; };
struct C {} decltype(D())::c; // expected-error {{'decltype' cannot be used to name a declaration}}
#endif
}

// PR11109 must appear at the end of the source file
class pr11109r3 { // expected-note{{to match this '{'}}
  public // expected-error{{expected ':'}} expected-error{{expected '}'}} expected-error{{expected ';' after class}}