From 76b2cec8ee80b57e7b01332e501cd01219c28a36 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Tue, 7 May 2024 12:33:55 +0200 Subject: [PATCH] src: fix compilation with clang --- src/check/dt_cxxlib.cpp | 27 +++++++++++++++++++++------ src/util/cxxlib.h | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/check/dt_cxxlib.cpp b/src/check/dt_cxxlib.cpp index 7ff4547f..6e86e671 100644 --- a/src/check/dt_cxxlib.cpp +++ b/src/check/dt_cxxlib.cpp @@ -250,6 +250,18 @@ struct Z2_X2 : public X2 { // util **************************************************************************/ +#if WITH_THREADS +TEST_CASE("upx::ptr_std_atomic_cast") { + (void) upx::ptr_std_atomic_cast((upx_int8_t *) nullptr); + (void) upx::ptr_std_atomic_cast((upx_int16_t *) nullptr); + (void) upx::ptr_std_atomic_cast((upx_int32_t *) nullptr); + (void) upx::ptr_std_atomic_cast((int *) nullptr); + (void) upx::ptr_std_atomic_cast((long *) nullptr); + (void) upx::ptr_std_atomic_cast((size_t *) nullptr); + (void) upx::ptr_std_atomic_cast((void **) nullptr); +} +#endif + TEST_CASE("upx::atomic_exchange") { { upx_int8_t x = -1; @@ -276,12 +288,15 @@ TEST_CASE("upx::atomic_exchange") { CHECK_EQ(y, (size_t) 0 - 1); } { - int dummy = -1; - int *x = &dummy; - int *y = upx::atomic_exchange(&x, (int *) nullptr); - CHECK_EQ(x, nullptr); - CHECK_EQ(y, &dummy); - CHECK_EQ(dummy, -1); + const int buf[2] = {101, 202}; + const int *ptr_array[2] = {&buf[0], &buf[1]}; + assert_noexcept(*ptr_array[0] == 101 && *ptr_array[1] == 202); + const int *p = upx::atomic_exchange(&ptr_array[0], ptr_array[1]); + CHECK_EQ(p, buf + 0); + assert_noexcept(*ptr_array[0] == 202 && *ptr_array[1] == 202); + p = upx::atomic_exchange(&ptr_array[1], p); + CHECK_EQ(p, buf + 1); + assert_noexcept(*ptr_array[0] == 202 && *ptr_array[1] == 101); } } diff --git a/src/util/cxxlib.h b/src/util/cxxlib.h index 77d712f2..a55b223a 100644 --- a/src/util/cxxlib.h +++ b/src/util/cxxlib.h @@ -194,7 +194,7 @@ forceinline std::atomic *ptr_std_atomic_cast(T *ptr) noexcept { // TODO later: make sure that this cast is indeed legal std::atomic *result = ptr_static_cast *>(ptr); static_assert(sizeof(*result) == sizeof(*ptr)); - static_assert(alignof(*result) == alignof(*ptr)); + static_assert(alignof(decltype(*result)) == alignof(decltype(*ptr))); return result; } #endif // WITH_THREADS