1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
4 // RUN: %clang_cc1 -fsyntax-only -verify %s
5 
6 // C++'0x [class.friend] p1:
7 //   A friend of a class is a function or class that is given permission to use
8 //   the private and protected member names from the class. A class specifies
9 //   its friends, if any, by way of friend declarations. Such declarations give
10 //   special access rights to the friends, but they do not make the nominated
11 //   friends members of the befriending class.
12 
13 struct S { static void f(); }; // expected-note 2 {{'S' declared here}}
g()14 S* g() { return 0; } // expected-note 2 {{'g' declared here}}
15 
16 struct X {
17   friend struct S;
18   friend S* g();
19 };
20 
test1()21 void test1() {
22   S s;
23   g()->f();
24   S::f();
25   X::g(); // expected-error{{no member named 'g' in 'X'; did you mean simply 'g'?}}
26   X::S x_s; // expected-error{{no type named 'S' in 'X'; did you mean simply 'S'?}}
27   X x;
28   x.g(); // expected-error{{no member named 'g' in 'X'}}
29 }
30 
31 // Test that we recurse through namespaces to find already declared names, but
32 // new names are declared within the enclosing namespace.
33 namespace N {
34   struct X {
35     friend struct S;
36     friend S* g();
37 
38     friend struct S2;
39     friend struct S2* g2();
40   };
41 
42   struct S2 { static void f2(); }; // expected-note 2 {{'S2' declared here}}
g2()43   S2* g2() { return 0; } // expected-note 2 {{'g2' declared here}}
44 
test()45   void test() {
46     g()->f();
47     S s;
48     S::f();
49     X::g(); // expected-error{{no member named 'g' in 'N::X'; did you mean simply 'g'?}}
50     X::S x_s; // expected-error{{no type named 'S' in 'N::X'; did you mean simply 'S'?}}
51     X x;
52     x.g(); // expected-error{{no member named 'g' in 'N::X'}}
53 
54     g2();
55     S2 s2;
56     ::g2(); // expected-error{{no member named 'g2' in the global namespace; did you mean simply 'g2'?}}
57     ::S2 g_s2; // expected-error{{no type named 'S2' in the global namespace; did you mean simply 'S2'?}}
58     X::g2(); // expected-error{{no member named 'g2' in 'N::X'; did you mean simply 'g2'?}}
59     X::S2 x_s2; // expected-error{{no type named 'S2' in 'N::X'; did you mean simply 'S2'?}}
60     x.g2(); // expected-error{{no member named 'g2' in 'N::X'}}
61   }
62 }
63 
64 namespace test0 {
65   class ClassFriend {
66     void test();
67   };
68 
69   class MemberFriend {
70   public:
71     void test();
72   };
73 
74   void declared_test();
75 
76   class Class {
77     static void member(); // expected-note 2 {{declared private here}}
78 
79     friend class ClassFriend;
80     friend class UndeclaredClassFriend;
81 
82     friend void undeclared_test();
83     friend void declared_test();
84     friend void MemberFriend::test();
85   };
86 
declared_test()87   void declared_test() {
88     Class::member();
89   }
90 
undeclared_test()91   void undeclared_test() {
92     Class::member();
93   }
94 
unfriended_test()95   void unfriended_test() {
96     Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
97   }
98 
test()99   void ClassFriend::test() {
100     Class::member();
101   }
102 
test()103   void MemberFriend::test() {
104     Class::member();
105   }
106 
107   class UndeclaredClassFriend {
test()108     void test() {
109       Class::member();
110     }
111   };
112 
113   class ClassNonFriend {
test()114     void test() {
115       Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
116     }
117   };
118 }
119 
120 // Make sure that friends have access to inherited protected members.
121 namespace test2 {
122   struct X;
123 
124   class ilist_half_node {
125     friend struct ilist_walker_bad;
126     X *Prev;
127   protected:
getPrev()128     X *getPrev() { return Prev; } // expected-note{{member is declared here}}
129   };
130 
131   class ilist_node : private ilist_half_node { // expected-note {{constrained by private inheritance here}}
132     friend struct ilist_walker;
133     X *Next;
getNext()134     X *getNext() { return Next; } // expected-note {{declared private here}}
135   };
136 
137   struct X : ilist_node {};
138 
139   struct ilist_walker {
getPrevtest2::ilist_walker140     static X *getPrev(X *N) { return N->getPrev(); }
getNexttest2::ilist_walker141     static X *getNext(X *N) { return N->getNext(); }
142   };
143 
144   struct ilist_walker_bad {
getPrevtest2::ilist_walker_bad145     static X *getPrev(X *N) { return N->getPrev(); } // \
146     // expected-error {{'getPrev' is a private member of 'test2::ilist_half_node'}}
147 
getNexttest2::ilist_walker_bad148     static X *getNext(X *N) { return N->getNext(); } // \
149     // expected-error {{'getNext' is a private member of 'test2::ilist_node'}}
150   };
151 }
152 
153 namespace test3 {
154   class A { protected: int x; }; // expected-note {{declared protected here}}
155 
156   class B : public A {
157     friend int foo(B*);
158   };
159 
foo(B * p)160   int foo(B *p) {
161     return p->x;
162   }
163 
foo(const B * p)164   int foo(const B *p) {
165     return p->x; // expected-error {{'x' is a protected member of 'test3::A'}}
166   }
167 }
168 
169 namespace test3a {
170   class A { protected: int x; };
171 
172   class B : public A {
173     friend int foo(B*);
174   };
175 
foo(B * const p)176   int foo(B * const p) {
177     return p->x;
178   }
179 }
180 
181 namespace test4 {
182   template <class T> class Holder {
183     T object;
operator ==(Holder & a,Holder & b)184     friend bool operator==(Holder &a, Holder &b) {
185       return a.object == b.object; // expected-error {{invalid operands to binary expression}}
186     }
187   };
188 
189   struct Inequal {};
test()190   bool test() {
191     Holder<Inequal> a, b;
192     return a == b;  // expected-note {{requested here}}
193   }
194 }
195 
196 
197 // PR6174
198 namespace test5 {
199   namespace ns {
200     class A;
201   }
202 
203   class ns::A {
204   private: int x;
205     friend class B;
206   };
207 
208   namespace ns {
209     class B {
test(A * p)210       int test(A *p) { return p->x; }
211     };
212   }
213 }
214 
215 // PR6207
216 namespace test6 {
217   struct A {};
218 
219   struct B {
220     friend
221 #if __cplusplus >= 201103L
222       constexpr
223 #endif
224       A::A();
225     friend A::~A();
226     friend
227 #if __cplusplus >= 201402L
228       constexpr
229 #endif
230       A &A::operator=(const A&);
231   };
232 }
233 
234 namespace test7 {
235   template <class T> struct X {
236     X();
237     ~X();
238     void foo();
239     void bar();
240   };
241 
242   class A {
243     friend void X<int>::foo();
244     friend X<int>::X();
245     friend
246 #if __cplusplus >= 201103L
247       constexpr
248 #endif
249       X<int>::X(const X&);
250 
251   private:
252     A(); // expected-note 2 {{declared private here}}
253   };
254 
foo()255   template<> void X<int>::foo() {
256     A a;
257   }
258 
bar()259   template<> void X<int>::bar() {
260     A a; // expected-error {{calling a private constructor}}
261   }
262 
X()263   template<> X<int>::X() {
264     A a;
265   }
266 
~X()267   template<> X<int>::~X() {
268     A a; // expected-error {{calling a private constructor}}
269   }
270 }
271 
272 // Return types, parameters and default arguments to friend functions.
273 namespace test8 {
274   class A {
275     typedef int I; // expected-note 4 {{declared private here}}
276     static const I x = 0; // expected-note {{implicitly declared private here}}
277     friend I f(I i);
278     template<typename T> friend I g(I i);
279   };
280 
281   const A::I A::x;
f(A::I i=A::x)282   A::I f(A::I i = A::x) {}
g(A::I i)283   template<typename T> A::I g(A::I i) {
284     T t;
285   }
286   template A::I g<A::I>(A::I i);
287 
f2(A::I i=A::x)288   A::I f2(A::I i = A::x) {} // expected-error 3 {{is a private member of}}
g2(A::I i)289   template<typename T> A::I g2(A::I i) { // expected-error 2 {{is a private member of}}
290     T t;
291   }
292   template A::I g2<A::I>(A::I i);
293 }
294 
295 // PR6885
296 namespace test9 {
297   class B {
298     friend class test9;
299   };
300 }
301 
302 // PR7230
303 namespace test10 {
304   extern "C" void test10_f(void);
305   extern "C" void test10_g(void);
306 
307   namespace NS {
308     class C {
309       void foo(void); // expected-note {{declared private here}}
310       friend void test10::test10_f(void);
311     };
312     static C* bar;
313   }
314 
test10_f(void)315   void test10_f(void) {
316     NS::bar->foo();
317   }
318 
test10_g(void)319   void test10_g(void) {
320     NS::bar->foo(); // expected-error {{private member}}
321   }
322 }
323 
324 // PR8705
325 namespace test11 {
326   class A {
327   public:
328     void test0(int);
329     void test1(int);
330     void test2(int);
331     void test3(int);
332   };
333 
334   class B {
335     typedef int private_type; // expected-note 2 {{implicitly declared private here}}
336     friend void A::test0(int);
337     friend void A::test1(int);
338   };
339 
test0(B::private_type x)340   void A::test0(B::private_type x) {}
test1(int x=B::private_type ())341   void A::test1(int x = B::private_type()) {}
test2(B::private_type x)342   void A::test2(B::private_type x) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
test3(int x=B::private_type ())343   void A::test3(int x = B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
344 }
345 
346 
347 // PR9221
348 namespace test12 {
349   struct A {
350     void foo();
351   };
352   class B : private A {
353     friend void A::foo();
354     void *mem;
355   };
foo()356   void A::foo() {
357     void *var = static_cast<B*>(this)->mem;
358   }
359 }
360 
361 namespace PR9103 {
362   struct base {
363   protected:
fooPR9103::base364     static void foo(void) {}
365   };
366 
367   struct cls: base {
bar(void)368     friend void bar(void) {
369       base::foo();
370     }
371   };
372 }
373 
374 // PR13642.  When computing the effective context, we were walking up
375 // the DC chain for the canonical decl, which is unfortunate if that's
376 // (e.g.) a friend declaration.
377 namespace test14 {
378   class A {
379     class B { // expected-note {{implicitly declared private here}}
380       static int i;
381       friend void c();
382     };
383   };
384 
c()385   void c() {
386     A::B::i = 5; // expected-error {{'B' is a private member of 'test14::A'}}
387   }
388 }
389