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