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 {{friend function definition cannot be qualified with 'C::'}}
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
);
234 friend void f10_d(W
);
237 void g(X x
, Y y
, Z z
) {
238 f10_d(); // expected-error {{undeclared identifier}}
239 ::test10::f10_d(); // expected-error {{no member named 'f10_d'}}
241 // f10_d is visible to ADL in the second and third cases.
242 f10_d(x
); // expected-error {{undeclared identifier}}
247 ::test10::f10_d(x
); // expected-error {{no type named 'f10_d'}}
248 ::test10::f10_d(y
); // expected-error {{no type named 'f10_d'}}
249 ::test10::f10_d(z
); // expected-error {{no type named 'f10_d'}}
252 void local_externs(W w
, X x
, Y y
) {
253 extern void f10_d(); // expected-note {{candidate}}
254 extern void f10_d(X
); // expected-note {{candidate}}
258 f10_d(w
); // expected-error {{no matching}}
261 f10_d(); // expected-error {{not a function}}
262 f10_d(x
); // expected-error {{not a function}}
263 f10_d(y
); // expected-error {{not a function}}
268 f10_d(); // expected-error {{undeclared identifier}}
269 f10_d(x
); // expected-error {{undeclared identifier}}
275 friend void f10_d(X
);
279 f10_d(); // expected-error {{undeclared identifier}}
280 f10_d(x
); // expected-error {{undeclared identifier}}
285 extern void f10_d(X
);
286 void k(X x
, Y y
, Z z
) {
298 class __attribute__((visibility("hidden"))) B
;
301 friend class __attribute__((visibility("hidden"), noreturn
)) B
; // expected-warning {{'noreturn' attribute only applies to functions and methods}}
306 // PR21851 was a problem where we assumed that when the friend function redecl
307 // lookup found a C++ method, it would necessarily have a qualifier. Below we
308 // have some test cases where unqualified lookup finds C++ methods without using
309 // qualifiers. Unfortunately, we can't exercise the case of an access check
310 // failure because nested classes always have access to the members of outer
313 void friend_own_method() {
320 void friend_enclosing_method() {
327 void enclosing_friend() {
336 friend void enclosing_friend();
343 static auto friend_file_func() {
344 extern void file_scope_friend();
347 friend void file_scope_friend();
352 void file_scope_friend() {
353 auto a
= friend_file_func();
361 friend void f_pr6954(int x
);
364 int array0_pr6954
[sizeof(X_pr6954
<int>)];
365 int array1_pr6954
[sizeof(X_pr6954
<float>)];
368 f_pr6954(5); // expected-error{{undeclared identifier 'f_pr6954'}}
371 namespace tag_redecl
{
386 namespace default_arg
{
388 void f(void*); // expected-note {{previous}}
390 friend void f(int a
, int b
= 0) {}
391 friend void f(void *p
= 0) {} // expected-error {{must be the only}}
397 template<typename T
> struct X
{
402 int g(); // expected-note {{previous}}
403 template<typename T
> struct Y
{
404 friend T
g(); // expected-error {{return type}}
406 Y
<float> yf
; // expected-note {{instantiation}}
408 int h(); // expected-note {{previous}}
409 template<typename T
> struct Z
{
410 friend T
h(); // expected-error {{return type}}
413 Z
<float> zf
; // expected-note {{instantiation}}
416 namespace qualified_friend_no_match
{
417 void f(int); // expected-note {{type mismatch at 1st parameter}}
418 template<typename T
> void f(T
*); // expected-note {{could not match 'T *' against 'double'}}
420 friend void qualified_friend_no_match::f(double); // expected-error {{friend declaration of 'f' does not match any declaration in namespace 'qualified_friend_no_match'}}
421 friend void qualified_friend_no_match::g(); // expected-error {{friend declaration of 'g' does not match any declaration in namespace 'qualified_friend_no_match'}}
425 void f(int); // expected-note {{type mismatch at 1st parameter}}
426 template<typename T
> void f(T
*); // expected-note {{could not match 'T *' against 'double'}}
429 friend void Y::f(double); // expected-error {{friend declaration of 'f' does not match any declaration in 'qualified_friend_no_match::Y'}}