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}}
14 S
* g() { return 0; } // expected-note 2 {{'g' declared here}}
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'?}}
28 x
.g(); // expected-error{{no member named 'g' in 'X'}}
31 // Test that we recurse through namespaces to find already declared names, but
32 // new names are declared within the enclosing namespace.
39 friend struct S2
* g2();
42 struct S2
{ static void f2(); }; // expected-note 2 {{'S2' declared here}}
43 S2
* g2() { return 0; } // expected-note 2 {{'g2' declared here}}
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'?}}
52 x
.g(); // expected-error{{no member named 'g' in 'N::X'}}
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'}}
77 static void member(); // expected-note 2 {{declared private here}}
79 friend class ClassFriend
;
80 friend class UndeclaredClassFriend
;
82 friend void undeclared_test();
83 friend void declared_test();
84 friend void MemberFriend::test();
87 void declared_test() {
91 void undeclared_test() {
95 void unfriended_test() {
96 Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
99 void ClassFriend::test() {
103 void MemberFriend::test() {
107 class UndeclaredClassFriend
{
113 class ClassNonFriend
{
115 Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}}
120 // Make sure that friends have access to inherited protected members.
124 class ilist_half_node
{
125 friend struct ilist_walker_bad
;
128 X
*getPrev() { return Prev
; } // expected-note{{member is declared here}}
131 class ilist_node
: private ilist_half_node
{ // expected-note {{constrained by private inheritance here}}
132 friend struct ilist_walker
;
134 X
*getNext() { return Next
; } // expected-note {{declared private here}}
137 struct X
: ilist_node
{};
139 struct ilist_walker
{
140 static X
*getPrev(X
*N
) { return N
->getPrev(); }
141 static X
*getNext(X
*N
) { return N
->getNext(); }
144 struct ilist_walker_bad
{
145 static X
*getPrev(X
*N
) { return N
->getPrev(); } // \
146 // expected-error {{'getPrev' is a private member of 'test2::ilist_half_node'}}
148 static X
*getNext(X
*N
) { return N
->getNext(); } // \
149 // expected-error {{'getNext' is a private member of 'test2::ilist_node'}}
154 class A
{ protected: int x
; }; // expected-note {{declared protected here}}
164 int foo(const B
*p
) {
165 return p
->x
; // expected-error {{'x' is a protected member of 'test3::A'}}
170 class A
{ protected: int x
; };
176 int foo(B
* const p
) {
182 template <class T
> class Holder
{
184 friend bool operator==(Holder
&a
, Holder
&b
) {
185 return a
.object
== b
.object
; // expected-error {{invalid operands to binary expression}}
191 Holder
<Inequal
> a
, b
;
192 return a
== b
; // expected-note {{requested here}}
210 int test(A
*p
) { return p
->x
; }
221 #if __cplusplus >= 201103L
225 #if __cplusplus >= 201703L
231 #if __cplusplus >= 201402L
234 A
&A::operator=(const A
&)
235 #if __cplusplus >= 201703L
243 template <class T
> struct X
{
251 friend void X
<int>::foo();
254 #if __cplusplus >= 201103L
258 #if __cplusplus >= 201703L
264 A(); // expected-note 2 {{declared private here}}
267 template<> void X
<int>::foo() {
271 template<> void X
<int>::bar() {
272 A a
; // expected-error {{calling a private constructor}}
275 template<> X
<int>::X() {
279 template<> X
<int>::~X() {
280 A a
; // expected-error {{calling a private constructor}}
284 // Return types, parameters and default arguments to friend functions.
287 typedef int I
; // expected-note 6 {{declared private here}}
288 static const I x
= 0; // expected-note {{implicitly declared private here}}
290 template<typename T
> friend I
g(I i
);
294 A::I
f(A::I i
= A::x
) {}
295 template<typename T
> A::I
g(A::I i
) {
298 template A::I g
<A::I
>(A::I i
);
300 A::I
f2(A::I i
= A::x
) {} // expected-error 3 {{is a private member of}}
301 template<typename T
> A::I
g2(A::I i
) { // expected-error 2 {{is a private member of}}
304 template <> A::I g2
<char>(A::I i
) { return 0; } // OK
305 template A::I g2
<A::I
>(A::I i
); // OK
306 template <> A::I g2
<char>(A::I i
); // OK
307 template <> A::I g2
<A::I
*>(A::I i
); // OK
308 template A::I g2
<unsigned>(A::I i
); // OK
309 template int g2
<A::I
**>(int i
); // OK
310 template A::I g2
<A::I
***>(A::I i
); // OK
312 template <typename T
> A::I
g3(A::I i
) { return 0; } // expected-error 2 {{is a private member of}}
313 template <> int g3
<A::I
>(int i
); // OK
325 extern "C" void test10_f(void);
326 extern "C" void test10_g(void);
330 void foo(void); // expected-note {{declared private here}}
331 friend void test10::test10_f(void);
336 void test10_f(void) {
340 void test10_g(void) {
341 NS::bar
->foo(); // expected-error {{private member}}
356 typedef int private_type
; // expected-note 2 {{implicitly declared private here}}
357 friend void A::test0(int);
358 friend void A::test1(int);
361 void A::test0(B::private_type x
) {}
362 void A::test1(int x
= B::private_type()) {}
363 void A::test2(B::private_type x
) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
364 void A::test3(int x
= B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}}
373 class B
: private A
{
374 friend void A::foo();
378 void *var
= static_cast<B
*>(this)->mem
;
385 static void foo(void) {}
389 friend void bar(void) {
395 // PR13642. When computing the effective context, we were walking up
396 // the DC chain for the canonical decl, which is unfortunate if that's
397 // (e.g.) a friend declaration.
400 class B
{ // expected-note {{implicitly declared private here}}
407 A::B::i
= 5; // expected-error {{'B' is a private member of 'test14::A'}}