cxx-class.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -fcxx-exceptions %s
  2. class C;
  3. class C {
  4. public:
  5. protected:
  6. typedef int A,B;
  7. static int sf(), u;
  8. struct S {};
  9. enum {}; // expected-warning{{declaration does not declare anything}}
  10. int; // expected-warning {{declaration does not declare anything}}
  11. int : 1, : 2;
  12. public:
  13. void m0() {}; // ok, one extra ';' is permitted
  14. void m1() {}
  15. ; // ok, one extra ';' is permitted
  16. void m() {
  17. int l = 2;
  18. };; // expected-warning{{extra ';' after member function definition}}
  19. template<typename T> void mt(T) { }
  20. ;
  21. ; // expected-warning{{extra ';' inside a class}}
  22. virtual int vf() const volatile = 0;
  23. virtual int vf0() = 0l; // expected-error {{does not look like a pure-specifier}}
  24. virtual int vf1() = 1; // expected-error {{does not look like a pure-specifier}}
  25. virtual int vf2() = 00; // expected-error {{does not look like a pure-specifier}}
  26. virtual int vf3() = 0x0; // expected-error {{does not look like a pure-specifier}}
  27. virtual int vf4() = 0.0; // expected-error {{does not look like a pure-specifier}}
  28. virtual int vf5(){0}; // expected-error +{{}} expected-warning {{unused}}
  29. virtual int vf5a(){0;}; // function definition, expected-warning {{unused}}
  30. virtual int vf6()(0); // expected-error +{{}} expected-note +{{}}
  31. virtual int vf7() = { 0 }; // expected-error {{does not look like a pure-specifier}}
  32. private:
  33. int x,f(),y,g();
  34. inline int h();
  35. static const int sci = 10;
  36. mutable int mi;
  37. };
  38. void glo()
  39. {
  40. struct local {};
  41. }
  42. // PR3177
  43. typedef union {
  44. __extension__ union {
  45. int a;
  46. float b;
  47. } y;
  48. } bug3177;
  49. // check that we don't consume the token after the access specifier
  50. // when it's not a colon
  51. class D {
  52. public // expected-error{{expected ':'}}
  53. int i;
  54. };
  55. // consume the token after the access specifier if it's a semicolon
  56. // that was meant to be a colon
  57. class E {
  58. public; // expected-error{{expected ':'}}
  59. int i;
  60. };
  61. class F {
  62. int F1 { return 1; } // expected-error{{function definition does not declare parameters}}
  63. void F2 {} // expected-error{{function definition does not declare parameters}}
  64. typedef int F3() { return 0; } // expected-error{{function definition declared 'typedef'}}
  65. typedef void F4() {} // expected-error{{function definition declared 'typedef'}}
  66. };
  67. namespace ctor_error {
  68. class Foo {};
  69. // By [class.qual]p2, this is a constructor declaration.
  70. Foo::Foo (F) = F(); // expected-error{{does not match any declaration in 'ctor_error::Foo'}}
  71. class Ctor { // expected-note{{not complete until the closing '}'}}
  72. Ctor(f)(int); // ok
  73. Ctor(g(int)); // ok
  74. Ctor(x[5]); // expected-error{{incomplete type}}
  75. Ctor(UnknownType *); // expected-error{{unknown type name 'UnknownType'}}
  76. void operator+(UnknownType*); // expected-error{{unknown type name 'UnknownType'}}
  77. };
  78. Ctor::Ctor (x) = { 0 }; // \
  79. // expected-error{{qualified reference to 'Ctor' is a constructor name}}
  80. Ctor::Ctor(UnknownType *) {} // \
  81. // expected-error{{unknown type name 'UnknownType'}}
  82. void Ctor::operator+(UnknownType*) {} // \
  83. // expected-error{{unknown type name 'UnknownType'}}
  84. }
  85. namespace nns_decl {
  86. struct A {
  87. struct B;
  88. };
  89. namespace N {
  90. union C;
  91. }
  92. struct A::B; // expected-error {{forward declaration of struct cannot have a nested name specifier}}
  93. union N::C; // expected-error {{forward declaration of union cannot have a nested name specifier}}
  94. }
  95. // PR13775: Don't assert here.
  96. namespace PR13775 {
  97. class bar
  98. {
  99. public:
  100. void foo ();
  101. void baz ();
  102. };
  103. void bar::foo ()
  104. {
  105. baz x(); // expected-error 3{{}}
  106. }
  107. }
  108. class pr16989 {
  109. void tpl_mem(int *) {
  110. return;
  111. class C2 {
  112. void f();
  113. };
  114. void C2::f() {} // expected-error{{function definition is not allowed here}}
  115. };
  116. };
  117. namespace CtorErrors {
  118. struct A {
  119. A(NonExistent); // expected-error {{unknown type name 'NonExistent'}}
  120. };
  121. struct B {
  122. B(NonExistent) : n(0) {} // expected-error {{unknown type name 'NonExistent'}}
  123. int n;
  124. };
  125. struct C {
  126. C(NonExistent) try {} catch (...) {} // expected-error {{unknown type name 'NonExistent'}}
  127. };
  128. struct D {
  129. D(NonExistent) {} // expected-error {{unknown type name 'NonExistent'}}
  130. };
  131. }
  132. namespace DtorErrors {
  133. struct A { ~A(); int n; } a;
  134. ~A::A() { n = 0; } // expected-error {{'~' in destructor name should be after nested name specifier}} expected-note {{previous}}
  135. A::~A() {} // expected-error {{redefinition}}
  136. struct B { ~B(); } *b;
  137. DtorErrors::~B::B() {} // expected-error {{'~' in destructor name should be after nested name specifier}}
  138. void f() {
  139. a.~A::A(); // expected-error {{'~' in destructor name should be after nested name specifier}}
  140. b->~DtorErrors::~B::B(); // expected-error {{'~' in destructor name should be after nested name specifier}}
  141. }
  142. struct C; // expected-note {{forward decl}}
  143. ~C::C() {} // expected-error {{incomplete}} expected-error {{'~' in destructor name should be after nested name specifier}}
  144. struct D { struct X {}; ~D() throw(X); };
  145. ~D::D() throw(X) {} // expected-error {{'~' in destructor name should be after nested name specifier}}
  146. ~Undeclared::Undeclared() {} // expected-error {{use of undeclared identifier 'Undeclared'}} expected-error {{'~' in destructor name should be after nested name specifier}}
  147. ~Undeclared:: {} // expected-error {{expected identifier}} expected-error {{'~' in destructor name should be after nested name specifier}}
  148. struct S {
  149. // For another struct's destructor, emit the same diagnostic like for
  150. // A::~A() in addition to the "~ in the wrong place" one.
  151. ~A::A() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{non-friend class member '~A' cannot have a qualified name}}
  152. A::~A() {} // expected-error {{non-friend class member '~A' cannot have a qualified name}}
  153. // An inline destructor with a redundant class name should also get the
  154. // same diagnostic as S::~S.
  155. ~S::S() {} // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{extra qualification on member '~S'}}
  156. // This just shouldn't crash.
  157. int I; // expected-note {{declared here}}
  158. ~I::I() {} // expected-error {{'I' is not a class, namespace, or enumeration}} expected-error {{'~' in destructor name should be after nested name specifier}}
  159. };
  160. struct T {};
  161. T t1 = t1.T::~T<int>; // expected-error {{destructor name 'T' does not refer to a template}} expected-error {{expected '(' for function-style cast or type construction}} expected-error {{expected expression}}
  162. // Emit the same diagnostic as for the previous case, plus something about ~.
  163. T t2 = t2.~T::T<int>; // expected-error {{'~' in destructor name should be after nested name specifier}} expected-error {{destructor name 'T' does not refer to a template}} expected-error {{expected '(' for function-style cast or type construction}} expected-error {{expected expression}}
  164. }
  165. namespace BadFriend {
  166. struct A {
  167. friend int : 3; // expected-error {{friends can only be classes or functions}}
  168. friend void f() = 123; // expected-error {{illegal initializer}}
  169. friend virtual void f(); // expected-error {{'virtual' is invalid in friend declarations}}
  170. friend void f() final; // expected-error {{'final' is invalid in friend declarations}}
  171. friend void f() override; // expected-error {{'override' is invalid in friend declarations}}
  172. };
  173. }
  174. class PR20760_a {
  175. int a = ); // expected-warning {{extension}} expected-error {{expected expression}}
  176. int b = }; // expected-warning {{extension}} expected-error {{expected expression}}
  177. int c = ]; // expected-warning {{extension}} expected-error {{expected expression}}
  178. };
  179. class PR20760_b {
  180. int d = d); // expected-warning {{extension}} expected-error {{expected ';'}}
  181. int e = d]; // expected-warning {{extension}} expected-error {{expected ';'}}
  182. int f = d // expected-warning {{extension}} expected-error {{expected ';'}}
  183. };
  184. namespace PR20887 {
  185. class X1 { a::operator=; }; // expected-error {{undeclared identifier 'a'}}
  186. class X2 { a::a; }; // expected-error {{undeclared identifier 'a'}}
  187. }
  188. class BadExceptionSpec {
  189. void f() throw(int; // expected-error {{expected ')'}} expected-note {{to match}}
  190. void g() throw(
  191. int(
  192. ; // expected-error {{unexpected ';' before ')'}}
  193. ));
  194. };
  195. // PR11109 must appear at the end of the source file
  196. class pr11109r3 { // expected-note{{to match this '{'}}
  197. public // expected-error{{expected ':'}} expected-error{{expected '}'}} expected-error{{expected ';' after class}}