Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -std=c++98 -verify %s |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 2 | namespace A { |
| 3 | struct C { |
| 4 | static int cx; |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 5 | |
| 6 | static int cx2; |
| 7 | |
| 8 | static int Ag1(); |
| 9 | static int Ag2(); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 10 | }; |
| 11 | int ax; |
| 12 | void Af(); |
| 13 | } |
| 14 | |
| 15 | A:: ; // expected-error {{expected unqualified-id}} |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 16 | // FIXME: there is a member 'ax'; it's just not a class. |
| 17 | ::A::ax::undef ex3; // expected-error {{no member named 'ax'}} |
| 18 | A::undef1::undef2 ex4; // expected-error {{no member named 'undef1'}} |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 19 | |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 20 | int A::C::Ag1() { return 0; } |
| 21 | |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 22 | static int A::C::Ag2() { return 0; } // expected-error{{'static' can}} |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 23 | |
| 24 | int A::C::cx = 17; |
| 25 | |
| 26 | |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 27 | static int A::C::cx2 = 17; // expected-error{{'static' can}} |
Douglas Gregor | 656de63 | 2009-03-11 23:52:16 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 29 | class C2 { |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 30 | void m(); // expected-note{{member declaration nearly matches}} |
| 31 | |
| 32 | void f(const int& parm); // expected-note{{member declaration nearly matches}} |
| 33 | void f(int) const; // expected-note{{member declaration nearly matches}} |
| 34 | void f(float); |
| 35 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 36 | int x; |
| 37 | }; |
| 38 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 39 | void C2::m() const { } // expected-error{{out-of-line definition of 'm' does not match any declaration in 'C2'}} |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 40 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 41 | void C2::f(int) { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'C2'}} |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 42 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 43 | void C2::m() { |
| 44 | x = 0; |
| 45 | } |
| 46 | |
| 47 | namespace B { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 48 | void ::A::Af() {} // expected-error {{definition or redeclaration of 'Af' not in a namespace enclosing 'A'}} |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void f1() { |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 52 | void A::Af(); // expected-error {{definition or redeclaration of 'Af' not allowed inside a function}} |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void f2() { |
| 56 | A:: ; // expected-error {{expected unqualified-id}} |
| 57 | A::C::undef = 0; // expected-error {{no member named 'undef'}} |
| 58 | ::A::C::cx = 0; |
| 59 | int x = ::A::ax = A::C::cx; |
| 60 | x = sizeof(A::C); |
| 61 | x = sizeof(::A::C::cx); |
| 62 | } |
| 63 | |
| 64 | A::C c1; |
| 65 | struct A::C c2; |
| 66 | struct S : public A::C {}; |
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 67 | struct A::undef; // expected-error {{no struct named 'undef' in namespace 'A'}} |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 68 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 69 | namespace A2 { |
| 70 | typedef int INT; |
| 71 | struct RC; |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 72 | struct CC { |
| 73 | struct NC; |
| 74 | }; |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | struct A2::RC { |
| 78 | INT x; |
| 79 | }; |
| 80 | |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 81 | struct A2::CC::NC { |
| 82 | void m() {} |
| 83 | }; |
| 84 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 85 | void f3() { |
| 86 | N::x = 0; // expected-error {{use of undeclared identifier 'N'}} |
| 87 | int N; |
| 88 | N::x = 0; // expected-error {{expected a class or namespace}} |
| 89 | { int A; A::ax = 0; } |
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame] | 90 | { typedef int A; A::ax = 0; } // expected-error{{expected a class or namespace}} |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 91 | { typedef A::C A; A::ax = 0; } // expected-error {{no member named 'ax'}} |
| 92 | { typedef A::C A; A::cx = 0; } |
| 93 | } |
Argyrios Kyrtzidis | 08b2c37 | 2008-11-19 15:22:16 +0000 | [diff] [blame] | 94 | |
| 95 | // make sure the following doesn't hit any asserts |
Chris Lattner | 8129edb | 2009-04-12 22:23:27 +0000 | [diff] [blame] | 96 | void f4(undef::C); // expected-error {{use of undeclared identifier 'undef'}} \ |
| 97 | expected-error {{variable has incomplete type 'void'}} |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 98 | |
| 99 | typedef void C2::f5(int); // expected-error{{typedef declarator cannot be qualified}} |
| 100 | |
| 101 | void f6(int A2::RC::x); // expected-error{{parameter declarator cannot be qualified}} |
| 102 | |
| 103 | int A2::RC::x; // expected-error{{non-static data member defined out-of-line}} |
| 104 | |
Chandler Carruth | 34fa294 | 2010-07-16 05:46:45 +0000 | [diff] [blame] | 105 | void A2::CC::NC::m(); // expected-error{{out-of-line declaration of a member must be a definition}} |
Douglas Gregor | 3dde5a3 | 2008-12-16 06:37:47 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | namespace E { |
| 109 | int X = 5; |
| 110 | |
| 111 | namespace Nested { |
| 112 | enum E { |
| 113 | X = 0 |
| 114 | }; |
| 115 | |
| 116 | void f() { |
| 117 | return E::X; // expected-error{{expected a class or namespace}} |
| 118 | } |
| 119 | } |
| 120 | } |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | class Operators { |
| 124 | Operators operator+(const Operators&) const; // expected-note{{member declaration nearly matches}} |
| 125 | operator bool(); |
| 126 | }; |
| 127 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 128 | Operators Operators::operator+(const Operators&) { // expected-error{{out-of-line definition of 'operator+' does not match any declaration in 'Operators'}} |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 129 | Operators ops; |
| 130 | return ops; |
| 131 | } |
| 132 | |
| 133 | Operators Operators::operator+(const Operators&) const { |
| 134 | Operators ops; |
| 135 | return ops; |
| 136 | } |
| 137 | |
| 138 | Operators::operator bool() { |
| 139 | return true; |
| 140 | } |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 141 | |
| 142 | namespace A { |
| 143 | void g(int&); // expected-note{{member declaration nearly matches}} |
| 144 | } |
| 145 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 146 | void A::f() {} // expected-error{{out-of-line definition of 'f' does not match any declaration in namespace 'A'}} |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 148 | void A::g(const int&) { } // expected-error{{out-of-line definition of 'g' does not match any declaration in namespace 'A'}} |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 149 | |
| 150 | struct Struct { }; |
| 151 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 152 | void Struct::f() { } // expected-error{{out-of-line definition of 'f' does not match any declaration in 'Struct'}} |
Douglas Gregor | 4ce205f | 2009-02-06 17:46:57 +0000 | [diff] [blame] | 153 | |
| 154 | void global_func(int); |
| 155 | void global_func2(int); |
| 156 | |
| 157 | namespace N { |
| 158 | void ::global_func(int) { } // expected-error{{definition or redeclaration of 'global_func' cannot name the global scope}} |
| 159 | |
| 160 | void f(); |
| 161 | // FIXME: if we move this to a separate definition of N, things break! |
| 162 | } |
| 163 | void ::global_func2(int) { } // expected-error{{definition or redeclaration of 'global_func2' cannot name the global scope}} |
| 164 | |
| 165 | void N::f() { } // okay |
Douglas Gregor | 9fa14a5 | 2009-03-06 19:06:37 +0000 | [diff] [blame] | 166 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 167 | struct Y; // expected-note{{forward declaration of 'Y'}} |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 168 | Y::foo y; // expected-error{{incomplete type 'Y' named in nested name specifier}} |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 9fa14a5 | 2009-03-06 19:06:37 +0000 | [diff] [blame] | 170 | X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}} \ |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 171 | // expected-error{{C++ requires a type specifier for all declarations}} \ |
| 172 | // expected-error{{only constructors take base initializers}} |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 173 | |
Argyrios Kyrtzidis | f37006b | 2009-07-21 06:43:26 +0000 | [diff] [blame] | 174 | struct foo_S { |
| 175 | static bool value; |
| 176 | }; |
| 177 | bool (foo_S::value); |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | namespace somens { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 181 | struct a { }; // expected-note{{candidate constructor (the implicit copy constructor)}} |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | template <typename T> |
| 185 | class foo { |
| 186 | }; |
| 187 | |
| 188 | |
Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 189 | // PR4452 / PR4451 |
| 190 | foo<somens:a> a2; // expected-error {{unexpected ':' in nested name specifier}} |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 192 | somens::a a3 = a2; // expected-error {{no viable conversion}} |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 193 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 194 | // typedefs and using declarations. |
| 195 | namespace test1 { |
| 196 | namespace ns { |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 197 | class Counter { public: static int count; }; |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 198 | typedef Counter counter; |
| 199 | } |
| 200 | using ns::counter; |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 201 | |
John McCall | 1d7c528 | 2009-12-18 10:40:03 +0000 | [diff] [blame] | 202 | class Test { |
| 203 | void test1() { |
| 204 | counter c; |
| 205 | c.count++; |
| 206 | counter::count++; |
| 207 | } |
| 208 | }; |
| 209 | } |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 210 | |
| 211 | // We still need to do lookup in the lexical scope, even if we push a |
| 212 | // non-lexical scope. |
| 213 | namespace test2 { |
| 214 | namespace ns { |
Sebastian Redl | 78a527a | 2010-01-26 18:52:33 +0000 | [diff] [blame] | 215 | extern int *count_ptr; |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 216 | } |
| 217 | namespace { |
| 218 | int count = 0; |
| 219 | } |
| 220 | |
| 221 | int *ns::count_ptr = &count; |
| 222 | } |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 223 | |
| 224 | // PR6259, invalid case |
| 225 | namespace test3 { |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 226 | class A; // expected-note {{forward declaration}} |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 227 | void foo(const char *path) { |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 228 | A::execute(path); // expected-error {{incomplete type 'test3::A' named in nested name specifier}} |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
Douglas Gregor | 00b4b03 | 2010-05-14 04:53:42 +0000 | [diff] [blame] | 231 | |
| 232 | namespace PR7133 { |
| 233 | namespace A { |
| 234 | class Foo; |
| 235 | } |
| 236 | |
| 237 | namespace A { |
| 238 | namespace B { |
| 239 | bool foo(Foo &); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | bool A::B::foo(Foo &) { |
| 244 | return false; |
| 245 | } |
| 246 | } |