From de77abee118f1900fceb25c34b0cc1ef7261c9ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Dumont?= Date: Fri, 30 Oct 2020 13:11:49 +0100 Subject: [PATCH] libstdc++: Fix gnu-version-namespace buid Co-authored-by: Jonathan Wakely 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 | 51 +++++++++++++++++-- libstdc++-v3/src/c++20/sstream-inst.cc | 3 ++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc index d52c0a937b9..c279809cf35 100644 --- a/libstdc++-v3/src/c++17/floating_from_chars.cc +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc @@ -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 #include #include @@ -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]] diff --git a/libstdc++-v3/src/c++20/sstream-inst.cc b/libstdc++-v3/src/c++20/sstream-inst.cc index e04560d28c8..8c6840115c5 100644 --- a/libstdc++-v3/src/c++20/sstream-inst.cc +++ b/libstdc++-v3/src/c++20/sstream-inst.cc @@ -29,6 +29,7 @@ // Instantiations in this file are only for the new SSO std::string ABI #include +#if _GLIBCXX_USE_CXX11_ABI namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -106,3 +107,5 @@ basic_stringstream::view() const noexcept; _GLIBCXX_END_NAMESPACE_VERSION } + +#endif //_GLIBCXX_USE_CXX11_ABI -- 2.30.2