xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx-class.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions %s
2f4a2713aSLionel Sambuc class C;
3f4a2713aSLionel Sambuc class C {
4f4a2713aSLionel Sambuc public:
5f4a2713aSLionel Sambuc protected:
6f4a2713aSLionel Sambuc   typedef int A,B;
7f4a2713aSLionel Sambuc   static int sf(), u;
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc   struct S {};
10f4a2713aSLionel Sambuc   enum {}; // expected-warning{{declaration does not declare anything}}
11f4a2713aSLionel Sambuc   int; // expected-warning {{declaration does not declare anything}}
12f4a2713aSLionel Sambuc   int : 1, : 2;
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc public:
m0()15f4a2713aSLionel Sambuc   void m0() {}; // ok, one extra ';' is permitted
m1()16f4a2713aSLionel Sambuc   void m1() {}
17f4a2713aSLionel Sambuc   ; // ok, one extra ';' is permitted
m()18f4a2713aSLionel Sambuc   void m() {
19f4a2713aSLionel Sambuc     int l = 2;
20f4a2713aSLionel Sambuc   };; // expected-warning{{extra ';' after member function definition}}
21f4a2713aSLionel Sambuc 
mt(T)22f4a2713aSLionel Sambuc   template<typename T> void mt(T) { }
23f4a2713aSLionel Sambuc   ;
24f4a2713aSLionel Sambuc   ; // expected-warning{{extra ';' inside a class}}
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc   virtual int vf() const volatile = 0;
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc private:
29f4a2713aSLionel Sambuc   int x,f(),y,g();
30f4a2713aSLionel Sambuc   inline int h();
31f4a2713aSLionel Sambuc   static const int sci = 10;
32f4a2713aSLionel Sambuc   mutable int mi;
33f4a2713aSLionel Sambuc };
glo()34f4a2713aSLionel Sambuc void glo()
35f4a2713aSLionel Sambuc {
36f4a2713aSLionel Sambuc   struct local {};
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc // PR3177
40f4a2713aSLionel Sambuc typedef union {
41f4a2713aSLionel Sambuc   __extension__ union {
42f4a2713aSLionel Sambuc     int a;
43f4a2713aSLionel Sambuc     float b;
44f4a2713aSLionel Sambuc   } y;
45f4a2713aSLionel Sambuc } bug3177;
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc // check that we don't consume the token after the access specifier
48f4a2713aSLionel Sambuc // when it's not a colon
49f4a2713aSLionel Sambuc class D {
50f4a2713aSLionel Sambuc public // expected-error{{expected ':'}}
51f4a2713aSLionel Sambuc   int i;
52f4a2713aSLionel Sambuc };
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc // consume the token after the access specifier if it's a semicolon
55f4a2713aSLionel Sambuc // that was meant to be a colon
56f4a2713aSLionel Sambuc class E {
57f4a2713aSLionel Sambuc public; // expected-error{{expected ':'}}
58f4a2713aSLionel Sambuc   int i;
59f4a2713aSLionel Sambuc };
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc class F {
62f4a2713aSLionel Sambuc     int F1 { return 1; } // expected-error{{function definition does not declare parameters}}
63f4a2713aSLionel Sambuc     void F2 {} // expected-error{{function definition does not declare parameters}}
64f4a2713aSLionel Sambuc     typedef int F3() { return 0; } // expected-error{{function definition declared 'typedef'}}
F4()65f4a2713aSLionel Sambuc     typedef void F4() {} // expected-error{{function definition declared 'typedef'}}
66f4a2713aSLionel Sambuc };
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc namespace ctor_error {
69f4a2713aSLionel Sambuc   class Foo {};
70f4a2713aSLionel Sambuc   // By [class.qual]p2, this is a constructor declaration.
71f4a2713aSLionel Sambuc   Foo::Foo (F) = F(); // expected-error{{does not match any declaration in 'ctor_error::Foo'}}
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc   class Ctor { // expected-note{{not complete until the closing '}'}}
74f4a2713aSLionel Sambuc     Ctor(f)(int); // ok
75f4a2713aSLionel Sambuc     Ctor(g(int)); // ok
76f4a2713aSLionel Sambuc     Ctor(x[5]); // expected-error{{incomplete type}}
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc     Ctor(UnknownType *); // expected-error{{unknown type name 'UnknownType'}}
79f4a2713aSLionel Sambuc     void operator+(UnknownType*); // expected-error{{unknown type name 'UnknownType'}}
80f4a2713aSLionel Sambuc   };
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc   Ctor::Ctor (x) = { 0 }; // \
83f4a2713aSLionel Sambuc     // expected-error{{qualified reference to 'Ctor' is a constructor name}}
84f4a2713aSLionel Sambuc 
Ctor(UnknownType *)85f4a2713aSLionel Sambuc   Ctor::Ctor(UnknownType *) {} // \
86f4a2713aSLionel Sambuc     // expected-error{{unknown type name 'UnknownType'}}
operator +(UnknownType *)87f4a2713aSLionel Sambuc   void Ctor::operator+(UnknownType*) {} // \
88f4a2713aSLionel Sambuc     // expected-error{{unknown type name 'UnknownType'}}
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc namespace nns_decl {
92f4a2713aSLionel Sambuc   struct A {
93f4a2713aSLionel Sambuc     struct B;
94f4a2713aSLionel Sambuc   };
95f4a2713aSLionel Sambuc   namespace N {
96f4a2713aSLionel Sambuc     union C;
97f4a2713aSLionel Sambuc   }
98f4a2713aSLionel Sambuc   struct A::B; // expected-error {{forward declaration of struct cannot have a nested name specifier}}
99f4a2713aSLionel Sambuc   union N::C; // expected-error {{forward declaration of union cannot have a nested name specifier}}
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc // PR13775: Don't assert here.
103f4a2713aSLionel Sambuc namespace PR13775 {
104f4a2713aSLionel Sambuc   class bar
105f4a2713aSLionel Sambuc   {
106f4a2713aSLionel Sambuc    public:
107f4a2713aSLionel Sambuc     void foo ();
108f4a2713aSLionel Sambuc     void baz ();
109f4a2713aSLionel Sambuc   };
foo()110f4a2713aSLionel Sambuc   void bar::foo ()
111f4a2713aSLionel Sambuc   {
112f4a2713aSLionel Sambuc     baz x(); // expected-error 3{{}}
113f4a2713aSLionel Sambuc   }
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc class pr16989 {
tpl_mem(int *)117*0a6a1f1dSLionel Sambuc   void tpl_mem(int *) {
118*0a6a1f1dSLionel Sambuc     return;
119*0a6a1f1dSLionel Sambuc     class C2 {
120*0a6a1f1dSLionel Sambuc       void f();
121*0a6a1f1dSLionel Sambuc     };
122*0a6a1f1dSLionel Sambuc     void C2::f() {} // expected-error{{function definition is not allowed here}}
123*0a6a1f1dSLionel Sambuc   };
124*0a6a1f1dSLionel Sambuc };
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc namespace CtorErrors {
127*0a6a1f1dSLionel Sambuc   struct A {
128*0a6a1f1dSLionel Sambuc     A(NonExistent); // expected-error {{unknown type name 'NonExistent'}}
129*0a6a1f1dSLionel Sambuc   };
130*0a6a1f1dSLionel Sambuc   struct B {
BCtorErrors::B131*0a6a1f1dSLionel Sambuc     B(NonExistent) : n(0) {} // expected-error {{unknown type name 'NonExistent'}}
132*0a6a1f1dSLionel Sambuc     int n;
133*0a6a1f1dSLionel Sambuc   };
134*0a6a1f1dSLionel Sambuc   struct C {
CCtorErrors::C135*0a6a1f1dSLionel Sambuc     C(NonExistent) try {} catch (...) {} // expected-error {{unknown type name 'NonExistent'}}
136*0a6a1f1dSLionel Sambuc   };
137*0a6a1f1dSLionel Sambuc   struct D {
DCtorErrors::D138*0a6a1f1dSLionel Sambuc     D(NonExistent) {} // expected-error {{unknown type name 'NonExistent'}}
139*0a6a1f1dSLionel Sambuc   };
140*0a6a1f1dSLionel Sambuc }
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc namespace DtorErrors {
143*0a6a1f1dSLionel Sambuc   struct A { ~A(); } a;
A()144*0a6a1f1dSLionel Sambuc   ~A::A() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-note {{previous}}
~A()145*0a6a1f1dSLionel Sambuc   A::~A() {} // expected-error {{redefinition}}
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc   struct B { ~B(); } *b;
B()148*0a6a1f1dSLionel Sambuc   DtorErrors::~B::B() {} // expected-error {{'~' in destructor name should be after nested name specifier}}
149*0a6a1f1dSLionel Sambuc 
f()150*0a6a1f1dSLionel Sambuc   void f() {
151*0a6a1f1dSLionel Sambuc     a.~A::A(); // expected-error {{'~' in destructor name should be after nested name specifier}}
152*0a6a1f1dSLionel Sambuc     b->~DtorErrors::~B::B(); // expected-error {{'~' in destructor name should be after nested name specifier}}
153*0a6a1f1dSLionel Sambuc   }
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc namespace BadFriend {
157*0a6a1f1dSLionel Sambuc   struct A {
158*0a6a1f1dSLionel Sambuc     friend int : 3; // expected-error {{friends can only be classes or functions}}
159*0a6a1f1dSLionel Sambuc     friend void f() = 123; // expected-error {{illegal initializer}}
160*0a6a1f1dSLionel Sambuc     friend virtual void f(); // expected-error {{'virtual' is invalid in friend declarations}}
161*0a6a1f1dSLionel Sambuc     friend void f() final; // expected-error {{'final' is invalid in friend declarations}}
162*0a6a1f1dSLionel Sambuc     friend void f() override; // expected-error {{'override' is invalid in friend declarations}}
163*0a6a1f1dSLionel Sambuc   };
164*0a6a1f1dSLionel Sambuc }
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc class PR20760_a {
167*0a6a1f1dSLionel Sambuc   int a = ); // expected-warning {{extension}} expected-error {{expected expression}}
168*0a6a1f1dSLionel Sambuc   int b = }; // expected-warning {{extension}} expected-error {{expected expression}}
169*0a6a1f1dSLionel Sambuc   int c = ]; // expected-warning {{extension}} expected-error {{expected expression}}
170*0a6a1f1dSLionel Sambuc };
171*0a6a1f1dSLionel Sambuc class PR20760_b {
172*0a6a1f1dSLionel Sambuc   int d = d); // expected-warning {{extension}} expected-error {{expected ';'}}
173*0a6a1f1dSLionel Sambuc   int e = d]; // expected-warning {{extension}} expected-error {{expected ';'}}
174*0a6a1f1dSLionel Sambuc   int f = d // expected-warning {{extension}} expected-error {{expected ';'}}
175*0a6a1f1dSLionel Sambuc };
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc namespace PR20887 {
178*0a6a1f1dSLionel Sambuc class X1 { a::operator=; }; // expected-error {{undeclared identifier 'a'}}
179*0a6a1f1dSLionel Sambuc class X2 { a::a; }; // expected-error {{undeclared identifier 'a'}}
180*0a6a1f1dSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc class BadExceptionSpec {
183*0a6a1f1dSLionel Sambuc   void f() throw(int; // expected-error {{expected ')'}} expected-note {{to match}}
184*0a6a1f1dSLionel Sambuc   void g() throw( // expected-note {{to match}}
185*0a6a1f1dSLionel Sambuc       int( // expected-note {{to match}}
186*0a6a1f1dSLionel Sambuc           ; // expected-error 2{{expected ')'}} expected-error {{unexpected end of exception specification}}
187*0a6a1f1dSLionel Sambuc           ));
188*0a6a1f1dSLionel Sambuc };
189*0a6a1f1dSLionel Sambuc 
190f4a2713aSLionel Sambuc // PR11109 must appear at the end of the source file
191f4a2713aSLionel Sambuc class pr11109r3 { // expected-note{{to match this '{'}}
192f4a2713aSLionel Sambuc   public // expected-error{{expected ':'}} expected-error{{expected '}'}} expected-error{{expected ';' after class}}
193