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 6 {{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 }
g2(A::I i)292 template <> A::I g2<char>(A::I i) { return 0; } // OK
293 template A::I g2<A::I>(A::I i); // OK
294 template <> A::I g2<char>(A::I i); // OK
295 template <> A::I g2<A::I *>(A::I i); // OK
296 template A::I g2<unsigned>(A::I i); // OK
297 template int g2<A::I **>(int i); // OK
298 template A::I g2<A::I ***>(A::I i); // OK
299
g3(A::I i)300 template <typename T> A::I g3(A::I i) { return 0; } // expected-error 2 {{is a private member of}}
301 template <> int g3<A::I>(int i); // OK
302 }
303
304 // PR6885
305 namespace test9 {
306 class B {
307 friend class test9;
308 };
309 }
310
311 // PR7230
312 namespace test10 {
313 extern "C" void test10_f(void);
314 extern "C" void test10_g(void);
315
316 namespace NS {
317 class C {
318 void foo(void); // expected-note {{declared private here}}
319 friend void test10::test10_f(void);
320 };
321 static C* bar;
322 }
323
test10_f(void)324 void test10_f(void) {
325 NS::bar->foo();
326 }
327
test10_g(void)328 void test10_g(void) {
329 NS::bar->foo(); // expected-error {{private member}}
330 }
331 }
332
333 // PR8705
334 namespace test11 {
335 class A {
336 public:
337 void test0(int);
338 void test1(int);
339 void test2(int);
340 void test3(int);
341 };
342
343 class B {
344 typedef int private_type; // expected-note 2 {{implicitly declared private here}}
345 friend void A::test0(int);
346 friend void A::test1(int);
347 };
348
test0(B::private_type x)349 void A::test0(B::private_type x) {}
test1(int x=B::private_type ())350 void A::test1(int x = B::private_type()) {}
test2(B::private_type x)351 void A::test2(B::private_type x) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
test3(int x=B::private_type ())352 void A::test3(int x = B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
353 }
354
355
356 // PR9221
357 namespace test12 {
358 struct A {
359 void foo();
360 };
361 class B : private A {
362 friend void A::foo();
363 void *mem;
364 };
foo()365 void A::foo() {
366 void *var = static_cast<B*>(this)->mem;
367 }
368 }
369
370 namespace PR9103 {
371 struct base {
372 protected:
fooPR9103::base373 static void foo(void) {}
374 };
375
376 struct cls: base {
bar(void)377 friend void bar(void) {
378 base::foo();
379 }
380 };
381 }
382
383 // PR13642. When computing the effective context, we were walking up
384 // the DC chain for the canonical decl, which is unfortunate if that's
385 // (e.g.) a friend declaration.
386 namespace test14 {
387 class A {
388 class B { // expected-note {{implicitly declared private here}}
389 static int i;
390 friend void c();
391 };
392 };
393
c()394 void c() {
395 A::B::i = 5; // expected-error {{'B' is a private member of 'test14::A'}}
396 }
397 }
398