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