[libcxx-commits] [libcxx] d278fa5 - [libc++] Fix constant initialization of unique_ptr in C++17 and prior (#108956)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 2 08:09:11 PDT 2024


Author: Nikolas Klauser
Date: 2024-10-02T17:09:06+02:00
New Revision: d278fa5e1a65cb0583fd4ca7814da6b238c66e9f

URL: https://github.com/llvm/llvm-project/commit/d278fa5e1a65cb0583fd4ca7814da6b238c66e9f
DIFF: https://github.com/llvm/llvm-project/commit/d278fa5e1a65cb0583fd4ca7814da6b238c66e9f.diff

LOG: [libc++] Fix constant initialization of unique_ptr in C++17 and prior (#108956)

This is already tested in
`std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp`
except that `TEST_CONSTINIT` doesn't do anything before C++20 without
this patch.

Added: 
    

Modified: 
    libcxx/include/__memory/compressed_pair.h
    libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
    libcxx/test/support/test_macros.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index 6454b2b7ab0700..9d44775bdb4871 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -56,7 +56,7 @@ template <class _ToPad,
           bool _Empty = ((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) ||
                          is_reference<_ToPad>::value || sizeof(_ToPad) == __datasizeof_v<_ToPad>)>
 class __compressed_pair_padding {
-  char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>];
+  char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {};
 };
 
 template <class _ToPad>

diff  --git a/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp b/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
index a20d22c1073fab..82c2e46bfa1ef5 100644
--- a/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.class/any.cons/default.pass.cpp
@@ -32,7 +32,7 @@ int main(int, char**)
         struct TestConstexpr : public std::any {
           constexpr TestConstexpr() : std::any() {}
         };
-        static TEST_CONSTINIT std::any a;
+        TEST_CONSTINIT static std::any a;
         (void)a;
     }
     {

diff  --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 3aa818af1d2695..ae171b3f223ab3 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -221,9 +221,11 @@
 #endif
 
 #if TEST_STD_VER > 17
-#define TEST_CONSTINIT constinit
+#  define TEST_CONSTINIT constinit
+#elif __has_cpp_attribute(clang::require_constant_initialization)
+#  define TEST_CONSTINIT [[clang::require_constant_initialization]]
 #else
-#define TEST_CONSTINIT
+#  define TEST_CONSTINIT
 #endif
 
 #if TEST_STD_VER < 11


        


More information about the libcxx-commits mailing list