PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/extern/llvm/tools/clang/test/SemaCXX/destructor.cpp

https://bitbucket.org/dwilliamson/clreflect/
C++ | 365 lines | 293 code | 56 blank | 16 comment | 0 complexity | 42471461a6ca96ff1278246ae3d95508 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause
  1. // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -verify %s
  2. class A {
  3. public:
  4. ~A();
  5. };
  6. class B {
  7. public:
  8. ~B() { }
  9. };
  10. class C {
  11. public:
  12. (~C)() { }
  13. };
  14. struct D {
  15. static void ~D(int, ...) const { } // \
  16. // expected-error{{static member function cannot have 'const' qualifier}} \
  17. // expected-error{{destructor cannot be declared 'static'}} \
  18. // expected-error{{destructor cannot have any parameters}} \
  19. // expected-error{{destructor cannot be variadic}} \
  20. // expected-error{{destructor cannot have a return type}} \
  21. // expected-error{{'const' qualifier is not allowed on a destructor}}
  22. };
  23. struct D2 {
  24. void ~D2() { } // \
  25. // expected-error{{destructor cannot have a return type}}
  26. };
  27. struct E;
  28. typedef E E_typedef;
  29. struct E {
  30. ~E_typedef(); // expected-error{{destructor cannot be declared using a typedef 'E_typedef' (aka 'E') of the class name}}
  31. };
  32. struct F {
  33. (~F)(); // expected-note {{previous declaration is here}}
  34. ~F(); // expected-error {{destructor cannot be redeclared}}
  35. };
  36. ~; // expected-error {{expected a class name after '~' to name a destructor}}
  37. ~undef(); // expected-error {{expected the class name after '~' to name a destructor}}
  38. ~operator+(int, int); // expected-error {{expected a class name after '~' to name a destructor}}
  39. ~F(){} // expected-error {{destructor must be a non-static member function}}
  40. struct G {
  41. ~G();
  42. };
  43. G::~G() { }
  44. // <rdar://problem/6841210>
  45. struct H {
  46. ~H(void) { }
  47. };
  48. struct X {};
  49. struct Y {
  50. ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}}
  51. };
  52. namespace PR6421 {
  53. class T; // expected-note{{forward declaration}}
  54. class QGenericArgument // expected-note{{declared here}}
  55. {
  56. template<typename U>
  57. void foo(T t) // expected-error{{variable has incomplete type}}
  58. { }
  59. void disconnect()
  60. {
  61. T* t;
  62. bob<QGenericArgument>(t); // expected-error{{undeclared identifier 'bob'}} \
  63. // expected-error{{does not refer to a value}}
  64. }
  65. };
  66. }
  67. namespace PR6709 {
  68. template<class T> class X { T v; ~X() { ++*v; } };
  69. void a(X<int> x) {}
  70. }
  71. struct X0 { virtual ~X0() throw(); };
  72. struct X1 : public X0 { };
  73. // Make sure we instantiate operator deletes when building a virtual
  74. // destructor.
  75. namespace test6 {
  76. template <class T> class A {
  77. public:
  78. void *operator new(__SIZE_TYPE__);
  79. void operator delete(void *p) {
  80. T::deleteIt(p); // expected-error {{type 'int' cannot be used prior to '::'}}
  81. }
  82. virtual ~A() {}
  83. };
  84. class B : A<int> { B(); }; // expected-note {{in instantiation of member function 'test6::A<int>::operator delete' requested here}}
  85. B::B() {}
  86. }
  87. // Make sure classes are marked invalid when they have invalid
  88. // members. This avoids a crash-on-invalid.
  89. namespace test7 {
  90. struct A {
  91. ~A() const; // expected-error {{'const' qualifier is not allowed on a destructor}}
  92. };
  93. struct B : A {};
  94. void test() {
  95. B *b;
  96. b->~B();
  97. }
  98. }
  99. namespace nonvirtualdtor {
  100. struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
  101. virtual void m();
  102. };
  103. struct S2 {
  104. ~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
  105. virtual void m();
  106. };
  107. struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
  108. virtual void m();
  109. };
  110. struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
  111. virtual void m();
  112. };
  113. struct B {
  114. virtual ~B();
  115. virtual void m();
  116. };
  117. struct S5 : public B {
  118. virtual void m();
  119. };
  120. struct S6 {
  121. virtual void m();
  122. private:
  123. ~S6();
  124. };
  125. struct S7 {
  126. virtual void m();
  127. protected:
  128. ~S7();
  129. };
  130. template<class T> class TS : public B {
  131. virtual void m();
  132. };
  133. TS<int> baz;
  134. template<class T> class TS2 { // expected-warning {{'nonvirtualdtor::TS2<int>' has virtual functions but non-virtual destructor}}
  135. virtual void m();
  136. };
  137. TS2<int> foo; // expected-note {{instantiation}}
  138. }
  139. namespace dnvd { // delete-non-virtual-dtor warning
  140. struct NP {};
  141. struct B { // expected-warning {{has virtual functions but non-virtual destructor}}
  142. virtual void foo();
  143. };
  144. struct D: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
  145. struct F final: B {}; // expected-warning {{has virtual functions but non-virtual destructor}}
  146. struct VB {
  147. virtual void foo();
  148. virtual ~VB();
  149. };
  150. struct VD: VB {};
  151. struct VF final: VB {};
  152. template <typename T>
  153. class simple_ptr {
  154. public:
  155. simple_ptr(T* t): _ptr(t) {}
  156. ~simple_ptr() { delete _ptr; } // \
  157. // expected-warning {{delete called on 'dnvd::B' that has virtual functions but non-virtual destructor}} \
  158. // expected-warning {{delete called on 'dnvd::D' that has virtual functions but non-virtual destructor}}
  159. T& operator*() const { return *_ptr; }
  160. private:
  161. T* _ptr;
  162. };
  163. template <typename T>
  164. class simple_ptr2 {
  165. public:
  166. simple_ptr2(T* t): _ptr(t) {}
  167. ~simple_ptr2() { delete _ptr; } // expected-warning {{delete called on 'dnvd::B' that has virtual functions but non-virtual destructor}}
  168. T& operator*() const { return *_ptr; }
  169. private:
  170. T* _ptr;
  171. };
  172. void use(B&);
  173. void use(VB&);
  174. void nowarnstack() {
  175. B b; use(b);
  176. D d; use(d);
  177. F f; use(f);
  178. VB vb; use(vb);
  179. VD vd; use(vd);
  180. VF vf; use(vf);
  181. }
  182. void nowarnnonpoly() {
  183. {
  184. NP* np = new NP();
  185. delete np;
  186. }
  187. {
  188. NP* np = new NP[4];
  189. delete[] np;
  190. }
  191. }
  192. void nowarnarray() {
  193. {
  194. B* b = new B[4];
  195. delete[] b;
  196. }
  197. {
  198. D* d = new D[4];
  199. delete[] d;
  200. }
  201. {
  202. VB* vb = new VB[4];
  203. delete[] vb;
  204. }
  205. {
  206. VD* vd = new VD[4];
  207. delete[] vd;
  208. }
  209. }
  210. template <typename T>
  211. void nowarntemplate() {
  212. {
  213. T* t = new T();
  214. delete t;
  215. }
  216. {
  217. T* t = new T[4];
  218. delete[] t;
  219. }
  220. }
  221. void nowarn0() {
  222. {
  223. F* f = new F();
  224. delete f;
  225. }
  226. {
  227. VB* vb = new VB();
  228. delete vb;
  229. }
  230. {
  231. VB* vb = new VD();
  232. delete vb;
  233. }
  234. {
  235. VD* vd = new VD();
  236. delete vd;
  237. }
  238. {
  239. VF* vf = new VF();
  240. delete vf;
  241. }
  242. }
  243. void warn0() {
  244. {
  245. B* b = new B();
  246. delete b; // expected-warning {{delete called on 'dnvd::B' that has virtual functions but non-virtual destructor}}
  247. }
  248. {
  249. B* b = new D();
  250. delete b; // expected-warning {{delete called on 'dnvd::B' that has virtual functions but non-virtual destructor}}
  251. }
  252. {
  253. D* d = new D();
  254. delete d; // expected-warning {{delete called on 'dnvd::D' that has virtual functions but non-virtual destructor}}
  255. }
  256. }
  257. void nowarn1() {
  258. {
  259. simple_ptr<F> f(new F());
  260. use(*f);
  261. }
  262. {
  263. simple_ptr<VB> vb(new VB());
  264. use(*vb);
  265. }
  266. {
  267. simple_ptr<VB> vb(new VD());
  268. use(*vb);
  269. }
  270. {
  271. simple_ptr<VD> vd(new VD());
  272. use(*vd);
  273. }
  274. {
  275. simple_ptr<VF> vf(new VF());
  276. use(*vf);
  277. }
  278. }
  279. void warn1() {
  280. {
  281. simple_ptr<B> b(new B()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::B>::~simple_ptr' requested here}}
  282. use(*b);
  283. }
  284. {
  285. simple_ptr2<B> b(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr2<dnvd::B>::~simple_ptr2' requested here}}
  286. use(*b);
  287. }
  288. {
  289. simple_ptr<D> d(new D()); // expected-note {{in instantiation of member function 'dnvd::simple_ptr<dnvd::D>::~simple_ptr' requested here}}
  290. use(*d);
  291. }
  292. }
  293. }
  294. namespace PR9238 {
  295. class B { public: ~B(); };
  296. class C : virtual B { public: ~C() { } };
  297. }
  298. namespace PR7900 {
  299. struct A { // expected-note 2{{type 'PR7900::A' is declared here}}
  300. };
  301. struct B : public A {
  302. };
  303. void foo() {
  304. B b;
  305. b.~B();
  306. b.~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}}
  307. (&b)->~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}}
  308. }
  309. }