[libcxx-commits] [libcxx] [libc++] std::atomic primary template should not have a difference type member type (PR #123236)
Damien L-G via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 16 12:17:39 PST 2025
https://github.com/dalg24 created https://github.com/llvm/llvm-project/pull/123236
The test would not check its absence and the code path intended for pointer was never actually instantiated.
I added a few pointer types since there was no coverage.
>From 66f30765289d7f72b036482137220cb0e303c6e5 Mon Sep 17 00:00:00 2001
From: Damien L-G <dalg24 at gmail.com>
Date: Thu, 16 Jan 2025 15:12:58 -0500
Subject: [PATCH 1/2] Fix tests for std::atomic member types
---
libcxx/test/std/atomics/types.pass.cpp | 220 +++++++++++++------------
1 file changed, 115 insertions(+), 105 deletions(-)
diff --git a/libcxx/test/std/atomics/types.pass.cpp b/libcxx/test/std/atomics/types.pass.cpp
index 33512c037804f8..dd8f770ac45434 100644
--- a/libcxx/test/std/atomics/types.pass.cpp
+++ b/libcxx/test/std/atomics/types.pass.cpp
@@ -29,163 +29,173 @@
# include <thread>
#endif
-template <class A, bool Integral>
-struct test_atomic
-{
- test_atomic()
- {
- A a; (void)a;
+// detect existence of the difference_type member type
+template <class...>
+using myvoid_t = void;
+template <typename T, typename = void>
+struct has_difference_type : std::false_type {};
+template <typename T>
+struct has_difference_type<T, myvoid_t<typename T::difference_type> > : std::true_type {};
+
+template <class A, bool IntegralOrFloating, bool Pointer>
+struct test_atomic {
+ test_atomic() {
+ static_assert(!IntegralOrFloating || !Pointer, "");
+ A a;
+ (void)a;
#if TEST_STD_VER >= 17
static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
+ static_assert(!has_difference_type<A>::value, "");
#endif
- }
+ }
};
template <class A>
-struct test_atomic<A, true>
-{
- test_atomic()
- {
- A a; (void)a;
+struct test_atomic<A, true, false> {
+ test_atomic() {
+ A a;
+ (void)a;
#if TEST_STD_VER >= 17
static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
static_assert((std::is_same_v<typename A::value_type, typename A::difference_type>), "");
#endif
- }
+ }
};
template <class A>
-struct test_atomic<A*, false>
-{
- test_atomic()
- {
- A a; (void)a;
+struct test_atomic<A, false, true> {
+ test_atomic() {
+ A a;
+ (void)a;
#if TEST_STD_VER >= 17
static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
static_assert((std::is_same_v<typename A::difference_type, std::ptrdiff_t>), "");
#endif
- }
+ }
};
template <class T>
-void
-test()
-{
- using A = std::atomic<T>;
+void test() {
+ using A = std::atomic<T>;
#if TEST_STD_VER >= 17
- static_assert((std::is_same_v<typename A::value_type, T>), "");
+ static_assert((std::is_same_v<typename A::value_type, T>), "");
#endif
- test_atomic<A, std::is_integral<T>::value && !std::is_same<T, bool>::value>();
+ constexpr bool IntegralOrFloating =
+ (std::is_integral<T>::value && !std::is_same<T, bool>::value) || std::is_floating_point<T>::value;
+ constexpr bool Pointer = std::is_pointer<T>::value;
+ test_atomic<A, IntegralOrFloating, Pointer>();
}
struct TriviallyCopyable {
- int i_;
+ int i_;
};
-struct WeirdTriviallyCopyable
-{
- char i, j, k; /* the 3 chars of doom */
+struct WeirdTriviallyCopyable {
+ char i, j, k; /* the 3 chars of doom */
};
-struct PaddedTriviallyCopyable
-{
- char i; int j; /* probably lock-free? */
+struct PaddedTriviallyCopyable {
+ char i;
+ int j; /* probably lock-free? */
};
-struct LargeTriviallyCopyable
-{
- int i, j[127]; /* decidedly not lock-free */
+struct LargeTriviallyCopyable {
+ int i, j[127]; /* decidedly not lock-free */
};
-int main(int, char**)
-{
- test<bool> ();
- test<char> ();
- test<signed char> ();
- test<unsigned char> ();
- test<short> ();
- test<unsigned short> ();
- test<int> ();
- test<unsigned int> ();
- test<long> ();
- test<unsigned long> ();
- test<long long> ();
- test<unsigned long long> ();
+int main(int, char**) {
+ test<bool>();
+ test<char>();
+ test<signed char>();
+ test<unsigned char>();
+ test<short>();
+ test<unsigned short>();
+ test<int>();
+ test<unsigned int>();
+ test<long>();
+ test<unsigned long>();
+ test<long long>();
+ test<unsigned long long>();
#if TEST_STD_VER > 17 && defined(__cpp_char8_t)
- test<char8_t> ();
+ test<char8_t>();
#endif
- test<char16_t> ();
- test<char32_t> ();
+ test<char16_t>();
+ test<char32_t>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
- test<wchar_t> ();
+ test<wchar_t>();
#endif
- test<std::int_least8_t> ();
- test<std::uint_least8_t> ();
- test<std::int_least16_t> ();
- test<std::uint_least16_t> ();
- test<std::int_least32_t> ();
- test<std::uint_least32_t> ();
- test<std::int_least64_t> ();
- test<std::uint_least64_t> ();
-
- test<std::int_fast8_t> ();
- test<std::uint_fast8_t> ();
- test<std::int_fast16_t> ();
- test<std::uint_fast16_t> ();
- test<std::int_fast32_t> ();
- test<std::uint_fast32_t> ();
- test<std::int_fast64_t> ();
- test<std::uint_fast64_t> ();
-
- test< std::int8_t> ();
- test<std::uint8_t> ();
- test< std::int16_t> ();
- test<std::uint16_t> ();
- test< std::int32_t> ();
- test<std::uint32_t> ();
- test< std::int64_t> ();
- test<std::uint64_t> ();
-
- test<std::intptr_t> ();
- test<std::uintptr_t> ();
- test<std::size_t> ();
- test<std::ptrdiff_t> ();
- test<std::intmax_t> ();
- test<std::uintmax_t> ();
-
- test<std::uintmax_t> ();
- test<std::uintmax_t> ();
-
- test<TriviallyCopyable>();
- test<PaddedTriviallyCopyable>();
+ test<std::int_least8_t>();
+ test<std::uint_least8_t>();
+ test<std::int_least16_t>();
+ test<std::uint_least16_t>();
+ test<std::int_least32_t>();
+ test<std::uint_least32_t>();
+ test<std::int_least64_t>();
+ test<std::uint_least64_t>();
+
+ test<std::int_fast8_t>();
+ test<std::uint_fast8_t>();
+ test<std::int_fast16_t>();
+ test<std::uint_fast16_t>();
+ test<std::int_fast32_t>();
+ test<std::uint_fast32_t>();
+ test<std::int_fast64_t>();
+ test<std::uint_fast64_t>();
+
+ test< std::int8_t>();
+ test<std::uint8_t>();
+ test< std::int16_t>();
+ test<std::uint16_t>();
+ test< std::int32_t>();
+ test<std::uint32_t>();
+ test< std::int64_t>();
+ test<std::uint64_t>();
+
+ test<std::intptr_t>();
+ test<std::uintptr_t>();
+ test<std::size_t>();
+ test<std::ptrdiff_t>();
+ test<std::intmax_t>();
+ test<std::uintmax_t>();
+
+ test<std::uintmax_t>();
+ test<std::uintmax_t>();
+
+ test<void*>();
+ test<const void*>();
+ test<int*>();
+ test<const int*>();
+
+ test<TriviallyCopyable>();
+ test<PaddedTriviallyCopyable>();
#ifndef __APPLE__ // Apple doesn't ship libatomic
- /*
+ /*
These aren't going to be lock-free,
so some libatomic.a is necessary.
*/
- test<WeirdTriviallyCopyable>();
- test<LargeTriviallyCopyable>();
+ test<WeirdTriviallyCopyable>();
+ test<LargeTriviallyCopyable>();
#endif
#ifndef TEST_HAS_NO_THREADS
- test<std::thread::id>();
+ test<std::thread::id>();
#endif
- test<std::chrono::nanoseconds>();
- test<float>();
+ test<std::chrono::nanoseconds>();
+ test<float>();
#if TEST_STD_VER >= 20
- test<std::atomic_signed_lock_free::value_type>();
- static_assert(std::is_signed_v<std::atomic_signed_lock_free::value_type>);
- static_assert(std::is_integral_v<std::atomic_signed_lock_free::value_type>);
+ test<std::atomic_signed_lock_free::value_type>();
+ static_assert(std::is_signed_v<std::atomic_signed_lock_free::value_type>);
+ static_assert(std::is_integral_v<std::atomic_signed_lock_free::value_type>);
- test<std::atomic_unsigned_lock_free::value_type>();
- static_assert(std::is_unsigned_v<std::atomic_unsigned_lock_free::value_type>);
- static_assert(std::is_integral_v<std::atomic_unsigned_lock_free::value_type>);
+ test<std::atomic_unsigned_lock_free::value_type>();
+ static_assert(std::is_unsigned_v<std::atomic_unsigned_lock_free::value_type>);
+ static_assert(std::is_integral_v<std::atomic_unsigned_lock_free::value_type>);
/*
test<std::shared_ptr<int>>();
*/
#endif
- return 0;
+ return 0;
}
>From 11739d35a57b29af8ef2a41a0571bdb38190a6a9 Mon Sep 17 00:00:00 2001
From: Damien L-G <dalg24 at gmail.com>
Date: Thu, 16 Jan 2025 15:13:11 -0500
Subject: [PATCH 2/2] Fix std::atomic primary template should not have
difference_type
---
libcxx/include/__atomic/atomic.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__atomic/atomic.h b/libcxx/include/__atomic/atomic.h
index 975a479e204004..1ed206ab2ff197 100644
--- a/libcxx/include/__atomic/atomic.h
+++ b/libcxx/include/__atomic/atomic.h
@@ -40,6 +40,8 @@ struct __atomic_base // false
{
mutable __cxx_atomic_impl<_Tp> __a_;
+ using value_type = _Tp;
+
#if _LIBCPP_STD_VER >= 17
static constexpr bool is_always_lock_free = __libcpp_is_always_lock_free<__cxx_atomic_impl<_Tp> >::__value;
#endif
@@ -145,6 +147,8 @@ template <class _Tp>
struct __atomic_base<_Tp, true> : public __atomic_base<_Tp, false> {
using __base _LIBCPP_NODEBUG = __atomic_base<_Tp, false>;
+ using difference_type = __base::value_type;
+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __atomic_base() _NOEXCEPT = default;
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __base(__d) {}
@@ -229,8 +233,6 @@ struct __atomic_waitable_traits<__atomic_base<_Tp, _IsIntegral> > {
template <class _Tp>
struct atomic : public __atomic_base<_Tp> {
using __base _LIBCPP_NODEBUG = __atomic_base<_Tp>;
- using value_type = _Tp;
- using difference_type = value_type;
#if _LIBCPP_STD_VER >= 20
_LIBCPP_HIDE_FROM_ABI atomic() = default;
@@ -258,8 +260,8 @@ struct atomic : public __atomic_base<_Tp> {
template <class _Tp>
struct atomic<_Tp*> : public __atomic_base<_Tp*> {
using __base _LIBCPP_NODEBUG = __atomic_base<_Tp*>;
- using value_type = _Tp*;
- using difference_type = ptrdiff_t;
+
+ using difference_type = ptrdiff_t;
_LIBCPP_HIDE_FROM_ABI atomic() _NOEXCEPT = default;
More information about the libcxx-commits
mailing list