Commit Graph

7681 Commits

Author SHA1 Message Date
Jonathan Wakely ca97c87758 libstdc++: Add <spanstream> declarations to <iosfwd> [PR105284]
libstdc++-v3/ChangeLog:

	PR libstdc++/105284
	* include/std/iosfwd: Add declarations for <spanstream> class
	templates and typedefs.
	* include/std/spanstream (basic_spanbuf, basic_ispanstream)
	(basic_ospanstream, basic_spanstream): Remove default template
	arguments.
	* testsuite/27_io/headers/iosfwd/synopsis.cc: Add <spanstream>
	declarations.
	* testsuite/27_io/headers/iosfwd/types.cc: Check <spanstream>
	default arguments.
2022-05-10 16:40:22 +01:00
Jonathan Wakely 1807e07825 libstdc++: Add <syncstream> declarations to <iosfwd> [PR105284]
libstdc++-v3/ChangeLog:

	PR libstdc++/105284
	* include/std/iosfwd: Add declarations for <synstream> class
	templates and typedefs.
	* include/std/syncstream (basic_syncbuf, basic_osyncstream):
	Remove default template arguments.
	* testsuite/27_io/headers/iosfwd/synopsis.cc: New test.
	* testsuite/27_io/headers/iosfwd/types.cc: New test.
2022-05-10 16:40:22 +01:00
Jonathan Wakely 42991a9116 libstdc++: Simplify std::normal_distribution equality operator
libstdc++-v3/ChangeLog:

	* include/bits/random.tcc (operator==): Only check
	normal_distribution::_M_saved_available once.
	* testsuite/26_numerics/random/normal_distribution/operators/equal.cc:
	Check equality after state changes.
	* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
	lineno.
2022-05-06 23:54:09 +01:00
Jonathan Wakely 909ef4e272 libstdc++: Fix deserialization for std::normal_distribution [PR105502]
This fixes a regression in std::normal_distribution deserialization that
caused the object to be left unchanged if the __state_avail value read
from the stream was false.

libstdc++-v3/ChangeLog:

	PR libstdc++/105502
	* include/bits/random.tcc
	(operator>>(basic_istream<C,T>&, normal_distribution<R>&)):
	Update state when __state_avail is false.
	* testsuite/26_numerics/random/normal_distribution/operators/serialize.cc:
	Check that deserialized object equals serialized one.
2022-05-06 23:54:09 +01:00
Alexandre Oliva 469c76f0d9 libstdc++: ppc: conditionalize vsx-only simd intrinsics
libstdc++'s bits/simd.h section for PowerPC, guarded by __ALTIVEC__,
defines various intrinsic vector types that are only available with
__VSX__: 64-bit long double, double, (un)signed long long, and 64-bit
(un)signed long.

experimental/simd/standard_abi_usable{,_2}.cc tests error out
reporting the unmet requirements when the target cpu doesn't enable
VSX.  Make the reported instrinsic types conditional on __VSX__ so
that <experimental/simd> can be used on PowerPC variants that do not
support VSX.


for  libstdc++-v3/ChangeLog

	* include/experimental/bits/simd.h [__ALTIVEC__]: Require VSX
	for double, long long, and 64-bit long intrinsic types.
	[__ALTIVEC__] (__intrinsic_type): Mention 128-bit in
	preexisting long double diagnostic, adjust no-VSX double
	diagnostic to cover 64-bit long double as well.
2022-05-06 15:43:42 -03:00
Jonathan Wakely e112e37f29 libstdc++: Do not include <cxxabi.h> in <stacktrace>
This avoids polluting the global namespace with the "abi" namespace
alias.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace: Do not include <cxxabi.h>.
	(__cxa_demangle): Declare.
2022-05-06 14:43:39 +01:00
Jonathan Wakely 488d268728 libstdc++: Do not use #include inside push visibility scope [PR99871]
libstdc++-v3/ChangeLog:

	PR libstdc++/99871
	* include/bits/specfun.h: Use visibility attribute on namespace,
	instead of pragma push/pop.
	* libsupc++/compare: Likewise.
	* libsupc++/exception: Likewise.
	* libsupc++/exception.h: Likewise.
	* libsupc++/exception_ptr.h: Likewise.
	* libsupc++/initializer_list: Likewise.
	* libsupc++/nested_exception.h: Likewise.
2022-05-06 14:43:39 +01:00
Jonathan Wakely b6b6600678 libstdc++: Fixes for tests that fail with -fno-rtti
This disables a use of dynamic_cast that is not valid for -fno-rtti and
adjusts some tests so they don't FAIL with -fno-rtti. Some tests are
skipped completely, and others just make use of typeid conditional on
the __cpp_rtti macro. A couple of tests were using typeid to verify
typedefs denote the right type, which can be done at compile-time using
templates instead.

libstdc++-v3/ChangeLog:

	* include/experimental/memory_resource [!__cpp_rtti]
	(__resource_adaptor_imp::do_is_equal): Do not use dynamic_cast
	when RTTI is disabled.
	* testsuite/17_intro/freestanding.cc: Require RTTI.
	* testsuite/18_support/exception/38732.cc: Likewise.
	* testsuite/18_support/exception_ptr/rethrow_exception.cc:
	Likewise.
	* testsuite/18_support/nested_exception/68139.cc: Likewise.
	* testsuite/18_support/nested_exception/rethrow_if_nested.cc:
	Likewise.
	* testsuite/18_support/type_info/103240.cc: Likewise.
	* testsuite/18_support/type_info/fundamental.cc: Likewise.
	* testsuite/18_support/type_info/hash_code.cc: Likewise.
	* testsuite/20_util/any/assign/emplace.cc: Likewise.
	* testsuite/20_util/any/cons/in_place.cc: Likewise.
	* testsuite/20_util/any/misc/any_cast.cc: Likewise.
	* testsuite/20_util/any/observers/type.cc: Likewise.
	* testsuite/20_util/function/1.cc: Likewise.
	* testsuite/20_util/function/2.cc: Likewise.
	* testsuite/20_util/function/3.cc: Likewise.
	* testsuite/20_util/function/4.cc: Likewise.
	* testsuite/20_util/function/5.cc: Likewise.
	* testsuite/20_util/function/6.cc: Likewise.
	* testsuite/20_util/function/7.cc: Likewise.
	* testsuite/20_util/function/8.cc: Likewise.
	* testsuite/20_util/polymorphic_allocator/resource.cc: Likewise.
	* testsuite/20_util/shared_ptr/casts/1.cc: Likewise.
	* testsuite/20_util/shared_ptr/casts/rval.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/unique_ptr_deleter_ref_2.cc:
	Likewise.
	* testsuite/20_util/shared_ptr/misc/get_deleter.cc: Likewise.
	* testsuite/20_util/typeindex/comparison_operators.cc: Likewise.
	* testsuite/20_util/typeindex/comparison_operators_c++20.cc:
	Likewise.
	* testsuite/20_util/typeindex/hash.cc: Likewise.
	* testsuite/20_util/typeindex/hash_code.cc: Likewise.
	* testsuite/20_util/typeindex/name.cc: Likewise.
	* testsuite/22_locale/ctype/is/string/89728_neg.cc: Likewise.
	* testsuite/22_locale/global_templates/standard_facet_hierarchies.cc:
	Likewise.
	* testsuite/22_locale/global_templates/user_facet_hierarchies.cc:
	Likewise.
	* testsuite/22_locale/locale/13630.cc: Check type without using
	RTTI.
	* testsuite/23_containers/array/requirements/non_default_constructible.cc:
	Require RTTI.
	* testsuite/27_io/basic_ostream/emit/1.cc: Likewise.
	* testsuite/27_io/fpos/14320-1.cc: Check type without using RTTI.
	* testsuite/27_io/fpos/mbstate_t/12065.cc: Require RTTI.
	* testsuite/27_io/ios_base/failure/dual_abi.cc: Likewise.
	* testsuite/experimental/any/misc/any_cast.cc: Likewise.
	* testsuite/experimental/any/observers/type.cc: Likewise.
	* testsuite/experimental/memory_resource/resource_adaptor.cc:
	Likewise.
	* testsuite/lib/libstdc++.exp (check_effective_target_rtti):
	Define new proc.
	* testsuite/tr1/3_function_objects/function/1.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/2.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/3.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/4.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/5.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/6.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/7.cc: Likewise.
	* testsuite/tr1/3_function_objects/function/8.cc: Likewise.
	* testsuite/tr2/bases/value.cc: Likewise.
	* testsuite/tr2/direct_bases/value.cc: Likewise.
	* testsuite/util/exception/safety.h [!__cpp_rtti]: Don't print
	types without RTTI.
2022-05-05 21:13:58 +01:00
Jonathan Wakely 22399ad6ed libstdc++: Add always_inline to the simplest std::array accessors [PR104719]
libstdc++-v3/ChangeLog:

	PR libstdc++/104719
	* include/std/array (array::size(), array::max_size())
	(array::empty(), array::data()): Add  always_inline attribute.
2022-05-04 16:24:56 +01:00
Jonathan Wakely ef8d5ac08b libstdc++: Simplify std::array accessors [PR104719]
This removes the __array_traits::_S_ref and __array_traits::_S_ptr
accessors, which only exist to make the special case of std::array<T, 0>
syntactically well-formed.

By changing the empty type used as the std::array<T, 0>::_M_elems data
member to support operator[] and conversion to a pointer, we can write
code using the natural syntax. The indirection through _S_ref and
_S_ptr is removed for the common case, and a function call is only used
for the special case of zero-size arrays.

The invalid member access for zero-sized arrays is changed to use
__builtin_trap() instead of a null dereference. This guarantees a
runtime error if it ever gets called, instead of undefined behaviour
that is likely to get optimized out as unreachable.

libstdc++-v3/ChangeLog:

	PR libstdc++/104719
	* include/std/array (__array_traits::_S_ref): Remove.
	(__array_traits::_S_ptr): Remove.
	(__array_traits<T, 0>::_Type): Define operator[] and operator T*
	to provide an array-like API.
	(array::_AT_Type): Remove public typeef.
	(array::operator[], array::at, array::front, array::back): Use
	index operator to access _M_elems instead of _S_ref.
	(array::data): Use implicit conversion from _M_elems to pointer.
	(swap(array&, array&)): Use __enable_if_t helper.
	(get<I>): Use index operator to access _M_elems.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc:
	Adjust dg-error line numbers.
2022-05-04 16:24:56 +01:00
Jonathan Wakely 41ccd683f0 libstdc++: Add std::atomic<shared_ptr>(nullptr_t) constructor (LWG 3661)
This DR was approved at the February 2022 plenary.

libstdc++-v3/ChangeLog:

	* include/bits/shared_ptr_atomic.h (atomic<shared_ptr>): Add
	constructor for constant initialization from nullptr_t.
	* testsuite/20_util/shared_ptr/atomic/atomic_shared_ptr.cc:
	Check for new constructor.
2022-04-26 13:22:18 +01:00
Jonathan Wakely e3c5e8360b libstdc++: Define std::hash<std::filesystem::path> (LWG 3657)
This DR was approved at the February 2022 plenary.

libstdc++-v3/ChangeLog:

	* include/bits/fs_path.h (hash<filesystem::path>): Define.
	* testsuite/27_io/filesystem/path/nonmember/hash_value.cc:
	Check std::hash specialization.
2022-04-26 13:22:18 +01:00
Jonathan Wakely 2fbdcf5e58 libstdc++: Implement constexpr std::unique_ptr for C++23 (P2273R3)
libstdc++-v3/ChangeLog:

	* include/bits/ptr_traits.h (__cpp_lib_constexpr_memory): Define
	conditionally.
	* include/bits/unique_ptr.h (__cpp_lib_constexpr_memory):
	Define for C++23.
	(default_delete, default_delete<T[]>, __uniq_ptr_impl)
	(unique_ptr, unique_ptr<T[], D>): Add constexpr to all member
	functions.
	* include/std/version (__cpp_lib_constexpr_memory): Define new
	value for C++23.
	* testsuite/20_util/unique_ptr/assign/constexpr.cc: New test.
	* testsuite/20_util/unique_ptr/comparison/constexpr.cc: New test.
	* testsuite/20_util/unique_ptr/cons/constexpr_c++20.cc: New test.
	* testsuite/20_util/unique_ptr/creation/constexpr.cc: New test.
	* testsuite/20_util/unique_ptr/modifiers/constexpr.cc: New test.
	* testsuite/20_util/unique_ptr/specialized_algorithms/constexpr.cc:
	New test.
2022-04-25 16:16:33 +01:00
Jonathan Wakely a5cee0480c libstdc++: Add deduction guides for std::packaged_task [PR105375]
This change was LWG 3117.

The test is copied from 20_util/function/cons/deduction.cc

libstdc++-v3/ChangeLog:

	PR libstdc++/105375
	* include/std/future (packaged_task): Add deduction guides.
	* testsuite/30_threads/packaged_task/cons/deduction.cc: New test.
2022-04-25 16:16:33 +01:00
Thomas W Rodgers 7c21556daf libstdc++: Make atomic notify_one and notify_all non-const
<recording this here for future reference>
PR102994 "atomics: std::atomic<ptr>::wait is not marked const" raises the
issue that the current libstdc++ implementation marks the notify members
const, the implementation strategy used by libstdc++, as well as libc++
and the Microsoft STL, do not require the atomic to be mutable (it is hard
to conceive of a desirable implementation approach that would require it).
The original paper proposing the wait/notify functionality for atomics
(p1185) also had these members marked const for the first three revisions,
but that was changed without explanation in r3 and subsequent revisions of
the paper.

After raising the issue to the authors of p1185 and the author of the
libc++ implementation, the consensus seems to be "meh, it's harmless" so
there seems little appetite for an LWG issue to revisit the subject.

This patch changes the libstdc++ implementation to be in agreement with
the standard by removing const from those notify_one/notify_all members.

libstdc++-v3/ChangeLog:

	PR libstdc++/102994
	* include/bits/atomic_base.h (atomic_flag::notify_one,
	notify_all): Remove const qualification.
	(__atomic_base::notify_one, notify_all): Likewise.
	* include/std/atomic (atomic<bool>::notify_one, notify_all):
	Likewise.
	(atomic::notify_one, notify_all): Likewise.
	(atomic<T*>::notify_one, notify_all): Likewise.
	(atomic_notify_one, atomic_notify_all): Likewise.
	* testsuite/29_atomics/atomic/wait_notify/102994.cc: Adjust test
	to account for change in notify_one/notify_all signature.
2022-04-22 15:53:41 -07:00
Patrick Palka 93dd7f36f2 libstdc++: Avoid ASCII assumptions in floating_from_chars.cc
In starts_with_ci and in __floating_from_chars_hex's inf/nan handling,
we were assuming that the letters are contiguous and that 'A' + 32 == 'a'
which is true for ASCII but not for other character encodings.

This patch fixes starts_with_ci by using a constexpr lookup table that
maps uppercase letters to lowercase, and fixes __floating_from_chars_hex
by using __from_chars_alnum_to_val.

libstdc++-v3/ChangeLog:

	* include/std/charconv (__from_chars_alnum_to_val_table):
	Simplify initialization of __lower/__upper_letters.
	(__from_chars_alnum_to_val): Default the template parameter to
	false.
	* src/c++17/floating_from_chars.cc (starts_with_ci): Don't
	assume the uppercase and lowercase letters are contiguous.
	(__floating_from_chars_hex): Likewise.
2022-04-21 12:11:01 -04:00
Patrick Palka 1e6c0e69af libstdc++: Work around modules ICE in <charconv> [PR105297]
This makes the initializer for __table in __from_chars_alnum_to_val
dependent in an artificial way, which works around the reported modules
testsuite ICE by preventing the compiler from evaluating the initializer
parse time.

Compared to the alternative workaround of using a non-local class type
for __table, this workaround has the advantage of slightly speeding up
compilation of <charconv>, since now the table won't get built (via
constexpr evaluation) until the integer std::from_chars overload is
instantiated.

	PR c++/105297
	PR c++/105322

libstdc++-v3/ChangeLog:

	* include/std/charconv (__from_chars_alnum_to_val): Make
	initializer for __table dependent in an artificial way.
2022-04-21 08:34:59 -04:00
Patrick Palka d210653f39 libstdc++: Micro-optimize __from_chars_pow2_base
In the first iteration of __from_chars_pow2_base's main loop, we need
to remember the value of the leading significant digit for sake of the
overflow check at the end (for base > 2).

This patch manually unrolls this first iteration so as to not encumber
the entire loop with logic that only the first iteration needs.  This
seems to significantly improve performance:

Base  Before  After (seconds, lower is better)
   2    9.36   9.37
   8    3.66   2.93
  16    2.93   1.91
  32    2.39   2.24

libstdc++-v3/ChangeLog:

	* include/std/charconv (__from_chars_pow2_base): Manually
	unroll the first iteration of the main loop and simplify
	accordingly.
2022-04-18 17:22:55 -04:00
Patrick Palka 7e76cef873 libstdc++: Avoid double-deref of __first in ranges::minmax [PR104858]
PR libstdc++/104858

libstdc++-v3/ChangeLog:

	* include/bits/ranges_algo.h (__minmax_fn): Avoid dereferencing
	__first twice at the start.
	* testsuite/25_algorithms/minmax/constrained.cc (test06): New test.
2022-04-15 14:41:14 -04:00
Patrick Palka a54137c880 libstdc++: Optimize integer std::from_chars
This applies the following optimizations to the integer std::from_chars
implementation:

  1. Use a lookup table for converting an alphanumeric digit to its
     base-36 value instead of using a range test (for 0-9) and switch
     (for a-z and A-Z).  The table is constructed using a C++14
     constexpr function which doesn't assume a particular character
     encoding or __CHAR_BIT__ value.  This new conversion function
     __from_chars_alnum_to_val is templated on whether we care
     only about the decimal digits, in which case we can perform the
     conversion with a single subtraction since the digit characters
     are guaranteed to be contiguous (unlike the letters).
  2. Generalize __from_chars_binary to handle all power-of-two bases.
     This function (now named __from_chars_pow2_base) is also templated
     on whether we care only about the decimal digits for the benefit of
     faster digit conversion for base 2, 4 and 8.
  3. In __from_chars_digit, use
       static_cast<unsigned char>(__c - '0') < __base
     instead of
       '0' <= __c && __c <= ('0' + (__base - 1)).
     as the digit recognition test (exhaustively verified that the two
     tests are equivalent).
  4. In __from_chars_alnum, use a nested loop to consume the rest of the
     digits in the overflow case (mirroring __from_chars_digit) so that
     the main loop doesn't have to maintain the overflow flag __valid.

At this point, __from_chars_digit is nearly identical to
__from_chars_alnum, so this patch merges the two functions by removing
the former and templatizing the latter according to whether we care only
about the decimal digits.  Finally,

  5. In __from_chars_alnum, maintain a lower bound on the number of
     unused bits in the result and use it to omit the overflow check
     when it's safe to do so.

In passing, this patch replaces the non-portable function ascii_to_hexit
used by __floating_from_chars_hex with the new conversion function.

Some runtime measurements for a simple 15-line benchmark that roundtrips
printing/parsing 200 million integers via std::to/from_chars (average of
5 runs):

  Base  Before  After (seconds, lower is better)
     2    9.37   9.37
     3   15.79  12.13
     8    4.15   3.67
    10    4.90   3.86
    11    6.84   5.03
    16    4.14   2.93
    32    3.85   2.39
    36    5.22   3.26

libstdc++-v3/ChangeLog:

	* include/std/charconv (__from_chars_alnum_to_val_table): Define.
	(__from_chars_alnum_to_val): Define.
	(__from_chars_binary): Rename to ...
	(__from_chars_pow2_base): ... this.  Generalize to handle any
	power-of-two base using __from_chars_alnum_to_val.
	(__from_chars_digit): Optimize digit recognition to a single
	test instead of two tests.  Use [[__unlikely___]] attribute.
	(__from_chars_alpha_to_num): Remove.
	(__from_chars_alnum): Use __from_chars_alnum_to_val.  Use a
	nested loop for the overflow case.  Maintain a lower bound
	on the number of available bits in the result and use it to
	omit the overflow check.
	(from_chars): Adjust appropriately.
	* src/c++17/floating_from_chars.cc (ascii_to_hexit): Remove.
	(__floating_from_chars_hex): Use __from_chars_alnum_to_val
	to recognize a hex digit instead.
2022-04-15 09:34:09 -04:00
Jonathan Wakely 41a72a744a libstdc++: Add new headers to <bits/stdc++.h> PCH
libstdc++-v3/ChangeLog:

	* include/precompiled/stdc++.h: Include <stacktrace> and
	<stdatomic.h> for C++23.
2022-04-14 11:02:43 +01:00
Jonathan Wakely d2f8208e9a libstdc++: Fix missing and incorrect feature test macros [PR105269]
libstdc++-v3/ChangeLog:

	PR libstdc++/105269
	* include/bits/stl_vector.h (__cpp_lib_constexpr_vector):
	Define.
	* include/c_compatibility/stdatomic.h (__cpp_lib_stdatomic_h):
	Define.
	* include/std/optional (__cpp_lib_optional): Define new value
	for C++23.
	(__cpp_lib_monadic_optional): Remove.
	* include/std/version (__cpp_lib_constexpr_vector): Define.
	(__cpp_lib_stdatomic_h): Define.
	(__cpp_lib_optional): Define new value for C++23.
	(__cpp_lib_monadic_optional): Remove.
	* testsuite/20_util/optional/monadic/and_then.cc: Adjust.
	* testsuite/20_util/optional/requirements.cc: Adjust for C++23.
	* testsuite/20_util/optional/version.cc: Likewise.
	* testsuite/23_containers/vector/cons/constexpr.cc: Check
	feature test macro.
	* testsuite/29_atomics/headers/stdatomic.h/c_compat.cc:
	Likewise.
	* testsuite/20_util/optional/monadic/version.cc: Removed.
	* testsuite/23_containers/vector/requirements/version.cc: New test.
	* testsuite/29_atomics/headers/stdatomic.h/version.cc: New test.
2022-04-14 11:02:37 +01:00
Jonathan Wakely b2c007b87d libstdc++: shrink-to-fit in std::basic_stacktrace::current(skip, max)
If a large stacktrace is reduced to a max depth that is less than half
the capacity it will now be reallocated to remove the unused capacity.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace::current): Reallocate
	a smaller container if the unused capacity is larger than the
	used size.
2022-04-12 22:38:31 +01:00
Jonathan Wakely 7cf8875995 libstdc++: Use allocator to construct std::stacktrace_entry objects
Because std::basic_stacktrace<A> is an allocator-aware container its
elements should be initialized using allocator_traits<A>::construct and
destroyed using allocator_traits<A>::destroy.

This adds new _M_clone and _M_assign helper functions to construct
elements correctly and uses those functions instead of calling
std::uninitialized_copy_n.

The _Impl::_M_destroy function needs to be passed an allocator to
destroy the elements correctly, so is replaced by _M_resize which can
also be used to trim the container to a smaller size.

Because destroying and creating std::stacktrace_entry objects is cheap,
the copy/move assignment operators can just destroy all existing
elements and use _Impl._M_clone or _Impl._M_assign to create new ones.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace): Use _Impl::_M_clone
	or _Impl::_M_assign to initialize elements in allocated storage.
	(basic_stacktrace::_M_clear()): Use _Impl::_M_resize instead of
	_Impl::_M_destroy.
	(basic_stacktrace::_Impl::_M_destroy()): Replace with ...
	(basic_stacktrace::_Impl::_M_resize(size_type, allocator&)): New
	function.
	(basic_stacktrace::_Impl::_M_push_back): Use _M_xclone. Construct
	new element using allocator.
	(basic_stacktrace::_Impl::_M_clone): New function.
	(basic_stacktrace::_Impl::_M_xclone): New function.
	(basic_stacktrace::_Impl::_M_assign): New function.
2022-04-12 22:38:31 +01:00
Jonathan Wakely e48933f00d libstdc++: Use nothrow new in std::stacktrace
We can avoid the overhead of handling a bad_alloc exception from
std::allocator<std::stacktrace_entry>::allocate by just calling the
nothrow operator new instead.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace::_Impl::_M_allocate):
	Use nothrow new instead of try block for std::allocator.
	(basic_stacktrace::_Impl::_M_deallocate): Use delete for
	std::allocator.
2022-04-12 22:38:31 +01:00
Jonathan Wakely 2ce0f5185b libstdc++: Reduce memory usage in std::stacktrace::current
This adds an alternative callback for use in the overload of
basic_stacktrace::current that takes a max_depth parameter. The new
callback will not allow the container to grow past the initial capacity,
which is set to the specified maximum depth.  This avoids allocating
memory for hundreds of frames only to discard them again because of a
small maximum depth limit.

For larger maximum depths the normal callback is used, with a smaller
initial capacity that can grow as needed. The container will be resized
to the given max depth after the entire backtrace has been produced
(relying on the fact that std::stacktrace_entry objects are trivially
destructible to elide their destruction).

Currently the value for "larger" limits is 128, so a max depth <= 128
will allocate capacity for exactly that many frames. A larger max depth
(or an unspecified max depth) will use an initial capacity of 64 frames
and grow as needed. Since each frame is only a uintptr_t value it might
be reasonable to increase the first value so that memory usage can be
capped for larger maximum depths.

This change also delays the creation of the libbacktrace state until we
actually need it, so that the state is not created if allocation fails.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace::current): Replace
	calls to _M_reserve and _S_curr_cb with call to _M_prepare.
	Check return value of backtrace_simple when max depth given.
	(basic_stacktrace::_M_reserve): Remove.
	(basic_stacktrace::_S_curr_cb): Remove.
	(basic_stacktrace::_M_prepare(size_type)): New function to
	reserve initial capacity and return callback.
	(basic_stacktrace::_Impl::_M_allocate): Remove check for 0 < n
	and remove redundant zeroing of _M_frames and _M_capacity.
	(basic_stacktrace::_Impl::_M_push_back): Add [[unlikely]]
	attribute. Assign _Impl instead of swapping.
	* testsuite/19_diagnostics/stacktrace/current.cc: New test.
2022-04-12 22:38:31 +01:00
Jonathan Wakely b1124648ff libstdc++: Fix incorrect branch prediction hint in std::stacktrace
libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace::_Impl::_M_allocate):
	Change [[unlikely]] attribute to [[likely]].
2022-04-11 22:10:00 +01:00
Jonathan Wakely 73da9ec556 libstdc++: Fix std::basic_stacktrace special members [PR105031]
The PR points out that there is a non-constant condition used for an
if-constexpr statement, but there are several other problems with the
copy, move and swap members of std::basic_stacktrace.

libstdc++-v3/ChangeLog:

	PR libstdc++/105031
	* include/std/stacktrace (basic_stacktrace::basic_stacktrace):
	Fix allocator usage in constructors.
	(basic_stacktrace::operator=(const basic_stacktrace&)): Do not
	try to reallocate using const allocator.
	(basic_stacktrace::operator=(basic_stacktrace&&)): Fix
	if-constexpr with non-constant condition. Do not allocate new
	storage if allocator propagates. Do not set _M_size if
	allocation fails.
	(basic_stacktrace::swap(basic_stacktrace&)): Fix typo. Add
	assertion that non-propagating allocators are equal.
	* testsuite/19_diagnostics/stacktrace/stacktrace.cc: New test.
2022-04-11 17:59:50 +01:00
Jonathan Wakely bdb9639ee9 libstdc++: Improve behaviour of std::stacktrace::current
This prevents inlining the current() function to guarantee that it is
present in the stacktrace, then tells libbacktrace to skip that frame.

To avoid overflow in the int argument to __glibcxx_backtrace_simple, we
need to check if the skip parameter exceeds INT_MAX (which is possible
for 16-bit targets where short and int have the same width). We also
need to limit the size of the returned value to the max_depth parameter,
which was missing previously.

This also fixes basic_stacktrace::max_size() to not exceed the maximum
size supported by the allocator, which might be smaller than the maximum
value of size_type.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (basic_stacktrace::current): Duplicate
	implementation into each overload. Add noinline attribute and
	skip current frame.
	(basic_stacktrace::max_size()): Call _Impl::_S_max_size.
	(basic_stacktrace::_S_curr_cb()): New function defining lambda.
	(basic_stacktrace::_Impl::_S_max_size): New function defining
	maximum size in terms of allocator and size_type.
	(basic_stacktrace::_Impl::_M_allocate): Check against
	max_size().
	* testsuite/19_diagnostics/stacktrace/entry.cc: Call function
	for non-constexpr checks. Check line number is correct.
2022-04-11 17:59:08 +01:00
Jonathan Wakely 0df39b0803 libstdc++: Move Filesystem TS path definitions out of class body
This fixes some errors with clang caused by instantiating vector<_Cmpt>
before the _Cmpt type is complete.

libstdc++-v3/ChangeLog:

	* include/experimental/bits/fs_path.h (path): Define special
	members after path::_Cmpt is complete.
2022-04-11 15:53:22 +01:00
Jonathan Wakely 7b4495d3c4 libstdc++: Fix constraints on std::expected<void, E> constructor [PR105153]
libstdc++-v3/ChangeLog:

	PR libstdc++/105153
	* include/std/expected
	(expected<void,E>::expected(expected<U,G>&&)): Fix constraints.
	* testsuite/20_util/expected/cons.cc: Check constructor.
2022-04-08 18:30:07 +01:00
Jonathan Wakely 0dfaf56252 libstdc++: Fix std::expected<void, E>::swap(expected&) [PR105154]
libstdc++-v3/ChangeLog:

	PR libstdc++/105154
	* include/std/expected (expected<void, E>::swap): Set
	_M_has_value to false for objects that previously had a value.
	* testsuite/20_util/expected/swap.cc: Fix test to check void
	specialization.
2022-04-08 18:30:06 +01:00
Jonathan Wakely 29e355d0d6 libstdc++: Fix std::bad_expected_access constructor [PR105146]
libstdc++-v3/ChangeLog:

	PR libstdc++/105146
	* include/std/expected (bad_expected_access): Move constructor
	parameter.
	* testsuite/20_util/expected/bad.cc: New test.
2022-04-08 18:30:06 +01:00
Jakub Jelinek 2a82301d40 libstdc++: Tweak source_location for clang trunk [PR105128]
Apparently clang trunk implemented __builtin_source_location(), but the
using __builtin_ret_type = decltype(__builtin_source_location());
which has been added for it isn't enough, they also need the
std::source_location::__impl class to be defined (but incomplete seems
to be good enough) before the builtin is used.

The following has been tested on godbolt with clang trunk (old version
fails with
error: 'std::source_location::__impl' was not found; it must be defined before '__builtin_source_location' is called
and some follow-up errors), getting back to just void * instead of
__builtin_ret_type and commenting out using doesn't work either and
just struct __impl; before using __builtin_ret_type doesn't work too.

2022-04-02  Jakub Jelinek  <jakub@redhat.com>

	PR libstdc++/105128
	* include/std/source_location (std::source_location::__impl): Move
	definition before using __builtin_ret_type.
2022-04-02 12:49:38 +02:00
Jonathan Wakely babaabbcc8 libstdc++: Implement std::unreachable() for C++23 (P0627R6)
This defines std::unreachable as an assertion for debug mode, a trap
when _GLIBCXX_ASSERTIONS is defined, and __builtin_unreachable()
otherwise.

The reason for only using __builtin_trap() in the second case is to
avoid the overhead of setting up a call to __glibcxx_assert_fail that
should never happen.

UBsan can detect if __builtin_unreachable() is executed, so if a feature
test macro for that sanitizer is added, we could change just use
__builtin_unreachable() when the sanitizer is enabled.

While thinking about what the debug assertion failure should print, I
noticed that the __glibcxx_assert_fail function doesn't check for null
pointers. This adds a check so we don't try to print them if null.

libstdc++-v3/ChangeLog:

	* include/std/utility (unreachable): Define for C++23.
	* include/std/version (__cpp_lib_unreachable): Define.
	* src/c++11/debug.cc (__glibcxx_assert_fail): Check for valid
	arguments. Handle only the function being given.
	* testsuite/20_util/unreachable/1.cc: New test.
	* testsuite/20_util/unreachable/version.cc: New test.
2022-04-01 13:17:18 +01:00
Jonathan Wakely 944da70a5d libstdc++: Fix mismatched noexcept-specifiers in Filesystem TS
The copy_file fix should have been part of r12-7063-gda72e0fd20f87b.

The path::begin() fix should have been part of r12-3930-gf2b7f56a15d9cb.
Thanks to Timm Bäder for reporting this one.

libstdc++-v3/ChangeLog:

	* include/experimental/bits/fs_fwd.h (copy_file): Remove
	incorrect noexcept from declaration.
	* include/experimental/bits/fs_path.h (path::begin, path::end):
	Add noexcept to declarations, to match definitions.
2022-04-01 13:17:18 +01:00
Timm Bäder 1e9c026848 libstdc++: Fix filenames in Doxygen @file comments
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	* include/bits/fs_ops.h: Fix filename in Doxygen comment.
	* include/experimental/bits/fs_ops.h: Likewise.
2022-04-01 11:34:43 +01:00
Jonathan Wakely 7255d29c57 libstdc++: Fix incorrect preprocessor conditions in <version>
The conditions that guard the feature test macros in <version> should
match the main definitions of the macros in other headers.

This doesn't matter for GCC, because it supports all the conditions
being tested here, but it does matter for non-GCC compilers without the
relevant C++20 features.

libstdc++-v3/ChangeLog:

	* include/std/version (__cpp_lib_variant): Fix conditions to
	match <variant>.
	(__cpp_lib_expected): Fix condition to match <expected>.
2022-03-29 10:06:33 +01:00
Jonathan Wakely b78e0ce28b libstdc++: Define std::expected for C++23 (P0323R12)
Because this adds a new class template called std::unexpected, we have
to stop declaring the std::unexpected() function (which was deprecated
in C++11 and removed in C++17).

libstdc++-v3/ChangeLog:

	* doc/doxygen/user.cfg.in: Add new header.
	* include/Makefile.am: Likewise.
	* include/Makefile.in: Regenerate.
	* include/precompiled/stdc++.h: Add new header.
	* include/std/version (__cpp_lib_expected): Define.
	* libsupc++/exception [__cplusplus > 202002] (unexpected)
	(unexpected_handler, set_unexpected): Do not declare for C++23.
	* include/std/expected: New file.
	* testsuite/20_util/expected/assign.cc: New test.
	* testsuite/20_util/expected/cons.cc: New test.
	* testsuite/20_util/expected/illformed_neg.cc: New test.
	* testsuite/20_util/expected/observers.cc: New test.
	* testsuite/20_util/expected/requirements.cc: New test.
	* testsuite/20_util/expected/swap.cc: New test.
	* testsuite/20_util/expected/synopsis.cc: New test.
	* testsuite/20_util/expected/unexpected.cc: New test.
	* testsuite/20_util/expected/version.cc: New test.
2022-03-27 18:50:31 +01:00
Jonathan Wakely 674ec67905 libstdc++: Add more doxygen comments in <bit>
libstdc++-v3/ChangeLog:

	* include/std/bit (bit_cast, byteswap, endian): Add doxygen
	comments.
2022-03-25 20:21:48 +00:00
Jonathan Wakely 4894d69a1f libstdc++: Add missing constraints to std::bit_cast [PR105027]
Our std::bit_cast was relying on the compiler to check for errors inside
__builtin_bit_cast, instead of checking them as constraints. That means
std::bit_cast was not SFINAE-friendly.

This fix uses a requires-clause, so for old versions of Clang without
concepts support the function will still be unconstrained. At some point
in future we can remove the #ifdef __cpp_concepts check and rely on all
compilers having full concepts support in C++20 mode.

libstdc++-v3/ChangeLog:

	PR libstdc++/105027
	* include/std/bit (bit_cast): Add constraints.
	* testsuite/26_numerics/bit/bit.cast/105027.cc: New test.
2022-03-23 12:17:16 +00:00
Jonathan Wakely bd81327b03 libstdc++: Fix feature test macros in <version> for freestanding
Some C++17 and C++20 feature test macros are only defined in <version>
for hosted builds, even though the features are supported for
freestanding.

All C++23 feature test macros are defined in <version> for freestanding,
but most of the features are only supported for hosted.

libstdc++-v3/ChangeLog:

	* include/std/version [!_GLIBCXX_HOSTED]
	(__cpp_lib_hardware_interference_size): Define for freestanding.
	(__cpp_lib_bit_cast): Likewise.
	(__cpp_lib_is_layout_compatible): Likewise.
	(__cpp_lib_is_pointer_interconvertible): Likewise.
	(__cpp_lib_adaptor_iterator_pair_constructor): Do not define for
	freestanding.
	(__cpp_lib_invoke_r): Likewise.
	(__cpp_lib_ios_noreplace): Likewise.
	(__cpp_lib_monadic_optional): Likewise.
	(__cpp_lib_move_only_function): Likewise.
	(__cpp_lib_spanstream): Likewise.
	(__cpp_lib_stacktrace): Likewise.
	(__cpp_lib_string_contains): Likewise.
	(__cpp_lib_string_resize_and_overwrite): Likewise.
	(__cpp_lib_to_underlying): Likewise.
2022-03-23 10:03:03 +00:00
Jonathan Wakely 5bf59b0048 libstdc++: Disable atomic wait for freestanding [PR105021]
We use either condition variables or futexes to implement atomic waits,
so we can't do it in freestanding. This is non-conforming, so should be
revisited later, probably by making freestanding atomic waiting
operations spin without ever blocking.

Reviewed-by: Thomas Rodgers <trodgers@redhat.com>

libstdc++-v3/ChangeLog:

	PR libstdc++/105021
	* include/bits/atomic_base.h [!_GLIBCXX_HOSTED]: Do not include
	<bits/atomic_wait.h> for freestanding.
2022-03-23 10:03:03 +00:00
Jakub Jelinek e63ac860af libstdc++: Work around clang misdesign in time_get<>::get [PR104990]
Apparently clang has a -fgnuc-version= option which allows it to pretend
it is any GCC version the user likes.  It is already bad that it claims to
be GCC 4.2 compatible by default when it is not (various unimplemented
extensions at least), but this option is a horrible idea.

Anyway, this patch adds a hack for it.

2022-03-21  Jakub Jelinek  <jakub@redhat.com>

	PR libstdc++/104990
	* include/bits/locale_facets_nonio.tcc (get): Don't check if do_get
	isn't overloaded if __clang__ is defined.
2022-03-21 11:02:04 +01:00
Jonathan Wakely 7a42b1fa1a libstdc++: Simplify constraints for std::any construction [PR104242]
Partially revert r12-4190-g6da36b7d0e43b6f9281c65c19a025d4888a25b2d
because using __and_<..., is_copy_constructible<T>> when T is incomplete
results in an error about deriving from is_copy_constructible<T> when
that is incomplete. I don't know how to fix that, so this simply
restores the previous constraint which worked in this case (even though
I think it's technically undefined to use is_copy_constructible<T> with
incomplete T). This doesn't restore exactly what we had before, but uses
the is_copy_constructible_v and __is_in_place_type_v variable templates
instead of the ::value member.

libstdc++-v3/ChangeLog:

	PR libstdc++/104242
	* include/std/any (any(T&&)): Revert change to constraints.
	* testsuite/20_util/any/cons/104242.cc: New test.
2022-03-18 15:40:03 +00:00
Jonathan Wakely ac73c944ea libstdc++: Reduce header dependencies from PSTL headers [PR92546]
This avoids including the whole of <functional> in <algorithm>, as the
<pstl/glue_algorithm_defs.h> header only actually needs std::pair.

This also avoids including <iterator> in <pstl/utils.h>, which only
needs <type_traits>, std::bad_alloc, and std::terminate (which can be
repalced with std::__terminate). This matters less, because
<pstl/utils.h> is only included by the <pstl/*_impl.h> headers and they
all use <iterator> anyway, and are only included by <execution>.

libstdc++-v3/ChangeLog:

	PR libstdc++/92546
	* include/pstl/glue_algorithm_defs.h: Replace <functional> with
	<bits/stl_pair.h>.
	* include/pstl/utils.h: Replace <iterator> with <type_traits>.
	(__pstl::__internal::__except_handler): Use std::__terminate
	instead of std::terminate.
	* src/c++17/fs_path.cc: Include <array>.
	* testsuite/25_algorithms/adjacent_find/constexpr.cc: Include
	<functional>.
	* testsuite/25_algorithms/binary_search/constexpr.cc: Likewise.
	* testsuite/25_algorithms/clamp/constrained.cc: Likewise.
	* testsuite/25_algorithms/equal/constrained.cc: Likewise.
	* testsuite/25_algorithms/for_each/constrained.cc: Likewise.
	* testsuite/25_algorithms/includes/constrained.cc: Likewise.
	* testsuite/25_algorithms/is_heap/constexpr.cc: Likewise.
	* testsuite/25_algorithms/is_heap_until/constexpr.cc: Likewise.
	* testsuite/25_algorithms/is_permutation/constrained.cc: Include
	<iterator>.
	* testsuite/25_algorithms/is_sorted/constexpr.cc: Include
	<functional>.
	* testsuite/25_algorithms/is_sorted_until/constexpr.cc:
	Likewise.
	* testsuite/25_algorithms/lexicographical_compare/constexpr.cc:
	Likewise.
	* testsuite/25_algorithms/lexicographical_compare/constrained.cc:
	Likewise.
	* testsuite/25_algorithms/lexicographical_compare_three_way/1.cc:
	Include <array>.
	* testsuite/25_algorithms/lower_bound/constexpr.cc: Include
	<functional>.
	* testsuite/25_algorithms/max/constrained.cc: Likewise.
	* testsuite/25_algorithms/max_element/constrained.cc: Likewise.
	* testsuite/25_algorithms/min/constrained.cc: Likewise.
	* testsuite/25_algorithms/min_element/constrained.cc: Likewise.
	* testsuite/25_algorithms/minmax_element/constrained.cc:
	Likewise.
	* testsuite/25_algorithms/mismatch/constexpr.cc: Likewise.
	* testsuite/25_algorithms/move/93872.cc: Likewise.
	* testsuite/25_algorithms/move_backward/93872.cc: Include
	<iterator>.
	* testsuite/25_algorithms/nth_element/constexpr.cc: Include
	<functional>.
	* testsuite/25_algorithms/partial_sort/constexpr.cc: Likewise.
	* testsuite/25_algorithms/partial_sort_copy/constexpr.cc:
	Likewise.
	* testsuite/25_algorithms/search/constexpr.cc: Likewise.
	* testsuite/25_algorithms/search_n/constrained.cc: Likewise.
	* testsuite/25_algorithms/set_difference/constexpr.cc: Likewise.
	* testsuite/25_algorithms/set_difference/constrained.cc:
	Likewise.
	* testsuite/25_algorithms/set_intersection/constexpr.cc:
	Likewise.
	* testsuite/25_algorithms/set_intersection/constrained.cc:
	Likewise.
	* testsuite/25_algorithms/set_symmetric_difference/constexpr.cc:
	Likewise.
	* testsuite/25_algorithms/set_union/constexpr.cc: Likewise.
	* testsuite/25_algorithms/set_union/constrained.cc: Likewise.
	* testsuite/25_algorithms/sort/constexpr.cc: Likewise.
	* testsuite/25_algorithms/sort_heap/constexpr.cc: Likewise.
	* testsuite/25_algorithms/transform/constrained.cc: Likewise.
	* testsuite/25_algorithms/unique/constexpr.cc: Likewise.
	* testsuite/25_algorithms/unique/constrained.cc: Likewise.
	* testsuite/25_algorithms/unique_copy/constexpr.cc: Likewise.
	* testsuite/25_algorithms/upper_bound/constexpr.cc: Likewise.
	* testsuite/std/ranges/adaptors/elements.cc: Include <vector>.
	* testsuite/std/ranges/adaptors/lazy_split.cc: Likewise.
	* testsuite/std/ranges/adaptors/split.cc: Likewise.
2022-03-18 10:16:36 +00:00
Jonathan Wakely 00df7ee447 libstdc++: Avoid including <algorithm> in <filesystem> [PR92546]
This only affects Windows, but reduces the preprocessed size of
<filesystem> significantly.

libstdc++-v3/ChangeLog:

	PR libstdc++/92546
	* include/bits/fs_path.h (path::make_preferred): Use
	handwritten loop instead of std::replace.
2022-03-17 17:51:54 +00:00
Jonathan Wakely 38ce448963 libstdc++: Rewrite __moneypunct_cache::_M_cache [PR104966]
GCC thinks the following can lead to a buffer overflow when __ns.size()
equals zero:

	  const basic_string<_CharT>& __ns = __mp.negative_sign();
	  _M_negative_sign_size = __ns.size();
	  __negative_sign = new _CharT[_M_negative_sign_size];
	  __ns.copy(__negative_sign, _M_negative_sign_size);

This happens because operator new might be replaced with something that
writes to this->_M_negative_sign_size and so the basic_string::copy call
could use a non-zero size to write to a zero-length buffer.

The solution suggested by Richi is to cache the size in a local variable
so that the compiler knows it won't be changed between the allocation
and the copy.

This commit goes further and rewrites the whole function to use RAII and
delay all modifications of *this until after all allocations have
succeeded. The RAII helper type caches the size and copies the string
and owns the memory until told to release it.

libstdc++-v3/ChangeLog:

	PR middle-end/104966
	* include/bits/locale_facets_nonio.tcc
	(__moneypunct_cache::_M_cache): Replace try-catch with RAII and
	make all string copies before any stores to *this.
2022-03-17 17:51:53 +00:00
Jonathan Wakely 5a4e208022 libstdc++: Ensure that std::from_chars is declared when supported
This adjusts the declarations in <charconv> to match when the definition
is present. This solves the issue that std::from_chars is present on
Solaris 11.3 (using fast_float) but was not declared in the header
(because the declarations were guarded by _GLIBCXX_HAVE_USELOCALE).

Additionally, do not define __cpp_lib_to_chars unless both from_chars
and to_chars are supported (which is only true for IEEE float and
double). We might still provide from_chars (via strtold) but if to_chars
isn't provided, we shouldn't define the feature test macro.

Finally, this simplifies some of the preprocessor checks in the bodies
of std::from_chars in src/c++17/floating_from_chars.cc and hoists the
repeated code for the strtod version into a new function template.

N.B. the long double overload of std::from_chars will always be defined
if the float and double overloads are defined. We can always use one of
strtold or fast_float's binary64 routines (although the latter might
produce errors for some long double values if they are not representable
as binary64).

libstdc++-v3/ChangeLog:

	* include/std/charconv (__cpp_lib_to_chars): Only define when
	both from_chars and to_chars are supported for floating-point
	types.
	(from_chars, to_chars): Adjust preprocessor conditions guarding
	declarations.
	* include/std/version (__cpp_lib_to_chars): Adjust condition to
	match <charconv> definition.
	* src/c++17/floating_from_chars.cc (from_chars_strtod): New
	function template.
	(from_chars): Simplify preprocessor checks and use
	from_chars_strtod when appropriate.
2022-03-16 16:06:29 +00:00
Detlef Vollmann b5417a0ba7 libstdc++: Move closing brace outside #endif [PR104866]
libstdc++-v3/ChangeLog:

	PR libstdc++/104866
	* include/bits/this_thread_sleep.h: Fix order of #endif and
	closing brace of namespace.
2022-03-10 21:04:01 +00:00