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
;
24 void foo(Protected
&); // expected-note 2 {{declared protected here}}
26 void foo(Private
&); // expected-note 2 {{declared private here}}
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}}
44 void operator+(Public
&);
45 void operator[](Public
&);
46 void operator()(Public
&);
47 typedef void (*PublicSurrogate
)(Public
&);
48 operator PublicSurrogate() const;
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}}
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
) {
70 a
+ prot
; // expected-error {{'operator+' is a protected member}}
71 a
+ priv
; // expected-error {{'operator+' is a private member}}
73 a
[prot
]; // expected-error {{'operator[]' is a protected member}}
74 a
[priv
]; // expected-error {{'operator[]' is a private member}}
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}}
85 // These are all surrogate calls
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.
96 A(); // expected-note 1+{{declared private here}}
101 A a
; // expected-error {{calling a private constructor}}
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
{
113 class D
: C
{ }; // expected-error {{inherited virtual base class 'A' has private default constructor}}
114 D d
; // expected-note{{implicit default constructor}}
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}}
126 D d
; // expected-error {{call to implicitly-deleted default constructor}}
130 // Implicit destructor calls.
134 ~A(); // expected-note 2 {{declared private here}}
138 A a
; // expected-error {{variable of type 'A' has private destructor}}
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}}
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}}
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}}
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}}
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}}
223 Base
<0>, // expected-note {{deleted because base class 'Base<0>' has an inaccessible destructor}}
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}}
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}}
253 Base
<0>, // expected-note {{deleted because base class 'Base<0>' has an inaccessible destructor}}
258 Derived3 d3
; // expected-error {{implicitly-deleted default constructor}}
260 #error "missing case of MSVC cross C++ versions"
264 // Conversion functions.
268 operator Private(); // expected-note 4 {{declared private here}}
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}}
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
{
299 Private
test1(Derived4
&d
) { return d
; }
300 Public
test2(Derived4
&d
) { return d
; }
303 // Implicit copy assignment operator uses.
306 void operator=(const A
&);
307 #if __cplusplus < 201103L
308 // expected-note@-2 2{{implicitly declared private here}}
312 #if __cplusplus < 201103L
313 class Test1
{ A a
; }; // expected-error {{private member}}
316 a
= Test1(); // expected-note{{implicit copy}}
319 class Test2
: A
{}; // expected-error {{private member}}
322 a
= Test2(); // expected-note{{implicit copy}}
325 class Test1
{ A a
; }; // expected-note {{because field 'a' has an inaccessible copy assignment operator}}
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}}
334 a
= Test2(); // expected-error {{copy assignment operator is implicitly deleted}}
339 // Implicit copy constructor uses.
343 private: A(const A
&);
344 #if __cplusplus < 201103L
345 // expected-note@-2 2{{declared private here}}
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}}
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}}
372 // Redeclaration lookups are not accesses.
378 int foo(int private_member
) {
384 // Ignored operator new and delete overloads are not
386 typedef __typeof__(sizeof(int)) size_t;
389 void *operator new(size_t s
);
390 void operator delete(void *p
);
392 void *operator new(size_t s
, int n
);
393 void operator delete(void *p
, int n
);
401 // Don't silently upgrade forbidden-access paths to private.
404 public: static int x
; // expected-note {{member is declared here}}
406 class B
: private A
{ // expected-note {{constrained by private inheritance here}}
409 static int getX() { return x
; } // expected-error {{'x' is a private member of 'test9::A'}}
416 value
= 10 // expected-note {{declared private here}}
423 value
= A::value
// expected-error {{'value' is a private member of 'test10::A'}}
436 protected: virtual ~A();
463 unsigned foo() const;
466 struct B
: protected A
{
478 // Destructors for temporaries.
481 private: ~A(); // expected-note {{declared private here}}
486 foo(); // expected-error {{temporary of type 'A' has private destructor}}
490 ~X(); // expected-note {{declared private here}}
498 const X
&xr
= Y1(); // expected-error{{temporary of type 'X' has private destructor}}
504 template <class T
> class A
{
506 int private_foo
; // expected-note {{declared private here}}
507 static int private_sfoo
; // expected-note {{declared private here}}
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}}
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}}
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'}}
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}}
579 A
<int> member
; // expected-error {{'A' is a private member of 'test18::A<int>'}}
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
; }
594 Foo(); // expected-note {{implicitly declared private here}}
599 Foo a
; // expected-error {{calling a private constructor}}
604 template <class T
> class A
{
607 class Inner
; // expected-note {{implicitly declared private here}}
611 template <class T
> class A
<T
>::Inner
{};
613 template <class T
> class A
<T
>::Inner
; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}}
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; };
632 template <typename T
> class A
{
637 template <typename T
> A
<T
> A
<T
>::instance
;
638 template class A
<int>;