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