p1.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. // This is just the test for [namespace.udecl]p4 with 'using'
  3. // uniformly stripped out.
  4. // C++03 [namespace.udecl]p4:
  5. // A using-declaration used as a member-declaration shall refer to a
  6. // member of a base class of the class being defined, shall refer to
  7. // a member of an anonymous union that is a member of a base class
  8. // of the class being defined, or shall refer to an enumerator for
  9. // an enumeration type that is a member of a base class of the class
  10. // being defined.
  11. // There is no directly analogous paragraph in C++0x, and the feature
  12. // works sufficiently differently there that it needs a separate test.
  13. namespace test0 {
  14. namespace NonClass {
  15. typedef int type;
  16. struct hiding {};
  17. int hiding;
  18. static union { double union_member; };
  19. enum tagname { enumerator };
  20. }
  21. class Test0 {
  22. NonClass::type; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}}
  23. NonClass::hiding; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}}
  24. NonClass::union_member; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}}
  25. NonClass::enumerator; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}}
  26. };
  27. }
  28. struct Opaque0 {};
  29. namespace test1 {
  30. struct A {
  31. typedef int type;
  32. struct hiding {}; // expected-note {{previous use is here}}
  33. Opaque0 hiding;
  34. union { double union_member; };
  35. enum tagname { enumerator };
  36. };
  37. struct B : A {
  38. A::type; // expected-warning {{access declarations are deprecated}}
  39. A::hiding; // expected-warning {{access declarations are deprecated}}
  40. A::union_member; // expected-warning {{access declarations are deprecated}}
  41. A::enumerator; // expected-warning {{access declarations are deprecated}}
  42. A::tagname; // expected-warning {{access declarations are deprecated}}
  43. void test0() {
  44. type t = 0;
  45. }
  46. void test1() {
  47. typedef struct A::hiding local;
  48. struct hiding _ = local();
  49. }
  50. void test2() {
  51. union hiding _; // expected-error {{tag type that does not match previous}}
  52. }
  53. void test3() {
  54. char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
  55. }
  56. void test4() {
  57. enum tagname _ = enumerator;
  58. }
  59. void test5() {
  60. Opaque0 _ = hiding;
  61. }
  62. };
  63. }
  64. namespace test2 {
  65. struct A {
  66. typedef int type;
  67. struct hiding {}; // expected-note {{previous use is here}}
  68. int hiding;
  69. union { double union_member; };
  70. enum tagname { enumerator };
  71. };
  72. template <class T> struct B : A {
  73. A::type; // expected-warning {{access declarations are deprecated}}
  74. A::hiding; // expected-warning {{access declarations are deprecated}}
  75. A::union_member; // expected-warning {{access declarations are deprecated}}
  76. A::enumerator; // expected-warning {{access declarations are deprecated}}
  77. A::tagname; // expected-warning {{access declarations are deprecated}}
  78. void test0() {
  79. type t = 0;
  80. }
  81. void test1() {
  82. typedef struct A::hiding local;
  83. struct hiding _ = local();
  84. }
  85. void test2() {
  86. union hiding _; // expected-error {{tag type that does not match previous}}
  87. }
  88. void test3() {
  89. char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
  90. }
  91. void test4() {
  92. enum tagname _ = enumerator;
  93. }
  94. void test5() {
  95. Opaque0 _ = hiding;
  96. }
  97. };
  98. }
  99. namespace test3 {
  100. struct hiding {};
  101. template <class T> struct A {
  102. typedef int type; // expected-note {{target of using declaration}}
  103. struct hiding {};
  104. Opaque0 hiding;
  105. union { double union_member; };
  106. enum tagname { enumerator }; // expected-note {{target of using declaration}}
  107. };
  108. template <class T> struct B : A<T> {
  109. A<T>::type; // expected-error {{dependent using declaration resolved to type without 'typename'}} // expected-warning {{access declarations are deprecated}}
  110. A<T>::hiding; // expected-warning {{access declarations are deprecated}}
  111. A<T>::union_member; // expected-warning {{access declarations are deprecated}}
  112. A<T>::enumerator; // expected-warning {{access declarations are deprecated}}
  113. A<T>::tagname; // expected-error {{dependent using declaration resolved to type without 'typename'}} // expected-warning {{access declarations are deprecated}}
  114. // FIXME: re-enable these when the various bugs involving tags are fixed
  115. #if 0
  116. void test1() {
  117. typedef struct A<T>::hiding local;
  118. struct hiding _ = local();
  119. }
  120. void test2() {
  121. typedef struct A<T>::hiding local;
  122. union hiding _ = local();
  123. }
  124. #endif
  125. void test3() {
  126. char array[sizeof(union_member) == sizeof(double) ? 1 : -1];
  127. }
  128. #if 0
  129. void test4() {
  130. enum tagname _ = enumerator;
  131. }
  132. #endif
  133. void test5() {
  134. Opaque0 _ = hiding;
  135. }
  136. };
  137. template struct B<int>; // expected-note {{in instantiation}}
  138. }
  139. namespace test4 {
  140. struct Base {
  141. int foo();
  142. };
  143. struct Unrelated {
  144. int foo();
  145. };
  146. struct Subclass : Base {
  147. };
  148. namespace InnerNS {
  149. int foo();
  150. }
  151. // We should be able to diagnose these without instantiation.
  152. template <class T> struct C : Base {
  153. InnerNS::foo; // expected-error {{not a class}} expected-warning {{access declarations are deprecated}}
  154. Base::bar; // expected-error {{no member named 'bar'}} expected-warning {{access declarations are deprecated}}
  155. Unrelated::foo; // expected-error {{not a base class}} expected-warning {{access declarations are deprecated}}
  156. C::foo; // legal in C++03 // expected-warning {{access declarations are deprecated}}
  157. Subclass::foo; // legal in C++03 // expected-warning {{access declarations are deprecated}}
  158. int bar(); //expected-note {{target of using declaration}}
  159. C::bar; // expected-error {{refers to its own class}} expected-warning {{access declarations are deprecated}}
  160. };
  161. }