Commit graph

7977 commits

Author SHA1 Message Date
Jonathan Wakely
33ed110858 libstdc++: Include std::ranges::subrange definition in <tuple> [PR102301]
In order for std::make_from_tuple to work with tuple-like types, the
overloads of std::get for those types must have been declared before the
definition of std::make_from_tuple. That means we need to include the
definition of std::ranges::subrange in <tuple>.

The definitions of std::pair and its overloads of std::get are already
included in <tuple>. We provide forward declarations of std::array and
its std::get overloads in <tuple>. We could just declare subrange
without defining it, and give ranges::get a non-deduced return type,
like so:

  namespace ranges
  {
    enum class subrange_kind : bool { unsized, sized};

    template<input_or_output_iterator I, sentinel_for<I> S,
	     subrange_kind K>
      requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
      class subrange;

    template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
      requires (_Num < 2)
      constexpr __conditional_t<_Num == 0, _It, _Sent>
      get(const subrange<_It, _Sent, _Kind>& __r);

    template<size_t _Num, class _It, class _Sent, subrange_kind _Kind>
      requires (_Num < 2)
      constexpr __conditional_t<_Num == 0, _It, _Sent>
      get(subrange<_It, _Sent, _Kind>&& __r)
  }
  using ranges::get;

It is a bit late in the GCC 13 dev cycle to do this, so just include the
right headers for now.

Also add the dangling check to std::make_from_tuple added by P2255.

libstdc++-v3/ChangeLog:

	PR libstdc++/102301
	* include/bits/ranges_base.h: Include <bits/stl_iterator.h> for
	std::make_reverse_iterator.
	* include/std/tuple: Include <bits/ranges_util.h> for subrange.
	(make_from_tuple): Add static assertion from P2255 to diagnose
	dangling references.
	* testsuite/20_util/tuple/make_from_tuple/dangling_ref.cc: New test.
	* testsuite/20_util/tuple/make_from_tuple/tuple_like.cc: New test.
2023-01-24 23:46:42 +00:00
François Dumont
c3c6c30779 libstdc++: [_GLIBCXX_DEBUG] Remove useless constructor checks
Creating a safe iterator from a normal iterator is done within the library where we
already know that it is done correctly. The rare situation where a user would use safe
iterators for his own purpose is non-Standard code so outside _GLIBCXX_DEBUG scope. For
those reasons the __msg_init_singular is useless and can be removed.

Additionally in the copy constructor used for post-increment and post-decrement operators
the __msg_init_copy_singular check can also be ommitted because of the preliminary
__msg_bad_incr and __msg_bad_decr checks.

libstdc++-v3/ChangeLog:

	* include/debug/safe_iterator.h (_Safe_iterator<>::_Unchecked): New.
	(_Safe_iterator(const _Safe_iterator&, _Unchecked)): New.
	(_Safe_iterator::operator++(int)): Use latter.
	(_Safe_iterator::operator--(int)): Likewise.
	(_Safe_iterator(_Iterator, const _Safe_sequence_base*)): Remove !_M_insular()
	check.
	* include/debug/safe_local_iterator.h (_Safe_local_iterator<>::_Unchecked):
	New.
	(_Safe_local_iterator(const _Safe_local_iterator&, _Unchecked)): New.
	(_Safe_local_iterator::operator++(int)): Use latter.
	* src/c++11/debug.cc (_S_debug_messages): Add as comment the _Debug_msg_id
	entry associated to the array entry.
2023-01-23 19:11:54 +01:00
Jonathan Wakely
33237e93d5 libstdc++: Deprecate std::filesystem::u8path for C++20
P0482R6 deprecated these functions for C++20. There was a ballot comment
on the C++23 CD saying to un-deprecate it, but LEWG just rejected that,
so let's add attributes to deprecate them.

libstdc++-v3/ChangeLog:

	* include/bits/fs_path.h (u8path): Add deprecated attribute.
	* testsuite/27_io/filesystem/path/construct/90281.cc: Add
	-Wno-deprecated-declarations for C++20 and later.
	* testsuite/27_io/filesystem/path/factory/u8path-char8_t.cc:
	Likewise.
	* testsuite/27_io/filesystem/path/factory/u8path.cc: Likewise.
	* testsuite/27_io/filesystem/path/native/string.cc: Likewise.
	* testsuite/27_io/filesystem/path/factory/u8path-depr.cc: New test.
2023-01-18 21:47:58 +00:00
Jonathan Wakely
26c68b8c31 libstdc++: Fix std::random_device::entropy() for non-posix targets
Since the r12-4515-g58f339fc5eaae7 change std::random_device::entropy()
returns non-zero for hardware sources such as RDRAND. However, the call
to the underlying _M_getentropy function is conditionally compiled
according to #if _GLIBCXX_USE_DEV_RANDOM which means it only happens for
targets that support /dev/random and /dev/urandom. This means entropy()
always returns zero for x86 Windows, even though the RDRAND and RDSEED
sources work there.

The _M_getentropy() function is always compiled into the library, it
just doesn't get called for targets without /dev/random. We can change
that just by removing the #if conditional. This is not an ABI change,
because new code will just start calling the existing _M_getentropy
function, old code that has inlined entropy() will not call it.

Similarly, the std::random_device destructor doesn't call the underlying
_M_fini function unless _GLIBCXX_USE_DEV_RANDOM is defined. That's less
of a problem because it's still true that the only resources that need
to be freed are when one of /dev/random or /dev/urandom has been opened
for reading, which is only possible when _GLIBCXX_USE_DEV_RANDOM is
defined. The _M_fini function does also destroy a random engine object
if a std::linear_congruential_engine object is used, but that destructor
is trivial and so no resources are leaked if it's not called. Remove the
preprocessor condition in the destructor too, so that we always call the
_M_fini function even if it doesn't have side effects. This makes the
destructor non-trivial for Windows and bare metal targets, but as the
class is non-copyable that shouldn't cause any ABI change in practice.

libstdc++-v3/ChangeLog:

	* include/bits/random.h (random_device) [!_GLIBCXX_USE_DEV_RANDOM]:
	Always call _M_fini and _M_getentropy.
2023-01-18 21:47:58 +00:00
Jakub Jelinek
83ffe9cde7 Update copyright years. 2023-01-16 11:52:17 +01:00
Jonathan Wakely
fa16c7d899 libstdc++: Fix copyright notice to use usual form [PR108413]
libstdc++-v3/ChangeLog:

	PR libstdc++/108413
	* include/c_compatibility/stdatomic.h: Change copyright line to
	be consistent with other headers contributed under DCO terms.
	* include/std/expected: Add full stop to copyright line.
	* src/c++20/tzdb.cc: Likewise.
2023-01-16 10:31:10 +00:00
François Dumont
5c9833d878 libstdc++: [_GLIBCXX_DEBUG] Complete deadlock fix on safe iterators [PR108288]
Complete fix on all _Safe_iterator post-increment and post-decrement implementations
and on _Safe_local_iterator.

libstdc++-v3/ChangeLog:

	PR libstdc++/108288
	* include/debug/safe_iterator.h (_Safe_iterator<>::operator++(int)): Extend deadlock
	fix to other iterator category.
	(_Safe_iterator<>::operator--(int)): Likewise.
	* include/debug/safe_local_iterator.h (_Safe_local_iterator<>::operator++(int)):
	Fix deadlock.
	* testsuite/util/debug/unordered_checks.h (invalid_local_iterator_pre_increment): New.
	(invalid_local_iterator_post_increment): New.
	* testsuite/23_containers/unordered_map/debug/invalid_local_iterator_post_increment_neg.cc:
	New test.
	* testsuite/23_containers/unordered_map/debug/invalid_local_iterator_pre_increment_neg.cc:
	New test.
2023-01-15 17:05:00 +01:00
Björn Schäpers
b1c839be83 libstdc++: Deliver names of C functions in <stacktrace>
__cxa_demangle is only to demangle C++ names, for all C functions,
extern "C" functions, and including main it returns -2, in that case
just adapt the given name. Otherwise it's kept empty, which doesn't look
nice in the stacktrace.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (stacktrace_entry::_S_demangle): Use
	raw __name if __cxa_demangle could not demangle it.

Signed-off-by: Björn Schäpers <bjoern@hazardy.de>
2023-01-14 20:49:45 +00:00
Jonathan Wakely
9afc914809 libstdc++: Fix ostream insertion operators for calendar types
libstdc++-v3/ChangeLog:

	* include/bits/chrono_io.h (operator<<): Fix syntax errors.
	* testsuite/std/time/month_day/io.cc: New test.
	* testsuite/std/time/month_day_last/io.cc: New test.
	* testsuite/std/time/month_weekday/io.cc: New test.
	* testsuite/std/time/month_weekday_last/io.cc: New test.
	* testsuite/std/time/weekday_indexed/io.cc: New test.
	* testsuite/std/time/weekday_last/io.cc: New test.
	* testsuite/std/time/year_month/io.cc: New test.
	* testsuite/std/time/year_month_day_last/io.cc: New test.
	* testsuite/std/time/year_month_weekday/io.cc: New test.
	* testsuite/std/time/year_month_weekday_last/io.cc: New test.
2023-01-14 16:59:58 +00:00
François Dumont
e176c031f0 libstdc++: [_GLIBCXX_INLINE_VERSION] Add to_chars/from_chars symbols export
libstdc++-v3/ChangeLog

	* include/std/format [_GLIBCXX_INLINE_VERSION](to_chars): Adapt __asm symbol
	specifications.
	* config/abi/pre/gnu-versioned-namespace.ver: Add to_chars/from_chars symbols
	export.
2023-01-14 14:24:35 +01:00
Jonathan Wakely
b4b1c6aa6d libstdc++: Add <errno.h> to <bits/std_mutex.h>
This needs to be included explicitly now that we don't include all of
<system_error> here.

libstdc++-v3/ChangeLog:

	* include/bits/std_mutex.h: Include <errno.h>.
2023-01-13 17:37:37 +00:00
Arsen Arsenović
42d3f74323 libstdc++: Enable string_view in freestanding
This enables the default contract handler in freestanding environments,
and, of course, provides freestanding users with string_view.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Install bits/char_traits.h,
	std/string_view
	* include/Makefile.in: Regenerate.
	* include/bits/char_traits.h: Gate hosted-only, wchar-only and
	mbstate-only bits behind appropriate #ifs.
	* include/std/string_view: Gate <iostream> functionality behind
	HOSTED.
	* include/std/version: Enable __cpp_lib_constexpr_string_view
	and __cpp_lib_starts_ends_with in !HOSTED.
	* include/std/ranges: Re-enable __is_basic_string_view on
	freestanding, include <string_view> directly.
	* include/precompiled/stdc++.h: Include <string_view> when
	!HOSTED too.
	* testsuite/20_util/function_objects/searchers.cc: Skip testing
	boyer_moore searchers on freestanding
	* testsuite/21_strings/basic_string_view/capacity/1.cc: Guard
	<string>-related tests behind __STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/cons/char/1.cc: Ditto.
	* testsuite/21_strings/basic_string_view/cons/char/2.cc: Remove
	unused <stdexcept> include.
	* testsuite/21_strings/basic_string_view/cons/char/3.cc: Remove
	unused <vector> include.
	* testsuite/21_strings/basic_string_view/cons/char/range.cc:
	Guard <string> related testing behind __STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/cons/wchar_t/1.cc:
	Guard <stdexcept> related tests behind __STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/element_access/char/1.cc:
	Ditto.
	* testsuite/21_strings/basic_string_view/element_access/wchar_t/1.cc:
	Guard <stdexcept> tests behind __STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/operations/contains/char/2.cc:
	Enable test on freestanding, guard <stdexcept> bits behind
	__STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/operations/substr/char.cc:
	Guard <stdexcept> bits behind __STDC_HOSTED__.
	* testsuite/21_strings/basic_string_view/operations/substr/wchar_t.cc:
	Ditto.
2023-01-13 13:34:21 +00:00
Jonathan Wakely
227351345d libstdc++: Do not include <system_error> in concurrency headers
The <condition_variable>, <mutex>, and <shared_mutex> headers use
std::errc constants, but don't use std::system_error itself. They only
use the __throw_system_error(int) function, which is defined in
<bits/functexcept.h>.

By including the header for the errc constants instead of the whole of
<system_error> we avoid depending on the whole std::string definition.

libstdc++-v3/ChangeLog:

	* include/bits/std_mutex.h: Remove <system_error> include.
	* include/std/condition_variable: Add <bits/error_constants.h>
	include.
	* include/std/mutex: Likewise.
	* include/std/shared_mutex: Likewise.
2023-01-13 00:15:01 +00:00
Jonathan Wakely
f629f63d2d libstdc++: Extend max_align_t special case to 64-bit HP-UX [PR77691]
GCC's std::max_align_t doesn't agree with the system malloc on HP-UX, so
generalize the current hack for Solaris to apply to that target too.

libstdc++-v3/ChangeLog:

	PR libstdc++/77691
	* include/experimental/memory_resource
	(_GLIBCXX_MAX_ALIGN_MATCHES_MALLOC): Define.
	(do_allocate, do_deallocate): Check it.
	* testsuite/experimental/memory_resource/new_delete_resource.cc:
	Relax expected behaviour for 64-bit hppa-hp-hpux11.11.
2023-01-12 20:57:34 +00:00
François Dumont
b3c9148cad libstdc++: Remove _Alloc_node instance in _Rb_tree [PR107189]
libstdc++-v3/ChangeLog:

	PR libstdc++/107189
	* include/bits/stl_tree.h (_Rb_tree<>::_M_insert_range_equal): Remove
	unused _Alloc_node instance.
2023-01-12 19:39:29 +01:00
Jonathan Wakely
dc22cf4612 libstdc++: Use lock-free type for __platform_wait_t
For non-futex targets the __platform_wait_t type is currently uint64_t,
but that requires a lock in libatomic for some 32-bit targets. We don't
really need a 64-bit type, so use unsigned long if that is lock-free,
and int otherwise. This should mean it's lock-free on a wider set of
targets.

libstdc++-v3/ChangeLog:

	* include/bits/atomic_wait.h (__detail::__platform_wait_t):
	Define as unsigned long if always lock-free, and unsigned int
	otherwise.
2023-01-12 11:01:09 +00:00
Jonathan Wakely
769fae76df libstdc++: Fix some algos for 16-bit size_t [PR108221]
Some standard algorithms fail to compile when size_t or ptrdiff_t is
narrower than int. The __lg helper function is ambiguous if ptrdiff_t is
short or __int20, so replace it with a function template that works for
those types as well as signed/unsigned int/long/long long. The helpers
for stable_sort perform arithmetic on size values and assume the types
won't change, which isn't true if the type promotes to int.

libstdc++-v3/ChangeLog:

	PR libstdc++/108221
	* include/bits/stl_algobase.h (__lg): Replace six overloads with
	a single function template for all integer types.
	* include/bits/stl_algo.h (__merge_adaptive_resize): Cast
	arithmetic results back to _Distance.
2023-01-10 10:09:21 +00:00
Jonathan Wakely
b39f4333d1 libstdc++: Fix std::span constraint for sizeof(size_t) < sizeof(int) [PR108221]
The default constructor has a constraint that is always false if
arithmetic on size_t values promotes to int. Rewrite the constraint
exactly as written in the standard, which works correctly.

libstdc++-v3/ChangeLog:

	PR libstdc++/108221
	* include/std/span (span::span()): Un-simplify constraint to
	work for size_t of lesser rank than int.
2023-01-10 10:09:21 +00:00
Patrick Palka
f7bd48c6bb libstdc++: Add feature-test macros for implemented C++23 views [PR108260]
PR libstdc++/108260

libstdc++-v3/ChangeLog:

	* include/bits/utility.h (__cpp_lib_ranges_zip): Define for C++23.
	* include/std/ranges (__cpp_lib_ranges_zip): Likewise.
	(__cpp_lib_ranges_chunk): Likewise.
	(__cpp_lib_ranges_slide): Likewise.
	(__cpp_lib_ranges_chunk_by): Likewise.
	(__cpp_lib_ranges_join_with): Likewise.
	(__cpp_lib_ranges_repeat): Likewise.
	(__cpp_lib_ranges_stride): Likewise.
	(__cpp_lib_ranges_cartesian_product): Likewise.
	(__cpp_lib_ranges_as_rvalue): Likewise.
	* include/std/version: Ditto.
	* testsuite/20_util/tuple/p2321r2.cc: Verify value of
	feature-test macro.
	* testsuite/std/ranges/adaptors/as_rvalue/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/chunk/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/chunk_by/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/join_with/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/slide/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/stride/1.cc: Likewise.
	* testsuite/std/ranges/cartesian_product/1.cc: Likewise.
	* testsuite/std/ranges/repeat/1.cc: Likewise.
	* testsuite/std/ranges/zip/1.cc: Likewise.
	* testsuite/std/ranges/version_c++23.cc: New test.
2023-01-06 10:32:10 -05:00
Jonathan Wakely
553332c19a libstdc++: Fix misuse of alloca in std::bitset [PR108214]
The use of alloca in a constructor is wrong, because the memory is gone
after the constructor returns, and will be overwritten by a subsequent
function call. This didn't show up in testing because function inlining
alters the stack usage.

libstdc++-v3/ChangeLog:

	PR libstdc++/108214
	* include/std/bitset (operator>>): Use alloca in the right
	scope, not in a constructor.
	* testsuite/20_util/bitset/io/input.cc: Check case from PR.
2023-01-06 13:50:36 +00:00
Jonathan Wakely
8c330fd494 libstdc++: Disable broken std::format for floating-point types [PR108221]
If we don't have std::to_chars for floating-point types (either because
float and double are not IEEE format, or size_t is 16-bit) then we can't
use them with std::format. This causes a bootstrap failure since
std/c++20/tzdb.cc was added to the library, because <chrono> now
includes <format>.

This change just disables formatting support for those types. This is
not a proper fix, but solves the bootstrap failure for now.

libstdc++-v3/ChangeLog:

	PR libstdc++/108221
	* include/std/format (basic_format_arg) [!__cpp_lib_to_chars]:
	Disable visiting floating-point types.
2023-01-06 13:24:57 +00:00
Jonathan Wakely
b9479ddc7a libstdc++: Fix deadlock in debug iterator increment [PR108288]
With -fno-elide-constructors the debug iterator post-increment and
post-decrement operators are susceptible to deadlock. They take a mutex
lock and then return a temporary, which also attempts to take a lock to
attach itself to the sequence. If the return value and *this happen to
collide and use the same mutex from the pool, then you get a deadlock
trying to lock a mutex that is already held by the current thread.

The solution is to construct the return value before taking the lock.
The copy constructor and pre-inc/pre-dec operators already manage locks
correctly, without deadlock, so just implement post-inc/post-dec in the
conventional way, taking a copy then modifying *this, then returning the
copy.

libstdc++-v3/ChangeLog:

	PR libstdc++/108288
	* include/debug/safe_iterator.h (_Safe_iterator::operator++(int))
	(_Safe_iterator::operator--(int)): Do not hold lock around
	construction of return value.
2023-01-06 11:52:01 +00:00
Jonathan Wakely
e2eab3c4ed libstdc++: Reduce size of std::bind_front(empty_type) result [PR108290]
libstdc++-v3/ChangeLog:

	PR libstdc++/108290
	* include/std/functional (_Bind_front): Add no_unique_address
	attribute to data members.
	* testsuite/20_util/function_objects/bind_front/107784.cc: Check
	size of call wrappers with empty types for targets and bound
	arguments.
2023-01-05 16:28:43 +00:00
Jonathan Wakely
e36e57b032 libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265]
libstdc++-v3/ChangeLog:

	PR libstdc++/108265
	* include/std/chrono (hh_mm_ss): Do not use chrono::abs if
	duration rep is unsigned.
	* testsuite/std/time/hh_mm_ss/1.cc: Check unsigned rep.
2023-01-05 00:46:00 +00:00
Jonathan Wakely
1530a9b1f4 libstdc++: Fix std::array<T, 0>::data() to be a constant expression [PR108258]
When I refactored the __array_traits helper I broke this.

libstdc++-v3/ChangeLog:

	PR libstdc++/108258
	* include/std/array (__array_traits<T, 0>::operator T*()): Add
	constexpr.
	* testsuite/23_containers/array/element_access/constexpr_c++17.cc: Check
	std::array<T, 0>::data().
2023-01-04 11:53:49 +00:00
Jonathan Wakely
f99b94865f libstdc++: Add std::format support to <chrono>
This adds the operator<< overloads and std::formatter specializations
required by C++20 so that <chrono> types can be written to ostreams and
printed with std::format.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/std/chrono (operator<<): Move to new header.
	(nonexistent_local_time::_M_make_what_str): Define correctly.
	(ambiguous_local_time::_M_make_what_str): Likewise.
	* include/bits/chrono_io.h: New file.
	* src/c++20/tzdb.cc (operator<<(ostream&, const Rule&)): Use
	new ostream output for month and weekday types.
	* testsuite/20_util/duration/io.cc: Test std::format support.
	* testsuite/std/time/exceptions.cc: Check what() strings.
	* testsuite/std/time/syn_c++20.cc: Uncomment local_time_format.
	* testsuite/std/time/time_zone/get_info_local.cc: Enable check
	for formatted output of local_info objects.
	* testsuite/std/time/clock/file/io.cc: New test.
	* testsuite/std/time/clock/gps/io.cc: New test.
	* testsuite/std/time/clock/system/io.cc: New test.
	* testsuite/std/time/clock/tai/io.cc: New test.
	* testsuite/std/time/clock/utc/io.cc: New test.
	* testsuite/std/time/day/io.cc: New test.
	* testsuite/std/time/format.cc: New test.
	* testsuite/std/time/hh_mm_ss/io.cc: New test.
	* testsuite/std/time/month/io.cc: New test.
	* testsuite/std/time/weekday/io.cc: New test.
	* testsuite/std/time/year/io.cc: New test.
	* testsuite/std/time/year_month_day/io.cc: New test.
2022-12-22 23:34:27 +00:00
Jonathan Wakely
9247402a29 libstdc++: Add helper function in <format>
Add a new __format::__write_padded_as_spec helper to remove duplicated
code in formatter specializations.

libstdc++-v3/ChangeLog:

	* include/std/format (__format::__write_padded_as_spec): New
	function.
	(__format::__formatter_str, __format::__formatter_int::format)
	(formatter<const void*, charT>): Use it.
2022-12-22 23:34:26 +00:00
Jonathan Wakely
9fc61d45fa libstdc++: Implement C++20 time zone support in <chrono>
This is the largest missing piece of C++20 support. Only the cxx11 ABI
is supported, due to the use of std::string in the API for time zones.
For the old gcc4 ABI, utc_clock and leap seconds are supported, but only
using a hardcoded list of leap seconds, no up-to-date tzdb::leap_seconds
information is available, and no time zones or zoned_time conversions.

The implementation currently depends on a tzdata.zi file being provided
by the OS or the user. The expected location is /usr/share/zoneinfo but
that can be changed using --with-libstdcxx-zoneinfo-dir=PATH. On targets
that support it there is also a weak symbol that users can override in
their own program (which also helps with testing):

extern "C++" const char* __gnu_cxx::zoneinfo_dir_override();

If no file is found, a fallback tzdb object will be created which only
contains the "Etc/UTC" and "Etc/GMT" time zones.

A leapseconds file is also expected in the same directory, but if that
isn't present then a hardcoded list of leapseconds is used, which is
correct at least as far as 2023-06-28 (and it currently looks like no
leap second will be inserted for a few years).

The tzdata.zi and leapseconds files from https://www.iana.org/time-zones
are in the public domain, so shipping copies of them with GCC would be
an option. However, the tzdata.zi file will rapidly become outdated, so
users should really provide it themselves (or convince their OS vendor
to do so). It would also be possible to implement an alternative parser
for the compiled tzdata files (one per time zone) under
/usr/share/zoneinfo. Those files are present on more operating systems,
but do not contain all the information present in tzdata.zi.
Specifically, the "links" are not present, so that e.g. "UTC" and
"Universal" are distinct time zones, rather than both being links to the
canonical "Etc/UTC" zone. For some platforms those files are hard links
to the same file, but there's no indication which zone is the canonical
name and which is a link. Other platforms just store them in different
inodes anyway. I do not plan to add such an alternative parser for the
compiled files. That would need to be contributed by maintainers or
users of targets that require it, if making tzdata.zi available is not
an option. The library ABI would not need to change for a new tzdb
implementation, because everything in tzdb_list, tzdb and time_zone is
implemented as a pimpl (except for the shared_ptr links between nodes,
described below). That means the new exported symbols added by this
commit should be stable even if the implementation is completely
rewritten.

The information from tzdata.zi is parsed and stored in data structures
that closely model the info in the file. This is a space-efficient
representation that uses less memory that storing every transition for
every time zone.  It also avoids spending time expanding that
information into time zone transitions that might never be needed by the
program.  When a conversion to/from a local time to UTC is requested the
information will be processed to determine the time zone transitions
close to the time being converted.

There is a bug in some time zone transitions. When generating a sys_info
object immediately after one that was previously generated, we need to
find the previous rule that was in effect and note its offset and
letters. This is so that the start time and abbreviation of the new
sys_info will be correct. This only affects time zones that use a format
like "C%sT" where the LETTERS replacing %s are non-empty for standard
time, e.g. "Asia/Shanghai" which uses "CST" for standard time and "CDT"
for daylight time.

The tzdb_list structure maintains a linked list of tzdb nodes using
shared_ptr links. This allows the iterators into the list to share
ownership with the list itself. This offers a non-portable solution to a
lifetime issue in the API. Because tzdb objects can be erased from the
list using tzdb_list::erase_after, separate modules/libraries in a large
program cannot guarantee that any const tzdb& or const time_zone*
remains valid indefinitely. Holding onto a tzdb_list::const_iterator
will extend the tzdb object's lifetime, even if it's erased from the
list. An alternative design would be for the list iterator to hold a
weak_ptr. This would allow users to test whether the tzdb still exists
when the iterator is dereferenced, which is better than just having a
dangling raw pointer. That doesn't actually extend the tzdb's lifetime
though, and every use of it would need to be preceded by checking the
weak_ptr. Using shared_ptr adds a little bit of overhead but allows
users to solve the lifetime issue if they rely on the libstdc++-specific
iterator property.

libstdc++-v3/ChangeLog:

	* acinclude.m4 (GLIBCXX_ZONEINFO_DIR): New macro.
	* config.h.in: Regenerate.
	* config/abi/pre/gnu.ver: Export new symbols.
	* configure: Regenerate.
	* configure.ac (GLIBCXX_ZONEINFO_DIR): Use new macro.
	* include/std/chrono (utc_clock::from_sys): Correct handling
	of leap seconds.
	(nonexistent_local_time::_M_make_what_str): Define.
	(ambiguous_local_time::_M_make_what_str): Define.
	(__throw_bad_local_time): Define new function.
	(time_zone, tzdb_list, tzdb): Implement all members.
	(remote_version, zoned_time, get_leap_second_info): Define.
	* include/std/version: Add comment for __cpp_lib_chrono.
	* src/c++20/Makefile.am: Add new file.
	* src/c++20/Makefile.in: Regenerate.
	* src/c++20/tzdb.cc: New file.
	* testsuite/lib/libstdc++.exp: Define effective target tzdb.
	* testsuite/std/time/clock/file/members.cc: Check file_time
	alias and file_clock::now() member.
	* testsuite/std/time/clock/gps/1.cc: Likewise for gps_clock.
	* testsuite/std/time/clock/tai/1.cc: Likewise for tai_clock.
	* testsuite/std/time/syn_c++20.cc: Uncomment everything except
	parse.
	* testsuite/std/time/clock/utc/leap_second_info.cc: New test.
	* testsuite/std/time/exceptions.cc: New test.
	* testsuite/std/time/time_zone/get_info_local.cc: New test.
	* testsuite/std/time/time_zone/get_info_sys.cc: New test.
	* testsuite/std/time/time_zone/requirements.cc: New test.
	* testsuite/std/time/tzdb/1.cc: New test.
	* testsuite/std/time/tzdb/leap_seconds.cc: New test.
	* testsuite/std/time/tzdb_list/1.cc: New test.
	* testsuite/std/time/tzdb_list/requirements.cc: New test.
	* testsuite/std/time/zoned_time/1.cc: New test.
	* testsuite/std/time/zoned_time/custom.cc: New test.
	* testsuite/std/time/zoned_time/deduction.cc: New test.
	* testsuite/std/time/zoned_time/req_neg.cc: New test.
	* testsuite/std/time/zoned_time/requirements.cc: New test.
	* testsuite/std/time/zoned_traits.cc: New test.
2022-12-22 23:34:20 +00:00
Jonathan Wakely
d2d3826cd4 libstdc++: Define and use variable templates in <chrono>
Thi defines a variable template for the internal __is_duration helper
trait, defines a new __is_time_point_v variable template (to be used in
a subsequent commit), and adds explicit specializations of the standard
chrono::treat_as_floating_point trait for common types.

A fast path is added to chrono::duration_cast for the no-op case where
no conversion is needed.

Finally, some SFINAE constraints are simplified by using the
__enable_if_t alias, or by using variable templates.

libstdc++-v3/ChangeLog:

	* include/bits/chrono.h (__is_duration_v, __is_time_point_v):
	New variable templates.
	(duration_cast): Add simplified definition for noconv case.
	(treat_as_floating_point_v): Add explicit specializations.
	(duration::operator%=, floor, ceil, round): Simplify SFINAE
	constraints.
2022-12-22 10:14:52 +00:00
Jonathan Wakely
ec8f914f57 libstdc++: Add [[nodiscard]] in <chrono>
libstdc++-v3/ChangeLog:

	* include/std/chrono: Use nodiscard attribute.
2022-12-22 10:14:52 +00:00
Arsen Arsenović
a39f454f0f contracts: Lowercase {MAYBE,NEVER}_CONTINUE
The lowercase constants are more consistent with the standard, and it is
unlikely that the uppercase versions would've been accepted.

gcc/cp/ChangeLog:

	* contracts.cc: Rename references to
	contract_violation_continuation_mode constants to be lowercase.

libstdc++-v3/ChangeLog:

	* include/experimental/contract: Lowercase the constants in
	contract_violation_continuation_mode.
2022-12-19 15:08:08 -05:00
Jonathan Wakely
8d9e2776a6 libstdc++: Add monadic operations to std::expected for C++23 (P2505R5)
This was approved for C++23 last month in Kona.

libstdc++-v3/ChangeLog:

	* include/std/expected (expected): Add monadic operations.
	(expected<void, E>): Likewise.
	* include/std/version (__cpp_lib_expected): Bump value.
	* testsuite/20_util/expected/synopsis.cc: Adjust expected macro
	value.
	* testsuite/20_util/expected/version.cc: Likewise.
	* testsuite/20_util/expected/illformed_neg.cc: Prune additional
	errors from ill-formed monadic operations.
	* testsuite/20_util/expected/observers.cc: Check error_or.
	* testsuite/20_util/expected/monadic.cc: New test.
2022-12-16 20:59:19 +00:00
Jonathan Wakely
59822c3920 libstdc++: Fixes for std::expected
This fixes some bugs in the swap functions for std::expected.

It also disables the noexcept-specifiers for equality operators, because
those are problematic when querying whether a std::expected is equality
comparable. The operator==(const expected<T,E>&, const U&) function is
not constrained, so is viable for comparing expected<T,E> with
expected<void,G>, but then we get an error from the noexcept-specifier.

libstdc++-v3/ChangeLog:

	* include/std/expected (expected::_M_swap_val_unex): Guard the
	correct object.
	(expected::swap): Move is_swappable
	requirement from static_assert to constraint.
	(swap): Likewise.
	(operator==): Remove noexcept-specifier.
	* testsuite/20_util/expected/swap.cc: Check swapping of
	types without non-throwing move constructor. Check constraints
	on swap.
	* testsuite/20_util/expected/unexpected.cc: Check constraints on
	swap.
	* testsuite/20_util/expected/equality.cc: New test.
2022-12-16 20:58:09 +00:00
Jonathan Wakely
64c986b495 libstdc++: Diagnose broken allocator rebind members
This adds a static assertion to std::allocator_traits::rebind_alloc to
diagnose violations of the rule that rebinding an allocator to its own
value type yields the same allocator type.

This helps to catch the easy mistake of deriving from std::allocator but
forgetting to override the rebind behaviour (no longer an issue in C++20
as std::allocator doesn't have a rebind member that can be inherited).
It also catches bugs like in 23_containers/vector/52591.cc where a typo
means the rebound allocator is a completely different type.

I initially wanted to put this static assert into the body of
allocator_traits:

      static_assert(is_same<rebind_alloc<value_type>, _Alloc>::value,
		    "rebind_alloc<value_type> must be Alloc");

However, this causes a regression in the test for PR libstdc++/72792.
It seems that instantiating std::allocator_traits should be allowed for
invalid allocator types as long as you don't try to rebind them. To
support that, only assert in the __allocator_traits_base::__rebind class
template (in both the primary template and the partial specialization).
As a result, the bug in 20_util/scoped_allocator/outermost.cc is not
diagnosed, because nothing in that test rebinds the allocator.

libstdc++-v3/ChangeLog:

	* include/bits/alloc_traits.h (__allocator_traits_base::__rebind):
	Add static assert for rebind requirement.
	* testsuite/20_util/allocator_traits/members/rebind_alloc.cc:
	Fix invalid rebind member in test allocator.
	* testsuite/20_util/allocator_traits/requirements/rebind_neg.cc:
	New test.
	* testsuite/20_util/scoped_allocator/outermost.cc: Add rebind to
	test allocator.
	* testsuite/23_containers/forward_list/48101_neg.cc: Prune new
	static assert error.
	* testsuite/23_containers/unordered_multiset/48101_neg.cc:
	Likewise.
	* testsuite/23_containers/unordered_set/48101_neg.cc:
	Likewise.
	* testsuite/23_containers/vector/52591.cc: Fix typo in rebind.
2022-12-16 20:58:09 +00:00
Jonathan Wakely
92eb0adc14 libstdc++: Fix self-move for std::weak_ptr [PR108118]
I think an alternative fix would be something like:

  _M_ptr = std::exchange(rhs._M_ptr, nullptr);
  _M_refcount = std::move(rhs._M_refcount);

The standard's move-and-swap implementation generates smaller code at
all levels except -O0 and -Og, so it seems simplest to just do what the
standard says.

libstdc++-v3/ChangeLog:

	PR libstdc++/108118
	* include/bits/shared_ptr_base.h (weak_ptr::operator=):
	Implement as move-and-swap exactly as specified in the standard.
	* testsuite/20_util/weak_ptr/cons/self_move.cc: New test.
2022-12-16 12:59:08 +00:00
Jonathan Wakely
881c6cabce libstdc++: Fix size passed to operator delete [PR108097]
The number of elements gets stored in _M_capacity so use a separate
variable for the number of bytes to allocate.

libstdc++-v3/ChangeLog:

	PR libstdc++/108097
	* include/std/stacktrace (basic_stracktrace::_Impl): Do not
	multiply N by sizeof(value_type) when allocating.
2022-12-14 14:11:13 +00:00
Jonathan Wakely
6c0f958401 libstdc++: Fix constraint on std::basic_format_string [PR108024]
Also remove some redundant std::move calls for return statements.

libstdc++-v3/ChangeLog:

	PR libstdc++/108024
	* include/std/format (basic_format_string): Fix constraint.
	* testsuite/std/format/format_string.cc: New test.
2022-12-12 14:00:09 +00:00
Jonathan Wakely
cb363fd9f1 libstdc++: Change names that clash with Win32 or Clang
Clang now defines an __is_unsigned built-in, and Windows defines an
_Out_ macro. Replace uses of those as identifiers.

There might also be a problem with __is_signed, which we use in several
places.

libstdc++-v3/ChangeLog:

	* include/std/chrono (hh_mm_ss): Rename __is_unsigned member to
	_S_is_unsigned.
	* include/std/format (basic_format_context): Rename _Out_
	template parameter to _Out2.
	* testsuite/17_intro/names.cc: Add Windows SAL annotation
	macros.
2022-12-12 14:00:09 +00:00
Jonathan Wakely
320ac807da libstdc++: Define atomic lock-free type aliases for C++20 [PR98034]
libstdc++-v3/ChangeLog:

	PR libstdc++/98034
	* include/std/atomic (__cpp_lib_atomic_lock_free_type_aliases):
	Define macro.
	(atomic_signed_lock_free, atomic_unsigned_lock_free): Define
	aliases.
	* include/std/version (__cpp_lib_atomic_lock_free_type_aliases):
	Define macro.
	* testsuite/29_atomics/atomic/lock_free_aliases.cc: New test.
2022-12-12 14:00:09 +00:00
Jonathan Wakely
2327d93314 libstdc++: Make operator<< for stacktraces less templated (LWG 3515)
This change was approved for C++23 last month.

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (operator<<): Only output to narrow
	ostreams (LWG 3515).
	* testsuite/19_diagnostics/stacktrace/synopsis.cc:
2022-12-12 14:00:08 +00:00
Jonathan Wakely
d61c0357eb libstdc++: Remove digit separators [PR108015]
These are not valid in C++11 and cause a warning when preprocessing,
even though they're inside a skipped group.

chrono:2436: warning: missing terminating ' character

libstdc++-v3/ChangeLog:

	PR libstdc++/108015
	* include/std/chrono (hh_mm_ss): Remove digit separators.
2022-12-09 00:33:58 +00:00
Jonathan Wakely
f76d7943bb libstdc++: Fix some -Wunused warnings in tests
libstdc++-v3/ChangeLog:

	* include/ext/pb_ds/detail/type_utils.hpp (PB_DS_STATIC_ASSERT):
	Add unused attribute to avoid -Wunused-local-typedef warnings.
	* testsuite/17_intro/tag_type_explicit_ctor.cc: Add pragma to
	ignore -Wunused-variable warnings
2022-12-09 00:33:46 +00:00
Jonathan Wakely
646e979c43 libstdc++: Add [[nodiscard]] to chrono conversion functions
Also add doxygen comments.

libstdc++-v3/ChangeLog:

	* include/bits/chrono.h (duration_cast, floor, round, abs, ceil)
	(time_point_cast): Add [[nodiscard]] attribute and doxygen
	comments.
	(treat_as_floating_point): Add doxygen commen.
2022-12-09 00:32:45 +00:00
Jonathan Wakely
7eec3114eb libstdc++: Change class-key for duration and time_point to class
We define these with the 'struct' keyword, but the standard uses
'class'. This results in warnings if users try to refer to them using
elaborated type specifiers.

libstdc++-v3/ChangeLog:

	* include/bits/chrono.h (duration, time_point): Change 'struct'
	to 'class'.
2022-12-09 00:32:44 +00:00
Jonathan Wakely
3ad0f470c1 libstdc++: Pass error handler to libbacktrace functions
Also pass threaded=1 to __glibcxx_backtrace_create_state and remove some
of the namespace scope declarations in the header.

Co-authored-by: François Dumont <frs.dumont@gmail.com>

libstdc++-v3/ChangeLog:

	* include/debug/formatter.h [_GLIBCXX_DEBUG_BACKTRACE]
	(_Error_formatter::_Error_formatter): Pass error handler to
	__glibcxx_backtrace_create_state. Pass 1 for threaded argument.
	(_Error_formatter::_S_err): Define empty function.
	* src/c++11/debug.cc (_Error_formatter::_M_error): Pass error
	handler to __glibcxx_backtrace_full.
2022-12-07 19:54:45 +00:00
Jonathan Wakely
9cce91a63d libstdc++: Add casts for integer-like difference type [PR107871]
libstdc++-v3/ChangeLog:

	PR libstdc++/107871
	* include/std/format (_Iter_sink::_M_overflow): Add cast to
	size_t.
	(_Iter_sink<CharT, contiguous_iterator auto>::_M_make_span): Use
	typedef instead of decltype.
	* testsuite/std/format/functions/107871.cc: New test.
2022-12-06 21:38:46 +00:00
Jonathan Wakely
5329e1a8e1 libstdc++: Make chrono::hh_mm_ss more compact
This uses a single byte for the minutes and seconds members, and places
the bool member next to those single bytes. This means we do not need 40
bytes to store a time that can fit in a single 8-byte integer.

When there is no subsecond precision we can do away with the _M_ss
member altogether. If the subsecond precision is coarse enough, we can
use a smaller representation for _M_ss, e.g. hh_mm_ss<milliseconds> only
needs uint_least32_t for _M_ss, and hh_mm_ss<duration<long, ratio<1,10>>
and hh_mm_ss<duration<int8_t, nano>> only need a single byte. In the
latter case the type can only ever represent up to 255ns anyway, so we
don't need a larger representation type (in such cases, we could even
remove the _M_h, _M_m and _M_s members, but it's a very unlikely
scenario that isn't worth optimizing for).

Except for specializations with a floating-point rep or using higher
precision than nanoseconds, hh_mm_ss should now fit in 16 bytes, or even
12 bytes for x86-32 where alignof(long long) == 4.

libstdc++-v3/ChangeLog:

	* include/std/chrono (chrono::hh_mm_ss): Do not use 64-bit
	representations for all four duration members. Reorder members.
	(hh_mm_ss::hh_mm_ss()): Define as defaulted.
	(hh_mm_ss::hh_mm_ss(Duration)): Delegate to a new private
	constructor, instead of calling chrono::abs repeatedly.
	* testsuite/std/time/hh_mm_ss/1.cc: Check floating-point
	representations. Check default constructor. Check sizes.
2022-12-06 21:38:46 +00:00
Jonathan Wakely
4ba94abf14 libstdc++: Add hint to compiler about vector invariants [PR106434]
The PR shows a bogus warning where jump threading generates code for the
undefined case that the insertion point is a value-initialized iterator
but _M_finish and _M_end_of_storage are unequal (so at least one must be
non-null). Using __builtin_unreachable() removes the bogus warning. Also
add an assertion to diagnose undefined misuses of a null iterator here,
so we don't just silently optimize that undefined code to something
unsafe.

libstdc++-v3/ChangeLog:

	PR c++/106434
	* include/bits/vector.tcc (insert(const_iterator, const T&)):
	Add assertion and optimization hint that the iterator for the
	insertion point must be non-null.
2022-12-06 21:33:29 +00:00
Jonathan Wakely
af177d7280 libstdc++: Add nodiscard attribute to mutex try_lock functions
libstdc++-v3/ChangeLog:

	* include/bits/std_mutex.h (mutex): Add nodiscard attribute to
	try_lock member function.
	* include/bits/unique_lock.h (unique_lock): Likewise for
	try_lock, try_lock_until, try_lock_for member functions, and
	owns_lock and mutex member functions.
	* include/std/mutex (recursive_mutex): Likewise for try_lock
	member function.
	(timed_mutex, recursive_timed_mutex, try_lock): Likewise for
	try_lock, try_lock_until, try_lock_for member functions.
	(try_lock): Likewise for non-member function.
	* include/std/shared_mutex (shared_mutex): Likewise for try_lock
	and try_lock_shared member functions.
	(shared_timed_mutex): Likewise for try_lock, try_lock_for,
	try_lock_shared, try_lock_shared_for, try_lock_until, and
	try_lock_shared_until member functions.
	(shared_lock): Likewise for try_lock, try_lock, try_lock_for,
	try_lock_until, owns_lock, and mutex member functions.
	* testsuite/30_threads/recursive_timed_mutex/try_lock_until/clock_neg.cc:
	Cast discarded value expression to void.
	* testsuite/30_threads/shared_lock/locking/3.cc: Likewise.
	* testsuite/30_threads/shared_lock/locking/4.cc: Likewise.
	* testsuite/30_threads/shared_lock/locking/clock_neg.cc:
	Likewise.
	* testsuite/30_threads/shared_timed_mutex/try_lock_until/clock_neg.cc:
	Likewise.
	* testsuite/30_threads/timed_mutex/try_lock_until/clock_neg.cc:
	Likewise.
	* testsuite/30_threads/try_lock/4.cc: Likewise.
	* testsuite/30_threads/unique_lock/cons/60497.cc: Likewise.
	* testsuite/30_threads/unique_lock/locking/3.cc: Likewise.
	* testsuite/30_threads/unique_lock/locking/clock_neg.cc:
	Likewise.
2022-12-06 21:33:29 +00:00
Jonathan Wakely
48e21e878b libstdc++: The Trouble with Tribbles
Fix digit grouping for integers formatted with "{:#Lx}" which were
including the "0x" prefix in the grouped digits. This resulted in output
like "0,xff,fff" instead of "0xff,fff".

Also change std:::basic_format_parse_context to not throw for an arg-id
that is larger than the actual number of format arguments. I clarified
with Victor Zverovich that this is the intended behaviour for the
run-time format-string checks. An out-of-range arg-id should be
diagnosed at compile-time (as clarified by LWG 3825) but not run-time.
The formatting function will still throw at run-time when args.arg(id)
returns an empty basic_format_arg.

libstdc++-v3/ChangeLog:

	* include/std/format (basic_format_parse_context::next_arg_id):
	Only check arg-id is in range during constant evaluation.
	* testsuite/std/format/functions/format.cc: Check "{:#Lx}".
	* testsuite/std/format/parse_ctx.cc: Adjust expected results for
	format-strings using an out-of-range arg-id.
2022-12-06 21:33:29 +00:00