p4.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
  2. // C++0x [class.access]p4:
  3. // Access control is applied uniformly to all names, whether the
  4. // names are referred to from declarations or expressions. In the
  5. // case of overloaded function names, access control is applied to
  6. // the function selected by overload resolution.
  7. class Public {} PublicInst;
  8. class Protected {} ProtectedInst;
  9. class Private {} PrivateInst;
  10. namespace test0 {
  11. class A {
  12. public:
  13. void foo(Public&);
  14. protected:
  15. void foo(Protected&); // expected-note 2 {{declared protected here}}
  16. private:
  17. void foo(Private&); // expected-note 2 {{declared private here}}
  18. };
  19. void test(A *op) {
  20. op->foo(PublicInst);
  21. op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
  22. op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
  23. void (A::*a)(Public&) = &A::foo;
  24. void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}}
  25. void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}}
  26. }
  27. }
  28. // Member operators.
  29. namespace test1 {
  30. class A {
  31. public:
  32. void operator+(Public&);
  33. void operator[](Public&);
  34. void operator()(Public&);
  35. typedef void (*PublicSurrogate)(Public&);
  36. operator PublicSurrogate() const;
  37. protected:
  38. void operator+(Protected&); // expected-note {{declared protected here}}
  39. void operator[](Protected&); // expected-note {{declared protected here}}
  40. void operator()(Protected&); // expected-note {{declared protected here}}
  41. typedef void (*ProtectedSurrogate)(Protected&);
  42. operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
  43. private:
  44. void operator+(Private&); // expected-note {{declared private here}}
  45. void operator[](Private&); // expected-note {{declared private here}}
  46. void operator()(Private&); // expected-note {{declared private here}}
  47. void operator-(); // expected-note {{declared private here}}
  48. typedef void (*PrivateSurrogate)(Private&);
  49. operator PrivateSurrogate() const; // expected-note {{declared private here}}
  50. };
  51. void operator+(const A &, Public&);
  52. void operator+(const A &, Protected&);
  53. void operator+(const A &, Private&);
  54. void operator-(const A &);
  55. void test(A &a, Public &pub, Protected &prot, Private &priv) {
  56. a + pub;
  57. a + prot; // expected-error {{'operator+' is a protected member}}
  58. a + priv; // expected-error {{'operator+' is a private member}}
  59. a[pub];
  60. a[prot]; // expected-error {{'operator[]' is a protected member}}
  61. a[priv]; // expected-error {{'operator[]' is a private member}}
  62. a(pub);
  63. a(prot); // expected-error {{'operator()' is a protected member}}
  64. a(priv); // expected-error {{'operator()' is a private member}}
  65. -a; // expected-error {{'operator-' is a private member}}
  66. const A &ca = a;
  67. ca + pub;
  68. ca + prot;
  69. ca + priv;
  70. -ca;
  71. // These are all surrogate calls
  72. ca(pub);
  73. ca(prot); // expected-error {{'operator void (*)(Protected &)' is a protected member}}
  74. ca(priv); // expected-error {{'operator void (*)(Private &)' is a private member}}
  75. }
  76. }
  77. // Implicit constructor calls.
  78. namespace test2 {
  79. class A {
  80. private:
  81. A(); // expected-note 3 {{declared private here}}
  82. static A foo;
  83. };
  84. A a; // expected-error {{calling a private constructor}}
  85. A A::foo; // okay
  86. class B : A { }; // expected-error {{base class 'test2::A' has private default constructor}}
  87. B b; // expected-note{{implicit default constructor}}
  88. class C : virtual A {
  89. public:
  90. C();
  91. };
  92. class D : C { }; // expected-error {{inherited virtual base class 'test2::A' has private default constructor}}
  93. D d; // expected-note{{implicit default constructor}}
  94. }
  95. // Implicit destructor calls.
  96. namespace test3 {
  97. class A {
  98. private:
  99. ~A(); // expected-note 2 {{declared private here}}
  100. static A foo;
  101. };
  102. A a; // expected-error {{variable of type 'test3::A' has private destructor}}
  103. A A::foo;
  104. void foo(A param) { // okay
  105. A local; // expected-error {{variable of type 'test3::A' has private destructor}}
  106. }
  107. template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}}
  108. class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \
  109. // expected-error {{base class 'Base<2>' has private destructor}}
  110. class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}}
  111. // These don't cause diagnostics because we don't need the destructor.
  112. class Derived0 : Base<0> { ~Derived0(); };
  113. class Derived1 : Base<1> { };
  114. class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
  115. // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
  116. Base<0>, // expected-error {{base class 'Base<0>' has private destructor}}
  117. virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
  118. Base2, // expected-error {{base class 'test3::Base2' has private destructor}}
  119. virtual Base3
  120. {
  121. ~Derived2() {}
  122. };
  123. class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \
  124. // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} \
  125. // expected-note 2{{implicit default constructor}}
  126. Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}}
  127. virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}}
  128. Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}}
  129. virtual Base3
  130. {};
  131. Derived3 d3; // expected-note {{implicit default constructor}}\
  132. // expected-note{{implicit destructor}}}
  133. }
  134. // Conversion functions.
  135. namespace test4 {
  136. class Base {
  137. private:
  138. operator Private(); // expected-note 4 {{declared private here}}
  139. public:
  140. operator Public(); // expected-note 2{{member is declared here}}
  141. };
  142. class Derived1 : private Base { // expected-note 2 {{declared private here}} \
  143. // expected-note {{constrained by private inheritance}}
  144. Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
  145. Public test2() { return *this; }
  146. };
  147. Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \
  148. // expected-error {{cannot cast 'test4::Derived1' to its private base class}}
  149. Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \
  150. // expected-error {{'operator Public' is a private member}}
  151. class Derived2 : public Base {
  152. Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
  153. Public test2() { return *this; }
  154. };
  155. Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
  156. Public test2(Derived2 &d) { return d; }
  157. class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \
  158. // expected-note {{declared private here}}
  159. public:
  160. operator Private();
  161. };
  162. Private test1(Derived3 &d) { return d; }
  163. Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \
  164. // expected-error {{cannot cast 'test4::Derived3' to its private base class}}
  165. class Derived4 : public Base {
  166. public:
  167. operator Private();
  168. };
  169. Private test1(Derived4 &d) { return d; }
  170. Public test2(Derived4 &d) { return d; }
  171. }
  172. // Implicit copy assignment operator uses.
  173. namespace test5 {
  174. class A {
  175. void operator=(const A &); // expected-note 2 {{implicitly declared private here}}
  176. };
  177. class Test1 { A a; }; // expected-error {{private member}}
  178. void test1() {
  179. Test1 a;
  180. a = Test1(); // expected-note{{implicit copy}}
  181. }
  182. class Test2 : A {}; // expected-error {{private member}}
  183. void test2() {
  184. Test2 a;
  185. a = Test2(); // expected-note{{implicit copy}}
  186. }
  187. }
  188. // Implicit copy constructor uses.
  189. namespace test6 {
  190. class A {
  191. public: A();
  192. private: A(const A &); // expected-note 2 {{declared private here}}
  193. };
  194. class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}}
  195. void test1(const Test1 &t) {
  196. Test1 a = t; // expected-note{{implicit copy}}
  197. }
  198. class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}}
  199. void test2(const Test2 &t) {
  200. Test2 a = t; // expected-note{{implicit copy}}
  201. }
  202. }
  203. // Redeclaration lookups are not accesses.
  204. namespace test7 {
  205. class A {
  206. int private_member;
  207. };
  208. class B : A {
  209. int foo(int private_member) {
  210. return 0;
  211. }
  212. };
  213. }
  214. // Ignored operator new and delete overloads are not
  215. namespace test8 {
  216. typedef __typeof__(sizeof(int)) size_t;
  217. class A {
  218. void *operator new(size_t s);
  219. void operator delete(void *p);
  220. public:
  221. void *operator new(size_t s, int n);
  222. void operator delete(void *p, int n);
  223. };
  224. void test() {
  225. new (2) A();
  226. }
  227. }
  228. // Don't silently upgrade forbidden-access paths to private.
  229. namespace test9 {
  230. class A {
  231. public: static int x; // expected-note {{member is declared here}}
  232. };
  233. class B : private A { // expected-note {{constrained by private inheritance here}}
  234. };
  235. class C : public B {
  236. static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}}
  237. };
  238. }
  239. namespace test10 {
  240. class A {
  241. enum {
  242. value = 10 // expected-note {{declared private here}}
  243. };
  244. friend class C;
  245. };
  246. class B {
  247. enum {
  248. value = A::value // expected-error {{'value' is a private member of 'test10::A'}}
  249. };
  250. };
  251. class C {
  252. enum {
  253. value = A::value
  254. };
  255. };
  256. }
  257. namespace test11 {
  258. class A {
  259. protected: virtual ~A();
  260. };
  261. class B : public A {
  262. ~B();
  263. };
  264. B::~B() {};
  265. }
  266. namespace test12 {
  267. class A {
  268. int x;
  269. void foo() {
  270. class Local {
  271. int foo(A *a) {
  272. return a->x;
  273. }
  274. };
  275. }
  276. };
  277. }
  278. namespace test13 {
  279. struct A {
  280. int x;
  281. unsigned foo() const;
  282. };
  283. struct B : protected A {
  284. using A::foo;
  285. using A::x;
  286. };
  287. void test() {
  288. A *d;
  289. d->foo();
  290. (void) d->x;
  291. }
  292. }
  293. // Destructors for temporaries.
  294. namespace test14 {
  295. class A {
  296. private: ~A(); // expected-note {{declared private here}}
  297. };
  298. A foo();
  299. void test() {
  300. foo(); // expected-error {{temporary of type 'test14::A' has private destructor}}
  301. }
  302. class X {
  303. ~X(); // expected-note {{declared private here}}
  304. };
  305. struct Y1 {
  306. operator X();
  307. };
  308. void g() {
  309. const X &xr = Y1(); // expected-error{{temporary of type 'test14::X' has private destructor}}
  310. }
  311. }
  312. // PR 7024
  313. namespace test15 {
  314. template <class T> class A {
  315. private:
  316. int private_foo; // expected-note {{declared private here}}
  317. static int private_sfoo; // expected-note {{declared private here}}
  318. protected:
  319. int protected_foo; // expected-note 3 {{declared protected here}} // expected-note {{can only access this member on an object of type 'test15::B<int>'}}
  320. static int protected_sfoo; // expected-note 3 {{declared protected here}}
  321. int test1(A<int> &a) {
  322. return a.private_foo; // expected-error {{private member}}
  323. }
  324. int test2(A<int> &a) {
  325. return a.private_sfoo; // expected-error {{private member}}
  326. }
  327. int test3(A<int> &a) {
  328. return a.protected_foo; // expected-error {{protected member}}
  329. }
  330. int test4(A<int> &a) {
  331. return a.protected_sfoo; // expected-error {{protected member}}
  332. }
  333. };
  334. template class A<int>;
  335. template class A<long>; // expected-note 4 {{in instantiation}}
  336. template <class T> class B : public A<T> {
  337. // TODO: These first two accesses can be detected as ill-formed at
  338. // definition time because they're member accesses and A<int> can't
  339. // be a subclass of B<T> for any T.
  340. int test1(A<int> &a) {
  341. return a.protected_foo; // expected-error 2 {{protected member}}
  342. }
  343. int test2(A<int> &a) {
  344. return a.protected_sfoo; // expected-error {{protected member}}
  345. }
  346. int test3(B<int> &b) {
  347. return b.protected_foo; // expected-error {{protected member}}
  348. }
  349. int test4(B<int> &b) {
  350. return b.protected_sfoo; // expected-error {{protected member}}
  351. }
  352. };
  353. template class B<int>; // expected-note {{in instantiation}}
  354. template class B<long>; // expected-note 4 {{in instantiation}}
  355. }
  356. // PR7281
  357. namespace test16 {
  358. class A { ~A(); }; // expected-note 2{{declared private here}}
  359. void b() { throw A(); } // expected-error{{temporary of type 'test16::A' has private destructor}} \
  360. // expected-error{{exception object of type 'test16::A' has private destructor}}
  361. }
  362. // rdar://problem/8146294
  363. namespace test17 {
  364. class A {
  365. template <typename T> class Inner { }; // expected-note {{declared private here}}
  366. };
  367. A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}}
  368. }
  369. namespace test18 {
  370. template <class T> class A {};
  371. class B : A<int> {
  372. A<int> member;
  373. };
  374. // FIXME: this access to A should be forbidden (because C++ is dumb),
  375. // but LookupResult can't express the necessary information to do
  376. // the check, so we aggressively suppress access control.
  377. class C : B {
  378. A<int> member;
  379. };
  380. }
  381. // PR8325
  382. namespace test19 {
  383. class A { ~A(); };
  384. // The destructor is not implicitly referenced here. Contrast to test16,
  385. // testing PR7281, earlier in this file.
  386. void b(A* x) { throw x; }
  387. }
  388. // PR7930
  389. namespace test20 {
  390. class Foo {
  391. Foo(); // expected-note {{implicitly declared private here}}
  392. };
  393. Foo::Foo() {}
  394. void test() {
  395. Foo a; // expected-error {{calling a private constructor}}
  396. }
  397. }
  398. namespace test21 {
  399. template <class T> class A {
  400. void foo();
  401. void bar();
  402. class Inner; // expected-note {{implicitly declared private here}}
  403. public:
  404. void baz();
  405. };
  406. template <class T> class A<T>::Inner {};
  407. class B {
  408. template <class T> class A<T>::Inner; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}}
  409. };
  410. void test() {
  411. A<int>::Inner i; // expected-error {{'Inner' is a private member}}
  412. }
  413. }
  414. namespace rdar8876150 {
  415. struct A { operator bool(); };
  416. struct B : private A { using A::operator bool; };
  417. bool f() {
  418. B b;
  419. return !b;
  420. }
  421. }
  422. namespace test23 {
  423. template <typename T> class A {
  424. A();
  425. static A instance;
  426. };
  427. template <typename T> A<T> A<T>::instance;
  428. template class A<int>;
  429. }