p4.cpp revision 9c127392efe91dadacbe28ca16b8a9a5fa7990b3
1// RUN: %clang_cc1 -fsyntax-only -faccess-control -verify %s 2 3// C++0x [class.access]p4: 4 5// Access control is applied uniformly to all names, whether the 6// names are referred to from declarations or expressions. In the 7// case of overloaded function names, access control is applied to 8// the function selected by overload resolution. 9 10class Public {} PublicInst; 11class Protected {} ProtectedInst; 12class Private {} PrivateInst; 13 14namespace test0 { 15 class A { 16 public: 17 void foo(Public&); 18 protected: 19 void foo(Protected&); // expected-note 2 {{declared protected here}} 20 private: 21 void foo(Private&); // expected-note 2 {{declared private here}} 22 }; 23 24 void test(A *op) { 25 op->foo(PublicInst); 26 op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}} 27 op->foo(PrivateInst); // expected-error {{'foo' is a private member}} 28 29 void (A::*a)(Public&) = &A::foo; 30 void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}} 31 void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}} 32 } 33} 34 35// Member operators. 36namespace test1 { 37 class A { 38 public: 39 void operator+(Public&); 40 void operator[](Public&); 41 void operator()(Public&); 42 typedef void (*PublicSurrogate)(Public&); 43 operator PublicSurrogate() const; 44 protected: 45 void operator+(Protected&); // expected-note {{declared protected here}} 46 void operator[](Protected&); // expected-note {{declared protected here}} 47 void operator()(Protected&); // expected-note {{declared protected here}} 48 typedef void (*ProtectedSurrogate)(Protected&); 49 operator ProtectedSurrogate() const; // expected-note {{declared protected here}} 50 private: 51 void operator+(Private&); // expected-note {{declared private here}} 52 void operator[](Private&); // expected-note {{declared private here}} 53 void operator()(Private&); // expected-note {{declared private here}} 54 void operator-(); // expected-note {{declared private here}} 55 typedef void (*PrivateSurrogate)(Private&); 56 operator PrivateSurrogate() const; // expected-note {{declared private here}} 57 }; 58 void operator+(const A &, Public&); 59 void operator+(const A &, Protected&); 60 void operator+(const A &, Private&); 61 void operator-(const A &); 62 63 void test(A &a, Public &pub, Protected &prot, Private &priv) { 64 a + pub; 65 a + prot; // expected-error {{'operator+' is a protected member}} 66 a + priv; // expected-error {{'operator+' is a private member}} 67 a[pub]; 68 a[prot]; // expected-error {{'operator[]' is a protected member}} 69 a[priv]; // expected-error {{'operator[]' is a private member}} 70 a(pub); 71 a(prot); // expected-error {{'operator()' is a protected member}} 72 a(priv); // expected-error {{'operator()' is a private member}} 73 -a; // expected-error {{'operator-' is a private member}} 74 75 const A &ca = a; 76 ca + pub; 77 ca + prot; 78 ca + priv; 79 -ca; 80 // These are all surrogate calls 81 ca(pub); 82 ca(prot); // expected-error {{'operator void (*)(class Protected &)' is a protected member}} 83 ca(priv); // expected-error {{'operator void (*)(class Private &)' is a private member}} 84 } 85} 86 87// Implicit constructor calls. 88namespace test2 { 89 class A { 90 private: 91 A(); // expected-note {{declared private here}} 92 93 static A foo; 94 }; 95 96 A a; // expected-error {{calling a private constructor}} 97 A A::foo; // okay 98} 99 100// Implicit destructor calls. 101namespace test3 { 102 class A { 103 private: 104 ~A(); // expected-note 2 {{declared private here}} 105 static A foo; 106 }; 107 108 A a; // expected-error {{variable of type 'test3::A' has private destructor}} 109 A A::foo; 110 111 void foo(A param) { // okay 112 A local; // expected-error {{variable of type 'test3::A' has private destructor}} 113 } 114 115 template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}} 116 class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \ 117 // expected-error {{base class 'Base<2>' has private destructor}} 118 class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}} 119 120 // These don't cause diagnostics because we don't need the destructor. 121 class Derived0 : Base<0> { ~Derived0(); }; 122 class Derived1 : Base<1> { }; 123 124 class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \ 125 // expected-error {{inherited virtual base class 'Base<3>' has private destructor}} 126 Base<0>, // expected-error {{base class 'Base<0>' has private destructor}} 127 virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}} 128 Base2, // expected-error {{base class 'test3::Base2' has private destructor}} 129 virtual Base3 130 { 131 ~Derived2() {} 132 }; 133 134 class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \ 135 // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} 136 Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}} 137 virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}} 138 Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}} 139 virtual Base3 140 {}; 141 Derived3 d3; 142} 143 144// Conversion functions. 145namespace test4 { 146 class Base { 147 private: 148 operator Private(); // expected-note 4 {{declared private here}} 149 public: 150 operator Public(); 151 }; 152 153 class Derived1 : private Base { // expected-note 2 {{declared private here}} \ 154 // expected-note {{constrained by private inheritance}} 155 Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} 156 Public test2() { return *this; } 157 }; 158 Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \ 159 // expected-error {{cannot cast 'test4::Derived1' to its private base class}} 160 Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \ 161 // expected-error {{'operator Public' is a private member}} 162 163 164 class Derived2 : public Base { 165 Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} 166 Public test2() { return *this; } 167 }; 168 Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}} 169 Public test2(Derived2 &d) { return d; } 170 171 class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \ 172 // expected-note {{declared private here}} 173 public: 174 operator Private(); 175 }; 176 Private test1(Derived3 &d) { return d; } 177 Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \ 178 // expected-error {{cannot cast 'test4::Derived3' to its private base class}} 179 180 class Derived4 : public Base { 181 public: 182 operator Private(); 183 }; 184 Private test1(Derived4 &d) { return d; } 185 Public test2(Derived4 &d) { return d; } 186} 187 188// Implicit copy assignment operator uses. 189namespace test5 { 190 class A { 191 void operator=(const A &); // expected-note 2 {{declared private here}} 192 }; 193 194 class Test1 { A a; }; // expected-error {{field of type 'test5::A' has private copy assignment operator}} 195 void test1() { 196 Test1 a; 197 a = Test1(); 198 } 199 200 class Test2 : A {}; // expected-error {{base class 'test5::A' has private copy assignment operator}} 201 void test2() { 202 Test2 a; 203 a = Test2(); 204 } 205} 206 207// Implicit copy constructor uses. 208namespace test6 { 209 class A { 210 public: A(); 211 private: A(const A &); // expected-note 2 {{declared private here}} 212 }; 213 214 class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}} 215 void test1(const Test1 &t) { 216 Test1 a = t; 217 } 218 219 class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}} 220 void test2(const Test2 &t) { 221 Test2 a = t; 222 } 223} 224 225// Redeclaration lookups are not accesses. 226namespace test7 { 227 class A { 228 int private_member; 229 }; 230 class B : A { 231 int foo(int private_member) { 232 return 0; 233 } 234 }; 235} 236 237// Ignored operator new and delete overloads are not 238namespace test8 { 239 typedef __typeof__(sizeof(int)) size_t; 240 241 class A { 242 void *operator new(size_t s); 243 void operator delete(void *p); 244 public: 245 void *operator new(size_t s, int n); 246 void operator delete(void *p, int n); 247 }; 248 249 void test() { 250 new (2) A(); 251 } 252} 253 254// Don't silently upgrade forbidden-access paths to private. 255namespace test9 { 256 class A { 257 public: static int x; 258 }; 259 class B : private A { // expected-note {{constrained by private inheritance here}} 260 }; 261 class C : public B { 262 static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}} 263 }; 264} 265 266namespace test10 { 267 class A { 268 enum { 269 value = 10 // expected-note {{declared private here}} 270 }; 271 friend class C; 272 }; 273 274 class B { 275 enum { 276 value = A::value // expected-error {{'value' is a private member of 'test10::A'}} 277 }; 278 }; 279 280 class C { 281 enum { 282 value = A::value 283 }; 284 }; 285} 286 287namespace test11 { 288 class A { 289 protected: virtual ~A(); 290 }; 291 292 class B : public A { 293 ~B(); 294 }; 295 296 B::~B() {}; 297} 298