blob: 8587e70158b06dcd27a71957f46c134d3d826a81 [file] [log] [blame]
Eli Friedmana03c5ee2013-08-12 21:54:01 +00001// RUN: %clang_cc1 -fsyntax-only -std=c++98 -verify -fblocks %s
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00002namespace A {
3 struct C {
4 static int cx;
Douglas Gregor656de632009-03-11 23:52:16 +00005
6 static int cx2;
7
8 static int Ag1();
9 static int Ag2();
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000010 };
11 int ax;
12 void Af();
13}
14
15A:: ; // expected-error {{expected unqualified-id}}
Jeffrey Yasskinedc28772010-04-07 23:29:58 +000016// FIXME: there is a member 'ax'; it's just not a class.
17::A::ax::undef ex3; // expected-error {{no member named 'ax'}}
18A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000019
Douglas Gregor656de632009-03-11 23:52:16 +000020int A::C::Ag1() { return 0; }
21
Douglas Gregora3a83512009-04-01 23:51:29 +000022static int A::C::Ag2() { return 0; } // expected-error{{'static' can}}
Douglas Gregor656de632009-03-11 23:52:16 +000023
24int A::C::cx = 17;
25
26
Douglas Gregora3a83512009-04-01 23:51:29 +000027static int A::C::cx2 = 17; // expected-error{{'static' can}}
Douglas Gregor656de632009-03-11 23:52:16 +000028
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000029class C2 {
Kaelyn Uhrain10553932011-10-10 18:01:37 +000030 void m(); // expected-note{{member declaration does not match because it is not const qualified}}
Douglas Gregor584049d2008-12-15 23:53:10 +000031
Kaelyn Uhrain4d9d1572011-08-04 17:40:00 +000032 void f(const int& parm); // expected-note{{type of 1st parameter of member declaration does not match definition ('const int &' vs 'int')}}
Kaelyn Uhrain10553932011-10-10 18:01:37 +000033 void f(int) const; // expected-note{{member declaration does not match because it is const qualified}}
Douglas Gregor584049d2008-12-15 23:53:10 +000034 void f(float);
35
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000036 int x;
37};
38
John McCall7c2342d2010-03-10 11:27:22 +000039void C2::m() const { } // expected-error{{out-of-line definition of 'm' does not match any declaration in 'C2'}}
Douglas Gregor584049d2008-12-15 23:53:10 +000040
John McCall7c2342d2010-03-10 11:27:22 +000041void C2::f(int) { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'C2'}}
Douglas Gregor584049d2008-12-15 23:53:10 +000042
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000043void C2::m() {
44 x = 0;
45}
46
47namespace B {
Richard Smitha1c4f7c2012-04-13 04:07:40 +000048 void ::A::Af() {} // expected-error {{cannot define or redeclare 'Af' here because namespace 'B' does not enclose namespace 'A'}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000049}
50
51void f1() {
Douglas Gregor584049d2008-12-15 23:53:10 +000052 void A::Af(); // expected-error {{definition or redeclaration of 'Af' not allowed inside a function}}
Eli Friedmana03c5ee2013-08-12 21:54:01 +000053 void (^x)() = ^{ void A::Af(); }; // expected-error {{definition or redeclaration of 'Af' not allowed inside a block}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000054}
55
56void f2() {
57 A:: ; // expected-error {{expected unqualified-id}}
58 A::C::undef = 0; // expected-error {{no member named 'undef'}}
59 ::A::C::cx = 0;
60 int x = ::A::ax = A::C::cx;
61 x = sizeof(A::C);
62 x = sizeof(::A::C::cx);
63}
64
65A::C c1;
66struct A::C c2;
67struct S : public A::C {};
Douglas Gregor1eabb7d2010-03-31 23:17:41 +000068struct A::undef; // expected-error {{no struct named 'undef' in namespace 'A'}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000069
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000070namespace A2 {
71 typedef int INT;
72 struct RC;
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +000073 struct CC {
74 struct NC;
75 };
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000076}
77
78struct A2::RC {
79 INT x;
80};
81
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +000082struct A2::CC::NC {
83 void m() {}
84};
85
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000086void f3() {
87 N::x = 0; // expected-error {{use of undeclared identifier 'N'}}
Stephen Hines651f13c2014-04-23 16:59:28 -070088 // FIXME: Consider including the kind of entity that 'N' is ("variable 'N'
89 // declared here", "template 'X' declared here", etc) to help explain what it
90 // is if it's 'not a class, namespace, or scoped enumeration'.
91 int N; // expected-note {{'N' declared here}}
92 N::x = 0; // expected-error {{'N' is not a class, namespace, or scoped enumeration}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000093 { int A; A::ax = 0; }
Stephen Hines651f13c2014-04-23 16:59:28 -070094 { typedef int A; A::ax = 0; } // expected-error{{'A' (aka 'int') is not a class, namespace, or scoped enumeration}}
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000095 { typedef A::C A; A::ax = 0; } // expected-error {{no member named 'ax'}}
96 { typedef A::C A; A::cx = 0; }
97}
Argyrios Kyrtzidis08b2c372008-11-19 15:22:16 +000098
99// make sure the following doesn't hit any asserts
Richard Smith05766812012-08-18 00:55:03 +0000100void f4(undef::C); // expected-error {{use of undeclared identifier 'undef'}}
Douglas Gregor584049d2008-12-15 23:53:10 +0000101
102typedef void C2::f5(int); // expected-error{{typedef declarator cannot be qualified}}
103
104void f6(int A2::RC::x); // expected-error{{parameter declarator cannot be qualified}}
105
106int A2::RC::x; // expected-error{{non-static data member defined out-of-line}}
107
Chandler Carruth34fa2942010-07-16 05:46:45 +0000108void A2::CC::NC::m(); // expected-error{{out-of-line declaration of a member must be a definition}}
Douglas Gregor3dde5a32008-12-16 06:37:47 +0000109
110
111namespace E {
112 int X = 5;
113
114 namespace Nested {
115 enum E {
116 X = 0
117 };
118
Kaelyn Uhrainef094a12012-06-07 23:57:08 +0000119 void f() {
Stephen Hines651f13c2014-04-23 16:59:28 -0700120 return E::X; // expected-error{{'E::Nested::E' is not a class, namespace, or scoped enumeration}}
Douglas Gregor3dde5a32008-12-16 06:37:47 +0000121 }
122 }
123}
Douglas Gregor70316a02008-12-26 15:00:45 +0000124
125
126class Operators {
Kaelyn Uhrain10553932011-10-10 18:01:37 +0000127 Operators operator+(const Operators&) const; // expected-note{{member declaration does not match because it is const qualified}}
Douglas Gregor70316a02008-12-26 15:00:45 +0000128 operator bool();
129};
130
John McCall7c2342d2010-03-10 11:27:22 +0000131Operators Operators::operator+(const Operators&) { // expected-error{{out-of-line definition of 'operator+' does not match any declaration in 'Operators'}}
Douglas Gregor70316a02008-12-26 15:00:45 +0000132 Operators ops;
133 return ops;
134}
135
136Operators Operators::operator+(const Operators&) const {
137 Operators ops;
138 return ops;
139}
140
141Operators::operator bool() {
142 return true;
143}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000144
145namespace A {
Kaelyn Uhrain4d9d1572011-08-04 17:40:00 +0000146 void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000147}
148
Stephen Hines651f13c2014-04-23 16:59:28 -0700149void A::f() {} // expected-error-re{{out-of-line definition of 'f' does not match any declaration in namespace 'A'{{$}}}}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000150
Douglas Gregor3f093272009-10-13 21:16:44 +0000151void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000152
153struct Struct { };
154
John McCall7c2342d2010-03-10 11:27:22 +0000155void Struct::f() { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'Struct'}}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000156
157void global_func(int);
158void global_func2(int);
159
160namespace N {
161 void ::global_func(int) { } // expected-error{{definition or redeclaration of 'global_func' cannot name the global scope}}
162
163 void f();
164 // FIXME: if we move this to a separate definition of N, things break!
165}
Stephen Hines651f13c2014-04-23 16:59:28 -0700166void ::global_func2(int) { } // expected-warning{{extra qualification on member 'global_func2'}}
Douglas Gregor4ce205f2009-02-06 17:46:57 +0000167
168void N::f() { } // okay
Douglas Gregor9fa14a52009-03-06 19:06:37 +0000169
John McCall7c2342d2010-03-10 11:27:22 +0000170struct Y; // expected-note{{forward declaration of 'Y'}}
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000171Y::foo y; // expected-error{{incomplete type 'Y' named in nested name specifier}}
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000172
Richard Smithb79b17b2013-10-15 00:00:26 +0000173X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}}
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000174
Argyrios Kyrtzidisf37006b2009-07-21 06:43:26 +0000175struct foo_S {
176 static bool value;
177};
178bool (foo_S::value);
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000179
180
181namespace somens {
John McCall220ccbf2010-01-13 00:25:19 +0000182 struct a { }; // expected-note{{candidate constructor (the implicit copy constructor)}}
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000183}
184
185template <typename T>
186class foo {
187};
188
189
Chris Lattner46646492009-12-07 01:36:53 +0000190// PR4452 / PR4451
191foo<somens:a> a2; // expected-error {{unexpected ':' in nested name specifier}}
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000192
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000193somens::a a3 = a2; // expected-error {{no viable conversion}}
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000194
John McCall1d7c5282009-12-18 10:40:03 +0000195// typedefs and using declarations.
196namespace test1 {
197 namespace ns {
John McCall7002f4c2010-04-09 19:03:51 +0000198 class Counter { public: static int count; };
John McCall1d7c5282009-12-18 10:40:03 +0000199 typedef Counter counter;
200 }
201 using ns::counter;
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000202
John McCall1d7c5282009-12-18 10:40:03 +0000203 class Test {
204 void test1() {
205 counter c;
206 c.count++;
207 counter::count++;
208 }
209 };
210}
John McCall731ad842009-12-19 09:28:58 +0000211
212// We still need to do lookup in the lexical scope, even if we push a
213// non-lexical scope.
214namespace test2 {
215 namespace ns {
Sebastian Redl78a527a2010-01-26 18:52:33 +0000216 extern int *count_ptr;
John McCall731ad842009-12-19 09:28:58 +0000217 }
218 namespace {
219 int count = 0;
220 }
221
222 int *ns::count_ptr = &count;
223}
John McCall4c72d3e2010-02-08 19:26:07 +0000224
225// PR6259, invalid case
226namespace test3 {
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000227 class A; // expected-note {{forward declaration}}
John McCall4c72d3e2010-02-08 19:26:07 +0000228 void foo(const char *path) {
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 A::execute(path); // expected-error {{incomplete type 'test3::A' named in nested name specifier}}
John McCall4c72d3e2010-02-08 19:26:07 +0000230 }
231}
Douglas Gregor00b4b032010-05-14 04:53:42 +0000232
233namespace PR7133 {
234 namespace A {
235 class Foo;
236 }
237
238 namespace A {
239 namespace B {
240 bool foo(Foo &);
241 }
242 }
243
244 bool A::B::foo(Foo &) {
245 return false;
246 }
247}
Francois Pichetc71d8eb2010-10-01 21:19:28 +0000248
249class CLASS {
Douglas Gregor75379452012-09-13 20:16:20 +0000250 void CLASS::foo2(); // expected-error {{extra qualification on member 'foo2'}}
Francois Pichetc71d8eb2010-10-01 21:19:28 +0000251};
Douglas Gregor922fff22010-10-13 22:19:53 +0000252
253namespace PR8159 {
254 class B { };
255
256 class A {
Douglas Gregor75379452012-09-13 20:16:20 +0000257 int A::a; // expected-error{{extra qualification on member 'a'}}
258 static int A::b; // expected-error{{extra qualification on member 'b'}}
Douglas Gregor922fff22010-10-13 22:19:53 +0000259 int ::c; // expected-error{{non-friend class member 'c' cannot have a qualified name}}
260 };
261}
Argyrios Kyrtzidis1de34dd2011-02-05 05:54:49 +0000262
263namespace rdar7980179 {
264 class A { void f0(); }; // expected-note {{previous}}
Richard Smith8ef1b372013-08-23 02:16:48 +0000265 int A::f0() {} // expected-error {{return type of out-of-line definition of 'rdar7980179::A::f0' differs}}
Argyrios Kyrtzidis1de34dd2011-02-05 05:54:49 +0000266}
Douglas Gregorfa0b48f2011-02-24 02:37:39 +0000267
268namespace alias = A;
269double *dp = (alias::C*)0; // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'alias::C *'}}
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +0000270
271// http://llvm.org/PR10109
272namespace PR10109 {
273template<typename T>
274struct A {
275protected:
276 struct B;
277 struct B::C; // expected-error {{requires a template parameter list}} \
Douglas Gregor42acead2012-03-17 23:06:31 +0000278 // expected-error {{no struct named 'C'}} \
279 // expected-error{{non-friend class member 'C' cannot have a qualified name}}
Argyrios Kyrtzidisa7bf7bb2011-06-24 19:59:27 +0000280};
281
282template<typename T>
283struct A2 {
284protected:
285 struct B;
286};
287template <typename T>
288struct A2<T>::B::C; // expected-error {{no struct named 'C'}}
289}
Kaelyn Uhrain8d3607b2012-06-06 20:54:51 +0000290
291namespace PR13033 {
292namespace NS {
293 int a; // expected-note {{'NS::a' declared here}}
294 int longer_b; //expected-note {{'NS::longer_b' declared here}}
295}
296
297// Suggest adding a namespace qualifier to both variable names even though one
298// is only a single character long.
299int foobar = a + longer_b; // expected-error {{use of undeclared identifier 'a'; did you mean 'NS::a'?}} \
300 // expected-error {{use of undeclared identifier 'longer_b'; did you mean 'NS::longer_b'?}}
301}
Douglas Gregor86a87302013-05-14 23:22:32 +0000302
303// <rdar://problem/13853540>
304namespace N {
305 struct X { };
306 namespace N {
307 struct Foo {
308 struct N::X *foo(); // expected-error{{no struct named 'X' in namespace 'N::N'}}
309 };
310 }
311}
Eli Friedmanc02bea42013-06-19 22:58:30 +0000312
313namespace TypedefNamespace { typedef int F; };
Stephen Hines651f13c2014-04-23 16:59:28 -0700314TypedefNamespace::F::NonexistentName BadNNSWithCXXScopeSpec; // expected-error {{'F' (aka 'int') is not a class, namespace, or scoped enumeration}}