public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Fix std::char_traits<C>::move [PR113200]
@ 2024-01-05 10:25 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2024-01-05 10:25 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux. Pushed to trunk. Backports needed too.

-- >8 --

The current constexpr implementation of std::char_traits<C>::move relies
on being able to compare the pointer parameters, which is not allowed
for unrelated pointers. We can use __builtin_constant_p to determine
whether it's safe to compare the pointers directly. If not, then we know
the ranges must be disjoint and so we can use char_traits<C>::copy to
copy forwards from the first character to the last. If the pointers can
be compared directly, then we can simplify the condition for copying
backwards to just two pointer comparisons.

libstdc++-v3/ChangeLog:

	PR libstdc++/113200
	* include/bits/char_traits.h (__gnu_cxx::char_traits::move): Use
	__builtin_constant_p to check for unrelated pointers that cannot
	be compared during constant evaluation.
	* testsuite/21_strings/char_traits/requirements/113200.cc: New
	test.
---
 libstdc++-v3/include/bits/char_traits.h       | 16 +++------------
 .../char_traits/requirements/113200.cc        | 20 +++++++++++++++++++
 2 files changed, 23 insertions(+), 13 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc

diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h
index 018eac28d0d..3074e9bb77e 100644
--- a/libstdc++-v3/include/bits/char_traits.h
+++ b/libstdc++-v3/include/bits/char_traits.h
@@ -227,19 +227,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus >= 202002L
       if (std::__is_constant_evaluated())
 	{
-	  if (__s1 == __s2) // unlikely, but saves a lot of work
-	    return __s1;
-	  const auto __end = __s2 + __n - 1;
-	  bool __overlap = false;
-	  for (std::size_t __i = 0; __i < __n - 1; ++__i)
-	    {
-	      if (__s1 + __i == __end)
-		{
-		  __overlap = true;
-		  break;
-		}
-	    }
-	  if (__overlap)
+	  // Use __builtin_constant_p to avoid comparing unrelated pointers.
+	  if (__builtin_constant_p(__s2 < __s1)
+		&& __s1 > __s2 && __s1 < (__s2 + __n))
 	    {
 	      do
 		{
diff --git a/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc b/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc
new file mode 100644
index 00000000000..0fe765d53bc
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/char_traits/requirements/113200.cc
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++20 } }
+
+// PR libstdc++/113200
+// char_traits::move is not constexpr when the argument is a string literal
+
+#include <string_view>
+
+template<std::size_t N> struct S
+{
+  char data_[ N ];
+
+  constexpr S( char const* p ): data_{}
+  {
+    std::char_traits<char>::move( data_, p, N );
+  }
+};
+
+template<std::size_t N> S( char const(&)[N] ) -> S<N>;
+
+constexpr S s( "test" );
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-05 10:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-05 10:25 [committed] libstdc++: Fix std::char_traits<C>::move [PR113200] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).