xref: /llvm-project/clang/test/Parser/cxx-class.cpp (revision 8282c58d9b1cd5b6df89ee3f68438fe0ee672f7f)
1 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify -pedantic -fcxx-exceptions %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions -std=c++11 %s
4 
5 class C;
6 class C {
7 public:
8 protected:
9   typedef int A,B;
10   static int sf(), u;
11 
12   struct S {};
13   enum {}; // expected-warning{{declaration does not declare anything}}
14   int; // expected-warning {{declaration does not declare anything}}
15   int : 1, : 2;
16 
17 public:
18   void m0() {}; // ok, one extra ';' is permitted
19   void m1() {}
20   ; // ok, one extra ';' is permitted
21   void m() {
22     int l = 2;
23   };; // expected-warning{{extra ';' after member function definition}}
24 
25   template<typename T> void mt(T) { }
26   ;
27   ; // expected-warning{{extra ';' inside a class}}
28 
29   virtual int vf() const volatile = 0;
30 
31   virtual int vf0() = 0l; // expected-error {{does not look like a pure-specifier}}
32   virtual int vf1() = 1; // expected-error {{does not look like a pure-specifier}}
33   virtual int vf2() = 00; // expected-error {{does not look like a pure-specifier}}
34   virtual int vf3() = 0x0; // expected-error {{does not look like a pure-specifier}}
35   virtual int vf4() = 0.0; // expected-error {{does not look like a pure-specifier}}
36   virtual int vf5(){0}; // expected-error +{{}} expected-warning {{unused}}
37   virtual int vf5a(){0;}; // function definition, expected-warning {{unused}}
38   virtual int vf6()(0); // expected-error +{{}} expected-note +{{}}
39   virtual int vf7() = { 0 }; // expected-error {{does not look like a pure-specifier}}
40   virtual int PR45267() = \
41                         0; // ok, despite escaped newline
42 
43 private:
44   int x,f(),y,g();
45   inline int h();
46   static const int sci = 10;
47   mutable int mi;
48 };
49 void glo()
50 {
51   struct local {};
52 }
53 
54 // PR3177
55 typedef union {
56   __extension__ union {
57     int a;
58     float b;
59   } y;
60 } bug3177;
61 
62 // check that we don't consume the token after the access specifier
63 // when it's not a colon
64 class D {
65 public // expected-error{{expected ':'}}
66   int i;
67 };
68 
69 // consume the token after the access specifier if it's a semicolon
70 // that was meant to be a colon
71 class E {
72 public; // expected-error{{expected ':'}}
73   int i;
74 };
75 
76 class F {
77     int F1 { return 1; }
78 #if __cplusplus <= 199711L
79     // expected-error@-2 {{function definition does not declare parameters}}
80 #else
81     // expected-error@-4 {{expected expression}}
82     // expected-error@-5 {{expected}}
83     // expected-note@-6 {{to match this '{'}}
84     // expected-error@-7 {{expected ';' after class}}
85 #endif
86 
87     void F2 {}
88 #if __cplusplus <= 199711L
89     // expected-error@-2 {{function definition does not declare parameters}}
90 #else
91     // expected-error@-4 {{variable has incomplete type 'void'}}
92     // expected-error@-5 {{expected ';' after top level declarator}}
93 #endif
94 
95     typedef int F3() { return 0; } // expected-error{{function definition declared 'typedef'}}
96     typedef void F4() {} // expected-error{{function definition declared 'typedef'}}
97 };
98 #if __cplusplus >= 201103L
99 // expected-error@-2 {{extraneous closing brace}}
100 #endif
101 
102 namespace ctor_error {
103   class Foo {}; // #defined-here
104   // By [class.qual]p2, this is a constructor declaration.
105   Foo::Foo (F) = F(); // expected-error{{does not match any declaration in 'ctor_error::Foo'}}
106                       // expected-note@#defined-here{{defined here}}
107 
108   class Ctor { // expected-note{{not complete until the closing '}'}}
109     Ctor(f)(int); // ok
110     Ctor(g(int)); // ok
111     Ctor(x[5]); // expected-error{{incomplete type}}
112 
113     Ctor(UnknownType *); // expected-error{{unknown type name 'UnknownType'}}
114     void operator+(UnknownType*); // expected-error{{unknown type name 'UnknownType'}}
115   };
116 
117   Ctor::Ctor (x) = { 0 }; // \
118     // expected-error{{qualified reference to 'Ctor' is a constructor name}}
119 
120   Ctor::Ctor(UnknownType *) {} // \
121     // expected-error{{unknown type name 'UnknownType'}}
122   void Ctor::operator+(UnknownType*) {} // \
123     // expected-error{{unknown type name 'UnknownType'}}
124 }
125 
126 namespace nns_decl {
127   struct A {
128     struct B;
129   };
130   namespace N {
131     union C;
132   }
133   struct A::B; // expected-error {{forward declaration of struct cannot have a nested name specifier}}
134   union N::C; // expected-error {{forward declaration of union cannot have a nested name specifier}}
135 }
136 
137 // PR13775: Don't assert here.
138 namespace PR13775 {
139   class bar
140   {
141    public:
142     void foo ();
143     void baz ();
144   };
145   void bar::foo ()
146   {
147     baz x(); // expected-error 3{{}}
148   }
149 }
150 
151 class pr16989 {
152   void tpl_mem(int *) {
153     return;
154     class C2 {
155       void f();
156     };
157     void C2::f() {} // expected-error{{function definition is not allowed here}}
158   };
159 };
160 
161 namespace CtorErrors {
162   struct A {
163     A(NonExistent); // expected-error {{unknown type name 'NonExistent'}}
164   };
165   struct B {
166     B(NonExistent) : n(0) {} // expected-error {{unknown type name 'NonExistent'}}
167     int n;
168   };
169   struct C {
170     C(NonExistent) try {} catch (...) {} // expected-error {{unknown type name 'NonExistent'}}
171   };
172   struct D {
173     D(NonExistent) {} // expected-error {{unknown type name 'NonExistent'}}
174   };
175 }
176 
177 namespace DtorErrors {
178   struct A { ~A(); int n; } a;
179   ~A::A() { n = 0; } // expected-error {{'~' in destructor name should be after nested name specifier}} expected-note {{previous}}
180   A::~A() {} // expected-error {{redefinition}}
181 
182   struct B { ~B(); } *b;
183   DtorErrors::~B::B() {} // expected-error {{'~' in destructor name should be after nested name specifier}}
184 
185   void f() {
186     a.~A::A(); // expected-error {{'~' in destructor name should be after nested name specifier}}
187     b->~DtorErrors::~B::B(); // expected-error {{'~' in destructor name should be after nested name specifier}}
188   }
189 
190   struct C; // expected-note {{forward decl}}
191   ~C::C() {} // expected-error {{incomplete}} expected-error {{'~' in destructor name should be after nested name specifier}}
192 
193   struct D { struct X {}; ~D() throw(X); };
194   ~D::D() throw(X) {} // expected-error {{'~' in destructor name should be after nested name specifier}}
195 
196   ~Undeclared::Undeclared() {} // expected-error {{use of undeclared identifier 'Undeclared'}} expected-error {{'~' in destructor name should be after nested name specifier}}
197   ~Undeclared:: {} // expected-error {{expected identifier}} expected-error {{'~' in destructor name should be after nested name specifier}}
198 
199   struct S {
200     // For another struct's destructor, emit the same diagnostic like for
201     // A::~A() in addition to the "~ in the wrong place" one.
202     ~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}}
203     A::~A() {} // expected-error {{non-friend class member '~A' cannot have a qualified name}}
204 
205     // An inline destructor with a redundant class name should also get the
206     // same diagnostic as S::~S.
207     ~S::S() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{extra qualification on member '~S'}}
208 
209     // This just shouldn't crash.
210     int I; // expected-note {{declared here}}
211     ~I::I() {} // expected-error {{'I' is not a class, namespace, or enumeration}} expected-error {{'~' in destructor name should be after nested name specifier}}
212   };
213 
214   struct T {};
215   T t1 = t1.T::~T<int>; // expected-error {{destructor name 'T' does not refer to a template}}
216   // Emit the same diagnostic as for the previous case, plus something about ~.
217   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}}
218 }
219 
220 namespace BadFriend {
221   struct A {
222     friend int : 3; // expected-error {{friends can only be classes or functions}}
223     friend void f() = 123; // expected-error {{illegal initializer}}
224     friend virtual void f(); // expected-error {{'virtual' is invalid in friend declarations}}
225     friend void f() final; // expected-error {{'final' is invalid in friend declarations}}
226     friend void f() override; // expected-error {{'override' is invalid in friend declarations}}
227   };
228 }
229 
230 class PR20760_a {
231   int a = ); // expected-error {{expected expression}}
232 #if __cplusplus <= 199711L
233   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
234 #endif
235 
236   int b = }; // expected-error {{expected expression}}
237 #if __cplusplus <= 199711L
238   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
239 #endif
240 
241   int c = ]; // expected-error {{expected expression}}
242 #if __cplusplus <= 199711L
243   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
244 #endif
245 
246 };
247 class PR20760_b {
248   int d = d); // expected-error {{expected ';'}}
249 #if __cplusplus <= 199711L
250   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
251 #endif
252 
253   int e = d]; // expected-error {{expected ';'}}
254 #if __cplusplus <= 199711L
255   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
256 #endif
257 
258   int f = d // expected-error {{expected ';'}}
259 #if __cplusplus <= 199711L
260   // expected-warning@-2 {{default member initializer for non-static data member is a C++11 extension}}
261 #endif
262 
263 };
264 
265 namespace PR20887 {
266 class X1 { a::operator=; }; // expected-error {{undeclared identifier 'a'}}
267 class X2 { a::a; }; // expected-error {{undeclared identifier 'a'}}
268 }
269 
270 class BadExceptionSpec {
271   void f() throw(int; // expected-error {{expected ')'}} expected-note {{to match}}
272   void g() throw(
273       int(
274           ; // expected-error {{unexpected ';' before ')'}}
275           ));
276 };
277 
278 namespace PR41192 {
279 extern struct A a;
280 struct A {} ::PR41192::a; // ok, no missing ';' here  expected-warning {{extra qualification}}
281 
282 #if __cplusplus >= 201103L
283 struct C;
284 struct D { static C c; };
285 struct C {} decltype(D())::c; // expected-error {{a 'decltype' specifier cannot be used in a declarative nested name specifier}}
286 #endif
287 }
288 
289 namespace ArrayMemberAccess {
290   struct A {
291     int x;
292     template<typename T> int f() const;
293   };
294   void f(const A (&a)[]) {
295     // OK: not a template-id.
296     bool cond = a->x < 10 && a->x > 0;
297     // OK: a template-id.
298     a->f<int>();
299   }
300 }
301 
302 namespace UndeclaredBaseTemplate {
303   template<class> struct imp;
304   template<class T> struct is_member_function_pointer : undeclared<imp<T>::member> {}; // expected-error {{unknown template name 'undeclared'}}
305 }
306 
307 // PR11109 must appear at the end of the source file
308 class pr11109r3 { // expected-note{{to match this '{'}}
309   public // expected-error{{expected ':'}} expected-error{{expected '}'}} expected-error{{expected ';' after class}}
310