Debian Codegraph @

clang/test/SemaCXX/nested-name-spec.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -std=c++98 -verify=expected,cxx98,cxx98-11 -fblocks %s
2 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify=expected,since-cxx11,cxx98-11 -fblocks %s
3 // RUN: %clang_cc1 -fsyntax-only -std=c++14 -verify=expected,since-cxx11 -fblocks %s
4 // RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify=expected,since-cxx11 -fblocks %s
5 // RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify=expected,since-cxx11,since-cxx20 -fblocks %s
6 // RUN: %clang_cc1 -fsyntax-only -std=c++23 -verify=expected,since-cxx11,since-cxx20 -fblocks %s
7 // RUN: %clang_cc1 -fsyntax-only -std=c++26 -verify=expected,since-cxx11,since-cxx20 -fblocks %s
8
9 namespace A {
10 struct C {
11 static int cx;
12
13 static int cx2;
14
15 static int Ag1();
16 static int Ag2();
17 };
18 int ax;
19 // expected-note@-1 {{'ax' declared here}}
20 // expected-note@-2 {{'::A::ax' declared here}}
21 void Af();
22 }
23
24 A:: ; // expected-error {{expected unqualified-id}}
25 ::A::ax::undef ex3; // expected-error {{'ax' is not a class, namespace, or enumeration}}
26 A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}}
27
28 int A::C::Ag1() { return 0; }
29
30 static int A::C::Ag2() { return 0; } // expected-error{{'static' can}}
31
32 int A::C::cx = 17;
33
34
35 static int A::C::cx2 = 17; // expected-error{{'static' can}}
36
37 class C2 { // #defined-here-C2
38 void m(); // expected-note{{member declaration does not match because it is not const qualified}}
39
40 void f(const int& parm); // expected-note{{type of 1st parameter of member declaration does not match definition ('const int &' vs 'int')}}
41 void f(int) const; // expected-note{{member declaration does not match because it is const qualified}}
42 void f(float);
43
44 int x;
45 };
46
47 void C2::m() const { } // expected-error{{out-of-line definition of 'm' does not match any declaration in 'C2'}}
48 // expected-note@#defined-here-C2{{defined here}}
49
50 void C2::f(int) { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'C2'}}
51 // expected-note@#defined-here-C2{{defined here}}
52
53 void C2::m() {
54 x = 0;
55 }
56
57 namespace B {
58 void ::A::Af() {} // expected-error {{cannot define or redeclare 'Af' here because namespace 'B' does not enclose namespace 'A'}}
59 }
60
61 void f1() {
62 void A::Af(); // expected-error {{definition or redeclaration of 'Af' not allowed inside a function}}
63 void (^x)() = ^{ void A::Af(); }; // expected-error {{definition or redeclaration of 'Af' not allowed inside a block}}
64 }
65
66 void f2() {
67 A:: ; // expected-error {{expected unqualified-id}}
68 A::C::undef = 0; // expected-error {{no member named 'undef'}}
69 ::A::C::cx = 0;
70 int x = ::A::ax = A::C::cx;
71 x = sizeof(A::C);
72 x = sizeof(::A::C::cx);
73 }
74
75 A::C c1;
76 struct A::C c2;
77 struct S : public A::C {};
78 struct A::undef; // expected-error {{no struct named 'undef' in namespace 'A'}}
79 // expected-error@-1 {{forward declaration of struct cannot have a nested name specifier}}
80 namespace A2 {
81 typedef int INT;
82 struct RC;
83 struct CC {
84 struct NC;
85 };
86 }
87
88 struct A2::RC {
89 INT x;
90 };
91
92 struct A2::CC::NC {
93 void m() {}
94 };
95
96 void f3() {
97 N::x = 0; // expected-error {{use of undeclared identifier 'N'}}
98 // FIXME: Consider including the kind of entity that 'N' is ("variable 'N'
99 // declared here", "template 'X' declared here", etc) to help explain what it
100 // is if it's 'not a class, namespace, or scoped enumeration'.
101 int N; // expected-note {{'N' declared here}}
102 N::x = 0; // expected-error {{'N' is not a class, namespace, or enumeration}}
103 { int A; A::ax = 0; }
104 { typedef int A; A::ax = 0; } // expected-error{{'A' (aka 'int') is not a class, namespace, or enumeration}}
105 { typedef A::C A; A::ax = 0; } // expected-error {{no member named 'ax' in 'A::C'; did you mean '::A::ax'?}}
106 { typedef A::C A; A::cx = 0; }
107 }
108
109 // make sure the following doesn't hit any asserts
110 void f4(undef::C); // expected-error {{use of undeclared identifier 'undef'}}
111
112 typedef void C2::f5(int); // expected-error{{typedef declarator cannot be qualified}}
113
114 void f6(int A2::RC::x); // expected-error{{parameter declarator cannot be qualified}}
115
116 int A2::RC::x; // expected-error{{non-static data member defined out-of-line}}
117
118 void A2::CC::NC::m(); // expected-error{{out-of-line declaration of a member must be a definition}}
119
120
121 namespace E {
122 int X = 5;
123
124 namespace Nested {
125 enum E {
126 X = 0
127 };
128
129 int f() {
130 return E::X; // cxx98-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
131 }
132 }
133 }
134
135
136 class Operators { // #defined-here-Operators
137 Operators operator+(const Operators&) const; // expected-note{{member declaration does not match because it is const qualified}}
138 operator bool();
139 };
140
141 Operators Operators::operator+(const Operators&) { // expected-error{{out-of-line definition of 'operator+' does not match any declaration in 'Operators'}}
142 // expected-note@#defined-here-Operators{{defined here}}
143 Operators ops;
144 return ops;
145 }
146
147 Operators Operators::operator+(const Operators&) const {
148 Operators ops;
149 return ops;
150 }
151
152 Operators::operator bool() {
153 return true;
154 }
155
156 namespace A {
157 void g(int&); // expected-note{{type of 1st parameter of member declaration does not match definition ('int &' vs 'const int &')}}
158 }
159
160 void A::f() {} // expected-error-re{{out-of-line definition of 'f' does not match any declaration in namespace 'A'{{$}}}}
161
162 void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}}
163
164 struct Struct { }; // #defined-here-Struct
165
166 void Struct::f() { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'Struct'}}
167 // expected-note@#defined-here-Struct{{defined here}}
168
169 void global_func(int);
170 void global_func2(int);
171
172 namespace N {
173 void ::global_func(int) { } // expected-error{{definition or redeclaration of 'global_func' cannot name the global scope}}
174
175 void f();
176 // FIXME: if we move this to a separate definition of N, things break!
177 }
178 void ::global_func2(int) { } // expected-warning{{extra qualification on member 'global_func2'}}
179
180 void N::f() { } // okay
181
182 // FIXME (GH147000): duplicate diagnostics
183 struct Y; // expected-note{{forward declaration of 'Y'}} since-cxx20-note{{forward declaration of 'Y'}}
184 Y::foo y; // expected-error{{incomplete type 'Y' named in nested name specifier}} since-cxx20-error{{incomplete type 'Y' named in nested name specifier}}
185
186 namespace PR25156 {
187 struct Y; // expected-note{{forward declaration of 'PR25156::Y'}}
188 void foo() {
189 Y::~Y(); // expected-error{{incomplete type 'PR25156::Y' named in nested name specifier}}
190 }
191 }
192
193 X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}}
194
195 struct foo_S {
196 static bool value;
197 };
198 bool (foo_S::value);
199
200
201 namespace somens {
202 struct a { };
203 // expected-note@-1 {{candidate constructor (the implicit copy constructor)}}
204 // since-cxx11-note@-2 {{candidate constructor (the implicit move constructor)}}
205 }
206
207 template <typename T>
208 class foo {
209 };
210
211
212 // PR4452 / PR4451
213 foo<somens:a> a2; // expected-error {{unexpected ':' in nested name specifier}}
214
215 somens::a a3 = a2; // expected-error {{no viable conversion}}
216
217 // typedefs and using declarations.
218 namespace test1 {
219 namespace ns {
220 class Counter { public: static int count; };
221 typedef Counter counter;
222 }
223 using ns::counter;
224
225 class Test {
226 void test1() {
227 counter c;
228 c.count++;
229 counter::count++;
230 }
231 };
232 }
233
234 // We still need to do lookup in the lexical scope, even if we push a
235 // non-lexical scope.
236 namespace test2 {
237 namespace ns {
238 extern int *count_ptr;
239 }
240 namespace {
241 int count = 0;
242 }
243
244 int *ns::count_ptr = &count;
245 }
246
247 // PR6259, invalid case
248 namespace test3 {
249 class A; // expected-note {{forward declaration}}
250 void foo(const char *path) {
251 A::execute(path); // expected-error {{incomplete type 'test3::A' named in nested name specifier}}
252 }
253 }
254
255 namespace PR7133 {
256 namespace A {
257 class Foo;
258 }
259
260 namespace A {
261 namespace B {
262 bool foo(Foo &);
263 }
264 }
265
266 bool A::B::foo(Foo &) {
267 return false;
268 }
269 }
270
271 class CLASS {
272 void CLASS::foo2(); // expected-error {{extra qualification on member 'foo2'}}
273 };
274
275 namespace PR8159 {
276 class B { };
277
278 class A {
279 int A::a; // expected-error{{extra qualification on member 'a'}}
280 static int A::b; // expected-error{{extra qualification on member 'b'}}
281 int ::c; // expected-error{{non-friend class member 'c' cannot have a qualified name}}
282 };
283 }
284
285 namespace rdar7980179 {
286 class A { void f0(); }; // expected-note {{previous}}
287 int A::f0() {} // expected-error {{return type of out-of-line definition of 'rdar7980179::A::f0' differs}}
288 }
289
290 namespace alias = A;
291 double *dp = (alias::C*)0; // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'alias::C *'}}
292
293 // http://llvm.org/PR10109
294 namespace PR10109 {
295 template<typename T>
296 struct A {
297 protected:
298 struct B;
299 struct B::C;
300 // expected-error@-1 {{requires a template parameter list}}
301 // expected-error@-2 {{no struct named 'C'}}
302 // expected-error@-3 {{non-friend class member 'C' cannot have a qualified name}}
303 // expected-error@-4 {{forward declaration of struct cannot have a nested name specifier}}
304 };
305
306 template<typename T>
307 struct A2 {
308 protected:
309 struct B;
310 };
311 template <typename T>
312 struct A2<T>::B::C; // expected-error {{no struct named 'C'}}
313 // expected-error@-1 {{forward declaration of struct cannot have a nested name specifier}}
314 }
315
316 namespace PR13033 {
317 namespace NS {
318 int a; // expected-note {{'NS::a' declared here}}
319 int longer_b; //expected-note {{'NS::longer_b' declared here}}
320 }
321
322 // Suggest adding a namespace qualifier to both variable names even though one
323 // is only a single character long.
324 int foobar = a + longer_b; // expected-error {{use of undeclared identifier 'a'; did you mean 'NS::a'?}} \
325 // expected-error {{use of undeclared identifier 'longer_b'; did you mean 'NS::longer_b'?}}
326 }
327
328 namespace N {
329 struct X { };
330 namespace N {
331 struct Foo {
332 struct N::X *foo(); // expected-error{{no struct named 'X' in namespace 'N::N'}}
333 };
334 }
335 }
336
337 namespace TypedefNamespace { typedef int F; };
338 TypedefNamespace::F::NonexistentName BadNNSWithCXXScopeSpec; // expected-error {{'TypedefNamespace::F' (aka 'int') is not a class, namespace, or enumeration}}
339
340 namespace PR18587 {
341
342 struct C1 {
343 int a, b, c;
344 typedef int C2;
345 struct B1 {
346 struct B2 {
347 int a, b, c;
348 };
349 };
350 };
351 struct C2 { static const unsigned N1 = 1; };
352 struct B1 {
353 enum E1 { B2 = 2 };
354 static const int B3 = 3;
355 };
356 const int N1 = 2;
357
358 // Function declarators
359 struct S1a { int f(C1::C2); };
360 struct S1b { int f(C1:C2); }; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
361
362 struct S2a {
363 C1::C2 f(C1::C2);
364 };
365 struct S2c {
366 C1::C2 f(C1:C2); // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
367 };
368
369 struct S3a {
370 int f(C1::C2), C2 : N1;
371 int g : B1::B2;
372 };
373 struct S3b {
374 int g : B1:B2; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
375 };
376
377 // Inside square brackets
378 struct S4a {
379 int f[C2::N1];
380 };
381 struct S4b {
382 int f[C2:N1]; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
383 };
384
385 struct S5a {
386 int f(int xx[B1::B3 ? C2::N1 : B1::B2]);
387 };
388 struct S5b {
389 int f(int xx[B1::B3 ? C2::N1 : B1:B2]); // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
390 };
391 struct S5c {
392 int f(int xx[B1:B3 ? C2::N1 : B1::B2]); // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
393 };
394
395 // Bit fields
396 struct S6a {
397 C1::C2 m1 : B1::B2;
398 };
399 struct S6c {
400 C1::C2 m1 : B1:B2; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
401 };
402 struct S6d {
403 int C2:N1;
404 };
405 struct S6e {
406 static const int N = 3;
407 B1::E1 : N;
408 };
409 struct S6g {
410 C1::C2 : B1:B2; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
411 B1::E1 : B1:B2; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
412 };
413
414 // Template parameters
415 template <int N> struct T1 {
416 int a,b,c;
417 static const unsigned N1 = N;
418 typedef unsigned C1;
419 };
420 T1<C2::N1> var_1a;
421 T1<C2:N1> var_1b; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
422 template<int N> int F() {}
423 int (*X1)() = (B1::B2 ? F<1> : F<2>);
424 int (*X2)() = (B1:B2 ? F<1> : F<2>); // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}} \
425 expected-note{{'PR18587::X2' declared here}}
426
427 // Bit fields + templates
428 struct S7a {
429 T1<B1::B2>::C1 m1 : T1<B1::B2>::N1;
430 };
431 struct S7b {
432 T1<B1:B2>::C1 m1 : T1<B1::B2>::N1; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
433 };
434 struct S7c {
435 T1<B1::B2>::C1 m1 : T1<B1:B2>::N1; // expected-error{{unexpected ':' in nested name specifier; did you mean '::'?}}
436 };
437
438 }
439
440 namespace PR16951 {
441 namespace ns {
442 enum an_enumeration {
443 ENUMERATOR // expected-note{{'ENUMERATOR' declared here}}
444 };
445 }
446
447 int x1 = ns::an_enumeration::ENUMERATOR; // cxx98-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
448
449 int x2 = ns::an_enumeration::ENUMERATOR::vvv; // cxx98-warning{{use of enumeration in a nested name specifier is a C++11 extension}} \
450 // expected-error{{'ENUMERATOR' is not a class, namespace, or enumeration}} \
451
452 int x3 = ns::an_enumeration::X; // cxx98-warning {{use of enumeration in a nested name specifier is a C++11 extension}} \
453 // expected-error{{no member named 'X'}}
454
455 enum enumerator_2 {
456 ENUMERATOR_2
457 };
458
459 int x4 = enumerator_2::ENUMERATOR_2; // cxx98-warning{{use of enumeration in a nested name specifier is a C++11 extension}}
460 int x5 = enumerator_2::X2; // cxx98-warning{{use of enumeration in a nested name specifier is a C++11 extension}} \
461 // expected-error{{no member named 'X2' in 'PR16951::enumerator_2'}} \
462 // expected-error{{cannot initialize a variable of type 'int' with an lvalue of type 'int (*)()'}}
463
464 }
465
466 namespace PR30619 {
467 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
468 // expected-error@-1 16{{unknown type name 'c'}}
469 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
470 // expected-error@-1 16{{unknown type name 'c'}}
471 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
472 // expected-error@-1 16{{unknown type name 'c'}}
473 c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d; c d;
474 // expected-error@-1 16{{unknown type name 'c'}}
475 namespace A {
476 class B {
477 typedef C D; // expected-error{{unknown type name 'C'}}
478 A::D::F;
479 // expected-error@-1{{'A::D' (aka 'int') is not a class, namespace, or enumeration}}
480 };
481 }
482 }
483
484 namespace DependentTemplateInTrivialNNSLoc {
485 // This testcase causes us to create trivial type source info when doing
486 // substitution into T::template g<>. That trivial type source info contained
487 // a NestedNameSpecifierLoc with no location information.
488 //
489 // Previously, creating a CXXScopeSpec from that resulted in an invalid scope
490 // spec, leading to crashes. Ensure we don't crash here.
491 template <typename T> void f(T &x) {
492 for (typename T::template g<> i : x) {} // expected-warning 0-1{{extension}}
493 x: goto x;
494 }
495 }
496
497 template <typename T>
498 struct x; // expected-note {{template is declared here}}
499
500 template <typename T>
501 int issue55962 = x::a; // expected-error {{use of class template 'x' requires template arguments}} \
502 // cxx98-11-warning {{variable templates are a C++14 extension}}
503
504 namespace ForwardDeclared {
505 typedef class A B;
506 struct A {
507 enum C {};
508 void F(B::C);
509 };
510 }
511
512 namespace GH63254 {
513 template <typename...> struct V {}; // cxx98-warning {{variadic templates are a C++11 extension}}
514 struct S : V<> {
515 using S::V; // expected-error {{using declaration refers to its own class}}
516 V<> v; // no crash
517 };
518 }