[libcxx-commits] [libcxx] [libcxx] Improve handling of DummyData blocks in libcxx tests (PR #88897)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 16 07:06:06 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Jack Styles (Stylie777)

<details>
<summary>Changes</summary>

Within the libcxx tests, some tests compare a pointer to a Global Variable to one that is stored locally. This can cause issues where, both are the same value, but the assert is failing as the compiler cannot properly compare them. 

This changes how these variables are defined to specific tests to ensure the asserts pass. No change to the tests functionality is included.

---
Full diff: https://github.com/llvm/llvm-project/pull/88897.diff


6 Files Affected:

- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp (+2-1) 
- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp (+20-5) 
- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp (+2-1) 
- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp (+20-5) 
- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp (+2-1) 
- (modified) libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp (+20-5) 


``````````diff
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
index 0b540e09bab3cc..1ccf5501bcea33 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
@@ -51,7 +51,8 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new OverAligned[3]);
+        OverAligned* dummy_data_block = new (std::nothrow) OverAligned;
+        OverAligned* x = DoNotOptimize(dummy_data_block);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
index 2d021ecb30e793..bdfed1bd8aae18 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
@@ -27,16 +27,31 @@
 #include "test_macros.h"
 #include "../types.h"
 
+class Data{
+public:
+    Data() = default;
+    ~Data() = default;
+
+    char *getDummyData() {
+        return this->dummy_data_;
+    }
+
+    std::size_t getDummyDataSize() {
+        return sizeof(this->dummy_data_);
+    }
+private:
+    alignas(OverAligned) char dummy_data_[alignof(OverAligned) * 3];
+};
+
 int new_called = 0;
 int delete_called = 0;
-
-alignas(OverAligned) char DummyData[alignof(OverAligned) * 3];
+Data data_class;
 
 void* operator new[](std::size_t s, std::align_val_t a) {
-    assert(s <= sizeof(DummyData));
+    assert(s <= data_class.getDummyDataSize());
     assert(static_cast<std::size_t>(a) == alignof(OverAligned));
     ++new_called;
-    return DummyData;
+    return data_class.getDummyData();
 }
 
 void operator delete[](void*, std::align_val_t) noexcept {
@@ -49,7 +64,7 @@ int main(int, char**) {
     {
         new_called = delete_called = 0;
         OverAligned* x = new OverAligned[3];
-        assert(static_cast<void*>(x) == DummyData);
+        assert(static_cast<void*>(x) == data_class.getDummyData());
         assert(new_called == 1);
 
         delete[] x;
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
index 227b20f0b1e18b..6c12a9b65f129a 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
@@ -55,7 +55,8 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned[3]);
+        OverAligned* dummy_data_block = new (std::nothrow) OverAligned[3];
+        OverAligned* x = DoNotOptimize(dummy_data_block);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
index e5ef5f1669752b..254b864a74341f 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
@@ -27,16 +27,31 @@
 #include "test_macros.h"
 #include "../types.h"
 
+class Data{
+public:
+    Data() = default;
+    ~Data() = default;
+
+    char *getDummyData() {
+        return this->dummy_data_;
+    }
+
+    std::size_t getDummyDataSize() {
+        return sizeof(this->dummy_data_);
+    }
+private:
+    alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
+};
+
 int new_called = 0;
 int delete_called = 0;
-
-alignas(OverAligned) char DummyData[alignof(OverAligned)];
+Data data_class;
 
 void* operator new(std::size_t s, std::align_val_t a) {
-    assert(s <= sizeof(DummyData));
+    assert(s <= data_class.getDummyDataSize());
     assert(static_cast<std::size_t>(a) == alignof(OverAligned));
     ++new_called;
-    return DummyData;
+    return data_class.getDummyData();
 }
 
 void operator delete(void*, std::align_val_t) noexcept {
@@ -49,7 +64,7 @@ int main(int, char**) {
     {
         new_called = delete_called = 0;
         OverAligned* x = new OverAligned;
-        assert(static_cast<void*>(x) == DummyData);
+        assert(static_cast<void*>(x) == data_class.getDummyData());
         assert(new_called == 1);
 
         delete x;
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
index 7eab0729f9ef1a..df0c454ecd3e17 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
@@ -54,7 +54,8 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned);
+        OverAligned* dummy_data_block = new (std::nothrow) OverAligned;
+        OverAligned* x = DoNotOptimize(dummy_data_block);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
index 9a5b53e039025c..5e7f2523de4b10 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
@@ -26,16 +26,31 @@
 #include "test_macros.h"
 #include "../types.h"
 
+class Data{
+public:
+    Data() = default;
+    ~Data() = default;
+
+    char *getDummyData() {
+        return this->dummy_data_;
+    }
+
+    std::size_t getDummyDataSize() {
+        return sizeof(this->dummy_data_);
+    }
+private:
+    alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
+};
+
 int new_nothrow_called = 0;
 int delete_called = 0;
-
-alignas(OverAligned) char DummyData[alignof(OverAligned)];
+Data data_class;
 
 void* operator new(std::size_t s, std::align_val_t a, std::nothrow_t const&) noexcept {
-    assert(s <= sizeof(DummyData));
+    assert(s <= data_class.getDummyDataSize());
     assert(static_cast<std::size_t>(a) == alignof(OverAligned));
     ++new_nothrow_called;
-    return DummyData;
+    return data_class.getDummyData();
 }
 
 void operator delete(void*, std::align_val_t) noexcept {
@@ -48,7 +63,7 @@ int main(int, char**) {
     {
         new_nothrow_called = delete_called = 0;
         OverAligned* x = new (std::nothrow) OverAligned;
-        assert(static_cast<void*>(x) == DummyData);
+        assert(static_cast<void*>(x) == data_class.getDummyData());
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
 
         delete x;

``````````

</details>


https://github.com/llvm/llvm-project/pull/88897


More information about the libcxx-commits mailing list