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
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.
13 struct S { static void f(); }; // expected-note 2 {{'S' declared here}}
18 friend S* g(); // expected-note 2 {{'g' declared here}}
19 // FIXME: The above two notes would be better attached to line 11.
26 X::g(); // expected-error{{no member named 'g' in 'X'; did you mean simply 'g'?}}
27 X::S x_s; // expected-error{{no type named 'S' in 'X'; did you mean simply 'S'?}}
29 x.g(); // expected-error{{no member named 'g' in 'X'}}
32 // Test that we recurse through namespaces to find already declared names, but
33 // new names are declared within the enclosing namespace.
40 friend struct S2* g2();
43 struct S2 { static void f2(); }; // expected-note 2 {{'S2' declared here}}
44 S2* g2() { return 0; } // expected-note 2 {{'g2' declared here}}
50 X::g(); // expected-error{{no member named 'g' in 'N::X'; did you mean simply 'g'?}}
51 X::S x_s; // expected-error{{no type named 'S' in 'N::X'; did you mean simply 'S'?}}
53 x.g(); // expected-error{{no member named 'g' in 'N::X'}}
57 ::g2(); // expected-error{{no member named 'g2' in the global namespace; did you mean simply 'g2'?}}
58 ::S2 g_s2; // expected-error{{no type named 'S2' in the global namespace; did you mean simply 'S2'?}}
59 X::g2(); // expected-error{{no member named 'g2' in 'N::X'; did you mean simply 'g2'?}}
60 X::S2 x_s2; // expected-error{{no type named 'S2' in 'N::X'; did you mean simply 'S2'?}}
61 x.g2(); // expected-error{{no member named 'g2' in 'N::X'}}
78 static void member(); // expected-note 2 {{declared private here}}
80 friend class ClassFriend;
81 friend class UndeclaredClassFriend;
83 friend void undeclared_test();
84 friend void declared_test();
85 friend void MemberFriend::test();
88 void declared_test() {
92 void undeclared_test() {
96 void unfriended_test() {
97 Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
100 void ClassFriend::test() {
104 void MemberFriend::test() {
108 class UndeclaredClassFriend {
114 class ClassNonFriend {
116 Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
121 // Make sure that friends have access to inherited protected members.
125 class ilist_half_node {
126 friend struct ilist_walker_bad;
129 X *getPrev() { return Prev; } // expected-note{{member is declared here}}
132 class ilist_node : private ilist_half_node { // expected-note {{declared private here}} expected-note {{constrained by private inheritance here}}
133 friend struct ilist_walker;
135 X *getNext() { return Next; } // expected-note {{declared private here}}
138 struct X : ilist_node {};
140 struct ilist_walker {
141 static X *getPrev(X *N) { return N->getPrev(); }
142 static X *getNext(X *N) { return N->getNext(); }
145 struct ilist_walker_bad {
146 static X *getPrev(X *N) { return N->getPrev(); } // \
147 // expected-error {{'getPrev' is a private member of 'test2::ilist_half_node'}} \
148 // expected-error {{cannot cast 'test2::X' to its private base class 'test2::ilist_half_node'}}
150 static X *getNext(X *N) { return N->getNext(); } // \
151 // expected-error {{'getNext' is a private member of 'test2::ilist_node'}}
156 class A { protected: int x; }; // expected-note {{declared protected here}}
166 int foo(const B *p) {
167 return p->x; // expected-error {{'x' is a protected member of 'test3::A'}}
172 class A { protected: int x; };
178 int foo(B * const p) {
184 template <class T> class Holder {
186 friend bool operator==(Holder &a, Holder &b) {
187 return a.object == b.object; // expected-error {{invalid operands to binary expression}}
193 Holder<Inequal> a, b;
194 return a == b; // expected-note {{requested here}}
212 int test(A *p) { return p->x; }
223 #if __cplusplus >= 201103L
229 #if __cplusplus >= 201402L
232 A &A::operator=(const A&);
237 template <class T> struct X {
245 friend void X<int>::foo();
248 #if __cplusplus >= 201103L
254 A(); // expected-note 2 {{declared private here}}
257 template<> void X<int>::foo() {
261 template<> void X<int>::bar() {
262 A a; // expected-error {{calling a private constructor}}
265 template<> X<int>::X() {
269 template<> X<int>::~X() {
270 A a; // expected-error {{calling a private constructor}}
274 // Return types, parameters and default arguments to friend functions.
277 typedef int I; // expected-note 4 {{declared private here}}
278 static const I x = 0; // expected-note {{implicitly declared private here}}
280 template<typename T> friend I g(I i);
284 A::I f(A::I i = A::x) {}
285 template<typename T> A::I g(A::I i) {
288 template A::I g<A::I>(A::I i);
290 A::I f2(A::I i = A::x) {} // expected-error 3 {{is a private member of}}
291 template<typename T> A::I g2(A::I i) { // expected-error 2 {{is a private member of}}
294 template A::I g2<A::I>(A::I i);
306 extern "C" void test10_f(void);
307 extern "C" void test10_g(void);
311 void foo(void); // expected-note {{declared private here}}
312 friend void test10::test10_f(void);
317 void test10_f(void) {
321 void test10_g(void) {
322 NS::bar->foo(); // expected-error {{private member}}
337 typedef int private_type; // expected-note 2 {{implicitly declared private here}}
338 friend void A::test0(int);
339 friend void A::test1(int);
342 void A::test0(B::private_type x) {}
343 void A::test1(int x = B::private_type()) {}
344 void A::test2(B::private_type x) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
345 void A::test3(int x = B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
354 class B : private A {
355 friend void A::foo();
359 void *var = static_cast<B*>(this)->mem;
366 static void foo(void) {}
370 friend void bar(void) {
376 // PR13642. When computing the effective context, we were walking up
377 // the DC chain for the canonical decl, which is unfortunate if that's
378 // (e.g.) a friend declaration.
381 class B { // expected-note {{implicitly declared private here}}
388 A::B::i = 5; // expected-error {{'B' is a private member of 'test14::A'}}