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