1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++14
3 friend class A; // expected-error {{'friend' used outside of class}}
4 void f() { friend class A; } // expected-error {{'friend' used outside of class}}
5 class C { friend class A; };
6 class D { void f() { friend class A; } }; // expected-error {{'friend' used outside of class}}
15 friend void ns::f(int a);
19 // Test derived from LLVM's Registry.h
21 template <class T> struct Outer {
24 friend void Outer::foo(T);
29 (void) Outer<int>::Inner();
40 friend void ::test2::foo::Func(int x);
47 friend const int getInt(int inInt = 0) {}
60 T4B *mB; // error here
66 namespace rdar8529993 {
71 template<int> friend A::~A(); // expected-error {{destructor cannot be declared as a template}}
78 struct A1 { friend void A(); };
80 struct B { friend void B(); };
93 vectorA(int i, const A& t = A()) {}
103 vector(int i, const T& t = T()) {}
108 friend class vector<A>;
120 void f(const T& t = T()) {}
125 friend void vector<A>::f(const A&);
137 friend int test7_f() { return 42; }
146 template<class T> void f(T t); // expected-note {{target of using declaration}}
148 using ns2::f; // expected-note {{using declaration}}
150 struct A { void f(); }; // expected-note 2{{target of using declaration}}
151 struct B : public A { using A::f; }; // expected-note {{using declaration}}
152 template<typename T> struct C : A { using A::f; }; // expected-note {{using declaration}}
154 template<class T> friend void ns1::f(T t); // expected-error {{cannot befriend target of using declaration}}
155 friend void B::f(); // expected-error {{cannot befriend target of using declaration}}
156 friend void C<int>::f(); // expected-error {{cannot befriend target of using declaration}}
165 friend void C::f(int, int, int) {} // expected-error {{no function named 'f' with type 'void (int, int, int)' was found in the specified scope}}
172 extern void f10_a(X);
178 friend void f10_a(X);
179 friend void f10_b(X);
180 friend void f10_c(X);
181 friend void f10_d(X);
184 extern void f10_b(X);
190 friend void f10_a(X);
191 friend void f10_b(X);
192 friend void f10_c(X);
193 friend void f10_d(X);
196 extern void f10_c(X);
198 // FIXME: Give a better diagnostic for the case where a function exists but is
204 f10_d(); // expected-error {{undeclared identifier}}
209 ::test10::f10_d(); // expected-error {{no member named 'f10_d'}}
214 f10_d(x); // PR16597: expected-error {{undeclared identifier}}
219 ::test10::f10_d(x); // expected-error {{no type named 'f10_d'}}
224 friend void f10_d(X);
230 friend void f10_d(X);
233 void g(X x, Y y, Z z) {
234 f10_d(); // expected-error {{undeclared identifier}}
235 ::test10::f10_d(); // expected-error {{no member named 'f10_d'}}
237 // f10_d is visible to ADL in the second and third cases.
238 f10_d(x); // expected-error {{undeclared identifier}}
243 ::test10::f10_d(x); // expected-error {{no type named 'f10_d'}}
244 ::test10::f10_d(y); // expected-error {{no type named 'f10_d'}}
245 ::test10::f10_d(z); // expected-error {{no type named 'f10_d'}}
248 void local_externs(X x, Y y) {
250 extern void f10_d(X);
253 // FIXME: This lookup should fail, because the local extern declaration
254 // should suppress ADL.
258 f10_d(); // expected-error {{not a function}}
259 f10_d(x); // expected-error {{not a function}}
260 f10_d(y); // expected-error {{not a function}}
265 f10_d(); // expected-error {{undeclared identifier}}
266 f10_d(x); // expected-error {{undeclared identifier}}
272 friend void f10_d(X);
276 f10_d(); // expected-error {{undeclared identifier}}
277 f10_d(x); // expected-error {{undeclared identifier}}
282 extern void f10_d(X);
283 void k(X x, Y y, Z z) {
295 class __attribute__((visibility("hidden"))) B;
298 friend class __attribute__((visibility("hidden"), noreturn)) B; // expected-warning {{'noreturn' attribute only applies to functions and methods}}
303 // PR21851 was a problem where we assumed that when the friend function redecl
304 // lookup found a C++ method, it would necessarily have a qualifier. Below we
305 // have some test cases where unqualified lookup finds C++ methods without using
306 // qualifiers. Unfortunately, we can't exercise the case of an access check
307 // failure because nested classes always have access to the members of outer
310 void friend_own_method() {
317 void friend_enclosing_method() {
324 void enclosing_friend() {
333 friend void enclosing_friend();
340 static auto friend_file_func() {
341 extern void file_scope_friend();
344 friend void file_scope_friend();
349 void file_scope_friend() {
350 auto a = friend_file_func();
358 friend void f_pr6954(int x);
361 int array0_pr6954[sizeof(X_pr6954<int>)];
362 int array1_pr6954[sizeof(X_pr6954<float>)];
365 f_pr6954(5); // expected-error{{undeclared identifier 'f_pr6954'}}
368 namespace tag_redecl {
383 namespace default_arg {
385 void f(void*); // expected-note {{previous}}
387 friend void f(int a, int b = 0) {}
388 friend void f(void *p = 0) {} // expected-error {{must be the only}}
394 template<typename T> struct X {
399 int g(); // expected-note {{previous}}
400 template<typename T> struct Y {
401 friend T g(); // expected-error {{return type}}
403 Y<float> yf; // expected-note {{instantiation}}
406 template<typename T> struct Z {
407 // FIXME: The note here should point at the non-friend declaration, not the
408 // instantiation in Z<int>.
409 friend T h(); // expected-error {{return type}} expected-note {{previous}}
412 Z<float> zf; // expected-note {{instantiation}}