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
7 
8 // C++0x [class.access]p4:
9 
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.
14 
15 class Public {} PublicInst;
16 class Protected {} ProtectedInst;
17 class Private {} PrivateInst;
18 
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}}
27   };
28 
test(A * op)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}}
33 
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}}
37   }
38 }
39 
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}}
62   };
63   void operator+(const A &, Public&);
64   void operator+(const A &, Protected&);
65   void operator+(const A &, Private&);
66   void operator-(const A &);
67 
test(A & a,Public & pub,Protected & prot,Private & priv)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}}
79 
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}}
89   }
90 }
91 
92 // Implicit constructor calls.
93 namespace test2 {
94   class A {
95   private:
96     A(); // expected-note 1+{{declared private here}}
97 
98     static A foo;
99   };
100 
101   A a; // expected-error {{calling a private constructor}}
102   A A::foo; // okay
103 
104 #if __cplusplus < 201103L
105   class B : A { }; // expected-error {{base class 'test2::A' has private default constructor}}
106   B b; // expected-note{{implicit default constructor}}
107 
108   class C : virtual A {
109   public:
110     C();
111   };
112 
113   class D : C { }; // expected-error {{inherited virtual base class 'test2::A' has private default constructor}}
114   D d; // expected-note{{implicit default constructor}}
115 #else
116   class B : A { }; // expected-note {{base class 'test2::A' has an inaccessible default constructor}}
117   B b; // expected-error {{call to implicitly-deleted default constructor}}
118 
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 'test2::A' has an inaccessible default constructor}}
121   public:
122     C();
123   };
124 
125   class D : C { };
126   D d; // expected-error {{call to implicitly-deleted default constructor}}
127 #endif
128 }
129 
130 // Implicit destructor calls.
131 namespace test3 {
132   class A {
133   private:
134     ~A(); // expected-note 2 {{declared private here}}
135     static A foo;
136   };
137 
138   A a; // expected-error {{variable of type 'test3::A' has private destructor}}
139   A A::foo;
140 
foo(A param)141   void foo(A param) { // okay
142     A local; // expected-error {{variable of type 'test3::A' has private destructor}}
143   }
144 
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}}
150 
151   // These don't cause diagnostics because we don't need the destructor.
152   class Derived0 : Base<0> { ~Derived0(); };
153   class Derived1 : Base<1> { };
154 
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 'test3::Base2' has private destructor}}
160     virtual Base3
161   {
~Derived2()162     ~Derived2() {}
163   };
164 
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 'test3::Base2' has private destructor}}
171     virtual Base3
172   {};
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}}
180 
181   // These don't cause diagnostics because we don't need the destructor.
182   class Derived0 : Base<0> { ~Derived0(); };
183   class Derived1 : Base<1> { };
184 
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 'test3::Base2' has private destructor}}
190     virtual Base3
191   {
~Derived2()192     ~Derived2() {} // expected-note 2{{in implicit destructor}}
193   };
194 
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 'test3::Base2' has private destructor}}
200     virtual Base3
201   {};
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(); };
207 
208   // These don't cause diagnostics because we don't need the destructor.
209   class Derived0 : Base<0> { ~Derived0(); };
210   class Derived1 : Base<1> { };
211 
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 'test3::Base2' has private destructor}}
217     virtual Base3
218   {
~Derived2()219     ~Derived2() {}
220   };
221 
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
227   {};
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(); };
235 
236   // These don't cause diagnostics because we don't need the destructor.
237   class Derived0 : Base<0> { ~Derived0(); };
238   class Derived1 : Base<1> { };
239 
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 'test3::Base2' has private destructor}}
245     virtual Base3
246   {
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}}
~Derived2()249     ~Derived2() {}
250   };
251 
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
257   {};
258   Derived3 d3; // expected-error {{implicitly-deleted default constructor}}
259 #else
260 #error "missing case of MSVC cross C++ versions"
261 #endif
262 }
263 
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}}
271   };
272 
273   class Derived1 : private Base { // expected-note {{constrained by private inheritance}}
test1()274     Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
test2()275     Public test2() { return *this; }
276   };
test1(Derived1 & d)277   Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}}
test2(Derived1 & d)278   Public test2(Derived1 &d) { return d; } // expected-error {{'operator Public' is a private member}}
279 
280 
281   class Derived2 : public Base {
test1()282     Private test1() { return *this; } // expected-error {{'operator Private' is a private member}}
test2()283     Public test2() { return *this; }
284   };
test1(Derived2 & d)285   Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}}
test2(Derived2 & d)286   Public test2(Derived2 &d) { return d; }
287 
288   class Derived3 : private Base { // expected-note {{constrained by private inheritance here}}
289   public:
290     operator Private();
291   };
test1(Derived3 & d)292   Private test1(Derived3 &d) { return d; }
test2(Derived3 & d)293   Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}}
294 
295   class Derived4 : public Base {
296   public:
297     operator Private();
298   };
test1(Derived4 & d)299   Private test1(Derived4 &d) { return d; }
test2(Derived4 & d)300   Public test2(Derived4 &d) { return d; }
301 }
302 
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
310   };
311 
312 #if __cplusplus < 201103L
313   class Test1 { A a; }; // expected-error {{private member}}
test1()314   void test1() {
315     Test1 a;
316     a = Test1(); // expected-note{{implicit copy}}
317   }
318 
319   class Test2 : A {}; // expected-error {{private member}}
test2()320   void test2() {
321     Test2 a;
322     a = Test2(); // expected-note{{implicit copy}}
323   }
324 #else
325   class Test1 { A a; }; // expected-note {{because field 'a' has an inaccessible copy assignment operator}}
test1()326   void test1() {
327     Test1 a;
328     a = Test1(); // expected-error {{copy assignment operator is implicitly deleted}}
329   }
330 
331   class Test2 : A {}; // expected-note {{because base class 'test5::A' has an inaccessible copy assignment operator}}
test2()332   void test2() {
333     Test2 a;
334     a = Test2(); // expected-error {{copy assignment operator is implicitly deleted}}
335   }
336 #endif
337 }
338 
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
347   };
348 
349 #if __cplusplus < 201103L
350   class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}}
test1(const Test1 & t)351   void test1(const Test1 &t) {
352     Test1 a = t; // expected-note{{implicit copy}}
353   }
354 
355   class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}}
test2(const Test2 & t)356   void test2(const Test2 &t) {
357     Test2 a = t; // expected-note{{implicit copy}}
358   }
359 #else
360   class Test1 { A a; }; // expected-note {{field 'a' has an inaccessible copy constructor}}
test1(const Test1 & t)361   void test1(const Test1 &t) {
362     Test1 a = t; // expected-error{{implicitly-deleted}}
363   }
364 
365   class Test2 : A {}; // expected-note {{base class 'test6::A' has an inaccessible copy constructor}}
test2(const Test2 & t)366   void test2(const Test2 &t) {
367     Test2 a = t; // expected-error{{implicitly-deleted}}
368   }
369 #endif
370 }
371 
372 // Redeclaration lookups are not accesses.
373 namespace test7 {
374   class A {
375     int private_member;
376   };
377   class B : A {
foo(int private_member)378     int foo(int private_member) {
379       return 0;
380     }
381   };
382 }
383 
384 // Ignored operator new and delete overloads are not
385 namespace test8 {
386   typedef __typeof__(sizeof(int)) size_t;
387 
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);
394   };
395 
test()396   void test() {
397     new (2) A();
398   }
399 }
400 
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}}
405   };
406   class B : private A { // expected-note {{constrained by private inheritance here}}
407   };
408   class C : public B {
getX()409     static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}}
410   };
411 }
412 
413 namespace test10 {
414   class A {
415     enum {
416       value = 10 // expected-note {{declared private here}}
417     };
418     friend class C;
419   };
420 
421   class B {
422     enum {
423       value = A::value // expected-error {{'value' is a private member of 'test10::A'}}
424     };
425   };
426 
427   class C {
428     enum {
429       value = A::value
430     };
431   };
432 }
433 
434 namespace test11 {
435   class A {
436     protected: virtual ~A();
437   };
438 
439   class B : public A {
440     ~B();
441   };
442 
~B()443   B::~B() {};
444 }
445 
446 namespace test12 {
447   class A {
448     int x;
449 
foo()450     void foo() {
451       class Local {
452         int foo(A *a) {
453           return a->x;
454         }
455       };
456     }
457   };
458 }
459 
460 namespace test13 {
461   struct A {
462     int x;
463     unsigned foo() const;
464   };
465 
466   struct B : protected A {
467     using A::foo;
468     using A::x;
469   };
470 
test()471   void test() {
472     A *d;
473     d->foo();
474     (void) d->x;
475   }
476 }
477 
478 // Destructors for temporaries.
479 namespace test14 {
480   class A {
481   private: ~A(); // expected-note {{declared private here}}
482   };
483   A foo();
484 
test()485   void test() {
486     foo(); // expected-error {{temporary of type 'test14::A' has private destructor}}
487   }
488 
489   class X {
490     ~X(); // expected-note {{declared private here}}
491   };
492 
493   struct Y1 {
494     operator X();
495   };
496 
g()497   void g() {
498     const X &xr = Y1(); // expected-error{{temporary of type 'test14::X' has private destructor}}
499   }
500 }
501 
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}}
511 
test1(A<int> & a)512     int test1(A<int> &a) {
513       return a.private_foo; // expected-error {{private member}}
514     }
515 
test2(A<int> & a)516     int test2(A<int> &a) {
517       return a.private_sfoo; // expected-error {{private member}}
518     }
519 
test3(A<int> & a)520     int test3(A<int> &a) {
521       return a.protected_foo; // expected-error {{protected member}}
522     }
523 
test4(A<int> & a)524     int test4(A<int> &a) {
525       return a.protected_sfoo; // expected-error {{protected member}}
526     }
527   };
528 
529   template class A<int>;
530   template class A<long>; // expected-note 4 {{in instantiation}}
531 
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.
536 
test1(A<int> & a)537     int test1(A<int> &a) {
538       return a.protected_foo; // expected-error 2 {{protected member}}
539     }
540 
test2(A<int> & a)541     int test2(A<int> &a) {
542       return a.protected_sfoo; // expected-error {{protected member}}
543     }
544 
test3(B<int> & b)545     int test3(B<int> &b) {
546       return b.protected_foo; // expected-error {{protected member}}
547     }
548 
test4(B<int> & b)549     int test4(B<int> &b) {
550       return b.protected_sfoo; // expected-error {{protected member}}
551     }
552   };
553 
554   template class B<int>;  // expected-note {{in instantiation}}
555   template class B<long>; // expected-note 4 {{in instantiation}}
556 }
557 
558 // PR7281
559 namespace test16 {
560   class A { ~A(); }; // expected-note 2{{declared private here}}
b()561   void b() { throw A(); } // expected-error{{temporary of type 'test16::A' has private destructor}} \
562   // expected-error{{exception object of type 'test16::A' has private destructor}}
563 }
564 
565 // rdar://problem/8146294
566 namespace test17 {
567   class A {
568     template <typename T> class Inner { }; // expected-note {{declared private here}}
569   };
570 
571   A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}}
572 }
573 
574 namespace test18 {
575   template <class T> class A {}; // expected-note {{member is declared here}}
576   class B : A<int> { // expected-note {{constrained by implicitly private inheritance here}}
577     A<int> member;
578   };
579   class C : B {
580     A<int> member; // expected-error {{'A' is a private member of 'test18::A<int>'}}
581   };
582 }
583 
584 // PR8325
585 namespace test19 {
586   class A { ~A(); };
587   // The destructor is not implicitly referenced here.  Contrast to test16,
588   // testing PR7281, earlier in this file.
b(A * x)589   void b(A* x) { throw x; }
590 }
591 
592 // PR7930
593 namespace test20 {
594   class Foo {
595     Foo(); // expected-note {{implicitly declared private here}}
596   };
Foo()597   Foo::Foo() {}
598 
test()599   void test() {
600     Foo a; // expected-error {{calling a private constructor}}
601   }
602 }
603 
604 namespace test21 {
605   template <class T> class A {
606     void foo();
607     void bar();
608     class Inner; // expected-note {{implicitly declared private here}}
609   public:
610     void baz();
611   };
612   template <class T> class A<T>::Inner {};
613   class B {
614     template <class T> class A<T>::Inner; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}}
615   };
616 
test()617   void test() {
618     A<int>::Inner i; // expected-error {{'Inner' is a private member}}
619   }
620 }
621 
622 namespace rdar8876150 {
623   struct A { operator bool(); };
624   struct B : private A { using A::operator bool; };
625 
f()626   bool f() {
627     B b;
628     return !b;
629   }
630 }
631 
632 namespace test23 {
633   template <typename T> class A {
634     A();
635     static A instance;
636   };
637 
638   template <typename T> A<T> A<T>::instance;
639   template class A<int>;
640 }
641