libstdc++: Fix gnu-version-namespace buid

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.
This commit is contained in:
François Dumont 2020-10-30 13:11:49 +01:00
parent 60d9f25487
commit de77abee11
2 changed files with 50 additions and 4 deletions

View 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]]

View 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