| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Test sized operator delete replacement. |
| 10 | |
| 11 | // UNSUPPORTED: c++03, c++11 |
| 12 | |
| 13 | // These compiler versions and platforms don't enable sized deallocation by default. |
| 14 | // ADDITIONAL_COMPILE_FLAGS(clang-18): -fsized-deallocation |
| 15 | // ADDITIONAL_COMPILE_FLAGS(apple-clang-15): -fsized-deallocation |
| 16 | // ADDITIONAL_COMPILE_FLAGS(apple-clang-16): -fsized-deallocation |
| 17 | // ADDITIONAL_COMPILE_FLAGS(apple-clang-17): -fsized-deallocation |
| 18 | // ADDITIONAL_COMPILE_FLAGS(target=x86_64-w64-windows-gnu): -fsized-deallocation |
| 19 | // ADDITIONAL_COMPILE_FLAGS(target=i686-w64-windows-gnu): -fsized-deallocation |
| 20 | // ADDITIONAL_COMPILE_FLAGS(target=aarch64-w64-windows-gnu): -fsized-deallocation |
| 21 | // ADDITIONAL_COMPILE_FLAGS(target=armv7-w64-windows-gnu): -fsized-deallocation |
| 22 | // ADDITIONAL_COMPILE_FLAGS(target=arm64ec-w64-windows-gnu): -fsized-deallocation |
| 23 | |
| 24 | // Android clang-r536225 identifies as clang-19.0 but it predates the real |
| 25 | // LLVM 19.0.0, so it also leaves sized deallocation off by default. |
| 26 | // UNSUPPORTED: android && clang-19.0 |
| 27 | |
| 28 | // UNSUPPORTED: sanitizer-new-delete |
| 29 | |
| 30 | // Sized deallocation was introduced in LLVM 11 |
| 31 | // XFAIL: using-built-library-before-llvm-11 |
| 32 | |
| 33 | // AIX, and z/OS default to -fno-sized-deallocation. |
| 34 | // XFAIL: target={{.+}}-aix{{.*}}, target={{.+}}-zos{{.*}} |
| 35 | |
| 36 | #if !defined(__cpp_sized_deallocation) |
| 37 | # error __cpp_sized_deallocation should be defined |
| 38 | #endif |
| 39 | |
| 40 | #if !(__cpp_sized_deallocation >= 201309L) |
| 41 | # error expected __cpp_sized_deallocation >= 201309L |
| 42 | #endif |
| 43 | |
| 44 | #include <new> |
| 45 | #include <cstddef> |
| 46 | #include <cstdlib> |
| 47 | #include <cassert> |
| 48 | |
| 49 | #include "test_macros.h" |
| 50 | |
| 51 | int unsized_delete_called = 0; |
| 52 | int unsized_delete_nothrow_called = 0; |
| 53 | int sized_delete_called = 0; |
| 54 | |
| 55 | void operator delete(void* p) TEST_NOEXCEPT |
| 56 | { |
| 57 | ++unsized_delete_called; |
| 58 | std::free(p); |
| 59 | } |
| 60 | |
| 61 | void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT |
| 62 | { |
| 63 | ++unsized_delete_nothrow_called; |
| 64 | std::free(p); |
| 65 | } |
| 66 | |
| 67 | void operator delete(void* p, std::size_t) TEST_NOEXCEPT |
| 68 | { |
| 69 | ++sized_delete_called; |
| 70 | std::free(p); |
| 71 | } |
| 72 | |
| 73 | int main(int, char**) |
| 74 | { |
| 75 | int *x = new int(42); |
| 76 | DoNotOptimize(x); |
| 77 | assert(0 == unsized_delete_called); |
| 78 | assert(0 == unsized_delete_nothrow_called); |
| 79 | assert(0 == sized_delete_called); |
| 80 | |
| 81 | delete x; |
| 82 | DoNotOptimize(x); |
| 83 | assert(0 == unsized_delete_called); |
| 84 | assert(1 == sized_delete_called); |
| 85 | assert(0 == unsized_delete_nothrow_called); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |