p4.cpp revision b8592063413d277f6583715c9a890bd58440c1d1
17002f4c03c2d0544f4e8bea8d3a5636519081e35John McCall// RUN: %clang_cc1 -fsyntax-only -verify %s 2c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 3c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// C++0x [class.access]p4: 4c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 5c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// Access control is applied uniformly to all names, whether the 6c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// names are referred to from declarations or expressions. In the 7c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// case of overloaded function names, access control is applied to 8c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// the function selected by overload resolution. 9c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 10c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Public {} PublicInst; 11c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Protected {} ProtectedInst; 12c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallclass Private {} PrivateInst; 13c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 14c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallnamespace test0 { 15c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall class A { 16c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall public: 17c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall void foo(Public&); 18c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall protected: 19c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall void foo(Protected&); // expected-note 2 {{declared protected here}} 20c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall private: 21c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall void foo(Private&); // expected-note 2 {{declared private here}} 22c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall }; 23c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 24c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall void test(A *op) { 25c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall op->foo(PublicInst); 266b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}} 276b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall op->foo(PrivateInst); // expected-error {{'foo' is a private member}} 28c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall 29c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall void (A::*a)(Public&) = &A::foo; 306b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}} 316b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}} 32c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall } 33c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall} 345357b615364c17ea024c757354c58ae2a520d216John McCall 355357b615364c17ea024c757354c58ae2a520d216John McCall// Member operators. 365357b615364c17ea024c757354c58ae2a520d216John McCallnamespace test1 { 375357b615364c17ea024c757354c58ae2a520d216John McCall class A { 385357b615364c17ea024c757354c58ae2a520d216John McCall public: 395357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(Public&); 405357b615364c17ea024c757354c58ae2a520d216John McCall void operator[](Public&); 4141d8903731782ee85ee2b19734008b006e01c76fJohn McCall void operator()(Public&); 42233a6419097ed97b67ff8efcacef9af613262ca3John McCall typedef void (*PublicSurrogate)(Public&); 43233a6419097ed97b67ff8efcacef9af613262ca3John McCall operator PublicSurrogate() const; 445357b615364c17ea024c757354c58ae2a520d216John McCall protected: 455357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(Protected&); // expected-note {{declared protected here}} 465357b615364c17ea024c757354c58ae2a520d216John McCall void operator[](Protected&); // expected-note {{declared protected here}} 4741d8903731782ee85ee2b19734008b006e01c76fJohn McCall void operator()(Protected&); // expected-note {{declared protected here}} 48233a6419097ed97b67ff8efcacef9af613262ca3John McCall typedef void (*ProtectedSurrogate)(Protected&); 49233a6419097ed97b67ff8efcacef9af613262ca3John McCall operator ProtectedSurrogate() const; // expected-note {{declared protected here}} 505357b615364c17ea024c757354c58ae2a520d216John McCall private: 515357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(Private&); // expected-note {{declared private here}} 525357b615364c17ea024c757354c58ae2a520d216John McCall void operator[](Private&); // expected-note {{declared private here}} 5341d8903731782ee85ee2b19734008b006e01c76fJohn McCall void operator()(Private&); // expected-note {{declared private here}} 545357b615364c17ea024c757354c58ae2a520d216John McCall void operator-(); // expected-note {{declared private here}} 55233a6419097ed97b67ff8efcacef9af613262ca3John McCall typedef void (*PrivateSurrogate)(Private&); 56233a6419097ed97b67ff8efcacef9af613262ca3John McCall operator PrivateSurrogate() const; // expected-note {{declared private here}} 575357b615364c17ea024c757354c58ae2a520d216John McCall }; 585357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(const A &, Public&); 595357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(const A &, Protected&); 605357b615364c17ea024c757354c58ae2a520d216John McCall void operator+(const A &, Private&); 615357b615364c17ea024c757354c58ae2a520d216John McCall void operator-(const A &); 625357b615364c17ea024c757354c58ae2a520d216John McCall 635357b615364c17ea024c757354c58ae2a520d216John McCall void test(A &a, Public &pub, Protected &prot, Private &priv) { 645357b615364c17ea024c757354c58ae2a520d216John McCall a + pub; 656b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a + prot; // expected-error {{'operator+' is a protected member}} 666b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a + priv; // expected-error {{'operator+' is a private member}} 675357b615364c17ea024c757354c58ae2a520d216John McCall a[pub]; 686b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a[prot]; // expected-error {{'operator[]' is a protected member}} 696b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a[priv]; // expected-error {{'operator[]' is a private member}} 7041d8903731782ee85ee2b19734008b006e01c76fJohn McCall a(pub); 716b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a(prot); // expected-error {{'operator()' is a protected member}} 726b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall a(priv); // expected-error {{'operator()' is a private member}} 736b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall -a; // expected-error {{'operator-' is a private member}} 745357b615364c17ea024c757354c58ae2a520d216John McCall 755357b615364c17ea024c757354c58ae2a520d216John McCall const A &ca = a; 765357b615364c17ea024c757354c58ae2a520d216John McCall ca + pub; 775357b615364c17ea024c757354c58ae2a520d216John McCall ca + prot; 785357b615364c17ea024c757354c58ae2a520d216John McCall ca + priv; 795357b615364c17ea024c757354c58ae2a520d216John McCall -ca; 80233a6419097ed97b67ff8efcacef9af613262ca3John McCall // These are all surrogate calls 81233a6419097ed97b67ff8efcacef9af613262ca3John McCall ca(pub); 826b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall ca(prot); // expected-error {{'operator void (*)(class Protected &)' is a protected member}} 836b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall ca(priv); // expected-error {{'operator void (*)(class Private &)' is a private member}} 845357b615364c17ea024c757354c58ae2a520d216John McCall } 855357b615364c17ea024c757354c58ae2a520d216John McCall} 864f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 874f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall// Implicit constructor calls. 884f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallnamespace test2 { 894f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall class A { 904f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall private: 919a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson A(); // expected-note 3 {{declared private here}} 924f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 934f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall static A foo; 944f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall }; 954f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 966b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall A a; // expected-error {{calling a private constructor}} 974f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall A A::foo; // okay 989a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson 999a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson class B : A { }; // expected-error {{base class 'test2::A' has private constructor}} 100c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor B b; // expected-note{{implicit default constructor}} 1019a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson 1029a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson class C : virtual A { 1039a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson public: 1049a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson C(); 1059a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson }; 106711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson 107711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson class D : C { }; // expected-error {{inherited virtual base class 'test2::A' has private constructor}} 108c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor D d; // expected-note{{implicit default constructor}} 1094f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall} 1104f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 1114f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall// Implicit destructor calls. 1124f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallnamespace test3 { 11358e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall class A { 1144f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall private: 1159c127392efe91dadacbe28ca16b8a9a5fa7990b3Douglas Gregor ~A(); // expected-note 2 {{declared private here}} 1164f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall static A foo; 1174f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall }; 1184f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 11958e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall A a; // expected-error {{variable of type 'test3::A' has private destructor}} 1204f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall A A::foo; 1214f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall 1229c127392efe91dadacbe28ca16b8a9a5fa7990b3Douglas Gregor void foo(A param) { // okay 12358e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall A local; // expected-error {{variable of type 'test3::A' has private destructor}} 1244f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall } 12558e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall 126ef027fe748894522653558d9475a220482395094John McCall template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}} 127ef027fe748894522653558d9475a220482395094John McCall class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \ 128ef027fe748894522653558d9475a220482395094John McCall // expected-error {{base class 'Base<2>' has private destructor}} 129ef027fe748894522653558d9475a220482395094John McCall class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}} 13058e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall 13158e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall // These don't cause diagnostics because we don't need the destructor. 13258e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall class Derived0 : Base<0> { ~Derived0(); }; 13358e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall class Derived1 : Base<1> { }; 13458e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall 13558e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \ 13658e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall // expected-error {{inherited virtual base class 'Base<3>' has private destructor}} 13758e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall Base<0>, // expected-error {{base class 'Base<0>' has private destructor}} 13858e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}} 13958e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall Base2, // expected-error {{base class 'test3::Base2' has private destructor}} 14058e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall virtual Base3 14158e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall { 14258e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall ~Derived2() {} 14358e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall }; 1446c790eac94101407acfd2c664400924cab45c0b1John McCall 145ef027fe748894522653558d9475a220482395094John McCall class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \ 146c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} \ 147c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor // expected-note 2{{implicit default constructor}} 148ef027fe748894522653558d9475a220482395094John McCall Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}} 149ef027fe748894522653558d9475a220482395094John McCall virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}} 150ef027fe748894522653558d9475a220482395094John McCall Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}} 1516c790eac94101407acfd2c664400924cab45c0b1John McCall virtual Base3 152c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor {}; 153c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor Derived3 d3; // expected-note {{implicit default constructor}}\ 154c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor // expected-note{{implicit default destructor}}} 1554f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall} 156b05b5f35f114505182b076aa70002843c0669bebJohn McCall 157b05b5f35f114505182b076aa70002843c0669bebJohn McCall// Conversion functions. 158b05b5f35f114505182b076aa70002843c0669bebJohn McCallnamespace test4 { 159b05b5f35f114505182b076aa70002843c0669bebJohn McCall class Base { 160b05b5f35f114505182b076aa70002843c0669bebJohn McCall private: 161b05b5f35f114505182b076aa70002843c0669bebJohn McCall operator Private(); // expected-note 4 {{declared private here}} 162b05b5f35f114505182b076aa70002843c0669bebJohn McCall public: 16376ef658c703faf72c00f324fb9edc03169718e3eDouglas Gregor operator Public(); // expected-note 2{{member is declared here}} 164b05b5f35f114505182b076aa70002843c0669bebJohn McCall }; 165b05b5f35f114505182b076aa70002843c0669bebJohn McCall 166b05b5f35f114505182b076aa70002843c0669bebJohn McCall class Derived1 : private Base { // expected-note 2 {{declared private here}} \ 167b05b5f35f114505182b076aa70002843c0669bebJohn McCall // expected-note {{constrained by private inheritance}} 168b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} 169b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2() { return *this; } 170b05b5f35f114505182b076aa70002843c0669bebJohn McCall }; 171b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \ 172b05b5f35f114505182b076aa70002843c0669bebJohn McCall // expected-error {{cannot cast 'test4::Derived1' to its private base class}} 173b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \ 174b05b5f35f114505182b076aa70002843c0669bebJohn McCall // expected-error {{'operator Public' is a private member}} 175b05b5f35f114505182b076aa70002843c0669bebJohn McCall 176b05b5f35f114505182b076aa70002843c0669bebJohn McCall 177b05b5f35f114505182b076aa70002843c0669bebJohn McCall class Derived2 : public Base { 178b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} 179b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2() { return *this; } 180b05b5f35f114505182b076aa70002843c0669bebJohn McCall }; 181b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}} 182b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2(Derived2 &d) { return d; } 183b05b5f35f114505182b076aa70002843c0669bebJohn McCall 184b05b5f35f114505182b076aa70002843c0669bebJohn McCall class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \ 185b05b5f35f114505182b076aa70002843c0669bebJohn McCall // expected-note {{declared private here}} 186b05b5f35f114505182b076aa70002843c0669bebJohn McCall public: 187b05b5f35f114505182b076aa70002843c0669bebJohn McCall operator Private(); 188b05b5f35f114505182b076aa70002843c0669bebJohn McCall }; 189b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1(Derived3 &d) { return d; } 190b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \ 191b05b5f35f114505182b076aa70002843c0669bebJohn McCall // expected-error {{cannot cast 'test4::Derived3' to its private base class}} 192b05b5f35f114505182b076aa70002843c0669bebJohn McCall 193b05b5f35f114505182b076aa70002843c0669bebJohn McCall class Derived4 : public Base { 194b05b5f35f114505182b076aa70002843c0669bebJohn McCall public: 195b05b5f35f114505182b076aa70002843c0669bebJohn McCall operator Private(); 196b05b5f35f114505182b076aa70002843c0669bebJohn McCall }; 197b05b5f35f114505182b076aa70002843c0669bebJohn McCall Private test1(Derived4 &d) { return d; } 198b05b5f35f114505182b076aa70002843c0669bebJohn McCall Public test2(Derived4 &d) { return d; } 199b05b5f35f114505182b076aa70002843c0669bebJohn McCall} 200b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 201b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall// Implicit copy assignment operator uses. 202b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCallnamespace test5 { 203b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall class A { 204b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall void operator=(const A &); // expected-note 2 {{declared private here}} 205b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall }; 206b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 20706a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor class Test1 { A a; }; // expected-error {{private member}} 208b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall void test1() { 209b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall Test1 a; 210c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor a = Test1(); // expected-note{{implicit default copy}} 211b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall } 212b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 21306a9f3680d22529a2fcf20c52d71cf221d99d910Douglas Gregor class Test2 : A {}; // expected-error {{private member}} 214b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall void test2() { 215b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall Test2 a; 216c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor a = Test2(); // expected-note{{implicit default copy}} 217b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall } 218b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall} 219b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 220b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall// Implicit copy constructor uses. 221b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCallnamespace test6 { 222b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall class A { 223b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall public: A(); 224b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall private: A(const A &); // expected-note 2 {{declared private here}} 225b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall }; 226b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 227b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}} 228b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall void test1(const Test1 &t) { 229c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor Test1 a = t; // expected-note{{implicit default copy}} 230b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall } 231b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall 232b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}} 233b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall void test2(const Test2 &t) { 234c63d2c8469d6b96712b324f76b4af07e1852313fDouglas Gregor Test2 a = t; // expected-note{{implicit default copy}} 235b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall } 236b020748a9954c995f2e616f50bb9ed4fe2df1f72John McCall} 23710f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall 23810f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall// Redeclaration lookups are not accesses. 23910f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCallnamespace test7 { 24010f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall class A { 24110f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall int private_member; 24210f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall }; 24310f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall class B : A { 24410f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall int foo(int private_member) { 24510f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall return 0; 24610f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall } 24710f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall }; 24810f2873c0df7f662bfdb9a3e8bc834b68c1ead48John McCall} 24990c8c57bcd84083df85f76aac2aa62acb85eb077John McCall 25090c8c57bcd84083df85f76aac2aa62acb85eb077John McCall// Ignored operator new and delete overloads are not 25190c8c57bcd84083df85f76aac2aa62acb85eb077John McCallnamespace test8 { 25290c8c57bcd84083df85f76aac2aa62acb85eb077John McCall typedef __typeof__(sizeof(int)) size_t; 25390c8c57bcd84083df85f76aac2aa62acb85eb077John McCall 25490c8c57bcd84083df85f76aac2aa62acb85eb077John McCall class A { 25590c8c57bcd84083df85f76aac2aa62acb85eb077John McCall void *operator new(size_t s); 25690c8c57bcd84083df85f76aac2aa62acb85eb077John McCall void operator delete(void *p); 25790c8c57bcd84083df85f76aac2aa62acb85eb077John McCall public: 25890c8c57bcd84083df85f76aac2aa62acb85eb077John McCall void *operator new(size_t s, int n); 25990c8c57bcd84083df85f76aac2aa62acb85eb077John McCall void operator delete(void *p, int n); 26090c8c57bcd84083df85f76aac2aa62acb85eb077John McCall }; 26190c8c57bcd84083df85f76aac2aa62acb85eb077John McCall 26290c8c57bcd84083df85f76aac2aa62acb85eb077John McCall void test() { 26390c8c57bcd84083df85f76aac2aa62acb85eb077John McCall new (2) A(); 26490c8c57bcd84083df85f76aac2aa62acb85eb077John McCall } 26590c8c57bcd84083df85f76aac2aa62acb85eb077John McCall} 2667aceaf8cee77c98478e8934dc283910292711a7eJohn McCall 2677aceaf8cee77c98478e8934dc283910292711a7eJohn McCall// Don't silently upgrade forbidden-access paths to private. 2687aceaf8cee77c98478e8934dc283910292711a7eJohn McCallnamespace test9 { 2697aceaf8cee77c98478e8934dc283910292711a7eJohn McCall class A { 27076ef658c703faf72c00f324fb9edc03169718e3eDouglas Gregor public: static int x; // expected-note {{member is declared here}} 2717aceaf8cee77c98478e8934dc283910292711a7eJohn McCall }; 2727aceaf8cee77c98478e8934dc283910292711a7eJohn McCall class B : private A { // expected-note {{constrained by private inheritance here}} 2737aceaf8cee77c98478e8934dc283910292711a7eJohn McCall }; 2747aceaf8cee77c98478e8934dc283910292711a7eJohn McCall class C : public B { 2757aceaf8cee77c98478e8934dc283910292711a7eJohn McCall static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}} 2767aceaf8cee77c98478e8934dc283910292711a7eJohn McCall }; 2777aceaf8cee77c98478e8934dc283910292711a7eJohn McCall} 278c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall 279c1b621daf98d83075a466c6f4ad9904dc845dd09John McCallnamespace test10 { 280c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall class A { 281c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall enum { 282c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall value = 10 // expected-note {{declared private here}} 283c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 284c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall friend class C; 285c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 286c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall 287c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall class B { 288c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall enum { 289c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall value = A::value // expected-error {{'value' is a private member of 'test10::A'}} 290c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 291c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 292c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall 293c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall class C { 294c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall enum { 295c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall value = A::value 296c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 297c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall }; 298c1b621daf98d83075a466c6f4ad9904dc845dd09John McCall} 29990f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall 30090f97892eb8b2ecfcf633c9df01e2504686d4d96John McCallnamespace test11 { 30190f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall class A { 30290f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall protected: virtual ~A(); 30390f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall }; 30490f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall 30590f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall class B : public A { 30690f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall ~B(); 30790f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall }; 30890f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall 30990f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall B::~B() {}; 31090f97892eb8b2ecfcf633c9df01e2504686d4d96John McCall} 3112cc2675d426af23476a9722c08c1b6c5266bd653John McCall 3122cc2675d426af23476a9722c08c1b6c5266bd653John McCallnamespace test12 { 3132cc2675d426af23476a9722c08c1b6c5266bd653John McCall class A { 3142cc2675d426af23476a9722c08c1b6c5266bd653John McCall int x; 3152cc2675d426af23476a9722c08c1b6c5266bd653John McCall 3162cc2675d426af23476a9722c08c1b6c5266bd653John McCall void foo() { 3172cc2675d426af23476a9722c08c1b6c5266bd653John McCall class Local { 3182cc2675d426af23476a9722c08c1b6c5266bd653John McCall int foo(A *a) { 3192cc2675d426af23476a9722c08c1b6c5266bd653John McCall return a->x; 3202cc2675d426af23476a9722c08c1b6c5266bd653John McCall } 3212cc2675d426af23476a9722c08c1b6c5266bd653John McCall }; 3222cc2675d426af23476a9722c08c1b6c5266bd653John McCall } 3232cc2675d426af23476a9722c08c1b6c5266bd653John McCall }; 3242cc2675d426af23476a9722c08c1b6c5266bd653John McCall} 3256bb8017bb9e828d118e15e59d71c66bba323c364John McCall 3266bb8017bb9e828d118e15e59d71c66bba323c364John McCallnamespace test13 { 3276bb8017bb9e828d118e15e59d71c66bba323c364John McCall struct A { 3286bb8017bb9e828d118e15e59d71c66bba323c364John McCall int x; 3296bb8017bb9e828d118e15e59d71c66bba323c364John McCall unsigned foo() const; 3306bb8017bb9e828d118e15e59d71c66bba323c364John McCall }; 3316bb8017bb9e828d118e15e59d71c66bba323c364John McCall 3326bb8017bb9e828d118e15e59d71c66bba323c364John McCall struct B : protected A { 3336bb8017bb9e828d118e15e59d71c66bba323c364John McCall using A::foo; 3346bb8017bb9e828d118e15e59d71c66bba323c364John McCall using A::x; 3356bb8017bb9e828d118e15e59d71c66bba323c364John McCall }; 3366bb8017bb9e828d118e15e59d71c66bba323c364John McCall 3376bb8017bb9e828d118e15e59d71c66bba323c364John McCall void test() { 3386bb8017bb9e828d118e15e59d71c66bba323c364John McCall A *d; 3396bb8017bb9e828d118e15e59d71c66bba323c364John McCall d->foo(); 3406bb8017bb9e828d118e15e59d71c66bba323c364John McCall (void) d->x; 3416bb8017bb9e828d118e15e59d71c66bba323c364John McCall } 3426bb8017bb9e828d118e15e59d71c66bba323c364John McCall} 343c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall 344c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall// Destructors for temporaries. 345c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCallnamespace test14 { 346c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall class A { 347c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall private: ~A(); // expected-note {{declared private here}} 348c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall }; 349c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall A foo(); 350c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall 351c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall void test() { 352c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall foo(); // expected-error {{temporary of type 'test14::A' has private destructor}} 353c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall } 3544154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor 3554154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor class X { 3564154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor ~X(); // expected-note {{declared private here}} 3574154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor }; 3584154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor 3594154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor struct Y1 { 3604154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor operator X(); 3614154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor }; 3624154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor 3634154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor void g() { 3644154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor const X &xr = Y1(); // expected-error{{temporary of type 'test14::X' has private destructor}} 3654154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor } 366c91cc66e92b084acd1fdbaa1c3c74242741b3d46John McCall} 3674154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor 36801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall// PR 7024 36901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCallnamespace test15 { 37001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template <class T> class A { 37101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall private: 37201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int private_foo; // expected-note {{declared private here}} 37301ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall static int private_sfoo; // expected-note {{declared private here}} 37401ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall protected: 37501ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int protected_foo; // expected-note 4 {{declared protected here}} 37601ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall static int protected_sfoo; // expected-note 3 {{declared protected here}} 37701ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 37801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test1(A<int> &a) { 37901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.private_foo; // expected-error {{private member}} 38001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 38101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 38201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test2(A<int> &a) { 38301ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.private_sfoo; // expected-error {{private member}} 38401ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 38501ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 38601ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test3(A<int> &a) { 38701ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.protected_foo; // expected-error {{protected member}} 38801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 38901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 39001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test4(A<int> &a) { 39101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.protected_sfoo; // expected-error {{protected member}} 39201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 39301ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall }; 39401ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 39501ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template class A<int>; 39601ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template class A<long>; // expected-note 4 {{in instantiation}} 39701ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 39801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template <class T> class B : public A<T> { 39901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall // TODO: These first two accesses can be detected as ill-formed at 40001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall // definition time because they're member accesses and A<int> can't 40101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall // be a subclass of B<T> for any T. 40201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 40301ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test1(A<int> &a) { 40401ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.protected_foo; // expected-error 2 {{protected member}} 40501ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 40601ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 40701ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test2(A<int> &a) { 40801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return a.protected_sfoo; // expected-error {{protected member}} 40901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 41001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 41101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test3(B<int> &b) { 41201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return b.protected_foo; // expected-error {{protected member}} 41301ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 41401ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 41501ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall int test4(B<int> &b) { 41601ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall return b.protected_sfoo; // expected-error {{protected member}} 41701ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall } 41801ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall }; 41901ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall 42001ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template class B<int>; // expected-note {{in instantiation}} 42101ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall template class B<long>; // expected-note 4 {{in instantiation}} 42201ebd9d1883e82c4188325900a4eb9c1e16353bbJohn McCall} 4235ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman 4245ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman// PR7281 4255ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedmannamespace test16 { 426ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor class A { ~A(); }; // expected-note 2{{declared private here}} 427ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor void b() { throw A(); } // expected-error{{temporary of type 'test16::A' has private destructor}} \ 428ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor // expected-error{{exception object of type 'test16::A' has private destructor}} 4295ed9b93c596c3926b6680b47de28c8ff6a8ff4b7Eli Friedman} 430b8592063413d277f6583715c9a890bd58440c1d1John McCall 431b8592063413d277f6583715c9a890bd58440c1d1John McCall// rdar://problem/8146294 432b8592063413d277f6583715c9a890bd58440c1d1John McCallnamespace test17 { 433b8592063413d277f6583715c9a890bd58440c1d1John McCall class A { 434b8592063413d277f6583715c9a890bd58440c1d1John McCall template <typename T> class Inner { }; // expected-note {{declared private here}} 435b8592063413d277f6583715c9a890bd58440c1d1John McCall }; 436b8592063413d277f6583715c9a890bd58440c1d1John McCall 437b8592063413d277f6583715c9a890bd58440c1d1John McCall A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}} 438b8592063413d277f6583715c9a890bd58440c1d1John McCall} 439