diff options
author | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
---|---|---|
committer | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
commit | 222e2a7620e6520ffaf4fc4e69d79c18da31542e (patch) | |
tree | 7bfbc05bfa3b41c8f9d2e56d53a0bc3e310df239 /clang/test/CXX/class.access | |
parent | 3d206f03985b50beacae843d880bccdc91a9f424 (diff) |
Add the clang library to the repo (with some of my changes, too).
Diffstat (limited to 'clang/test/CXX/class.access')
-rw-r--r-- | clang/test/CXX/class.access/class.access.base/p1.cpp | 155 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.access.base/p5.cpp | 75 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.access.dcl/p1.cpp | 199 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.access.nest/p1.cpp | 33 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.friend/p1.cpp | 356 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.friend/p11.cpp | 19 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.friend/p2-cxx03.cpp | 13 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp | 29 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.friend/p6.cpp | 20 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.protected/p1-cxx11.cpp | 20 | ||||
-rw-r--r-- | clang/test/CXX/class.access/class.protected/p1.cpp | 519 | ||||
-rw-r--r-- | clang/test/CXX/class.access/p4.cpp | 510 | ||||
-rw-r--r-- | clang/test/CXX/class.access/p6.cpp | 192 |
13 files changed, 2140 insertions, 0 deletions
diff --git a/clang/test/CXX/class.access/class.access.base/p1.cpp b/clang/test/CXX/class.access/class.access.base/p1.cpp new file mode 100644 index 0000000..43cc99e --- /dev/null +++ b/clang/test/CXX/class.access/class.access.base/p1.cpp @@ -0,0 +1,155 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// C++0x [class.access.base]p1(a): +// If a class is declared to be a base class for another class using +// the public access specifier, the public members of the base class +// are accessible as public members of the derived class and protected +// members of the base class are accessible as protected members of +// the derived class. +namespace test0 { + class Base { + public: int pub; static int spub; + protected: int prot; static int sprot; // expected-note 4 {{declared protected here}} + private: int priv; static int spriv; // expected-note 8 {{declared private here}} + }; + + class Test : public Base { + void test() { + pub++; + spub++; + prot++; + sprot++; + priv++; // expected-error {{private member}} + spriv++; // expected-error {{private member}} + + Base::pub++; + Base::spub++; + Base::prot++; + Base::sprot++; + Base::priv++; // expected-error {{private member}} + Base::spriv++; // expected-error {{private member}} + } + }; + + void test(Test *t) { + t->pub++; + t->spub++; + t->prot++; // expected-error {{protected member}} + t->sprot++; // expected-error {{protected member}} + t->priv++; // expected-error {{private member}} + t->spriv++; // expected-error {{private member}} + + t->Base::pub++; + t->Base::spub++; + t->Base::prot++; // expected-error {{protected member}} + t->Base::sprot++; // expected-error {{protected member}} + t->Base::priv++; // expected-error {{private member}} + t->Base::spriv++; // expected-error {{private member}} + } +} + +// C++0x [class.access.base]p1(b): +// If a class is declared to be a base class for another class using +// the protected access specifier, the public and protected members +// of the base class are accessible as protected members of the +// derived class. +namespace test1 { + class Base { // expected-note 6{{member is declared here}} + public: + int pub; // expected-note{{member is declared here}} + static int spub; // expected-note{{member is declared here}} + protected: int prot; static int sprot; // expected-note 4 {{declared protected here}} + private: int priv; static int spriv; // expected-note 8 {{declared private here}} + }; + + class Test : protected Base { // expected-note 6 {{declared protected here}} expected-note 8 {{constrained by protected inheritance here}} + void test() { + pub++; + spub++; + prot++; + sprot++; + priv++; // expected-error {{private member}} + spriv++; // expected-error {{private member}} + + Base::pub++; + Base::spub++; + Base::prot++; + Base::sprot++; + Base::priv++; // expected-error {{private member}} + Base::spriv++; // expected-error {{private member}} + } + }; + + void test(Test *t) { + t->pub++; // expected-error {{protected member}} expected-error {{protected base class}} + t->spub++; // expected-error {{protected member}} + t->prot++; // expected-error {{protected member}} expected-error {{protected base class}} + t->sprot++; // expected-error {{protected member}} + t->priv++; // expected-error {{private member}} expected-error {{protected base class}} + t->spriv++; // expected-error {{private member}} + + // Two possible errors here: one for Base, one for the member + t->Base::pub++; // expected-error {{protected member}} expected-error {{protected base class}} + t->Base::spub++; // expected-error {{protected member}} + t->Base::prot++; // expected-error 2 {{protected member}} expected-error {{protected base class}} + t->Base::sprot++; // expected-error 2 {{protected member}} + t->Base::priv++; // expected-error {{protected member}} expected-error {{private member}} expected-error {{protected base class}} + t->Base::spriv++; // expected-error {{protected member}} expected-error {{private member}} + } +} + +// C++0x [class.access.base]p1(b): +// If a class is declared to be a base class for another class using +// the private access specifier, the public and protected members of +// the base class are accessible as private members of the derived +// class. +namespace test2 { + class Base { // expected-note 6{{member is declared here}} + public: + int pub; // expected-note{{member is declared here}} + static int spub; // expected-note{{member is declared here}} + protected: + int prot; // expected-note {{declared protected here}} \ + // expected-note{{member is declared here}} + static int sprot; // expected-note {{declared protected here}} \ + // expected-note{{member is declared here}} + private: + int priv; // expected-note 4 {{declared private here}} + static int spriv; // expected-note 4 {{declared private here}} + }; + + class Test : private Base { // expected-note 6 {{declared private here}} \ + // expected-note 10 {{constrained by private inheritance here}} + void test() { + pub++; + spub++; + prot++; + sprot++; + priv++; // expected-error {{private member}} + spriv++; // expected-error {{private member}} + + Base::pub++; + Base::spub++; + Base::prot++; + Base::sprot++; + Base::priv++; // expected-error {{private member}} + Base::spriv++; // expected-error {{private member}} + } + }; + + void test(Test *t) { + t->pub++; // expected-error {{private member}} expected-error {{private base class}} + t->spub++; // expected-error {{private member}} + t->prot++; // expected-error {{private member}} expected-error {{private base class}} + t->sprot++; // expected-error {{private member}} + t->priv++; // expected-error {{private member}} expected-error {{private base class}} + t->spriv++; // expected-error {{private member}} + + t->Base::pub++; // expected-error {{private member}} expected-error {{private base class}} + t->Base::spub++; // expected-error {{private member}} + t->Base::prot++; // expected-error {{protected member}} expected-error {{private member}} expected-error {{private base class}} + t->Base::sprot++; // expected-error {{protected member}} expected-error {{private member}} + t->Base::priv++; // expected-error 2 {{private member}} expected-error {{private base class}} + t->Base::spriv++; // expected-error 2 {{private member}} + } +} diff --git a/clang/test/CXX/class.access/class.access.base/p5.cpp b/clang/test/CXX/class.access/class.access.base/p5.cpp new file mode 100644 index 0000000..255fbfc --- /dev/null +++ b/clang/test/CXX/class.access/class.access.base/p5.cpp @@ -0,0 +1,75 @@ +// RUN: %clang_cc1 -verify %s + +namespace test0 { + struct A { + static int x; + }; + struct B : A {}; + struct C : B {}; + + int test() { + return A::x + + B::x + + C::x; + } +} + +namespace test1 { + struct A { + private: static int x; // expected-note 5 {{declared private here}} + static int test() { return x; } + }; + struct B : public A { + static int test() { return x; } // expected-error {{private member}} + }; + struct C : private A { + static int test() { return x; } // expected-error {{private member}} + }; + + struct D { + public: static int x; // expected-note{{member is declared here}} + static int test() { return x; } + }; + struct E : private D { // expected-note{{constrained by private inheritance}} + static int test() { return x; } + }; + + int test() { + return A::x // expected-error {{private member}} + + B::x // expected-error {{private member}} + + C::x // expected-error {{private member}} + + D::x + + E::x; // expected-error {{private member}} + } +} + +namespace test2 { + class A { + protected: static int x; // expected-note{{member is declared here}} + }; + + class B : private A {}; // expected-note {{private inheritance}} + class C : private A { + int test(B *b) { + return b->x; // expected-error {{private member}} + } + }; +} + +namespace test3 { + class A { + protected: static int x; + }; + + class B : public A {}; + class C : private A { + int test(B *b) { + // x is accessible at C when named in A. + // A is an accessible base of B at C. + // Therefore this succeeds. + return b->x; + } + }; +} + +// TODO: flesh out these cases diff --git a/clang/test/CXX/class.access/class.access.dcl/p1.cpp b/clang/test/CXX/class.access/class.access.dcl/p1.cpp new file mode 100644 index 0000000..aab5fff --- /dev/null +++ b/clang/test/CXX/class.access/class.access.dcl/p1.cpp @@ -0,0 +1,199 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// This is just the test for [namespace.udecl]p4 with 'using' +// uniformly stripped out. + +// C++03 [namespace.udecl]p4: +// A using-declaration used as a member-declaration shall refer to a +// member of a base class of the class being defined, shall refer to +// a member of an anonymous union that is a member of a base class +// of the class being defined, or shall refer to an enumerator for +// an enumeration type that is a member of a base class of the class +// being defined. + +// There is no directly analogous paragraph in C++0x, and the feature +// works sufficiently differently there that it needs a separate test. + +namespace test0 { + namespace NonClass { + typedef int type; + struct hiding {}; + int hiding; + static union { double union_member; }; + enum tagname { enumerator }; + } + + class Test0 { + NonClass::type; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}} + NonClass::hiding; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}} + NonClass::union_member; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}} + NonClass::enumerator; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}} + }; +} + +struct Opaque0 {}; + +namespace test1 { + struct A { + typedef int type; + struct hiding {}; // expected-note {{previous use is here}} + Opaque0 hiding; + union { double union_member; }; + enum tagname { enumerator }; + }; + + struct B : A { + A::type; // expected-warning {{access declarations are deprecated}} + A::hiding; // expected-warning {{access declarations are deprecated}} + A::union_member; // expected-warning {{access declarations are deprecated}} + A::enumerator; // expected-warning {{access declarations are deprecated}} + A::tagname; // expected-warning {{access declarations are deprecated}} + + void test0() { + type t = 0; + } + + void test1() { + typedef struct A::hiding local; + struct hiding _ = local(); + } + + void test2() { + union hiding _; // expected-error {{tag type that does not match previous}} + } + + void test3() { + char array[sizeof(union_member) == sizeof(double) ? 1 : -1]; + } + + void test4() { + enum tagname _ = enumerator; + } + + void test5() { + Opaque0 _ = hiding; + } + }; +} + +namespace test2 { + struct A { + typedef int type; + struct hiding {}; // expected-note {{previous use is here}} + int hiding; + union { double union_member; }; + enum tagname { enumerator }; + }; + + template <class T> struct B : A { + A::type; // expected-warning {{access declarations are deprecated}} + A::hiding; // expected-warning {{access declarations are deprecated}} + A::union_member; // expected-warning {{access declarations are deprecated}} + A::enumerator; // expected-warning {{access declarations are deprecated}} + A::tagname; // expected-warning {{access declarations are deprecated}} + + void test0() { + type t = 0; + } + + void test1() { + typedef struct A::hiding local; + struct hiding _ = local(); + } + + void test2() { + union hiding _; // expected-error {{tag type that does not match previous}} + } + + void test3() { + char array[sizeof(union_member) == sizeof(double) ? 1 : -1]; + } + + void test4() { + enum tagname _ = enumerator; + } + + void test5() { + Opaque0 _ = hiding; + } + }; +} + +namespace test3 { + struct hiding {}; + + template <class T> struct A { + typedef int type; // expected-note {{target of using declaration}} + struct hiding {}; + Opaque0 hiding; + union { double union_member; }; + enum tagname { enumerator }; // expected-note {{target of using declaration}} + }; + + template <class T> struct B : A<T> { + A<T>::type; // expected-error {{dependent using declaration resolved to type without 'typename'}} // expected-warning {{access declarations are deprecated}} + A<T>::hiding; // expected-warning {{access declarations are deprecated}} + A<T>::union_member; // expected-warning {{access declarations are deprecated}} + A<T>::enumerator; // expected-warning {{access declarations are deprecated}} + A<T>::tagname; // expected-error {{dependent using declaration resolved to type without 'typename'}} // expected-warning {{access declarations are deprecated}} + + // FIXME: re-enable these when the various bugs involving tags are fixed +#if 0 + void test1() { + typedef struct A<T>::hiding local; + struct hiding _ = local(); + } + + void test2() { + typedef struct A<T>::hiding local; + union hiding _ = local(); + } +#endif + + void test3() { + char array[sizeof(union_member) == sizeof(double) ? 1 : -1]; + } + +#if 0 + void test4() { + enum tagname _ = enumerator; + } +#endif + + void test5() { + Opaque0 _ = hiding; + } + }; + + template struct B<int>; // expected-note {{in instantiation}} +} + +namespace test4 { + struct Base { + int foo(); + }; + + struct Unrelated { + int foo(); + }; + + struct Subclass : Base { + }; + + namespace InnerNS { + int foo(); + } + + // We should be able to diagnose these without instantiation. + template <class T> struct C : Base { + InnerNS::foo; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}} + Base::bar; // expected-error {{no member named 'bar'}} expected-warning {{access declarations are deprecated}} + Unrelated::foo; // expected-error {{not a base class}} expected-warning {{access declarations are deprecated}} + C::foo; // legal in C++03 // expected-warning {{access declarations are deprecated}} + Subclass::foo; // legal in C++03 // expected-warning {{access declarations are deprecated}} + + int bar(); //expected-note {{target of using declaration}} + C::bar; // expected-error {{refers to its own class}} expected-warning {{access declarations are deprecated}} + }; +} + diff --git a/clang/test/CXX/class.access/class.access.nest/p1.cpp b/clang/test/CXX/class.access/class.access.nest/p1.cpp new file mode 100644 index 0000000..eceffcf --- /dev/null +++ b/clang/test/CXX/class.access/class.access.nest/p1.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// Derived from GNU's std::string +namespace test0 { + class A { + struct B { + unsigned long length; + }; + struct C : B { + static const unsigned long max_length; + }; + }; + + const unsigned long A::C::max_length = sizeof(B); +} + +// Example from the standard. +namespace test1 { + class E { + int x; + class B {}; + + class I { + B b; + int y; // expected-note {{declared private here}} + void f(E* p, int i) { + p->x = i; + } + }; + + int g(I* p) { return p->y; } // expected-error {{'y' is a private member of 'test1::E::I'}} + }; +} diff --git a/clang/test/CXX/class.access/class.friend/p1.cpp b/clang/test/CXX/class.access/class.friend/p1.cpp new file mode 100644 index 0000000..68ff83f --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p1.cpp @@ -0,0 +1,356 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// C++'0x [class.friend] p1: +// A friend of a class is a function or class that is given permission to use +// the private and protected member names from the class. A class specifies +// its friends, if any, by way of friend declarations. Such declarations give +// special access rights to the friends, but they do not make the nominated +// friends members of the befriending class. + +struct S { static void f(); }; +S* g() { return 0; } + +struct X { + friend struct S; + friend S* g(); +}; + +void test1() { + S s; + g()->f(); + S::f(); + X::g(); // expected-error{{no member named 'g' in 'X'}} + X::S x_s; // expected-error{{no member named 'S' in 'X'}} + X x; + x.g(); // expected-error{{no member named 'g' in 'X'}} +} + +// Test that we recurse through namespaces to find already declared names, but +// new names are declared within the enclosing namespace. +namespace N { + struct X { + friend struct S; + friend S* g(); + + friend struct S2; + friend struct S2* g2(); + }; + + struct S2 { static void f2(); }; + S2* g2() { return 0; } + + void test() { + g()->f(); + S s; + S::f(); + X::g(); // expected-error{{no member named 'g' in 'N::X'}} + X::S x_s; // expected-error{{no member named 'S' in 'N::X'}} + X x; + x.g(); // expected-error{{no member named 'g' in 'N::X'}} + + g2(); + S2 s2; + ::g2(); // expected-error{{no member named 'g2' in the global namespace}} + ::S2 g_s2; // expected-error{{no member named 'S2' in the global namespace}} + X::g2(); // expected-error{{no member named 'g2' in 'N::X'}} + X::S2 x_s2; // expected-error{{no member named 'S2' in 'N::X'}} + x.g2(); // expected-error{{no member named 'g2' in 'N::X'}} + } +} + +namespace test0 { + class ClassFriend { + void test(); + }; + + class MemberFriend { + void test(); + }; + + void declared_test(); + + class Class { + static void member(); // expected-note 2 {{declared private here}} + + friend class ClassFriend; + friend class UndeclaredClassFriend; + + friend void undeclared_test(); + friend void declared_test(); + friend void MemberFriend::test(); + }; + + void declared_test() { + Class::member(); + } + + void undeclared_test() { + Class::member(); + } + + void unfriended_test() { + Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}} + } + + void ClassFriend::test() { + Class::member(); + } + + void MemberFriend::test() { + Class::member(); + } + + class UndeclaredClassFriend { + void test() { + Class::member(); + } + }; + + class ClassNonFriend { + void test() { + Class::member(); // expected-error {{'member' is a private member of 'test0::Class'}} + } + }; +} + +// Make sure that friends have access to inherited protected members. +namespace test2 { + struct X; + + class ilist_half_node { + friend struct ilist_walker_bad; + X *Prev; + protected: + X *getPrev() { return Prev; } // expected-note{{member is declared here}} + }; + + class ilist_node : private ilist_half_node { // expected-note {{declared private here}} expected-note {{constrained by private inheritance here}} + friend struct ilist_walker; + X *Next; + X *getNext() { return Next; } // expected-note {{declared private here}} + }; + + struct X : ilist_node {}; + + struct ilist_walker { + static X *getPrev(X *N) { return N->getPrev(); } + static X *getNext(X *N) { return N->getNext(); } + }; + + struct ilist_walker_bad { + static X *getPrev(X *N) { return N->getPrev(); } // \ + // expected-error {{'getPrev' is a private member of 'test2::ilist_half_node'}} \ + // expected-error {{cannot cast 'test2::X' to its private base class 'test2::ilist_half_node'}} + + static X *getNext(X *N) { return N->getNext(); } // \ + // expected-error {{'getNext' is a private member of 'test2::ilist_node'}} + }; +} + +namespace test3 { + class A { protected: int x; }; // expected-note {{declared protected here}} + + class B : public A { + friend int foo(B*); + }; + + int foo(B *p) { + return p->x; + } + + int foo(const B *p) { + return p->x; // expected-error {{'x' is a protected member of 'test3::A'}} + } +} + +namespace test3a { + class A { protected: int x; }; + + class B : public A { + friend int foo(B*); + }; + + int foo(B * const p) { + return p->x; + } +} + +namespace test4 { + template <class T> class Holder { + T object; + friend bool operator==(Holder &a, Holder &b) { + return a.object == b.object; // expected-error {{invalid operands to binary expression}} + } + }; + + struct Inequal {}; + bool test() { + Holder<Inequal> a, b; + return a == b; // expected-note {{requested here}} + } +} + + +// PR6174 +namespace test5 { + namespace ns { + class A; + } + + class ns::A { + private: int x; + friend class B; + }; + + namespace ns { + class B { + int test(A *p) { return p->x; } + }; + } +} + +// PR6207 +namespace test6 { + struct A {}; + + struct B { + friend A::A(); + friend A::~A(); + friend A &A::operator=(const A&); + }; +} + +namespace test7 { + template <class T> struct X { + X(); + ~X(); + void foo(); + void bar(); + }; + + class A { + friend void X<int>::foo(); + friend X<int>::X(); + friend X<int>::X(const X&); + + private: + A(); // expected-note 2 {{declared private here}} + }; + + template<> void X<int>::foo() { + A a; + } + + template<> void X<int>::bar() { + A a; // expected-error {{calling a private constructor}} + } + + template<> X<int>::X() { + A a; + } + + template<> X<int>::~X() { + A a; // expected-error {{calling a private constructor}} + } +} + +// Return types, parameters and default arguments to friend functions. +namespace test8 { + class A { + typedef int I; // expected-note 4 {{declared private here}} + static const I x = 0; // expected-note {{implicitly declared private here}} + friend I f(I i); + template<typename T> friend I g(I i); + }; + + const A::I A::x; + A::I f(A::I i = A::x) {} + template<typename T> A::I g(A::I i) { + T t; + } + template A::I g<A::I>(A::I i); + + A::I f2(A::I i = A::x) {} // expected-error 3 {{is a private member of}} + template<typename T> A::I g2(A::I i) { // expected-error 2 {{is a private member of}} + T t; + } + template A::I g2<A::I>(A::I i); +} + +// PR6885 +namespace test9 { + class B { + friend class test9; + }; +} + +// PR7230 +namespace test10 { + extern "C" void f(void); + extern "C" void g(void); + + namespace NS { + class C { + void foo(void); // expected-note {{declared private here}} + friend void test10::f(void); + }; + static C* bar; + } + + void f(void) { + NS::bar->foo(); + } + + void g(void) { + NS::bar->foo(); // expected-error {{private member}} + } +} + +// PR8705 +namespace test11 { + class A { + void test0(int); + void test1(int); + void test2(int); + void test3(int); + }; + + class B { + typedef int private_type; // expected-note 2 {{implicitly declared private here}} + friend void A::test0(int); + friend void A::test1(int); + }; + + void A::test0(B::private_type x) {} + void A::test1(int x = B::private_type()) {} + void A::test2(B::private_type x) {} // expected-error {{'private_type' is a private member of 'test11::B'}} + void A::test3(int x = B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}} +} + + +// PR9221 +namespace test12 { + struct A { + void foo(); + }; + class B : private A { + friend void A::foo(); + void *mem; + }; + void A::foo() { + void *var = static_cast<B*>(this)->mem; + } +} + +namespace PR9103 { + struct base { + protected: + static void foo(void) {} + }; + + struct cls: base { + friend void bar(void) { + base::foo(); + } + }; +} diff --git a/clang/test/CXX/class.access/class.friend/p11.cpp b/clang/test/CXX/class.access/class.friend/p11.cpp new file mode 100644 index 0000000..a05b2d2 --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p11.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// rdar://problem/8540720 +namespace test0 { + void foo() { + void bar(); + class A { + friend void bar(); + }; + } +} + +namespace test1 { + void foo() { + class A { + friend void bar(); // expected-error {{no matching function found in local scope}} + }; + } +} diff --git a/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp b/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp new file mode 100644 index 0000000..f8cabfd --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p2-cxx03.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +template<typename T> +class X0 { + friend T; // expected-warning{{non-class friend type 'T' is a C++11 extension}} +}; + +class X1 { }; +enum E1 { }; +X0<X1> x0a; +X0<X1 *> x0b; +X0<int> x0c; +X0<E1> x0d; + diff --git a/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp b/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp new file mode 100644 index 0000000..00fc0a3 --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p3-cxx0x.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s +template<typename T> +class X0 { + friend T; +}; + +class Y1 { }; +enum E1 { }; +X0<Y1> x0a; +X0<Y1 *> x0b; +X0<int> x0c; +X0<E1> x0d; + +template<typename T> +class X1 { + friend typename T::type; // expected-error{{no type named 'type' in 'Y1'}} +}; + +struct Y2 { + struct type { }; +}; + +struct Y3 { + typedef int type; +}; + +X1<Y2> x1a; +X1<Y3> x1b; +X1<Y1> x1c; // expected-note{{in instantiation of template class 'X1<Y1>' requested here}} diff --git a/clang/test/CXX/class.access/class.friend/p6.cpp b/clang/test/CXX/class.access/class.friend/p6.cpp new file mode 100644 index 0000000..7f7d909 --- /dev/null +++ b/clang/test/CXX/class.access/class.friend/p6.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void f1(); + +struct X { + void f2(); +}; + +struct Y { + friend void ::f1() { } // expected-error{{friend function definition cannot be qualified with '::'}} + friend void X::f2() { } // expected-error{{friend function definition cannot be qualified with 'X::'}} +}; + +void local() { + void f(); + + struct Local { + friend void f() { } // expected-error{{friend function cannot be defined in a local class}} + }; +} diff --git a/clang/test/CXX/class.access/class.protected/p1-cxx11.cpp b/clang/test/CXX/class.access/class.protected/p1-cxx11.cpp new file mode 100644 index 0000000..dc9b20d --- /dev/null +++ b/clang/test/CXX/class.access/class.protected/p1-cxx11.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// PR12497 +namespace test0 { + class A { + protected: + A() {} + A(const A &) {} + ~A() {} + A &operator=(const A &a) { return *this; } + }; + + class B : public A {}; + + void test() { + B b1; + B b2 = b1; + b1 = b2; + } +} diff --git a/clang/test/CXX/class.access/class.protected/p1.cpp b/clang/test/CXX/class.access/class.protected/p1.cpp new file mode 100644 index 0000000..c9491e1 --- /dev/null +++ b/clang/test/CXX/class.access/class.protected/p1.cpp @@ -0,0 +1,519 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +namespace test0 { + class A { + protected: int x; // expected-note 3 {{declared}} \ + // expected-note {{member is declared here}} + static int sx; // expected-note 3 {{declared}} \ + // expected-note {{member is declared here}} + }; + class B : public A { + }; + class C : protected A { // expected-note {{declared}} + }; + class D : private B { // expected-note 3 {{constrained}} + }; + + void test(A &a) { + (void) a.x; // expected-error {{'x' is a protected member}} + (void) a.sx; // expected-error {{'sx' is a protected member}} + } + void test(B &b) { + (void) b.x; // expected-error {{'x' is a protected member}} + (void) b.sx; // expected-error {{'sx' is a protected member}} + } + void test(C &c) { + (void) c.x; // expected-error {{'x' is a protected member}} expected-error {{protected base class}} + (void) c.sx; // expected-error {{'sx' is a protected member}} + } + void test(D &d) { + (void) d.x; // expected-error {{'x' is a private member}} expected-error {{private base class}} + (void) d.sx; // expected-error {{'sx' is a private member}} + } +} + +namespace test1 { + class A { + protected: int x; + static int sx; + static void test(A&); + }; + class B : public A { + static void test(B&); + }; + class C : protected A { + static void test(C&); + }; + class D : private B { + static void test(D&); + }; + + void A::test(A &a) { + (void) a.x; + (void) a.sx; + } + void B::test(B &b) { + (void) b.x; + (void) b.sx; + } + void C::test(C &c) { + (void) c.x; + (void) c.sx; + } + void D::test(D &d) { + (void) d.x; + (void) d.sx; + } +} + +namespace test2 { + class A { + protected: int x; // expected-note 3 {{can only access this member on an object of type}} + static int sx; + static void test(A&); + }; + class B : public A { + static void test(A&); + }; + class C : protected A { + static void test(A&); + }; + class D : private B { + static void test(A&); + }; + + void A::test(A &a) { + (void) a.x; + (void) a.sx; + } + void B::test(A &a) { + (void) a.x; // expected-error {{'x' is a protected member}} + (void) a.sx; + } + void C::test(A &a) { + (void) a.x; // expected-error {{'x' is a protected member}} + (void) a.sx; + } + void D::test(A &a) { + (void) a.x; // expected-error {{'x' is a protected member}} + (void) a.sx; + } +} + +namespace test3 { + class B; + class A { + protected: int x; //expected-note {{declared protected}} // expected-note {{can only access this member on an object of type}} + static int sx; + static void test(B&); + }; + class B : public A { + static void test(B&); + }; + class C : protected A { + static void test(B&); + }; + class D : private B { + static void test(B&); + }; + + void A::test(B &b) { + (void) b.x; + (void) b.sx; + } + void B::test(B &b) { + (void) b.x; + (void) b.sx; + } + void C::test(B &b) { + (void) b.x; // expected-error {{'x' is a protected member}} + (void) b.sx; + } + void D::test(B &b) { + (void) b.x; // expected-error {{'x' is a protected member}} + (void) b.sx; + } +} + +namespace test4 { + class C; + class A { + protected: int x; // expected-note 2{{declared protected here}} expected-note{{member is declared here}} + static int sx; // expected-note 3{{member is declared here}} + static void test(C&); + }; + class B : public A { + static void test(C&); + }; + class C : protected A { // expected-note 4 {{constrained}} expected-note 3 {{declared}} + static void test(C&); + }; + class D : private B { + static void test(C&); + }; + + void A::test(C &c) { + (void) c.x; // expected-error {{'x' is a protected member}} \ + // expected-error {{protected base class}} + (void) c.sx; // expected-error {{'sx' is a protected member}} + } + void B::test(C &c) { + (void) c.x; // expected-error {{'x' is a protected member}} \ + // expected-error {{protected base class}} + (void) c.sx; // expected-error {{'sx' is a protected member}} + } + void C::test(C &c) { + (void) c.x; + (void) c.sx; + } + void D::test(C &c) { + (void) c.x; // expected-error {{'x' is a protected member}} \ + // expected-error {{protected base class}} + (void) c.sx; // expected-error {{'sx' is a protected member}} + } +} + +namespace test5 { + class D; + class A { + protected: int x; // expected-note 3{{member is declared here}} + static int sx; // expected-note 3{{member is declared here}} + static void test(D&); + }; + class B : public A { + static void test(D&); + }; + class C : protected A { + static void test(D&); + }; + class D : private B { // expected-note 9 {{constrained}} + static void test(D&); + }; + + void A::test(D &d) { + (void) d.x; // expected-error {{'x' is a private member}} \ + // expected-error {{cannot cast}} + (void) d.sx; // expected-error {{'sx' is a private member}} + } + void B::test(D &d) { + (void) d.x; // expected-error {{'x' is a private member}} \ + // expected-error {{cannot cast}} + (void) d.sx; // expected-error {{'sx' is a private member}} + } + void C::test(D &d) { + (void) d.x; // expected-error {{'x' is a private member}} \ + // expected-error {{cannot cast}} + (void) d.sx; // expected-error {{'sx' is a private member}} + } + void D::test(D &d) { + (void) d.x; + (void) d.sx; + } +} + +namespace test6 { + class Static {}; + class A { + protected: + void foo(int); // expected-note 3 {{can only access this member on an object of type}} + void foo(long); + static void foo(Static); + + static void test(A&); + }; + class B : public A { + static void test(A&); + }; + class C : protected A { + static void test(A&); + }; + class D : private B { + static void test(A&); + }; + + void A::test(A &a) { + a.foo(10); + a.foo(Static()); + } + void B::test(A &a) { + a.foo(10); // expected-error {{'foo' is a protected member}} + a.foo(Static()); + } + void C::test(A &a) { + a.foo(10); // expected-error {{'foo' is a protected member}} + a.foo(Static()); + } + void D::test(A &a) { + a.foo(10); // expected-error {{'foo' is a protected member}} + a.foo(Static()); + } +} + +namespace test7 { + class Static {}; + class A { + protected: + void foo(int); // expected-note 3 {{must name member using the type of the current context}} + void foo(long); + static void foo(Static); + + static void test(); + }; + class B : public A { + static void test(); + }; + class C : protected A { + static void test(); + }; + class D : private B { + static void test(); + }; + + void A::test() { + void (A::*x)(int) = &A::foo; + void (*sx)(Static) = &A::foo; + } + void B::test() { + void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}} + void (*sx)(Static) = &A::foo; + } + void C::test() { + void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}} + void (*sx)(Static) = &A::foo; + } + void D::test() { + void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}} + void (*sx)(Static) = &A::foo; + } +} + +namespace test8 { + class Static {}; + class A { + protected: + void foo(int); // expected-note 3 {{must name member using the type of the current context}} + void foo(long); + static void foo(Static); + + static void test(); + }; + class B : public A { + static void test(); + }; + class C : protected A { + static void test(); + }; + class D : private B { + static void test(); + }; + void call(void (A::*)(int)); + void calls(void (*)(Static)); + + void A::test() { + call(&A::foo); + calls(&A::foo); + } + void B::test() { + call(&A::foo); // expected-error {{'foo' is a protected member}} + calls(&A::foo); + } + void C::test() { + call(&A::foo); // expected-error {{'foo' is a protected member}} + calls(&A::foo); + } + void D::test() { + call(&A::foo); // expected-error {{'foo' is a protected member}} + calls(&A::foo); + } +} + +namespace test9 { + class A { // expected-note {{member is declared here}} + protected: int foo(); // expected-note 4 {{declared}} expected-note 2 {{can only access this member on an object of type}} expected-note {{member is declared here}} + }; + + class B : public A { // expected-note {{member is declared here}} + friend class D; + }; + + class C : protected B { // expected-note {{declared}} \ + // expected-note 9 {{constrained}} + }; + + class D : public A { + static void test(A &a) { + a.foo(); // expected-error {{'foo' is a protected member}} + a.A::foo(); // expected-error {{'foo' is a protected member}} + a.B::foo(); + a.C::foo(); // expected-error {{'foo' is a protected member}} + } + + static void test(B &b) { + b.foo(); + b.A::foo(); + b.B::foo(); + b.C::foo(); // expected-error {{'foo' is a protected member}} + } + + static void test(C &c) { + c.foo(); // expected-error {{'foo' is a protected member}} \ + // expected-error {{cannot cast}} + c.A::foo(); // expected-error {{'A' is a protected member}} \ + // expected-error {{cannot cast}} + c.B::foo(); // expected-error {{'B' is a protected member}} \ + // expected-error {{cannot cast}} + c.C::foo(); // expected-error {{'foo' is a protected member}} \ + // expected-error {{cannot cast}} + } + + static void test(D &d) { + d.foo(); + d.A::foo(); + d.B::foo(); + d.C::foo(); // expected-error {{'foo' is a protected member}} + } + }; +} + +namespace test10 { + template<typename T> class A { + protected: + int foo(); + int foo() const; + + ~A() { foo(); } + }; + + template class A<int>; +} + +// rdar://problem/8360285: class.protected friendship +namespace test11 { + class A { + protected: + int foo(); + }; + + class B : public A { + friend class C; + }; + + class C { + void test() { + B b; + b.A::foo(); + } + }; +} + +// This friendship is considered because a public member of A would be +// a private member of C. +namespace test12 { + class A { protected: int foo(); }; + class B : public virtual A {}; + class C : private B { friend void test(); }; + class D : private C, public virtual A {}; + + void test() { + D d; + d.A::foo(); + } +} + +// This friendship is not considered because a public member of A is +// inaccessible in C. +namespace test13 { + class A { protected: int foo(); }; // expected-note {{can only access this member on an object of type}} + class B : private virtual A {}; + class C : private B { friend void test(); }; + class D : public virtual A {}; + + void test() { + D d; + d.A::foo(); // expected-error {{protected member}} + } +} + +// PR8058 +namespace test14 { + class A { + protected: + template <class T> void temp(T t); // expected-note {{must name member using the type of the current context}} + + void nontemp(int); // expected-note {{must name member using the type of the current context}} + + template <class T> void ovl_temp(T t); // expected-note {{must name member using the type of the current context}} + void ovl_temp(float); + + void ovl_nontemp(int); // expected-note {{must name member using the type of the current context}} + void ovl_nontemp(float); + + template <class T> void ovl_withtemp(T); + void ovl_withtemp(int); // expected-note {{must name member using the type of the current context}} + }; + + class B : public A { + void use() { + void (A::*ptr)(int); + ptr = &A::temp; // expected-error {{protected member}} + ptr = &A::nontemp; // expected-error {{protected member}} + ptr = &A::ovl_temp; // expected-error {{protected member}} + ptr = &A::ovl_nontemp; // expected-error {{protected member}} + ptr = &A::ovl_withtemp; // expected-error {{protected member}} + } + }; +} + +namespace test15 { + class A { + protected: + A(); // expected-note 2 {{protected constructor can only be used to construct a base class subobject}} + A(const A &); // expected-note {{protected constructor can only be used to construct a base class subobject}} + ~A(); // expected-note 3 {{protected destructor can only be used to destroy a base class subobject}} + }; + + class B : public A { + // The uses here are fine. + B() {} + B(int i) : A() {} + ~B() {} + + // All these uses are bad. + + void test0() { + A a; // expected-error {{protected constructor}} expected-error {{protected destructor}} + } + + A *test1() { + return new A(); // expected-error {{protected constructor}} + } + + void test2(A *a) { + delete a; // expected-error {{protected destructor}} + } + + A test3(A *a) { + return *a; // expected-error {{protected constructor}} + } + + void test4(A *a) { + a->~A(); // expected-error {{protected member}} + } + }; +} + +namespace test16 { + class A { + protected: + ~A(); + }; + + class B : public virtual A { + public: + ~B() {} + }; + + class C : public B { + ~C() {} + }; +} diff --git a/clang/test/CXX/class.access/p4.cpp b/clang/test/CXX/class.access/p4.cpp new file mode 100644 index 0000000..5ad738b --- /dev/null +++ b/clang/test/CXX/class.access/p4.cpp @@ -0,0 +1,510 @@ +// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s + +// C++0x [class.access]p4: + +// Access control is applied uniformly to all names, whether the +// names are referred to from declarations or expressions. In the +// case of overloaded function names, access control is applied to +// the function selected by overload resolution. + +class Public {} PublicInst; +class Protected {} ProtectedInst; +class Private {} PrivateInst; + +namespace test0 { + class A { + public: + void foo(Public&); + protected: + void foo(Protected&); // expected-note 2 {{declared protected here}} + private: + void foo(Private&); // expected-note 2 {{declared private here}} + }; + + void test(A *op) { + op->foo(PublicInst); + op->foo(ProtectedInst); // expected-error {{'foo' is a protected member}} + op->foo(PrivateInst); // expected-error {{'foo' is a private member}} + + void (A::*a)(Public&) = &A::foo; + void (A::*b)(Protected&) = &A::foo; // expected-error {{'foo' is a protected member}} + void (A::*c)(Private&) = &A::foo; // expected-error {{'foo' is a private member}} + } +} + +// Member operators. +namespace test1 { + class A { + public: + void operator+(Public&); + void operator[](Public&); + void operator()(Public&); + typedef void (*PublicSurrogate)(Public&); + operator PublicSurrogate() const; + protected: + void operator+(Protected&); // expected-note {{declared protected here}} + void operator[](Protected&); // expected-note {{declared protected here}} + void operator()(Protected&); // expected-note {{declared protected here}} + typedef void (*ProtectedSurrogate)(Protected&); + operator ProtectedSurrogate() const; // expected-note {{declared protected here}} + private: + void operator+(Private&); // expected-note {{declared private here}} + void operator[](Private&); // expected-note {{declared private here}} + void operator()(Private&); // expected-note {{declared private here}} + void operator-(); // expected-note {{declared private here}} + typedef void (*PrivateSurrogate)(Private&); + operator PrivateSurrogate() const; // expected-note {{declared private here}} + }; + void operator+(const A &, Public&); + void operator+(const A &, Protected&); + void operator+(const A &, Private&); + void operator-(const A &); + + void test(A &a, Public &pub, Protected &prot, Private &priv) { + a + pub; + a + prot; // expected-error {{'operator+' is a protected member}} + a + priv; // expected-error {{'operator+' is a private member}} + a[pub]; + a[prot]; // expected-error {{'operator[]' is a protected member}} + a[priv]; // expected-error {{'operator[]' is a private member}} + a(pub); + a(prot); // expected-error {{'operator()' is a protected member}} + a(priv); // expected-error {{'operator()' is a private member}} + -a; // expected-error {{'operator-' is a private member}} + + const A &ca = a; + ca + pub; + ca + prot; + ca + priv; + -ca; + // These are all surrogate calls + ca(pub); + ca(prot); // expected-error {{'operator void (*)(class Protected &)' is a protected member}} + ca(priv); // expected-error {{'operator void (*)(class Private &)' is a private member}} + } +} + +// Implicit constructor calls. +namespace test2 { + class A { + private: + A(); // expected-note 3 {{declared private here}} + + static A foo; + }; + + A a; // expected-error {{calling a private constructor}} + A A::foo; // okay + + class B : A { }; // expected-error {{base class 'test2::A' has private default constructor}} + B b; // expected-note{{implicit default constructor}} + + class C : virtual A { + public: + C(); + }; + + class D : C { }; // expected-error {{inherited virtual base class 'test2::A' has private default constructor}} + D d; // expected-note{{implicit default constructor}} +} + +// Implicit destructor calls. +namespace test3 { + class A { + private: + ~A(); // expected-note 2 {{declared private here}} + static A foo; + }; + + A a; // expected-error {{variable of type 'test3::A' has private destructor}} + A A::foo; + + void foo(A param) { // okay + A local; // expected-error {{variable of type 'test3::A' has private destructor}} + } + + template <unsigned N> class Base { ~Base(); }; // expected-note 14 {{declared private here}} + class Base2 : virtual Base<2> { ~Base2(); }; // expected-note 3 {{declared private here}} \ + // expected-error {{base class 'Base<2>' has private destructor}} + class Base3 : virtual Base<3> { public: ~Base3(); }; // expected-error {{base class 'Base<3>' has private destructor}} + + // These don't cause diagnostics because we don't need the destructor. + class Derived0 : Base<0> { ~Derived0(); }; + class Derived1 : Base<1> { }; + + class Derived2 : // expected-error {{inherited virtual base class 'Base<2>' has private destructor}} \ + // expected-error {{inherited virtual base class 'Base<3>' has private destructor}} + Base<0>, // expected-error {{base class 'Base<0>' has private destructor}} + virtual Base<1>, // expected-error {{base class 'Base<1>' has private destructor}} + Base2, // expected-error {{base class 'test3::Base2' has private destructor}} + virtual Base3 + { + ~Derived2() {} + }; + + class Derived3 : // expected-error 2 {{inherited virtual base class 'Base<2>' has private destructor}} \ + // expected-error 2 {{inherited virtual base class 'Base<3>' has private destructor}} \ + // expected-note 2{{implicit default constructor}} + Base<0>, // expected-error 2 {{base class 'Base<0>' has private destructor}} + virtual Base<1>, // expected-error 2 {{base class 'Base<1>' has private destructor}} + Base2, // expected-error 2 {{base class 'test3::Base2' has private destructor}} + virtual Base3 + {}; + Derived3 d3; // expected-note {{implicit default constructor}}\ + // expected-note{{implicit default destructor}}} +} + +// Conversion functions. +namespace test4 { + class Base { + private: + operator Private(); // expected-note 4 {{declared private here}} + public: + operator Public(); // expected-note 2{{member is declared here}} + }; + + class Derived1 : private Base { // expected-note 2 {{declared private here}} \ + // expected-note {{constrained by private inheritance}} + Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} + Public test2() { return *this; } + }; + Private test1(Derived1 &d) { return d; } // expected-error {{'operator Private' is a private member}} \ + // expected-error {{cannot cast 'test4::Derived1' to its private base class}} + Public test2(Derived1 &d) { return d; } // expected-error {{cannot cast 'test4::Derived1' to its private base class}} \ + // expected-error {{'operator Public' is a private member}} + + + class Derived2 : public Base { + Private test1() { return *this; } // expected-error {{'operator Private' is a private member}} + Public test2() { return *this; } + }; + Private test1(Derived2 &d) { return d; } // expected-error {{'operator Private' is a private member}} + Public test2(Derived2 &d) { return d; } + + class Derived3 : private Base { // expected-note {{constrained by private inheritance here}} \ + // expected-note {{declared private here}} + public: + operator Private(); + }; + Private test1(Derived3 &d) { return d; } + Public test2(Derived3 &d) { return d; } // expected-error {{'operator Public' is a private member of 'test4::Base'}} \ + // expected-error {{cannot cast 'test4::Derived3' to its private base class}} + + class Derived4 : public Base { + public: + operator Private(); + }; + Private test1(Derived4 &d) { return d; } + Public test2(Derived4 &d) { return d; } +} + +// Implicit copy assignment operator uses. +namespace test5 { + class A { + void operator=(const A &); // expected-note 2 {{implicitly declared private here}} + }; + + class Test1 { A a; }; // expected-error {{private member}} + void test1() { + Test1 a; + a = Test1(); // expected-note{{implicit default copy}} + } + + class Test2 : A {}; // expected-error {{private member}} + void test2() { + Test2 a; + a = Test2(); // expected-note{{implicit default copy}} + } +} + +// Implicit copy constructor uses. +namespace test6 { + class A { + public: A(); + private: A(const A &); // expected-note 2 {{declared private here}} + }; + + class Test1 { A a; }; // expected-error {{field of type 'test6::A' has private copy constructor}} + void test1(const Test1 &t) { + Test1 a = t; // expected-note{{implicit default copy}} + } + + class Test2 : A {}; // expected-error {{base class 'test6::A' has private copy constructor}} + void test2(const Test2 &t) { + Test2 a = t; // expected-note{{implicit default copy}} + } +} + +// Redeclaration lookups are not accesses. +namespace test7 { + class A { + int private_member; + }; + class B : A { + int foo(int private_member) { + return 0; + } + }; +} + +// Ignored operator new and delete overloads are not +namespace test8 { + typedef __typeof__(sizeof(int)) size_t; + + class A { + void *operator new(size_t s); + void operator delete(void *p); + public: + void *operator new(size_t s, int n); + void operator delete(void *p, int n); + }; + + void test() { + new (2) A(); + } +} + +// Don't silently upgrade forbidden-access paths to private. +namespace test9 { + class A { + public: static int x; // expected-note {{member is declared here}} + }; + class B : private A { // expected-note {{constrained by private inheritance here}} + }; + class C : public B { + static int getX() { return x; } // expected-error {{'x' is a private member of 'test9::A'}} + }; +} + +namespace test10 { + class A { + enum { + value = 10 // expected-note {{declared private here}} + }; + friend class C; + }; + + class B { + enum { + value = A::value // expected-error {{'value' is a private member of 'test10::A'}} + }; + }; + + class C { + enum { + value = A::value + }; + }; +} + +namespace test11 { + class A { + protected: virtual ~A(); + }; + + class B : public A { + ~B(); + }; + + B::~B() {}; +} + +namespace test12 { + class A { + int x; + + void foo() { + class Local { + int foo(A *a) { + return a->x; + } + }; + } + }; +} + +namespace test13 { + struct A { + int x; + unsigned foo() const; + }; + + struct B : protected A { + using A::foo; + using A::x; + }; + + void test() { + A *d; + d->foo(); + (void) d->x; + } +} + +// Destructors for temporaries. +namespace test14 { + class A { + private: ~A(); // expected-note {{declared private here}} + }; + A foo(); + + void test() { + foo(); // expected-error {{temporary of type 'test14::A' has private destructor}} + } + + class X { + ~X(); // expected-note {{declared private here}} + }; + + struct Y1 { + operator X(); + }; + + void g() { + const X &xr = Y1(); // expected-error{{temporary of type 'test14::X' has private destructor}} + } +} + +// PR 7024 +namespace test15 { + template <class T> class A { + private: + int private_foo; // expected-note {{declared private here}} + static int private_sfoo; // expected-note {{declared private here}} + protected: + int protected_foo; // expected-note 3 {{declared protected here}} // expected-note {{can only access this member on an object of type 'test15::B<int>'}} + static int protected_sfoo; // expected-note 3 {{declared protected here}} + + int test1(A<int> &a) { + return a.private_foo; // expected-error {{private member}} + } + + int test2(A<int> &a) { + return a.private_sfoo; // expected-error {{private member}} + } + + int test3(A<int> &a) { + return a.protected_foo; // expected-error {{protected member}} + } + + int test4(A<int> &a) { + return a.protected_sfoo; // expected-error {{protected member}} + } + }; + + template class A<int>; + template class A<long>; // expected-note 4 {{in instantiation}} + + template <class T> class B : public A<T> { + // TODO: These first two accesses can be detected as ill-formed at + // definition time because they're member accesses and A<int> can't + // be a subclass of B<T> for any T. + + int test1(A<int> &a) { + return a.protected_foo; // expected-error 2 {{protected member}} + } + + int test2(A<int> &a) { + return a.protected_sfoo; // expected-error {{protected member}} + } + + int test3(B<int> &b) { + return b.protected_foo; // expected-error {{protected member}} + } + + int test4(B<int> &b) { + return b.protected_sfoo; // expected-error {{protected member}} + } + }; + + template class B<int>; // expected-note {{in instantiation}} + template class B<long>; // expected-note 4 {{in instantiation}} +} + +// PR7281 +namespace test16 { + class A { ~A(); }; // expected-note 2{{declared private here}} + void b() { throw A(); } // expected-error{{temporary of type 'test16::A' has private destructor}} \ + // expected-error{{exception object of type 'test16::A' has private destructor}} +} + +// rdar://problem/8146294 +namespace test17 { + class A { + template <typename T> class Inner { }; // expected-note {{declared private here}} + }; + + A::Inner<int> s; // expected-error {{'Inner' is a private member of 'test17::A'}} +} + +namespace test18 { + template <class T> class A {}; + class B : A<int> { + A<int> member; + }; + + // FIXME: this access to A should be forbidden (because C++ is dumb), + // but LookupResult can't express the necessary information to do + // the check, so we aggressively suppress access control. + class C : B { + A<int> member; + }; +} + +// PR8325 +namespace test19 { + class A { ~A(); }; + // The destructor is not implicitly referenced here. Contrast to test16, + // testing PR7281, earlier in this file. + void b(A* x) { throw x; } +} + +// PR7930 +namespace test20 { + class Foo { + Foo(); // expected-note {{implicitly declared private here}} + }; + Foo::Foo() {} + + void test() { + Foo a; // expected-error {{calling a private constructor}} + } +} + +namespace test21 { + template <class T> class A { + void foo(); + void bar(); + class Inner; // expected-note {{implicitly declared private here}} + public: + void baz(); + }; + template <class T> class A<T>::Inner {}; + class B { + template <class T> class A<T>::Inner; // expected-error{{non-friend class member 'Inner' cannot have a qualified name}} + }; + + void test() { + A<int>::Inner i; // expected-error {{'Inner' is a private member}} + } +} + +namespace rdar8876150 { + struct A { operator bool(); }; + struct B : private A { using A::operator bool; }; + + bool f() { + B b; + return !b; + } +} + +namespace test23 { + template <typename T> class A { + A(); + static A instance; + }; + + template <typename T> A<T> A<T>::instance; + template class A<int>; +} diff --git a/clang/test/CXX/class.access/p6.cpp b/clang/test/CXX/class.access/p6.cpp new file mode 100644 index 0000000..fbdc87b --- /dev/null +++ b/clang/test/CXX/class.access/p6.cpp @@ -0,0 +1,192 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// C++0x [class.access]p6: +// All access controls in [class.access] affect the ability to +// access a class member name from a particular scope. For purposes +// of access control, the base-specifiers of a class and the +// definitions of class members that appear outside of the class +// definition are considered to be within the scope of that +// class. In particular, access controls apply as usual to member +// names accessed as part of a function return type, even though it +// is not possible to determine the access privileges of that use +// without first parsing the rest of the function +// declarator. Similarly, access control for implicit calls to the +// constructors, the conversion functions, or the destructor called +// to create and destroy a static data member is performed as if +// these calls appeared in the scope of the member's class. + +struct Public {}; struct Protected {}; struct Private {}; + +namespace test0 { + class A { + typedef int type; // expected-note {{declared private here}} + type foo(); + }; + + A::type foo() { } // expected-error {{'type' is a private member}} + A::type A::foo() { } +} + +// conversion decls +namespace test1 { + class A { + public: + A(); + operator Public (); + A(Public); + protected: + operator Protected (); // expected-note {{declared protected here}} + A(Protected); // expected-note {{declared protected here}} + private: + operator Private (); // expected-note {{declared private here}} + A(Private); // expected-note {{declared private here}} + }; + + void test() { + A a; + Public pub = a; + Protected prot = a; // expected-error {{'operator Protected' is a protected member}} + Private priv = a; // expected-error {{'operator Private' is a private member}} + A apub = pub; + A aprot = prot; // expected-error {{protected constructor}} + A apriv = priv; // expected-error {{private constructor}} + } +} + +// PR6967 +namespace test2 { + class A { + public: + template <class T> static void set(T &t, typename T::type v) { + t.value = v; + } + template <class T> static typename T::type get(const T &t) { + return t.value; + } + }; + + class B { + friend class A; + + private: + typedef int type; + type value; + }; + + int test() { + B b; + A::set(b, 0); + return A::get(b); + } +} + +namespace test3 { + class Green {}; class Blue {}; + + // We have to wrap this in a class because a partial specialization + // isn't actually in the context of the template. + struct Outer { + template <class T, class Nat> class A { + }; + }; + + template <class T> class Outer::A<T, typename T::nature> { + public: + static void foo(); + }; + + class B { + private: typedef Green nature; + friend class Outer; + }; + + void test() { + Outer::A<B, Green>::foo(); + Outer::A<B, Blue>::foo(); // expected-error {{no member named 'foo'}} + } +} + +namespace test4 { + template <class T> class A { + private: typedef int type; + template <class U> friend void foo(U &, typename U::type); + }; + + template <class U> void foo(U &, typename U::type) {} + + void test() { + A<int> a; + foo(a, 0); + } +} + +// PR7644 +namespace test5 { + class A { + enum Enum { E0, E1, E2 }; // expected-note 4 {{declared private here}} + template <Enum> void foo(); + template <Enum> class bar; + }; + + template <A::Enum en> void A::foo() {} + template <A::Enum en> class A::bar {}; + + template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}} + template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}} + + class B { + template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}} + template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}} + }; +} + +namespace test6 { + class A { + public: class public_inner {}; + protected: class protected_inner {}; + private: class private_inner {}; // expected-note {{declared private here}} + }; + + class B : A { + public_inner a; + protected_inner b; + private_inner c; // expected-error {{'private_inner' is a private member of 'test6::A'}} + }; +} + +// PR9229 +namespace test7 { + void foo(int arg[1]); + class A { + void check(); + }; + class B { + friend class A; + A ins; + }; + void A::check() { + void foo(int arg[__builtin_offsetof(B, ins)]); + } +} + +// rdar://problem/10155256 +namespace test8 { + class A { + typedef void* (A::*UnspecifiedBoolType)() const; + operator UnspecifiedBoolType() const; // expected-note {{implicitly declared private here}} + }; + + void test(A &a) { + if (a) return; // expected-error {{'operator void *(class test8::A::*)(void) const' is a private member of 'test8::A'}} + } +} + +namespace test9 { + class A { + operator char*() const; // expected-note {{implicitly declared private here}} + }; + + void test(A &a) { + delete a; // expected-error {{'operator char *' is a private member of 'test9::A'}} + } +} |