John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -faccess-control -verify %s |
| 2 | |
| 3 | // C++0x [class.access]p4: |
| 4 | |
| 5 | // Access control is applied uniformly to all names, whether the |
| 6 | // names are referred to from declarations or expressions. In the |
| 7 | // case of overloaded function names, access control is applied to |
| 8 | // the function selected by overload resolution. |
| 9 | |
| 10 | class Public {} PublicInst; |
| 11 | class Protected {} ProtectedInst; |
| 12 | class Private {} PrivateInst; |
| 13 | |
| 14 | namespace test0 { |
| 15 | class A { |
| 16 | public: |
| 17 | void foo(Public&); |
| 18 | protected: |
| 19 | void foo(Protected&); // expected-note 2 {{declared protected here}} |
| 20 | private: |
| 21 | void foo(Private&); // expected-note 2 {{declared private here}} |
| 22 | }; |
| 23 | |
| 24 | void test(A *op) { |
| 25 | op->foo(PublicInst); |
| 26 | op->foo(ProtectedInst); // expected-error {{access to protected member outside any class}} |
| 27 | op->foo(PrivateInst); // expected-error {{access to private member outside any class}} |
| 28 | |
| 29 | void (A::*a)(Public&) = &A::foo; |
| 30 | void (A::*b)(Protected&) = &A::foo; // expected-error {{access to protected member outside any class}} |
| 31 | void (A::*c)(Private&) = &A::foo; // expected-error {{access to private member outside any class}} |
| 32 | } |
| 33 | } |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 34 | |
| 35 | // Member operators. |
| 36 | namespace test1 { |
| 37 | class A { |
| 38 | public: |
| 39 | void operator+(Public&); |
| 40 | void operator[](Public&); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 41 | void operator()(Public&); |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 42 | typedef void (*PublicSurrogate)(Public&); |
| 43 | operator PublicSurrogate() const; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 44 | protected: |
| 45 | void operator+(Protected&); // expected-note {{declared protected here}} |
| 46 | void operator[](Protected&); // expected-note {{declared protected here}} |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 47 | void operator()(Protected&); // expected-note {{declared protected here}} |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 48 | typedef void (*ProtectedSurrogate)(Protected&); |
| 49 | operator ProtectedSurrogate() const; // expected-note {{declared protected here}} |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 50 | private: |
| 51 | void operator+(Private&); // expected-note {{declared private here}} |
| 52 | void operator[](Private&); // expected-note {{declared private here}} |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 53 | void operator()(Private&); // expected-note {{declared private here}} |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 54 | void operator-(); // expected-note {{declared private here}} |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 55 | typedef void (*PrivateSurrogate)(Private&); |
| 56 | operator PrivateSurrogate() const; // expected-note {{declared private here}} |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 57 | }; |
| 58 | void operator+(const A &, Public&); |
| 59 | void operator+(const A &, Protected&); |
| 60 | void operator+(const A &, Private&); |
| 61 | void operator-(const A &); |
| 62 | |
| 63 | void test(A &a, Public &pub, Protected &prot, Private &priv) { |
| 64 | a + pub; |
| 65 | a + prot; // expected-error {{access to protected member}} |
| 66 | a + priv; // expected-error {{access to private member}} |
| 67 | a[pub]; |
| 68 | a[prot]; // expected-error {{access to protected member}} |
| 69 | a[priv]; // expected-error {{access to private member}} |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 70 | a(pub); |
| 71 | a(prot); // expected-error {{access to protected member}} |
| 72 | a(priv); // expected-error {{access to private member}} |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 73 | -a; // expected-error {{access to private member}} |
| 74 | |
| 75 | const A &ca = a; |
| 76 | ca + pub; |
| 77 | ca + prot; |
| 78 | ca + priv; |
| 79 | -ca; |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 80 | // These are all surrogate calls |
| 81 | ca(pub); |
| 82 | ca(prot); // expected-error {{access to protected member}} |
| 83 | ca(priv); // expected-error {{access to private member}} |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 84 | } |
| 85 | } |