]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix gnu-version-namespace buid
authorFrançois Dumont <fdumont@gcc.gnu.org>
Fri, 30 Oct 2020 12:11:49 +0000 (13:11 +0100)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Sat, 31 Oct 2020 17:10:10 +0000 (18:10 +0100)
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog

* src/c++17/floating_from_chars.cc (_GLIBCXX_USE_CX11_ABI): Add define.
(buffering_string): New.
[!_GLIBCXX_USE_CXX11_ABI](reserve_string): New.
(from_chars): Adapt.
* src/c++20/sstream-inst.cc: Limit instantiations to
_GLIBCXX_USE_CXX11_ABI.

libstdc++-v3/src/c++17/floating_from_chars.cc
libstdc++-v3/src/c++20/sstream-inst.cc

index d52c0a937b9f09648a644c70e5bd48a85f66f2b3..c279809cf35d2f10743e2f79fe9b082dded9e319 100644 (file)
@@ -27,6 +27,9 @@
 // 23.2.9  Primitive numeric input conversion [utility.from.chars]
 //
 
+// Prefer to use std::pmr::string if possible, which requires the cxx11 ABI.
+#define _GLIBCXX_USE_CXX11_ABI 1
+
 #include <charconv>
 #include <string>
 #include <memory_resource>
@@ -87,6 +90,12 @@ namespace
     void* m_ptr = nullptr;
   };
 
+#if _GLIBCXX_USE_CXX11_ABI
+  using buffered_string = std::pmr::string;
+#else
+  using buffered_string = std::string;
+#endif
+
   inline bool valid_fmt(chars_format fmt)
   {
     return fmt != chars_format{}
@@ -130,7 +139,7 @@ namespace
   // Returns a nullptr if a valid pattern is not present.
   const char*
   pattern(const char* const first, const char* last,
-         chars_format& fmt, pmr::string& buf)
+         chars_format& fmt, buffered_string& buf)
   {
     // fmt has the value of one of the enumerators of chars_format.
     __glibcxx_assert(valid_fmt(fmt));
@@ -359,6 +368,22 @@ namespace
     return result;
   }
 
+#if ! _GLIBCXX_USE_CXX11_ABI
+  inline bool
+  reserve_string(std::string& s) noexcept
+  {
+    __try
+      {
+       s.reserve(buffer_resource::guaranteed_capacity());
+      }
+    __catch (const std::bad_alloc&)
+      {
+       return false;
+      }
+    return true;
+  }
+#endif
+
 } // namespace
 
 // FIXME: This should be reimplemented so it doesn't use strtod and newlocale.
@@ -369,10 +394,16 @@ from_chars_result
 from_chars(const char* first, const char* last, float& value,
           chars_format fmt) noexcept
 {
+  errc ec = errc::invalid_argument;
+#if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
   pmr::string buf(&mr);
+#else
+  string buf;
+  if (!reserve_string(buf))
+    return make_result(first, 0, {}, ec);
+#endif
   size_t len = 0;
-  errc ec = errc::invalid_argument;
   __try
     {
       if (const char* pat = pattern(first, last, fmt, buf)) [[likely]]
@@ -389,10 +420,16 @@ from_chars_result
 from_chars(const char* first, const char* last, double& value,
           chars_format fmt) noexcept
 {
+  errc ec = errc::invalid_argument;
+#if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
   pmr::string buf(&mr);
+#else
+  string buf;
+  if (!reserve_string(buf))
+    return make_result(first, 0, {}, ec);
+#endif
   size_t len = 0;
-  errc ec = errc::invalid_argument;
   __try
     {
       if (const char* pat = pattern(first, last, fmt, buf)) [[likely]]
@@ -409,10 +446,16 @@ from_chars_result
 from_chars(const char* first, const char* last, long double& value,
           chars_format fmt) noexcept
 {
+  errc ec = errc::invalid_argument;
+#if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
   pmr::string buf(&mr);
+#else
+  string buf;
+  if (!reserve_string(buf))
+    return make_result(first, 0, {}, ec);
+#endif
   size_t len = 0;
-  errc ec = errc::invalid_argument;
   __try
     {
       if (const char* pat = pattern(first, last, fmt, buf)) [[likely]]
index e04560d28c8d00643d19f3dcfca25e5554515f30..8c6840115c507c3b12f80157832be9788e0b3aab 100644 (file)
@@ -29,6 +29,7 @@
 // Instantiations in this file are only for the new SSO std::string ABI
 #include <sstream>
 
+#if _GLIBCXX_USE_CXX11_ABI
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -106,3 +107,5 @@ basic_stringstream<wchar_t>::view() const noexcept;
 
 _GLIBCXX_END_NAMESPACE_VERSION
 }
+
+#endif //_GLIBCXX_USE_CXX11_ABI