Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / class.access / p4.cpp
blobfef5b7aa88726ea5dfae7c68c817596b16a8c465
1 // RUN: %clang_cc1 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s
2 // RUN: %clang_cc1 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
3 // RUN: %clang_cc1 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
4 // RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-compatibility-version=19 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s
5 // RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-compatibility-version=19 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
6 // RUN: %clang_cc1 -triple x86_64-windows-msvc -fms-compatibility-version=19 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
8 // C++0x [class.access]p4:
10 // Access control is applied uniformly to all names, whether the
11 // names are referred to from declarations or expressions. In the
12 // case of overloaded function names, access control is applied to
13 // the function selected by overload resolution.
15 class Public {} PublicInst;
16 class Protected {} ProtectedInst;
17 class Private {} PrivateInst;
19 namespace test0 {
20 class A {
21 public:
22 void foo(Public&);
23 protected:
24 void foo(Protected&); // expected-note 2 {{declared protected here}}
25 private:
26 void foo(Private&); // expected-note 2 {{declared private here}}
29 void test(A *op) {
30 op->foo(PublicInst);
31 op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}}
32 op->foo(PrivateInst); // expected-error {{'foo' is a private member}}
34 void (A::*a)(Public&) = &A::foo;
35 void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}}
36 void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}}
40 // Member operators.
41 namespace test1 {
42 class A {
43 public:
44 void operator+(Public&);
45 void operator[](Public&);
46 void operator()(Public&);
47 typedef void (*PublicSurrogate)(Public&);
48 operator PublicSurrogate() const;
49 protected:
50 void operator+(Protected&); // expected-note {{declared protected here}}
51 void operator[](Protected&); // expected-note {{declared protected here}}
52 void operator()(Protected&); // expected-note {{declared protected here}}
53 typedef void (*ProtectedSurrogate)(Protected&);
54 operator ProtectedSurrogate() const; // expected-note {{declared protected here}}
55 private:
56 void operator+(Private&); // expected-note {{declared private here}}
57 void operator[](Private&); // expected-note {{declared private here}}
58 void operator()(Private&); // expected-note {{declared private here}}
59 void operator-(); // expected-note {{declared private here}}
60 typedef void (*PrivateSurrogate)(Private&);
61 operator PrivateSurrogate() const; // expected-note {{declared private here}}
63 void operator+(const A &, Public&);
64 void operator+(const A &, Protected&);
65 void operator+(const A &, Private&);
66 void operator-(const A &);
68 void test(A &a, Public &pub, Protected &prot, Private &priv) {
69 a + pub;
70 a + prot; // expected-error {{'operator+' is a protected member}}
71 a + priv; // expected-error {{'operator+' is a private member}}
72 a[pub];
73 a[prot]; // expected-error {{'operator[]' is a protected member}}
74 a[priv]; // expected-error {{'operator[]' is a private member}}
75 a(pub);
76 a(prot); // expected-error {{'operator()' is a protected member}}
77 a(priv); // expected-error {{'operator()' is a private member}}
78 -a; // expected-error {{'operator-' is a private member}}
80 const A &ca = a;
81 ca + pub;
82 ca + prot;
83 ca + priv;
84 -ca;
85 // These are all surrogate calls
86 ca(pub);
87 ca(prot); // expected-error {{'operator void (*)(Protected &)' is a protected member}}
88 ca(priv); // expected-error {{'operator void (*)(Private &)' is a private member}}
92 // Implicit constructor calls.
93 namespace test2 {
94 class A {
95 private:
96 A(); // expected-note 1+{{declared private here}}
98 static A foo;
101 A a; // expected-error {{calling a private constructor}}
102 A A::foo; // okay
104 #if __cplusplus < 201103L
105 class B : A { }; // expected-error {{base class 'A' has private default constructor}}
106 B b; // expected-note{{implicit default constructor}}
108 class C : virtual A {
109 public:
110 C();
113 class D : C { }; // expected-error {{inherited virtual base class 'A' has private default constructor}}
114 D d; // expected-note{{implicit default constructor}}
115 #else
116 class B : A { }; // expected-note {{base class 'A' has an inaccessible default constructor}}
117 B b; // expected-error {{call to implicitly-deleted default constructor}}
119 // FIXME: Do a better job of explaining how we get here from class D.
120 class C : virtual A { // expected-note {{default constructor of 'D' is implicitly deleted because base class 'A' has an inaccessible default constructor}}
121 public:
122 C();
125 class D : C { };
126 D d; // expected-error {{call to implicitly-deleted default constructor}}
127 #endif
130 // Implicit destructor calls.
131 namespace test3 {
132 class A {
133 private:
134 ~A(); // expected-note 2 {{declared private here}}
135 static A foo;
138 A a; // expected-error {{variable of type 'A' has private destructor}}
139 A A::foo;
141 void foo(A param) { // okay
142 A local; // expected-error {{variable of type 'A' has private destructor}}
145 #if __cplusplus < 201103L && !defined(_MSC_VER)
146 template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}}
147 class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \
148 // expected-error {{base class 'Base<2>' has private destructor}}
149 class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}}
151 // These don't cause diagnostics because we don't need the destructor.
152 class Derived0 : Base<0> { ~Derived0(); };
153 class Derived1 : Base<1> { };
155 class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
156 // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
157 Base<0>, // expected-error {{base class 'Base<0>' has private destructor}}
158 virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
159 Base2, // expected-error {{base class 'Base2' has private destructor}}
160 virtual Base3
162 ~Derived2() {}
165 class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \
166 // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} \
167 // expected-note 2{{implicit default constructor}}
168 Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}}
169 virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}}
170 Base2, // expected-error 2 {{base class 'Base2' has private destructor}}
171 virtual Base3
173 Derived3 d3; // expected-note{{implicit destructor}}} \
174 // expected-note 3 {{implicit default constructor}}
175 #elif __cplusplus < 201103L && defined(_MSC_VER)
176 template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}}
177 class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \
178 // expected-error {{base class 'Base<2>' has private destructor}}
179 class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}}
181 // These don't cause diagnostics because we don't need the destructor.
182 class Derived0 : Base<0> { ~Derived0(); };
183 class Derived1 : Base<1> { };
185 class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
186 // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
187 Base<0>, // expected-error {{base class 'Base<0>' has private destructor}}
188 virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
189 Base2, // expected-error {{base class 'Base2' has private destructor}}
190 virtual Base3
192 ~Derived2() {} // expected-note 2{{in implicit destructor}}
195 class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \
196 // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}}
197 Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}}
198 virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}}
199 Base2, // expected-error 2 {{base class 'Base2' has private destructor}}
200 virtual Base3
202 Derived3 d3; // expected-note{{implicit destructor}}} expected-note {{implicit default constructor}}
203 #elif __cplusplus >= 201103L && !defined(_MSC_VER)
204 template <unsigned N> class Base { ~Base(); }; // expected-note 4{{declared private here}}
205 class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 1{{declared private here}}
206 class Base3 : virtual Base<3> { public: ~Base3(); };
208 // These don't cause diagnostics because we don't need the destructor.
209 class Derived0 : Base<0> { ~Derived0(); };
210 class Derived1 : Base<1> { };
212 class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
213 // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
214 Base<0>, // expected-error {{base class 'Base<0>' has private destructor}}
215 virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
216 Base2, // expected-error {{base class 'Base2' has private destructor}}
217 virtual Base3
219 ~Derived2() {}
222 class Derived3 :
223 Base<0>, // expected-note {{deleted because base class 'Base<0>' has an inaccessible destructor}}
224 virtual Base<1>,
225 Base2,
226 virtual Base3
228 Derived3 d3; // expected-error {{implicitly-deleted default constructor}}
229 #elif __cplusplus >= 201103L && defined(_MSC_VER)
230 template <unsigned N> class Base { ~Base(); }; // expected-note 6{{declared private here}}
231 // expected-error@+1 {{inherited virtual base class 'Base<2>' has private destructor}}
232 class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 1{{declared private here}}
233 // expected-error@+1 {{inherited virtual base class 'Base<3>' has private destructor}}
234 class Base3 : virtual Base<3> { public: ~Base3(); };
236 // These don't cause diagnostics because we don't need the destructor.
237 class Derived0 : Base<0> { ~Derived0(); };
238 class Derived1 : Base<1> { };
240 class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \
241 // expected-error {{inherited virtual base class 'Base<3>' has private destructor}}
242 Base<0>, // expected-error {{base class 'Base<0>' has private destructor}}
243 virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}}
244 Base2, // expected-error {{base class 'Base2' has private destructor}}
245 virtual Base3
247 // expected-note@+2 {{in implicit destructor for 'test3::Base2' first required here}}
248 // expected-note@+1 {{in implicit destructor for 'test3::Base3' first required here}}
249 ~Derived2() {}
252 class Derived3 :
253 Base<0>, // expected-note {{deleted because base class 'Base<0>' has an inaccessible destructor}}
254 virtual Base<1>,
255 Base2,
256 virtual Base3
258 Derived3 d3; // expected-error {{implicitly-deleted default constructor}}
259 #else
260 #error "missing case of MSVC cross C++ versions"
261 #endif
264 // Conversion functions.
265 namespace test4 {
266 class Base {
267 private:
268 operator Private(); // expected-note 4 {{declared private here}}
269 public:
270 operator Public(); // expected-note 2{{member is declared here}}
273 class Derived1 : private Base { // expected-note {{constrained by private inheritance}}
274 Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
275 Public test2() { return *this; }
277 Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}}
278 Public test2(Derived1 &d) { return d; } // expected-error {{'operator Public' is a private member}}
281 class Derived2 : public Base {
282 Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
283 Public test2() { return *this; }
285 Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
286 Public test2(Derived2 &d) { return d; }
288 class Derived3 : private Base { // expected-note {{constrained by private inheritance here}}
289 public:
290 operator Private();
292 Private test1(Derived3 &d) { return d; }
293 Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}}
295 class Derived4 : public Base {
296 public:
297 operator Private();
299 Private test1(Derived4 &d) { return d; }
300 Public test2(Derived4 &d) { return d; }
303 // Implicit copy assignment operator uses.
304 namespace test5 {
305 class A {
306 void operator=(const A &);
307 #if __cplusplus < 201103L
308 // expected-note@-2 2{{implicitly declared private here}}
309 #endif
312 #if __cplusplus < 201103L
313 class Test1 { A a; }; // expected-error {{private member}}
314 void test1() {
315 Test1 a;
316 a = Test1(); // expected-note{{implicit copy}}
319 class Test2 : A {}; // expected-error {{private member}}
320 void test2() {
321 Test2 a;
322 a = Test2(); // expected-note{{implicit copy}}
324 #else
325 class Test1 { A a; }; // expected-note {{because field 'a' has an inaccessible copy assignment operator}}
326 void test1() {
327 Test1 a;
328 a = Test1(); // expected-error {{copy assignment operator is implicitly deleted}}
331 class Test2 : A {}; // expected-note {{because base class 'A' has an inaccessible copy assignment operator}}
332 void test2() {
333 Test2 a;
334 a = Test2(); // expected-error {{copy assignment operator is implicitly deleted}}
336 #endif
339 // Implicit copy constructor uses.
340 namespace test6 {
341 class A {
342 public: A();
343 private: A(const A &);
344 #if __cplusplus < 201103L
345 // expected-note@-2 2{{declared private here}}
346 #endif
349 #if __cplusplus < 201103L
350 class Test1 { A a; }; // expected-error {{field of type 'A' has private copy constructor}}
351 void test1(const Test1 &t) {
352 Test1 a = t; // expected-note{{implicit copy}}
355 class Test2 : A {}; // expected-error {{base class 'A' has private copy constructor}}
356 void test2(const Test2 &t) {
357 Test2 a = t; // expected-note{{implicit copy}}
359 #else
360 class Test1 { A a; }; // expected-note {{field 'a' has an inaccessible copy constructor}}
361 void test1(const Test1 &t) {
362 Test1 a = t; // expected-error{{implicitly-deleted}}
365 class Test2 : A {}; // expected-note {{base class 'A' has an inaccessible copy constructor}}
366 void test2(const Test2 &t) {
367 Test2 a = t; // expected-error{{implicitly-deleted}}
369 #endif
372 // Redeclaration lookups are not accesses.
373 namespace test7 {
374 class A {
375 int private_member;
377 class B : A {
378 int foo(int private_member) {
379 return 0;
384 // Ignored operator new and delete overloads are not
385 namespace test8 {
386 typedef __typeof__(sizeof(int)) size_t;
388 class A {
389 void *operator new(size_t s);
390 void operator delete(void *p);
391 public:
392 void *operator new(size_t s, int n);
393 void operator delete(void *p, int n);
396 void test() {
397 new (2) A();
401 // Don't silently upgrade forbidden-access paths to private.
402 namespace test9 {
403 class A {
404 public: static int x; // expected-note {{member is declared here}}
406 class B : private A { // expected-note {{constrained by private inheritance here}}
408 class C : public B {
409 static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}}
413 namespace test10 {
414 class A {
415 enum {
416 value = 10 // expected-note {{declared private here}}
418 friend class C;
421 class B {
422 enum {
423 value = A::value // expected-error {{'value' is a private member of 'test10::A'}}
427 class C {
428 enum {
429 value = A::value
434 namespace test11 {
435 class A {
436 protected: virtual ~A();
439 class B : public A {
440 ~B();
443 B::~B() {};
446 namespace test12 {
447 class A {
448 int x;
450 void foo() {
451 class Local {
452 int foo(A *a) {
453 return a->x;
460 namespace test13 {
461 struct A {
462 int x;
463 unsigned foo() const;
466 struct B : protected A {
467 using A::foo;
468 using A::x;
471 void test() {
472 A *d;
473 d->foo();
474 (void) d->x;
478 // Destructors for temporaries.
479 namespace test14 {
480 class A {
481 private: ~A(); // expected-note {{declared private here}}
483 A foo();
485 void test() {
486 foo(); // expected-error {{temporary of type 'A' has private destructor}}
489 class X {
490 ~X(); // expected-note {{declared private here}}
493 struct Y1 {
494 operator X();
497 void g() {
498 const X &xr = Y1(); // expected-error{{temporary of type 'X' has private destructor}}
502 // PR 7024
503 namespace test15 {
504 template <class T> class A {
505 private:
506 int private_foo; // expected-note {{declared private here}}
507 static int private_sfoo; // expected-note {{declared private here}}
508 protected:
509 int protected_foo; // expected-note 3 {{declared protected here}} // expected-note {{can only access this member on an object of type 'test15::B<int>'}}
510 static int protected_sfoo; // expected-note 3 {{declared protected here}}
512 int test1(A<int> &a) {
513 return a.private_foo; // expected-error {{private member}}
516 int test2(A<int> &a) {
517 return a.private_sfoo; // expected-error {{private member}}
520 int test3(A<int> &a) {
521 return a.protected_foo; // expected-error {{protected member}}
524 int test4(A<int> &a) {
525 return a.protected_sfoo; // expected-error {{protected member}}
529 template class A<int>;
530 template class A<long>; // expected-note 4 {{in instantiation}}
532 template <class T> class B : public A<T> {
533 // TODO: These first two accesses can be detected as ill-formed at
534 // definition time because they're member accesses and A<int> can't
535 // be a subclass of B<T> for any T.
537 int test1(A<int> &a) {
538 return a.protected_foo; // expected-error 2 {{protected member}}
541 int test2(A<int> &a) {
542 return a.protected_sfoo; // expected-error {{protected member}}
545 int test3(B<int> &b) {
546 return b.protected_foo; // expected-error {{protected member}}
549 int test4(B<int> &b) {
550 return b.protected_sfoo; // expected-error {{protected member}}
554 template class B<int>; // expected-note {{in instantiation}}
555 template class B<long>; // expected-note 4 {{in instantiation}}
558 // PR7281
559 namespace test16 {
560 class A { ~A(); }; // expected-note 2{{declared private here}}
561 void b() { throw A(); } // expected-error{{temporary of type 'A' has private destructor}} \
562 // expected-error{{exception object of type 'A' has private destructor}}
565 namespace test17 {
566 class A {
567 template <typename T> class Inner { }; // expected-note {{declared private here}}
570 A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}}
573 namespace test18 {
574 template <class T> class A {}; // expected-note {{member is declared here}}
575 class B : A<int> { // expected-note {{constrained by implicitly private inheritance here}}
576 A<int> member;
578 class C : B {
579 A<int> member; // expected-error {{'A' is a private member of 'test18::A<int>'}}
583 // PR8325
584 namespace test19 {
585 class A { ~A(); };
586 // The destructor is not implicitly referenced here. Contrast to test16,
587 // testing PR7281, earlier in this file.
588 void b(A* x) { throw x; }
591 // PR7930
592 namespace test20 {
593 class Foo {
594 Foo(); // expected-note {{implicitly declared private here}}
596 Foo::Foo() {}
598 void test() {
599 Foo a; // expected-error {{calling a private constructor}}
603 namespace test21 {
604 template <class T> class A {
605 void foo();
606 void bar();
607 class Inner; // expected-note {{implicitly declared private here}}
608 public:
609 void baz();
611 template <class T> class A<T>::Inner {};
612 class B {
613 template <class T> class A<T>::Inner; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}}
616 void test() {
617 A<int>::Inner i; // expected-error {{'Inner' is a private member}}
621 namespace rdar8876150 {
622 struct A { operator bool(); };
623 struct B : private A { using A::operator bool; };
625 bool f() {
626 B b;
627 return !b;
631 namespace test23 {
632 template <typename T> class A {
633 A();
634 static A instance;
637 template <typename T> A<T> A<T>::instance;
638 template class A<int>;