p6.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. // C++0x [class.access]p6:
  3. // All access controls in [class.access] affect the ability to
  4. // access a class member name from a particular scope. For purposes
  5. // of access control, the base-specifiers of a class and the
  6. // definitions of class members that appear outside of the class
  7. // definition are considered to be within the scope of that
  8. // class. In particular, access controls apply as usual to member
  9. // names accessed as part of a function return type, even though it
  10. // is not possible to determine the access privileges of that use
  11. // without first parsing the rest of the function
  12. // declarator. Similarly, access control for implicit calls to the
  13. // constructors, the conversion functions, or the destructor called
  14. // to create and destroy a static data member is performed as if
  15. // these calls appeared in the scope of the member's class.
  16. struct Public {}; struct Protected {}; struct Private {};
  17. namespace test0 {
  18. class A {
  19. typedef int type; // expected-note {{declared private here}}
  20. type foo();
  21. };
  22. A::type foo() { } // expected-error {{'type' is a private member}}
  23. A::type A::foo() { }
  24. }
  25. // conversion decls
  26. namespace test1 {
  27. class A {
  28. public:
  29. A();
  30. operator Public ();
  31. A(Public);
  32. protected:
  33. operator Protected (); // expected-note {{declared protected here}}
  34. A(Protected); // expected-note {{declared protected here}}
  35. private:
  36. operator Private (); // expected-note {{declared private here}}
  37. A(Private); // expected-note {{declared private here}}
  38. };
  39. void test() {
  40. A a;
  41. Public pub = a;
  42. Protected prot = a; // expected-error {{'operator Protected' is a protected member}}
  43. Private priv = a; // expected-error {{'operator Private' is a private member}}
  44. A apub = pub;
  45. A aprot = prot; // expected-error {{protected constructor}}
  46. A apriv = priv; // expected-error {{private constructor}}
  47. }
  48. }
  49. // PR6967
  50. namespace test2 {
  51. class A {
  52. public:
  53. template <class T> static void set(T &t, typename T::type v) {
  54. t.value = v;
  55. }
  56. template <class T> static typename T::type get(const T &t) {
  57. return t.value;
  58. }
  59. };
  60. class B {
  61. friend class A;
  62. private:
  63. typedef int type;
  64. type value;
  65. };
  66. int test() {
  67. B b;
  68. A::set(b, 0);
  69. return A::get(b);
  70. }
  71. }
  72. namespace test3 {
  73. class Green {}; class Blue {};
  74. // We have to wrap this in a class because a partial specialization
  75. // isn't actually in the context of the template.
  76. struct Outer {
  77. template <class T, class Nat> class A {
  78. };
  79. };
  80. template <class T> class Outer::A<T, typename T::nature> {
  81. public:
  82. static void foo(); // expected-note {{'Outer::A<B, Green>::foo' declared here}}
  83. };
  84. class B {
  85. private: typedef Green nature;
  86. friend class Outer;
  87. };
  88. void test() {
  89. Outer::A<B, Green>::foo();
  90. Outer::A<B, Blue>::foo(); // expected-error {{no member named 'foo' in 'test3::Outer::A<test3::B, test3::Blue>'; did you mean 'Outer::A<B, Green>::foo'?}}
  91. }
  92. }
  93. namespace test4 {
  94. template <class T> class A {
  95. private: typedef int type;
  96. template <class U> friend void foo(U &, typename U::type);
  97. };
  98. template <class U> void foo(U &, typename U::type) {}
  99. void test() {
  100. A<int> a;
  101. foo(a, 0);
  102. }
  103. }
  104. // PR7644
  105. namespace test5 {
  106. class A {
  107. enum Enum { E0, E1, E2 }; // expected-note 4 {{declared private here}}
  108. template <Enum> void foo();
  109. template <Enum> class bar;
  110. };
  111. template <A::Enum en> void A::foo() {}
  112. template <A::Enum en> class A::bar {};
  113. template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}}
  114. template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}}
  115. class B {
  116. template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}}
  117. template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}}
  118. };
  119. }
  120. namespace test6 {
  121. class A {
  122. public: class public_inner {};
  123. protected: class protected_inner {};
  124. private: class private_inner {}; // expected-note {{declared private here}}
  125. };
  126. class B : A {
  127. public_inner a;
  128. protected_inner b;
  129. private_inner c; // expected-error {{'private_inner' is a private member of 'test6::A'}}
  130. };
  131. }
  132. // PR9229
  133. namespace test7 {
  134. void foo(int arg[1]);
  135. class A {
  136. void check();
  137. };
  138. class B {
  139. friend class A;
  140. A ins;
  141. };
  142. void A::check() {
  143. void foo(int arg[__builtin_offsetof(B, ins)]);
  144. }
  145. }
  146. // rdar://problem/10155256
  147. namespace test8 {
  148. class A {
  149. typedef void* (A::*UnspecifiedBoolType)() const;
  150. operator UnspecifiedBoolType() const; // expected-note {{implicitly declared private here}}
  151. };
  152. void test(A &a) {
  153. if (a) return; // expected-error-re {{'operator void *(test8::A::*)(){{( __attribute__\(\(thiscall\)\))?}} const' is a private member of 'test8::A'}}
  154. }
  155. }
  156. namespace test9 {
  157. class A {
  158. operator char*() const; // expected-note {{implicitly declared private here}}
  159. };
  160. void test(A &a) {
  161. delete a; // expected-error {{'operator char *' is a private member of 'test9::A'}}
  162. }
  163. }