1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // test sized operator delete[] replacement.
11 
12 // UNSUPPORTED: sanitizer-new-delete, c++98, c++03, c++11
13 
14 // NOTE: Clang does not enable sized-deallocation in c++14 and beyond by
15 // default. It is only enabled when -fsized-deallocation is given.
16 // (except clang-3.6 which temporarily enabled sized-deallocation)
17 // UNSUPPORTED: clang, apple-clang
18 
19 // NOTE: GCC 4.9.1 does not support sized-deallocation in c++14. However
20 // GCC 5.1 does.
21 // XFAIL: gcc-4.7, gcc-4.8, gcc-4.9
22 
23 #include <new>
24 #include <cstddef>
25 #include <cstdlib>
26 #include <cassert>
27 
28 int unsized_delete_called = 0;
29 int unsized_delete_nothrow_called = 0;
30 int sized_delete_called = 0;
31 
operator delete[](void * p)32 void operator delete[](void* p) throw()
33 {
34     ++unsized_delete_called;
35     std::free(p);
36 }
37 
operator delete[](void * p,const std::nothrow_t &)38 void operator delete[](void* p, const std::nothrow_t&) throw()
39 {
40     ++unsized_delete_nothrow_called;
41     std::free(p);
42 }
43 
operator delete[](void * p,std::size_t)44 void operator delete[](void* p, std::size_t) throw()
45 {
46     ++sized_delete_called;
47     std::free(p);
48 }
49 
50 // NOTE: Use a class with a non-trivial destructor as the test type in order
51 // to ensure the correct overload is called.
52 // C++14 5.3.5 [expr.delete]p10
53 // - If the type is complete and if, for the second alternative (delete array)
54 //   only, the operand is a pointer to a class type with a non-trivial
55 //   destructor or a (possibly multi-dimensional) array thereof, the function
56 //   with two parameters is selected.
57 // - Otherwise, it is unspecified which of the two deallocation functions is
58 //   selected.
~AA59 struct A { ~A() {} };
60 
main()61 int main()
62 {
63     A* x = new A[3];
64     assert(0 == unsized_delete_called);
65     assert(0 == unsized_delete_nothrow_called);
66     assert(0 == sized_delete_called);
67 
68     delete [] x;
69     assert(0 == unsized_delete_called);
70     assert(0 == unsized_delete_nothrow_called);
71     assert(1 == sized_delete_called);
72 }
73