xref: /aosp_15_r20/external/clang/test/Parser/cxx-decl.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -pedantic-errors -fcxx-exceptions -fexceptions %s
2*67e74705SXin Li // RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -pedantic-errors -fcxx-exceptions -fexceptions -std=c++98 %s
3*67e74705SXin Li // RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -pedantic-errors -fcxx-exceptions -fexceptions -std=c++11 %s
4*67e74705SXin Li 
5*67e74705SXin Li const char const *x10; // expected-error {{duplicate 'const' declaration specifier}}
6*67e74705SXin Li 
7*67e74705SXin Li int x(*g); // expected-error {{use of undeclared identifier 'g'}}
8*67e74705SXin Li 
9*67e74705SXin Li struct Type {
10*67e74705SXin Li   int Type;
11*67e74705SXin Li };
12*67e74705SXin Li 
13*67e74705SXin Li // rdar://8365458
14*67e74705SXin Li // rdar://9132143
15*67e74705SXin Li typedef char bool; // expected-error {{redeclaration of C++ built-in type 'bool'}}
16*67e74705SXin Li 
17*67e74705SXin Li // PR4451 - We should recover well from the typo of '::' as ':' in a2.
18*67e74705SXin Li namespace y {
19*67e74705SXin Li   struct a { };
20*67e74705SXin Li   typedef int b;
21*67e74705SXin Li }
22*67e74705SXin Li 
23*67e74705SXin Li y::a a1;
24*67e74705SXin Li y:a a2;  // expected-error {{unexpected ':' in nested name specifier}}
25*67e74705SXin Li y::a a3 = a2;
26*67e74705SXin Li 
27*67e74705SXin Li // Some valid colons:
foo()28*67e74705SXin Li void foo() {
29*67e74705SXin Li y:  // label
30*67e74705SXin Li   y::a s;
31*67e74705SXin Li 
32*67e74705SXin Li   int a = 4;
33*67e74705SXin Li   a = a ? a : a+1;
34*67e74705SXin Li }
35*67e74705SXin Li 
36*67e74705SXin Li struct b : y::a {};
37*67e74705SXin Li 
38*67e74705SXin Li template <typename T>
39*67e74705SXin Li class someclass {
40*67e74705SXin Li 
bar()41*67e74705SXin Li   int bar() {
42*67e74705SXin Li     T *P;
43*67e74705SXin Li     return 1 ? P->x : P->y;
44*67e74705SXin Li   }
45*67e74705SXin Li };
46*67e74705SXin Li 
47*67e74705SXin Li class asm_class_test {
48*67e74705SXin Li   void foo() __asm__("baz");
49*67e74705SXin Li };
50*67e74705SXin Li 
51*67e74705SXin Li enum { fooenum = 1, };
52*67e74705SXin Li #if __cplusplus <= 199711L
53*67e74705SXin Li // expected-error@-2 {{commas at the end of enumerator lists are a C++11 extension}}
54*67e74705SXin Li #endif
55*67e74705SXin Li 
56*67e74705SXin Li struct a {
57*67e74705SXin Li   int Type : fooenum;
58*67e74705SXin Li };
59*67e74705SXin Li 
test(struct Type * P)60*67e74705SXin Li void test(struct Type *P) {
61*67e74705SXin Li   int Type;
62*67e74705SXin Li   Type = 1 ? P->Type : Type;
63*67e74705SXin Li 
64*67e74705SXin Li   Type = (y:b) 4;   // expected-error {{unexpected ':' in nested name specifier}}
65*67e74705SXin Li   Type = 1 ? (
66*67e74705SXin Li               (y:b)  // expected-error {{unexpected ':' in nested name specifier}}
67*67e74705SXin Li               4) : 5;
68*67e74705SXin Li }
69*67e74705SXin Li 
70*67e74705SXin Li struct test4 {
71*67e74705SXin Li   int x  // expected-error {{expected ';' at end of declaration list}}
72*67e74705SXin Li   int y;
73*67e74705SXin Li   int z  // expected-error {{expected ';' at end of declaration list}}
74*67e74705SXin Li };
75*67e74705SXin Li 
76*67e74705SXin Li // Make sure we know these are legitimate commas and not typos for ';'.
77*67e74705SXin Li namespace Commas {
78*67e74705SXin Li   struct S {
79*67e74705SXin Li     static int a;
80*67e74705SXin Li     int c,
81*67e74705SXin Li     operator()();
82*67e74705SXin Li   };
83*67e74705SXin Li 
84*67e74705SXin Li   int global1,
85*67e74705SXin Li   __attribute__(()) global2,
86*67e74705SXin Li   (global5),
87*67e74705SXin Li   *global6,
88*67e74705SXin Li   &global7 = global1,
89*67e74705SXin Li   &&global8 = static_cast<int&&>(global1),
90*67e74705SXin Li #if __cplusplus <= 199711L
91*67e74705SXin Li   // expected-error@-2 2{{rvalue references are a C++11 extension}}
92*67e74705SXin Li #endif
93*67e74705SXin Li 
94*67e74705SXin Li   S::a,
95*67e74705SXin Li   global9,
96*67e74705SXin Li   global10 = 0,
97*67e74705SXin Li   global11 == 0, // expected-error {{did you mean '='}}
98*67e74705SXin Li   global12 __attribute__(()),
99*67e74705SXin Li   global13(0),
100*67e74705SXin Li   global14[2],
101*67e74705SXin Li   global15;
102*67e74705SXin Li 
g()103*67e74705SXin Li   void g() {
104*67e74705SXin Li     static int a,
105*67e74705SXin Li     b __asm__("ebx"), // expected-error {{expected ';' at end of declaration}}
106*67e74705SXin Li     Statics:return;
107*67e74705SXin Li   }
108*67e74705SXin Li }
109*67e74705SXin Li 
110*67e74705SXin Li // PR5825
111*67e74705SXin Li struct test5 {};
112*67e74705SXin Li ::new(static_cast<void*>(0)) test5; // expected-error {{expected unqualified-id}}
113*67e74705SXin Li 
114*67e74705SXin Li 
115*67e74705SXin Li // PR6782
116*67e74705SXin Li template<class T>
117*67e74705SXin Li class Class1;
118*67e74705SXin Li 
119*67e74705SXin Li class Class2 {
120*67e74705SXin Li } // expected-error {{expected ';' after class}}
121*67e74705SXin Li 
122*67e74705SXin Li typedef Class1<Class2> Type1;
123*67e74705SXin Li 
124*67e74705SXin Li // rdar : // 8307865
125*67e74705SXin Li struct CodeCompleteConsumer {
126*67e74705SXin Li };
127*67e74705SXin Li 
128*67e74705SXin Li void CodeCompleteConsumer::() { // expected-error {{xpected unqualified-id}}
129*67e74705SXin Li }
130*67e74705SXin Li 
131*67e74705SXin Li ;
132*67e74705SXin Li 
133*67e74705SXin Li // PR4111
134*67e74705SXin Li void f(sqrgl); // expected-error {{unknown type name 'sqrgl'}}
135*67e74705SXin Li 
136*67e74705SXin Li // PR9903
137*67e74705SXin Li struct S {
138*67e74705SXin Li   typedef void a() { }; // expected-error {{function definition declared 'typedef'}}
139*67e74705SXin Li   typedef void c() try { } catch(...) { } // expected-error {{function definition declared 'typedef'}}
140*67e74705SXin Li   int n, m;
141*67e74705SXin Li   typedef S() : n(1), m(2) { } // expected-error {{function definition declared 'typedef'}}
142*67e74705SXin Li };
143*67e74705SXin Li 
144*67e74705SXin Li 
145*67e74705SXin Li namespace TestIsValidAfterTypeSpecifier {
146*67e74705SXin Li struct s {} v;
147*67e74705SXin Li 
148*67e74705SXin Li namespace a {
149*67e74705SXin Li struct s operator++(struct s a)
150*67e74705SXin Li { return a; }
151*67e74705SXin Li }
152*67e74705SXin Li 
153*67e74705SXin Li namespace b {
154*67e74705SXin Li // The newline after s should make no difference.
155*67e74705SXin Li struct s
156*67e74705SXin Li operator++(struct s a)
157*67e74705SXin Li { return a; }
158*67e74705SXin Li }
159*67e74705SXin Li 
160*67e74705SXin Li struct X {
161*67e74705SXin Li   struct s
162*67e74705SXin Li   friend f();
163*67e74705SXin Li   struct s
164*67e74705SXin Li   virtual f();
165*67e74705SXin Li };
166*67e74705SXin Li 
167*67e74705SXin Li struct s
168*67e74705SXin Li &r0 = v;
169*67e74705SXin Li struct s
170*67e74705SXin Li bitand r2 = v;
171*67e74705SXin Li 
172*67e74705SXin Li }
173*67e74705SXin Li 
174*67e74705SXin Li struct DIE {
175*67e74705SXin Li   void foo() {}
176*67e74705SXin Li };
177*67e74705SXin Li 
178*67e74705SXin Li void test (DIE die, DIE *Die, DIE INT, DIE *FLOAT) {
179*67e74705SXin Li   DIE.foo();  // expected-error {{cannot use dot operator on a type}}
180*67e74705SXin Li   die.foo();
181*67e74705SXin Li 
182*67e74705SXin Li   DIE->foo();  // expected-error {{cannot use arrow operator on a type}}
183*67e74705SXin Li   Die->foo();
184*67e74705SXin Li 
185*67e74705SXin Li   int.foo();  // expected-error {{cannot use dot operator on a type}}
186*67e74705SXin Li   INT.foo();
187*67e74705SXin Li 
188*67e74705SXin Li   float->foo();  // expected-error {{cannot use arrow operator on a type}}
189*67e74705SXin Li   FLOAT->foo();
190*67e74705SXin Li }
191*67e74705SXin Li 
192*67e74705SXin Li namespace PR15017 {
193*67e74705SXin Li   template<typename T = struct X { int i; }> struct S {}; // expected-error {{'PR15017::X' cannot be defined in a type specifier}}
194*67e74705SXin Li }
195*67e74705SXin Li 
196*67e74705SXin Li // Ensure we produce at least some diagnostic for attributes in C++98.
197*67e74705SXin Li [[]] struct S;
198*67e74705SXin Li #if __cplusplus <= 199711L
199*67e74705SXin Li // expected-error@-2 {{expected expression}}
200*67e74705SXin Li // expected-error@-3 {{expected unqualified-id}}
201*67e74705SXin Li #else
202*67e74705SXin Li // expected-error@-5 {{an attribute list cannot appear here}}
203*67e74705SXin Li #endif
204*67e74705SXin Li 
205*67e74705SXin Li namespace test7 {
206*67e74705SXin Li   struct Foo {
207*67e74705SXin Li     void a();
208*67e74705SXin Li     void b();
209*67e74705SXin Li   };
210*67e74705SXin Li 
211*67e74705SXin Li   void Foo::
212*67e74705SXin Li   // Comment!
213*67e74705SXin Li   a() {}
214*67e74705SXin Li 
215*67e74705SXin Li 
216*67e74705SXin Li   void Foo::  // expected-error {{expected unqualified-id}}
217*67e74705SXin Li   // Comment!
218*67e74705SXin Li }
219*67e74705SXin Li 
220*67e74705SXin Li void test8() {
221*67e74705SXin Li   struct {} o;
222*67e74705SXin Li   // This used to crash.
223*67e74705SXin Li   (&o)->(); // expected-error{{expected unqualified-id}}
224*67e74705SXin Li }
225*67e74705SXin Li 
226*67e74705SXin Li namespace PR5066 {
227*67e74705SXin Li   template<typename T> struct X {};
228*67e74705SXin Li   X<int N> x; // expected-error {{type-id cannot have a name}}
229*67e74705SXin Li 
230*67e74705SXin Li   using T = int (*T)(); // expected-error {{type-id cannot have a name}}
231*67e74705SXin Li #if __cplusplus <= 199711L
232*67e74705SXin Li   // expected-error@-2 {{alias declarations are a C++11 extensio}}
233*67e74705SXin Li #endif
234*67e74705SXin Li 
235*67e74705SXin Li }
236*67e74705SXin Li 
237*67e74705SXin Li namespace PR17255 {
238*67e74705SXin Li void foo() {
239*67e74705SXin Li   typename A::template B<>; // expected-error {{use of undeclared identifier 'A'}}
240*67e74705SXin Li #if __cplusplus <= 199711L
241*67e74705SXin Li   // expected-error@-2 {{'template' keyword outside of a template}}
242*67e74705SXin Li #endif
243*67e74705SXin Li   // expected-error@-4 {{expected a qualified name after 'typename'}}
244*67e74705SXin Li }
245*67e74705SXin Li }
246*67e74705SXin Li 
247*67e74705SXin Li namespace PR17567 {
248*67e74705SXin Li   struct Foobar { // expected-note 2{{declared here}}
249*67e74705SXin Li     FooBar(); // expected-error {{missing return type for function 'FooBar'; did you mean the constructor name 'Foobar'?}}
250*67e74705SXin Li     ~FooBar(); // expected-error {{expected the class name after '~' to name a destructor}}
251*67e74705SXin Li   };
252*67e74705SXin Li   FooBar::FooBar() {} // expected-error {{undeclared}} expected-error {{missing return type}}
253*67e74705SXin Li   FooBar::~FooBar() {} // expected-error {{undeclared}} expected-error {{expected the class name}}
254*67e74705SXin Li }
255*67e74705SXin Li 
256*67e74705SXin Li namespace DuplicateFriend {
257*67e74705SXin Li   struct A {
258*67e74705SXin Li     friend void friend f(); // expected-warning {{duplicate 'friend' declaration specifier}}
259*67e74705SXin Li     friend struct B friend; // expected-warning {{duplicate 'friend' declaration specifier}}
260*67e74705SXin Li #if __cplusplus >= 201103L
261*67e74705SXin Li     // expected-error@-2 {{'friend' must appear first in a non-function declaration}}
262*67e74705SXin Li #endif
263*67e74705SXin Li   };
264*67e74705SXin Li }
265*67e74705SXin Li 
266*67e74705SXin Li // PR8380
267*67e74705SXin Li extern ""      // expected-error {{unknown linkage language}}
268*67e74705SXin Li test6a { ;// expected-error {{C++ requires a type specifier for all declarations}}
269*67e74705SXin Li #if __cplusplus <= 199711L
270*67e74705SXin Li // expected-error@-2 {{expected ';' after top level declarator}}
271*67e74705SXin Li #else
272*67e74705SXin Li // expected-error@-4 {{expected expression}}
273*67e74705SXin Li // expected-note@-5 {{to match this}}
274*67e74705SXin Li #endif
275*67e74705SXin Li 
276*67e74705SXin Li   int test6b;
277*67e74705SXin Li #if __cplusplus >= 201103L
278*67e74705SXin Li // expected-error@+3 {{expected}}
279*67e74705SXin Li // expected-error@-3 {{expected ';' after top level declarator}}
280*67e74705SXin Li #endif
281*67e74705SXin Li 
282