[committed] libstdc++: Fix test FAIL due to -Wpointer-arith

Message ID 20240927154445.2277705-1-jwakely@redhat.com
State Committed
Commit 500046d178bc78a8f18d529c62f7b45c8ad2c861
Headers
Series [committed] libstdc++: Fix test FAIL due to -Wpointer-arith |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged

Commit Message

Jonathan Wakely Sept. 27, 2024, 3:44 p.m. UTC
  Tested x86_64-linux. Pushed to trunk.

-- >8 --

This fixes a FAIL due to a -Wpointer-arith warning when testing with
c++11 or c++14 dialects. As an extension our std::atomic<void*> supports
pointer arithmetic in C++11 and C++14, but due to the system header
changes there is now a warning about it. The warning seems reasonable,
so rather than suppress it we should make the test expect it.

While looking into this I decided to simplify some of the code related
to atomic<T*> arithmetic.

libstdc++-v3/ChangeLog:

	* include/bits/atomic_base.h (__atomic_base<T*>::_M_type_size):
	Replace overloaded functions with static _S_type_size.
	* include/std/atomic (atomic<T*>): Use is_object_v instead of
	is_object.
	* testsuite/29_atomics/atomic/operators/pointer_partial_void.cc:
	Add dg-warning for -Wpointer-arith warning.
---
 libstdc++-v3/include/bits/atomic_base.h       | 33 +++++++++----------
 libstdc++-v3/include/std/atomic               | 32 +++++++++---------
 .../atomic/operators/pointer_partial_void.cc  |  1 +
 3 files changed, 32 insertions(+), 34 deletions(-)
  

Patch

diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
index 7093d0fc822..72cc4bae6cf 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -687,12 +687,9 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       __pointer_type 	_M_p _GLIBCXX20_INIT(nullptr);
 
-      // Factored out to facilitate explicit specialization.
-      constexpr ptrdiff_t
-      _M_type_size(ptrdiff_t __d) const { return __d * sizeof(_PTp); }
-
-      constexpr ptrdiff_t
-      _M_type_size(ptrdiff_t __d) const volatile { return __d * sizeof(_PTp); }
+      static constexpr ptrdiff_t
+      _S_type_size(ptrdiff_t __d)
+      { return __d * sizeof(_PTp); }
 
     public:
       __atomic_base() noexcept = default;
@@ -742,42 +739,42 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       __pointer_type
       operator++() noexcept
-      { return __atomic_add_fetch(&_M_p, _M_type_size(1),
+      { return __atomic_add_fetch(&_M_p, _S_type_size(1),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator++() volatile noexcept
-      { return __atomic_add_fetch(&_M_p, _M_type_size(1),
+      { return __atomic_add_fetch(&_M_p, _S_type_size(1),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator--() noexcept
-      { return __atomic_sub_fetch(&_M_p, _M_type_size(1),
+      { return __atomic_sub_fetch(&_M_p, _S_type_size(1),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator--() volatile noexcept
-      { return __atomic_sub_fetch(&_M_p, _M_type_size(1),
+      { return __atomic_sub_fetch(&_M_p, _S_type_size(1),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator+=(ptrdiff_t __d) noexcept
-      { return __atomic_add_fetch(&_M_p, _M_type_size(__d),
+      { return __atomic_add_fetch(&_M_p, _S_type_size(__d),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator+=(ptrdiff_t __d) volatile noexcept
-      { return __atomic_add_fetch(&_M_p, _M_type_size(__d),
+      { return __atomic_add_fetch(&_M_p, _S_type_size(__d),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator-=(ptrdiff_t __d) noexcept
-      { return __atomic_sub_fetch(&_M_p, _M_type_size(__d),
+      { return __atomic_sub_fetch(&_M_p, _S_type_size(__d),
 				  int(memory_order_seq_cst)); }
 
       __pointer_type
       operator-=(ptrdiff_t __d) volatile noexcept
-      { return __atomic_sub_fetch(&_M_p, _M_type_size(__d),
+      { return __atomic_sub_fetch(&_M_p, _S_type_size(__d),
 				  int(memory_order_seq_cst)); }
 
       bool
@@ -932,22 +929,22 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _GLIBCXX_ALWAYS_INLINE __pointer_type
       fetch_add(ptrdiff_t __d,
 		memory_order __m = memory_order_seq_cst) noexcept
-      { return __atomic_fetch_add(&_M_p, _M_type_size(__d), int(__m)); }
+      { return __atomic_fetch_add(&_M_p, _S_type_size(__d), int(__m)); }
 
       _GLIBCXX_ALWAYS_INLINE __pointer_type
       fetch_add(ptrdiff_t __d,
 		memory_order __m = memory_order_seq_cst) volatile noexcept
-      { return __atomic_fetch_add(&_M_p, _M_type_size(__d), int(__m)); }
+      { return __atomic_fetch_add(&_M_p, _S_type_size(__d), int(__m)); }
 
       _GLIBCXX_ALWAYS_INLINE __pointer_type
       fetch_sub(ptrdiff_t __d,
 		memory_order __m = memory_order_seq_cst) noexcept
-      { return __atomic_fetch_sub(&_M_p, _M_type_size(__d), int(__m)); }
+      { return __atomic_fetch_sub(&_M_p, _S_type_size(__d), int(__m)); }
 
       _GLIBCXX_ALWAYS_INLINE __pointer_type
       fetch_sub(ptrdiff_t __d,
 		memory_order __m = memory_order_seq_cst) volatile noexcept
-      { return __atomic_fetch_sub(&_M_p, _M_type_size(__d), int(__m)); }
+      { return __atomic_fetch_sub(&_M_p, _S_type_size(__d), int(__m)); }
     };
 
   namespace __atomic_impl
diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic
index 37fd7103bd9..c0568d3320b 100644
--- a/libstdc++-v3/include/std/atomic
+++ b/libstdc++-v3/include/std/atomic
@@ -453,7 +453,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator++(int) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b++;
       }
@@ -462,7 +462,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator++(int) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b++;
       }
@@ -471,7 +471,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator--(int) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b--;
       }
@@ -480,7 +480,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator--(int) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b--;
       }
@@ -489,7 +489,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator++() noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return ++_M_b;
       }
@@ -498,7 +498,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator++() volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return ++_M_b;
       }
@@ -507,7 +507,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator--() noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return --_M_b;
       }
@@ -516,7 +516,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator--() volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return --_M_b;
       }
@@ -525,7 +525,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator+=(ptrdiff_t __d) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.operator+=(__d);
       }
@@ -534,7 +534,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator+=(ptrdiff_t __d) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.operator+=(__d);
       }
@@ -543,7 +543,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator-=(ptrdiff_t __d) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.operator-=(__d);
       }
@@ -552,7 +552,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator-=(ptrdiff_t __d) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.operator-=(__d);
       }
@@ -673,7 +673,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		memory_order __m = memory_order_seq_cst) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.fetch_add(__d, __m);
       }
@@ -683,7 +683,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		memory_order __m = memory_order_seq_cst) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.fetch_add(__d, __m);
       }
@@ -693,7 +693,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		memory_order __m = memory_order_seq_cst) noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.fetch_sub(__d, __m);
       }
@@ -703,7 +703,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		memory_order __m = memory_order_seq_cst) volatile noexcept
       {
 #if __cplusplus >= 201703L
-	static_assert( is_object<_Tp>::value, "pointer to object type" );
+	static_assert( is_object_v<_Tp>, "pointer to object type" );
 #endif
 	return _M_b.fetch_sub(__d, __m);
       }
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc b/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc
index cef43ec26f3..60f6a624aae 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc
@@ -68,3 +68,4 @@  int main(void)
 
   return 0;
 }
+// { dg-warning "invalid application of 'sizeof' to a void type" "" { target *-*-* } 0 }