Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / class.access / class.protected / p1.cpp
blob8c72387c6c21b3de6d36a19c89b94426e68df925
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
3 namespace test0 {
4 class A {
5 protected: int x; // expected-note 3 {{declared}} \
6 // expected-note {{member is declared here}}
7 static int sx; // expected-note 3 {{declared}} \
8 // expected-note {{member is declared here}}
9 };
10 class B : public A {
12 class C : protected A {
14 class D : private B { // expected-note 2 {{constrained}}
17 void test(A &a) {
18 (void) a.x; // expected-error {{'x' is a protected member}}
19 (void) a.sx; // expected-error {{'sx' is a protected member}}
21 void test(B &b) {
22 (void) b.x; // expected-error {{'x' is a protected member}}
23 (void) b.sx; // expected-error {{'sx' is a protected member}}
25 void test(C &c) {
26 (void) c.x; // expected-error {{'x' is a protected member}}
27 (void) c.sx; // expected-error {{'sx' is a protected member}}
29 void test(D &d) {
30 (void) d.x; // expected-error {{'x' is a private member}}
31 (void) d.sx; // expected-error {{'sx' is a private member}}
35 namespace test1 {
36 class A {
37 protected: int x;
38 static int sx;
39 static void test(A&);
41 class B : public A {
42 static void test(B&);
44 class C : protected A {
45 static void test(C&);
47 class D : private B {
48 static void test(D&);
51 void A::test(A &a) {
52 (void) a.x;
53 (void) a.sx;
55 void B::test(B &b) {
56 (void) b.x;
57 (void) b.sx;
59 void C::test(C &c) {
60 (void) c.x;
61 (void) c.sx;
63 void D::test(D &d) {
64 (void) d.x;
65 (void) d.sx;
69 namespace test2 {
70 class A {
71 protected: int x; // expected-note 3 {{can only access this member on an object of type}}
72 static int sx;
73 static void test(A&);
75 class B : public A {
76 static void test(A&);
78 class C : protected A {
79 static void test(A&);
81 class D : private B {
82 static void test(A&);
85 void A::test(A &a) {
86 (void) a.x;
87 (void) a.sx;
89 void B::test(A &a) {
90 (void) a.x; // expected-error {{'x' is a protected member}}
91 (void) a.sx;
93 void C::test(A &a) {
94 (void) a.x; // expected-error {{'x' is a protected member}}
95 (void) a.sx;
97 void D::test(A &a) {
98 (void) a.x; // expected-error {{'x' is a protected member}}
99 (void) a.sx;
103 namespace test3 {
104 class B;
105 class A {
106 protected: int x; //expected-note {{declared protected}} // expected-note {{can only access this member on an object of type}}
107 static int sx;
108 static void test(B&);
110 class B : public A {
111 static void test(B&);
113 class C : protected A {
114 static void test(B&);
116 class D : private B {
117 static void test(B&);
120 void A::test(B &b) {
121 (void) b.x;
122 (void) b.sx;
124 void B::test(B &b) {
125 (void) b.x;
126 (void) b.sx;
128 void C::test(B &b) {
129 (void) b.x; // expected-error {{'x' is a protected member}}
130 (void) b.sx;
132 void D::test(B &b) {
133 (void) b.x; // expected-error {{'x' is a protected member}}
134 (void) b.sx;
138 namespace test4 {
139 class C;
140 class A {
141 protected: int x; // expected-note 2{{declared protected here}} expected-note{{member is declared here}}
142 static int sx; // expected-note 3{{member is declared here}}
143 static void test(C&);
145 class B : public A {
146 static void test(C&);
148 class C : protected A { // expected-note 4 {{constrained}}
149 static void test(C&);
151 class D : private B {
152 static void test(C&);
155 void A::test(C &c) {
156 (void) c.x; // expected-error {{'x' is a protected member}}
157 (void) c.sx; // expected-error {{'sx' is a protected member}}
159 void B::test(C &c) {
160 (void) c.x; // expected-error {{'x' is a protected member}}
161 (void) c.sx; // expected-error {{'sx' is a protected member}}
163 void C::test(C &c) {
164 (void) c.x;
165 (void) c.sx;
167 void D::test(C &c) {
168 (void) c.x; // expected-error {{'x' is a protected member}}
169 (void) c.sx; // expected-error {{'sx' is a protected member}}
173 namespace test5 {
174 class D;
175 class A {
176 protected: int x; // expected-note 3{{member is declared here}}
177 static int sx; // expected-note 3{{member is declared here}}
178 static void test(D&);
180 class B : public A {
181 static void test(D&);
183 class C : protected A {
184 static void test(D&);
186 class D : private B { // expected-note 6 {{constrained}}
187 static void test(D&);
190 void A::test(D &d) {
191 (void) d.x; // expected-error {{'x' is a private member}}
192 (void) d.sx; // expected-error {{'sx' is a private member}}
194 void B::test(D &d) {
195 (void) d.x; // expected-error {{'x' is a private member}}
196 (void) d.sx; // expected-error {{'sx' is a private member}}
198 void C::test(D &d) {
199 (void) d.x; // expected-error {{'x' is a private member}}
200 (void) d.sx; // expected-error {{'sx' is a private member}}
202 void D::test(D &d) {
203 (void) d.x;
204 (void) d.sx;
208 namespace test6 {
209 class Static {};
210 class A {
211 protected:
212 void foo(int); // expected-note 3 {{can only access this member on an object of type}}
213 void foo(long);
214 static void foo(Static);
216 static void test(A&);
218 class B : public A {
219 static void test(A&);
221 class C : protected A {
222 static void test(A&);
224 class D : private B {
225 static void test(A&);
228 void A::test(A &a) {
229 a.foo(10);
230 a.foo(Static());
232 void B::test(A &a) {
233 a.foo(10); // expected-error {{'foo' is a protected member}}
234 a.foo(Static());
236 void C::test(A &a) {
237 a.foo(10); // expected-error {{'foo' is a protected member}}
238 a.foo(Static());
240 void D::test(A &a) {
241 a.foo(10); // expected-error {{'foo' is a protected member}}
242 a.foo(Static());
246 namespace test7 {
247 class Static {};
248 class A {
249 protected:
250 void foo(int); // expected-note 3 {{must name member using the type of the current context}}
251 void foo(long);
252 static void foo(Static);
254 static void test();
256 class B : public A {
257 static void test();
259 class C : protected A {
260 static void test();
262 class D : private B {
263 static void test();
266 void A::test() {
267 void (A::*x)(int) = &A::foo;
268 void (*sx)(Static) = &A::foo;
270 void B::test() {
271 void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
272 void (*sx)(Static) = &A::foo;
274 void C::test() {
275 void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
276 void (*sx)(Static) = &A::foo;
278 void D::test() {
279 void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
280 void (*sx)(Static) = &A::foo;
284 namespace test8 {
285 class Static {};
286 class A {
287 protected:
288 void foo(int); // expected-note 3 {{must name member using the type of the current context}}
289 void foo(long);
290 static void foo(Static);
292 static void test();
294 class B : public A {
295 static void test();
297 class C : protected A {
298 static void test();
300 class D : private B {
301 static void test();
303 void call(void (A::*)(int));
304 void calls(void (*)(Static));
306 void A::test() {
307 call(&A::foo);
308 calls(&A::foo);
310 void B::test() {
311 call(&A::foo); // expected-error {{'foo' is a protected member}}
312 calls(&A::foo);
314 void C::test() {
315 call(&A::foo); // expected-error {{'foo' is a protected member}}
316 calls(&A::foo);
318 void D::test() {
319 call(&A::foo); // expected-error {{'foo' is a protected member}}
320 calls(&A::foo);
324 namespace test9 {
325 class A { // expected-note {{member is declared here}}
326 protected: int foo(); // expected-note 4 {{declared}} expected-note 3 {{can only access this member on an object of type}} expected-note 2 {{member is declared here}}
329 class B : public A { // expected-note {{member is declared here}}
330 friend class D;
333 class C : protected B { // expected-note {{declared}} \
334 // expected-note 7 {{constrained}}
337 class D : public A {
338 static void test(A &a) {
339 a.foo(); // expected-error {{'foo' is a protected member}}
340 a.A::foo(); // expected-error {{'foo' is a protected member}}
341 a.B::foo(); // expected-error {{'foo' is a protected member}}
342 a.C::foo(); // expected-error {{'foo' is a protected member}}
343 a.D::foo(); // expected-error {{'foo' is a protected member}}
346 static void test(B &b) {
347 b.foo();
348 b.A::foo();
349 b.B::foo(); // accessible as named in A
350 b.C::foo(); // expected-error {{'foo' is a protected member}}
353 static void test(C &c) {
354 c.foo(); // expected-error {{'foo' is a protected member}}
355 c.A::foo(); // expected-error {{'A' is a protected member}} \
356 // expected-error {{cannot cast}}
357 c.B::foo(); // expected-error {{'B' is a protected member}} \
358 // expected-error {{cannot cast}}
359 c.C::foo(); // expected-error {{'foo' is a protected member}}
362 static void test(D &d) {
363 d.foo();
364 d.A::foo();
365 d.B::foo();
366 d.C::foo(); // expected-error {{'foo' is a protected member}}
371 namespace test10 {
372 template<typename T> class A {
373 protected:
374 int foo();
375 int foo() const;
377 ~A() { foo(); }
380 template class A<int>;
383 // class.protected friendship
384 namespace test11 {
385 class A {
386 protected:
387 int foo();
390 class B : public A {
391 friend class C;
394 class C {
395 void test() {
396 B b;
397 b.A::foo();
402 // This friendship is considered because a public member of A would be
403 // a private member of C.
404 namespace test12 {
405 class A { protected: int foo(); };
406 class B : public virtual A {};
407 class C : private B { friend void test(); };
408 class D : private C, public virtual A {};
410 void test() {
411 D d;
412 d.A::foo();
416 // This friendship is not considered because a public member of A is
417 // inaccessible in C.
418 namespace test13 {
419 class A { protected: int foo(); }; // expected-note {{declared protected here}}
420 class B : private virtual A {};
421 class C : private B { friend void test(); };
422 class D : public virtual A {};
424 void test() {
425 D d;
426 d.A::foo(); // expected-error {{protected member}}
430 // PR8058
431 namespace test14 {
432 class A {
433 protected:
434 template <class T> void temp(T t); // expected-note {{must name member using the type of the current context}}
436 void nontemp(int); // expected-note {{must name member using the type of the current context}}
438 template <class T> void ovl_temp(T t); // expected-note {{must name member using the type of the current context}}
439 void ovl_temp(float);
441 void ovl_nontemp(int); // expected-note {{must name member using the type of the current context}}
442 void ovl_nontemp(float);
444 template <class T> void ovl_withtemp(T);
445 void ovl_withtemp(int); // expected-note {{must name member using the type of the current context}}
448 class B : public A {
449 void use() {
450 void (A::*ptr)(int);
451 ptr = &A::temp; // expected-error {{protected member}}
452 ptr = &A::nontemp; // expected-error {{protected member}}
453 ptr = &A::ovl_temp; // expected-error {{protected member}}
454 ptr = &A::ovl_nontemp; // expected-error {{protected member}}
455 ptr = &A::ovl_withtemp; // expected-error {{protected member}}
460 namespace test15 {
461 class A {
462 protected:
463 A(); // expected-note 2 {{protected constructor can only be used to construct a base class subobject}}
464 A(const A &); // expected-note {{protected constructor can only be used to construct a base class subobject}}
465 ~A(); // expected-note 3 {{protected destructor can only be used to destroy a base class subobject}}
468 class B : public A {
469 // The uses here are fine.
470 B() {}
471 B(int i) : A() {}
472 ~B() {}
474 // All these uses are bad.
476 void test0() {
477 A a; // expected-error {{protected constructor}} expected-error {{protected destructor}}
480 A *test1() {
481 return new A(); // expected-error {{protected constructor}}
484 void test2(A *a) {
485 delete a; // expected-error {{protected destructor}}
488 A test3(A *a) {
489 return *a; // expected-error {{protected constructor}}
492 void test4(A *a) {
493 a->~A(); // expected-error {{protected member}}
498 namespace test16 {
499 class A {
500 protected:
501 ~A();
504 class B : public virtual A {
505 public:
506 ~B() {}
509 class C : public B {
510 ~C() {}