]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Clear padding bits in std::atomic ctor in C++11 [PR114865]
authorPatrick Palka <ppalka@redhat.com>
Thu, 12 Feb 2026 02:35:21 +0000 (21:35 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 12 Feb 2026 02:35:21 +0000 (21:35 -0500)
After the front end change r16-7199 both GCC and Clang allow non-empty
constexpr constructor bodies in C++11 as an extension, so we can now
unconditionally enable the __builtin_clear_padding logic in std::atomic's
constructor.

PR libstdc++/114865

libstdc++-v3/ChangeLog:

* include/std/atomic (atomic<_Tp>::atomic(_Tp)) [C++11]:
Enable __builtin_clear_padding logic.
* testsuite/29_atomics/atomic/compare_exchange_padding.cc: Enable
this test in earlier modes, including C++11.
* testsuite/29_atomics/atomic/cons/zero_padding.cc [C++11]:
Enable tests verifying cleared padding bits for a non-static-init
std::atomic object.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/include/std/atomic
libstdc++-v3/testsuite/29_atomics/atomic/compare_exchange_padding.cc
libstdc++-v3/testsuite/29_atomics/atomic/cons/zero_padding.cc

index 56dbe7bf5b95fb9f066e311809b9727b1ef1e248..306667e3c633870726bfa028cb89cbe8e8cd49ed 100644 (file)
@@ -247,14 +247,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       atomic& operator=(const atomic&) = delete;
       atomic& operator=(const atomic&) volatile = delete;
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++14-extensions" // constexpr ctor body
       constexpr atomic(_Tp __i) noexcept : _M_i(__i)
       {
-#if __cplusplus >= 201402L && __has_builtin(__builtin_clear_padding)
+#if __has_builtin(__builtin_clear_padding)
        if _GLIBCXX17_CONSTEXPR (__atomic_impl::__maybe_has_padding<_Tp>())
          if (!std::__is_constant_evaluated())
            __builtin_clear_padding(std::__addressof(_M_i));
 #endif
       }
+#pragma GCC diagnostic pop
 
       operator _Tp() const noexcept
       { return load(); }
index a6081968ca86986555405844d683801e00fe832e..d9c7b90c1a5c1ef81f338788c7493d6422b9bfbc 100644 (file)
@@ -1,4 +1,4 @@
-// { dg-do run { target c++20 } }
+// { dg-do run { target c++11 } }
 // { dg-require-atomic-cmpxchg-word "" }
 // { dg-add-options libatomic }
 // { dg-additional-options "-fno-tree-sra" }
index 0d6579b5ab6e82342e4311d2976e9e3f1f469cab..ad48d66de3a4f4eb0587c7b6b655590be7c6f84d 100644 (file)
@@ -57,15 +57,11 @@ void test_struct(std::atomic<T>& g, const T& zp)
 
   std::atomic<T> l(T{1, 2});
   std::memcpy(&t, &zp, sizeof(T));
-#if __cplusplus >= 201402L // Remove once PR114865 is fixed
   VERIFY( l.compare_exchange_strong(t, d) );
-#endif
 
   std::atomic<T>* h = new std::atomic<T>(T{1, 2});
   std::memcpy(&t, &zp, sizeof(T));
-#if __cplusplus >= 201402L // Remove once PR114865 is fixed
   VERIFY( h->compare_exchange_strong(t, d) );
-#endif
   delete h;
 
   constexpr std::atomic<T> cl(T{1, 2});