Commit graph

7951 commits

Author SHA1 Message Date
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
Björn Schäpers
40adb39566 libstdc++: Add error handler for <stacktrace>
Not providing an error handler results in a null pointer dereference
when an error occurs.

Co-authored-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	* include/std/stacktrace (stacktrace_entry::_S_err_handler): New
	static function.
	(stacktrace_entry, basic_stacktrace): Pass &_S_err_handler to
	all calls to libbacktrace.
2022-12-02 00:17:42 +00:00
Jonathan Wakely
cca06f0d6d libstdc++: Avoid bogus warning in std::vector::insert [PR107852]
GCC assumes that any global variable might be modified by operator new,
and so in the testcase for this PR all data members get reloaded after
allocating new storage. By making local copies of the _M_start and
_M_finish members we avoid that, and then the compiler has enough info
to remove the dead branches that trigger bogus -Warray-bounds warnings.

libstdc++-v3/ChangeLog:

	PR libstdc++/107852
	PR libstdc++/106199
	PR libstdc++/100366
	* include/bits/vector.tcc (vector::_M_fill_insert): Copy
	_M_start and _M_finish members before allocating.
	(vector::_M_default_append): Likewise.
	(vector::_M_range_insert): Likewise.
2022-11-29 17:01:32 +00:00
Jonathan Wakely
81cf0354d5 libstdc++: Remove unnecessary tag dispatching in std::vector
There's no need to call a _M_xxx_dispatch function with a
statically-known __false_type tag, we can just directly call the
function that should be dispatched to. This will compile a tiny bit
faster and save a function call with optimization or inlining turned
off.

Also add the always_inline attribute to the __iterator_category helper
used for dispatching on the iterator category.

libstdc++-v3/ChangeLog:

	* include/bits/stl_iterator_base_types.h (__iterator_category):
	Add always_inline attribute.
	* include/bits/stl_vector.h (assign(Iter, Iter)): Call
	_M_assign_aux directly, instead of _M_assign_dispatch.
	(insert(const_iterator, Iter, Iter)): Call _M_range_insert
	directly instead of _M_insert_dispatch.
2022-11-29 16:01:08 +00:00
Jonathan Wakely
0ded30b361 libstdc++: Do not use __used or __packed as identifiers
These names (and __unused) are defined as macros by newlib.

libstdc++-v3/ChangeLog:

	* include/std/format: Rename all variables called __used or
	__packed.
	* testsuite/17_intro/badnames.cc: Add no_pch options.
	* testsuite/17_intro/names.cc: Check __packed, __unused and
	__used.
2022-11-29 15:32:50 +00:00
Jonathan Wakely
a7b97a1f6b libstdc++: Fix std::string_view for I32LP16 targets
For H8/300 with -msx -mn -mint32 the type of (_M_len - __pos) is int,
because int is wider than size_t so the operands are promoted.

libstdc++-v3/ChangeLog:

	* include/std/string_view (basic_string_view::copy) Use explicit
	template argument for call to std::min<size_t>.
	(basic_string_view::substr): Likewise.
2022-11-28 16:57:36 +00:00
Jonathan Wakely
a64775a0ed libstdc++: Make 16-bit std::subtract_with_carry_engine work [PR107466]
This implements the proposed resolution of LWG 3809, so that
std::subtract_with_carry_engine can be used with a 16-bit result_type.
Currently this produces a narrowing error when instantiating the
std::linear_congruential_engine to create the initial state. It also
truncates the default_seed constant when passing it as a result_type
argument.

Change the type of the constant to uint_least32_t and pass 0u when the
default_seed should be used.

libstdc++-v3/ChangeLog:

	PR libstdc++/107466
	* include/bits/random.h (subtract_with_carry_engine): Use 32-bit
	type for default seed. Use 0u as default argument for
	subtract_with_carry_engine(result_type) constructor and
	seed(result_type) member function.
	* include/bits/random.tcc (subtract_with_carry_engine): Use
	32-bit type for default seed and engine used for initial state.
	* testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc:
	New test.
2022-11-28 15:02:04 +00:00
Jonathan Wakely
f54ceb2062 libstdc++: Call predicate with non-const values in std::erase_if [PR107850]
As specified in the standard, the predicate for std::erase_if has to be
invocable as non-const with a non-const lvalues argument. Restore
support for predicates that only accept non-const arguments.

It's not strictly nevessary to change it for the set and unordered_set
overloads, because they only give const access to the elements anyway.
I've done it for them too just to keep them all consistent.

libstdc++-v3/ChangeLog:

	PR libstdc++/107850
	* include/bits/erase_if.h (__erase_nodes_if): Use non-const
	reference to the container.
	* include/experimental/map (erase_if): Likewise.
	* include/experimental/set (erase_if): Likewise.
	* include/experimental/unordered_map (erase_if): Likewise.
	* include/experimental/unordered_set (erase_if): Likewise.
	* include/std/map (erase_if): Likewise.
	* include/std/set (erase_if): Likewise.
	* include/std/unordered_map (erase_if): Likewise.
	* include/std/unordered_set (erase_if): Likewise.
	* testsuite/23_containers/map/erasure.cc: Check with
	const-incorrect predicate.
	* testsuite/23_containers/set/erasure.cc: Likewise.
	* testsuite/23_containers/unordered_map/erasure.cc: Likewise.
	* testsuite/23_containers/unordered_set/erasure.cc: Likewise.
	* testsuite/experimental/map/erasure.cc: Likewise.
	* testsuite/experimental/set/erasure.cc: Likewise.
	* testsuite/experimental/unordered_map/erasure.cc: Likewise.
	* testsuite/experimental/unordered_set/erasure.cc: Likewise.
2022-11-25 15:06:25 +00:00
Jonathan Wakely
48e4a9d938 libstdc++: Do not define operator!= in <random> for C++20
These overloads are not needed in C++20 as they can be synthesized by
the compiler. Removing them means less code to compile when including
these headers.

libstdc++-v3/ChangeLog:

	* include/bits/random.h [three_way_comparison] (operator!=):
	Do not define inequality operators when C++20 three way
	comparisons are supported.
	* include/ext/random [three_way_comparison] (operator!=):
	Likewise.
2022-11-25 15:01:50 +00:00
Jonathan Wakely
6bd8d11922 libstdc++: Add always_inline to trivial iterator operations
libstdc++-v3/ChangeLog:

	* include/bits/stl_iterator_base_funcs.h (__distance):
	Add always_inline attribute to overload for random
	access iterators.
	(advance, distance, next, prev): Add always_inline attribute to
	inline functions that just forward to another function.
2022-11-25 15:01:49 +00:00
Jonathan Wakely
4581328022 libstdc++: Change return type of std::bit_width to int (LWG 3656)
libstdc++-v3/ChangeLog:

	* doc/html/manual/bugs.html: Regenerate.
	* doc/xml/manual/intro.xml: Document LWG 3656 change.
	* include/std/bit (__bit_width, bit_width): Return int.
	* testsuite/26_numerics/bit/bit.pow.two/lwg3656.cc: New test.
2022-11-25 00:22:01 +00:00
Jonathan Wakely
dfc1ea414e libstdc++: Replace std::isdigit and std::isxdigit in <format> [PR107817]
These functions aren't usable in constant expressions. Provide our own
implementations, based on __from_chars_alnum_to_val from <charconv>.

libstdc++-v3/ChangeLog:

	PR libstdc++/107817
	* include/std/charconv (__from_chars_alnum_to_val): Add
	constexpr for C++20.
	* include/std/format (__is_digit, __is_xdigit): New functions.
	(_Spec::_S_parse_width_or_precision): Use __is_digit.
	(__formatter_fp::parse): Use __is_xdigit.
2022-11-22 17:45:46 +00:00
Jonathan Wakely
cbd05ca5ab libstdc++: Reduce size of std::bind_front(F) result
When there are no bound arguments to a std::bind_front call we don't
need the overhead of compiling, initializing, and accessing an empty
tuple.

libstdc++-v3/ChangeLog:

	* include/std/functional (_Bind_front0): New class template.
	(_Bind_front_t): Use _Bind_front0 when there are no bound
	arguments.
	* testsuite/20_util/function_objects/bind_front/107784.cc:
	New test.
2022-11-21 17:46:42 +00:00
Jonathan Wakely
ed77dcb9be libstdc++: Check static assertions earlier in chrono::duration
This ensures that we fail a static assertion before giving any other
errors. Instantiating chrono::duration<int, chrono::seconds> will now
print this before the other errors caused by it:

error: static assertion failed: period must be a specialization of ratio

libstdc++-v3/ChangeLog:

	* include/bits/chrono.h (duration): Check preconditions on
	template arguments before using them.
2022-11-21 17:46:42 +00:00
Jonathan Wakely
94f7baf219 libstdc++: Improve Doxygen comments in <tuple>
libstdc++-v3/ChangeLog:

	* include/std/tuple: Add better Doxygen comments.
2022-11-21 17:46:42 +00:00
François Dumont
a16a546044 libstdc++: Add std qualification on isxdigit calls
Those qualifications are needed in _GLIBCXX_INLINE_VERSION mode because in <cctype>
symbols are not put in versioned namespace.

libstdc++-v3/ChangeLog

	* include/std/format: Add std qualification on isxdigit calls.
2022-11-20 21:07:10 +01:00
Jonathan Wakely
0723ad3902 libstdc++: Add always_inline to trivial range access functions
This makes all the [iterator.range] functions always-inline, except the
ones that construct a std::reverse_iterator, as they do a little more
work. They could probably be made always_inline too though, and maybe
the std::reverse_iterator constructor too.

This means that even for -O0 these functions have no runtime overhead
compared with calling a member of the container, or performing pointer
arithmetic for arrays.

libstdc++-v3/ChangeLog:

	* include/bits/range_access.h: Add always_inline attribute to
	trivial functions.
2022-11-19 17:44:14 +00:00
Jonathan Wakely
18169e8eee libstdc++: Fix -Wsign-compare warnings in std::format
libstdc++-v3/ChangeLog:

	* include/std/format: Fix -Wsign-compare warnings.
2022-11-19 15:05:28 +00:00
Jonathan Wakely
fca0f50b14 libstdc++: Fix Doxygen warning
This fixes a Doxygen warning about a mismatched parameter name. The
standard uses 'r' here, like the Doxygen comment, so use '__r' instead
of '__e'.

libstdc++-v3/ChangeLog:

	* include/bits/ptr_traits.h (pointer_traits::pointer_to): Rename
	parameter.
2022-11-19 15:05:28 +00:00
Jonathan Wakely
945e86ddaa libstdc++: Fix one more malformed requires-clause [PR107649]
libstdc++-v3/ChangeLog:

	PR libstdc++/107649
	* include/std/complex (__complex_proj): Fix requires-clause.
2022-11-19 15:05:28 +00:00
Jeff Chapman II
ea63396f6b libstdc++: add experimental Contracts support
This patch adds the library support for the experimental C++ Contracts
implementation.  This now consists only of a default definition of the
violation handler, which users can override through defining their own
version.  To avoid ABI stability problems with libstdc++.so this is added to
a separate -lstdc++exp static library, which the driver knows to add when it
sees -fcontracts.

Co-authored-by: Andrew Marmaduke <amarmaduke@lock3software.com>
Co-authored-by: Jason Merrill  <jason@redhat.com>

libstdc++-v3/ChangeLog:

	* acinclude.m4 (glibcxx_SUBDIRS): Add src/experimental.
	* include/Makefile.am (experimental_headers): Add contract.
	* include/Makefile.in: Regenerate.
	* src/Makefile.am (SUBDIRS): Add experimental.
	* src/Makefile.in: Regenerate.
	* configure: Regenerate.
	* src/experimental/contract.cc: New file.
	* src/experimental/Makefile.am: New file.
	* src/experimental/Makefile.in: New file.
	* include/experimental/contract: New file.
2022-11-18 21:40:29 -05:00
Jonathan Wakely
f69a8299c1 libstdc++: Ensure std::to_chars overloads all declared in <format> [PR107720]
For powerpc64le we need to be able to format both of __ieee128 and
__ibm128, so we need the std::to_chars overloads for both types to be
visible at once. The __ieee128 overloads are always visible in C++23
mode, because they're used to implement the _Float128 overloads. The
__ibm128 overloads are only visible when long double is __ibm128.

libstdc++-v3/ChangeLog:

	PR libstdc++/107720
	* include/std/format [_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT]:
	Declare overloads of std::to_chars for the alternative long
	double type.
2022-11-17 00:34:42 +00:00
Jonathan Wakely
dbdce6adb7 libstdc++: Fix dumb typos in ALT128 support in <format> [PR107720]
This is only a partial fix for the PR.

libstdc++-v3/ChangeLog:

	PR libstdc++/107720
	* include/std/format (__format::_Arg_t): Fix typo in enumerator
	name.
	(_Arg_value::_S_get): Fix missing semi-colons.
2022-11-16 20:52:53 +00:00
Jonathan Wakely
629897ed80 libstdc++: Improve performance of chrono::utc_clock::now()
We can use an array instead of a std::vector, and we can avoid the
binary search for the common case of a time point after the most recent
leap second. On one system where I tested this, utc_clock::now() now
takes about 16ns instead of 31ns.

libstdc++-v3/ChangeLog:

	* include/std/chrono (get_leap_second_info): Optimize.
2022-11-16 20:52:53 +00:00
Jonathan Wakely
2f5c071860 libstdc++: Adjust <format> for Clang compatibility [PR107712]
Clang doesn't define __builtin_toupper, so use std::toupper.

Also add some (not actually required since C++20) typename keywords to
help Clang versions up to and including 15.

libstdc++-v3/ChangeLog:

	PR libstdc++/107712
	* include/std/format (__format::__formatter_int::format): Use
	std::toupper when __builtin_toupper isn't available.
	(basic_format_arg::handle): Add 'typename'.
	* include/std/complex (complex<T>): Add 'typename'.
2022-11-16 20:52:37 +00:00
Jonathan Wakely
22cb0fea71 libstdc++: Disable std::format of _Float128 if std::to_chars is innaccurate
This restricts std::format support for _Float128 (and __float128) to
targets where glibc provides __strfromf128 and so can give correct
output.

libstdc++-v3/ChangeLog:

	* include/std/format [__FLT128_DIG__] (_GLIBCXX_FORMAT_F128):
	Only support formatting _Float128 when glibc provides the
	functionality needed for accurate std::to_chars.
2022-11-16 20:52:03 +00:00
Patrick Palka
ec59848074 libstdc++: Fix stream initialization with static library [PR107701]
When linking with a static library, the linker seems to discard a
constituent .o object (including its global initializers) if nothing
defined in the object is referenced by the program (unless e.g.
--whole-archive is used).  This behavior breaks iostream with static
libstdc++.a (on systems that support init priorities) because we define
the global initializer for the standard stream objects in a separate TU
(ios_init.cc) from the stream object definitions (globals_io.cc).

This patch fixes this by moving the stream initialization object into
the same TU that defines the stream objects, so that any use of the
streams prevents the linker from discarding this global initializer.

	PR libstdc++/107701

libstdc++-v3/ChangeLog:

	* include/std/iostream (__ioinit): Adjust comment.
	* src/c++98/globals_io.cc: Include "io_base_init.h" here
	instead of ...
	* src/c++98/ios_init.cc: ... here.
	* src/c++98/ios_base_init.h (__ioinit): More comments.
	* testsuite/17_intro/static.cc: dg-do run instead of just link.
2022-11-16 08:53:51 -05:00