Commit Graph

11958 Commits

Author SHA1 Message Date
GCC Administrator d62586ee56 Daily bump. 2020-11-20 00:16:40 +00:00
Jonathan Wakely 08b4d32571 libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989]
Since glibc 2.27 the pthread_self symbol has been defined in libc rather
than libpthread. Because we only call pthread_self through a weak alias
it's possible for statically linked executables to end up without a
definition of pthread_self. This crashes when trying to call an
undefined weak symbol.

We can use the __GLIBC_PREREQ version check to detect the version of
glibc where pthread_self is no longer in libpthread, and call it
directly rather than through the weak reference.

It would be better to check for pthread_self in libc during configure
instead of hardcoding the __GLIBC_PREREQ check. That would be
complicated by the fact that prior to glibc 2.27 libc.a didn't have the
pthread_self symbol, but libc.so.6 did.  The configure checks would need
to try to link both statically and dynamically, and the result would
depend on whether the static libc.a happens to be installed during
configure (which could vary between different systems using the same
version of glibc). Doing it properly is left for a future date, as that
will be needed anyway after glibc moves all pthread symbols from
libpthread to libc. When that happens we should revisit the whole
approach of using weak symbols for pthread symbols.

For the purposes of std::this_thread::get_id() we call
pthread_self() directly when using glibc 2.27 or later. Otherwise, if
__gthread_active_p() is true then we know the libpthread symbol is
available so we call that. Otherwise, we are single-threaded and just
use ((__gthread_t)1) as the thread ID.

An undesirable consequence of this change is that code compiled prior to
the change might inline the old definition of this_thread::get_id()
which always returns (__gthread_t)1 in a program that isn't linked to
libpthread. Code compiled after the change will use pthread_self() and
so get a real TID. That could result in the main thread having different
thread::id values in different translation units. This seems acceptable,
as there are not expected to be many uses of thread::id in programs
that aren't linked to libpthread.

An earlier version of this patch also changed __gthread_self() to use
__GLIBC_PREREQ(2, 27) and only use the weak symbol for older glibc. Tha
might still make sense to do, but isn't needed by libstdc++ now.

libstdc++-v3/ChangeLog:

	PR libstdc++/95989
	* config/os/gnu-linux/os_defines.h (_GLIBCXX_NATIVE_THREAD_ID):
	Define new macro to get reliable thread ID.
	* include/bits/std_thread.h: (this_thread::get_id): Use new
	macro if it's defined.
	* testsuite/30_threads/jthread/95989.cc: New test.
	* testsuite/30_threads/this_thread/95989.cc: New test.
2020-11-19 21:07:06 +00:00
Jonathan Wakely 5e6a43158d libstdc++: Add missing header to some tests
These tests use std::this_thread::sleep_for without including <thread>.

libstdc++-v3/ChangeLog:

	* testsuite/30_threads/async/async.cc: Include <thread>.
	* testsuite/30_threads/future/members/93456.cc: Likewise.
2020-11-19 16:17:33 +00:00
Jonathan Wakely b204d7722d libstdc++: Move std::thread to a new header
This makes it possible to use std::thread without including the whole of
<thread>. It also makes this_thread::get_id() and this_thread::yield()
available even when there is no gthreads support (e.g. when GCC is built
with --disable-threads or --enable-threads=single).

In order for the std:🧵:id return type of this_thread::get_id() to
be defined, std:thread itself is defined unconditionally. However the
constructor that creates new threads is not defined for single-threaded
builds. The thread::join() and thread::detach() member functions are
defined inline for single-threaded builds and just throw an exception
(because we know the thread cannot be joinable if the constructor that
creates joinable threads doesn't exit).

The thread::hardware_concurrency() member function is also defined
inline and returns 0 (as suggested by the standard when the value "is
not computable or well-defined").

The main benefit for most targets is that other headers such as <future>
do not need to include the whole of <thread> just to be able to create a
std::thread. That avoids including <stop_token> and std::jthread where
not required. This is another partial fix for PR 92546.

This also means we can use this_thread::get_id() and this_thread::yield()
in <stop_token> instead of using the gthread functions directly. This
removes some preprocessor conditionals, simplifying the code.

libstdc++-v3/ChangeLog:

	PR libstdc++/92546
	* include/Makefile.am: Add new <bits/std_thread.h> header.
	* include/Makefile.in: Regenerate.
	* include/std/future: Include new header instead of <thread>.
	* include/std/stop_token: Include new header instead of
	<bits/gthr.h>.
	(stop_token::_S_yield()): Use this_thread::yield().
	(_Stop_state_t::_M_requester): Change type to std:🧵:id.
	(_Stop_state_t::_M_request_stop()): Use this_thread::get_id().
	(_Stop_state_t::_M_remove_callback(_Stop_cb*)): Likewise.
	Use __is_single_threaded() to decide whether to synchronize.
	* include/std/thread (thread, operator==, this_thread::get_id)
	(this_thread::yield): Move to new header.
	(operator<=>, operator!=, operator<, operator<=, operator>)
	(operator>=, hash<thread::id>, operator<<): Define even when
	gthreads not available.
	* src/c++11/thread.cc: Include <memory>.
	* include/bits/std_thread.h: New file.
	(thread, operator==, this_thread::get_id, this_thread::yield):
	Define even when gthreads not available.
	[!_GLIBCXX_HAS_GTHREADS] (thread::join, thread::detach)
	(thread::hardware_concurrency): Define inline.
2020-11-19 13:36:15 +00:00
Jonathan Wakely b108faa940 libstdc++: Fix overflow checks to use the correct "time_t" [PR 93456]
I recently added overflow checks to src/c++11/futex.cc for PR 93456, but
then changed the type of the timespec for PR 93421. This meant the
overflow checks were no longer using the right range, because the
variable being written to might be smaller than time_t.

This introduces new typedef that corresponds to the tv_sec member of the
struct being passed to the syscall, and uses that typedef in the range
checks.

libstdc++-v3/ChangeLog:

	PR libstdc++/93421
	PR libstdc++/93456
	* src/c++11/futex.cc (syscall_time_t): New typedef for
	the type of the syscall_timespec::tv_sec member.
	(relative_timespec, _M_futex_wait_until)
	(_M_futex_wait_until_steady): Use syscall_time_t in overflow
	checks, not time_t.
2020-11-19 13:33:11 +00:00
GCC Administrator 25bb75f841 Daily bump. 2020-11-19 00:16:30 +00:00
Patrick Palka d4a788c717 libstdc++: Fix ranges::join_view::_Iterator::operator-> [LWG 3500]
This applies the proposed resolution of LWG 3500, which corrects the
return type and constraints of this member function to use the right
iterator type.  Additionally, a nearby local variable is uglified.

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_satisfy): Uglify
	local variable inner.
	(join_view::_Iterator::operator->): Use _Inner_iter instead of
	_Outer_iter in the function signature as per LWG 3500.
	* testsuite/std/ranges/adaptors/join.cc (test08): Test it.
2020-11-18 10:23:57 -05:00
GCC Administrator 4dabb03719 Daily bump. 2020-11-18 00:16:34 +00:00
Jonathan Wakely 1e3e6c700f libstdc++: Revert changes for SYS_clock_gettime64 [PR 93421]
As discussed in the PR, it's incredibly unlikely that a system that
needs to use the SYS_clock_gettime syscall (e.g. glibc 2.16 or older) is
going to define the SYS_clock_gettime64 macro. Ancient systems that need
to use the syscall aren't going to have time64 support.

This reverts the recent changes to try and make clock_gettime syscalls
be compatible with systems that have been updated for time64 (those
changes were wrong anyway as they misspelled the SYS_clock_gettime64
macro). The changes for futex syscalls are retained, because we still
use them on modern systems that might be using time64.

To ensure that the clock_gettime syscalls are safe, configure will fail
if SYS_clock_gettime is needed, and SYS_clock_gettime64 is also defined
(but to a distinct value from SYS_clock_gettime), and the tv_sec member
of timespec is larger than long. This means we will be unable to build
on a hypothetical system where we need the time32 version of
SYS_clock_gettime but where userspace is using a time64 struct timespec.
In the unlikely event that this failure is triggered on any real
systems, we can fix it later. But we probably won't need to.

libstdc++-v3/ChangeLog:

	PR libstdc++/93421
	* acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Fail if struct
	timespec isn't compatible with SYS_clock_gettime.
	* configure: Regenerate.
	* src/c++11/chrono.cc: Revert changes for time64 compatibility.
	Add static_assert instead.
	* src/c++11/futex.cc (_M_futex_wait_until_steady): Assume
	SYS_clock_gettime can use struct timespec.
2020-11-17 22:38:49 +00:00
Jonathan Wakely ecf65330c1 libstdc++: Fix unconditional definition of __cpp_lib_span in <version> [PR 97869}
The <span> header is empty unless Concepts are supported, but <version>
defines the __cpp_lib_span feature test macro unconditionally. It should
be guarded by the same conditions as in <span>.

libstdc++-v3/ChangeLog:

	PR libstdc++/97869
	* include/precompiled/stdc++.h: Include <coroutine>.
	* include/std/version (__cpp_lib_span): Check __cpp_lib_concepts
	before defining.
2020-11-17 16:13:14 +00:00
Patrick Palka 8661f4faa8 libstdc++: Fix ranges::search_n for random access iterators [PR97828]
My ranges transcription of the std::search_n implementation for random
access iterators missed a crucial part of the algorithm which the
existing tests didn't exercise.  When __remainder is less than __count
at the start of an iteration of the outer while loop, it means we're
continuing a partial match of __count - __remainder elements from the
previous iteration.  If at the end of the iteration we don't complete
this partial match, we need to reset __remainder so that it's only
offset by the size of the most recent partial match before starting the
next iteration.

This patch fixes this appropriately, mirroring how it's done in the
corresponding std::search_n implementation.

libstdc++-v3/ChangeLog:

	PR libstdc++/97828
	* include/bits/ranges_algo.h (__search_n_fn::operator()): Check
	random_access_iterator before using the backtracking
	implementation.  When the backwards scan fails prematurely,
	reset __remainder appropriately.
	* testsuite/25_algorithms/search_n/97828.cc: New test.
2020-11-17 10:28:20 -05:00
GCC Administrator 29c5d9ceb9 Daily bump. 2020-11-17 00:16:27 +00:00
Jonathan Wakely b2099e9fd9 libstdc++: Fix error shown during Solaris build
Currently this is shown when building libstdc++ on Solaris:

-lrt: open: No such file or directory

The error comes from the make_sunver.pl script which tries to open each
of its arguments. The arguments are passed by this make rule:

	perl ${glibcxx_srcdir}/scripts/make_exports.pl \
	  libstdc++-symbols.ver \
	  $(libstdc___la_OBJECTS:%.lo=.libs/%.o) \
	 `echo $(libstdc___la_LIBADD) | \
	    sed 's,/\([^/.]*\)\.la,/.libs/\1.a,g'` \
	 > $@ || (rm -f $@ ; exit 1)

The $(libstdc___la_LIBADD) variable includes $(GLIBCXX_LIBS) which
contains -lrt on Solaris.

This patch adds another sed script to filter -l arguments from the echo
command. In order to reliably match ' -l[^ ]* ' the echo arguments are
quoted and a space added before and after them. This might be overkill
just to remove -lrt from the start of the string, but should be robust
in case other -l arguments are added to $(GLIBCXX_LIBS), or in case the
$(libstdc___la_LIBADD) libraries are reordered.

libstdc++-v3/ChangeLog:

	* src/Makefile.am (libstdc++-symbols.ver-sun): Remove -lrt from
	arguments passed to make_sunver.pl script.
	* src/Makefile.in: Regenerate.
2020-11-16 11:54:22 +00:00
GCC Administrator cba306519c Daily bump. 2020-11-16 00:16:31 +00:00
Jason Merrill baf38d2e36 c++: Check abstract type only on object creation. [PR86252]
Abstract checking has been problematic for a while; when I implemented an
earlier issue resolution to do more checking it led to undesirable
instantiations, and so backed some of it out.  During the C++20 process we
decided with P0929R2 that we should go the other way, and only check
abstractness when we're actually creating an object, not when merely forming
an array or function type.  This means that we can remove the machinery for
checking whether a newly complete class makes some earlier declaration
ill-formed.  This change was moved as a DR, so I'm applying it to all
standard levels.  This could be reconsidered if it causes problems, but I
don't expect it to.

The change to the libstdc++ result_of test brings the expected behavior in
line with that for incomplete types, but as in PR97841 I think the libstdc++
handling of incomplete types in this and other type_traits is itself wrong,
so I expect these lines and others to change again before long.

gcc/cp/ChangeLog:

	* decl.c (cp_finish_decl): Only check abstractness on definition.
	(require_complete_types_for_parms): Check abstractness here.
	(create_array_type_for_decl): Not here.
	(grokdeclarator, grokparms, complete_vars): Not here.
	* pt.c (tsubst, tsubst_arg_types, tsubst_function_type): Not here.
	* typeck2.c (struct pending_abstract_type): Remove.
	(struct abstract_type_hasher): Remove.
	(abstract_pending_vars, complete_type_check_abstract): Remove.
	(abstract_virtuals_error_sfinae): Handle arrays.
	* call.c (conv_is_prvalue): Split out from...
	(conv_binds_ref_to_prvalue): ...here.
	(implicit_conversion_1): Rename from implicit_conversion.
	(implicit_conversion): An abstract prvalue is bad.
	(convert_like_internal): Don't complain if expr is already
	error_mark_node.

gcc/testsuite/ChangeLog:

	* g++.dg/other/abstract1.C: Adjust.
	* g++.dg/other/abstract2.C: Adjust.
	* g++.dg/other/abstract4.C: Adjust.
	* g++.dg/other/abstract5.C: Adjust.
	* g++.dg/other/abstract8.C: New test.
	* g++.dg/template/sfinae-dr657.C: Adjust.
	* g++.old-deja/g++.other/decl3.C: Adjust.

libstdc++-v3/ChangeLog:

	* testsuite/20_util/result_of/sfinae_friendly_1.cc: Adjust.
2020-11-15 12:07:13 -05:00
GCC Administrator 77f67db2a4 Daily bump. 2020-11-14 00:16:38 +00:00
Jonathan Wakely 4d039cb9a1 libstdc++: Use custom timespec in system calls [PR 93421]
On 32-bit targets where userspace has switched to 64-bit time_t, we
cannot pass struct timespec to SYS_futex or SYS_clock_gettime, because
the userspace definition of struct timespec will not match what the
kernel expects.

We use the existence of the SYS_futex_time64 or SYS_clock_gettime_time64
macros to imply that userspace *might* have switched to the new timespec
definition.  This is a conservative assumption. It's possible that the
new syscall numbers are defined in the libc headers but that timespec
hasn't been updated yet (as is the case for glibc currently). But using
the alternative struct with two longs is still OK, it's just redundant
if userspace timespec still uses a 32-bit time_t.

We also check that SYS_futex_time64 != SYS_futex so that we don't try
to use a 32-bit tv_sec on modern targets that only support the 64-bit
system calls and define the old macro to the same value as the new one.

We could possibly check #ifdef __USE_TIME_BITS64 to see whether
userspace has actually been updated, but it's not clear if user code
is meant to inspect that or if it's only for libc internal use.

libstdc++-v3/ChangeLog:

	PR libstdc++/93421
	* src/c++11/chrono.cc [_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL]
	(syscall_timespec): Define a type suitable for SYS_clock_gettime
	calls.
	(system_clock::now(), steady_clock::now()): Use syscall_timespec
	instead of timespec.
	* src/c++11/futex.cc (syscall_timespec): Define a type suitable
	for SYS_futex and SYS_clock_gettime calls.
	(relative_timespec): Use syscall_timespec instead of timespec.
	(__atomic_futex_unsigned_base::_M_futex_wait_until): Likewise.
	(__atomic_futex_unsigned_base::_M_futex_wait_until_steady):
	Likewise.
2020-11-13 23:08:35 +00:00
Jonathan Wakely b8d36dcc91 libstdc++: Remove redundant overflow check for futex timeout [PR 93456]
The relative_timespec function already checks for the case where the
specified timeout is in the past, so the difference can never be
negative. That means we dn't need to check if it's more negative than
the minimum time_t value.

libstdc++-v3/ChangeLog:

	PR libstdc++/93456
	* src/c++11/futex.cc (relative_timespec): Remove redundant check
	negative values.
	* testsuite/30_threads/future/members/wait_until_overflow.cc: Moved to...
	* testsuite/30_threads/future/members/93456.cc: ...here.
2020-11-13 23:08:34 +00:00
Jonathan Wakely 91004436da libstdc++: Avoid more 32-bit time_t overflows in futex calls
This fixes another overflow in code converting a std::chrono::seconds
duration to a time_t. This time in the new code using a futex wait with
an absolute timeout (so this one doesn't need to be backported to the
release branches).

A timeout after the epochalypse would overflow the tv_sec field,
producing an incorrect value. If that incorrect value happened to be
negative, the syscall would return with EINVAL and then the caller would
keep retrying, spinning until the timeout was reached.  If the value
happened to be positive, we would wake up too soon and incorrectly
report a timeout

libstdc++-v3/ChangeLog:

	* src/c++11/futex.cc (relative_timespec): Add [[unlikely]]
	attributes.
	(__atomic_futex_unsigned_base::_M_futex_wait_until)
	(__atomic_futex_unsigned_base::_M_futex_wait_until_steady):
	Check for overflow.
	* testsuite/30_threads/future/members/wait_until_overflow.cc:
	New test.
2020-11-13 19:11:07 +00:00
Jonathan Wakely e7e0eeeb6e libstdc++: Avoid 32-bit time_t overflows in futex calls
The existing code doesn't check whether the chrono::seconds value is out
of range of time_t. When using a timeout before the epoch (with a
negative value) subtracting the current time (as time_t) and then
assigning it to a time_t can overflow to a large positive value. This
means that we end up waiting several years even though the specific
timeout was in the distant past.

We do have a check for negative timeouts, but that happens after the
conversion to time_t so happens after the overflow.

The conversion to a relative timeout is done in two places, so this
factors it into a new function and adds the overflow checks there.

libstdc++-v3/ChangeLog:

	* src/c++11/futex.cc (relative_timespec): New function to
	create relative time from two absolute times.
	(__atomic_futex_unsigned_base::_M_futex_wait_until)
	(__atomic_futex_unsigned_base::_M_futex_wait_until_steady):
	Use relative_timespec.
2020-11-13 17:19:12 +00:00
Jonathan Wakely 8c4e33d203 libstdc++: Add -pthread options to std::future polling test
For linux targets this test doesn't need -lpthread because it only uses
atomics, but for all other targets std::call_once still needs pthreads.
Add the necessary test directives to make that work.

The timings in this test might be too fragile or too target-specific, so
it might need to be adjusted in future, or restricted to only run on
specific targets. For now I've increased the allowed ratio between
wait_for calls before and after the future is made ready, because it was
failing with -O3 -march=native sometimes.

libstdc++-v3/ChangeLog:

	* testsuite/30_threads/future/members/poll.cc: Require gthreads
	and add -pthread for targets that require it. Relax required
	ratio of wait_for calls before/after the future is ready.
2020-11-13 11:01:24 +00:00
GCC Administrator a5a115258a Daily bump. 2020-11-13 00:16:35 +00:00
Jonathan Wakely 93fc477468 libstdc++: Optimise std::future::wait_for and fix futex polling
To poll a std::future to see if it's ready you have to call one of the
timed waiting functions. The most obvious way is wait_for(0s) but this
was previously very inefficient because it would turn the relative
timeout to an absolute one by calling system_clock::now(). When the
relative timeout is zero (or less) we're obviously going to get a time
that has already passed, but the overhead of obtaining the current time
can be dozens of microseconds. The alternative is to call wait_until
with an absolute timeout that is in the past. If you know the clock's
epoch is in the past you can use a default constructed time_point.
Alternatively, using some_clock::time_point::min() gives the earliest
time point supported by the clock, which should be safe to assume is in
the past. However, using a futex wait with an absolute timeout before
the UNIX epoch fails and sets errno=EINVAL. The new code using futex
waits with absolute timeouts was not checking for this case, which could
result in hangs (or killing the process if the libray is built with
assertions enabled).

This patch checks for times before the epoch before attempting to wait
on a futex with an absolute timeout, which fixes the hangs or crashes.
It also makes it very fast to poll using an absolute timeout before the
epoch (because we skip the futex syscall).

It also makes future::wait_for avoid waiting at all when the relative
timeout is zero or less, to avoid the unnecessary overhead of getting
the current time. This makes polling with wait_for(0s) take only a few
cycles instead of dozens of milliseconds.

libstdc++-v3/ChangeLog:

	* include/std/future (future::wait_for): Do not wait for
	durations less than or equal to zero.
	* src/c++11/futex.cc (_M_futex_wait_until)
	(_M_futex_wait_until_steady): Do not wait for timeouts before
	the epoch.
	* testsuite/30_threads/future/members/poll.cc: New test.
2020-11-12 23:47:04 +00:00
Jonathan Wakely d21776ef90 libstdc++: Simplify __numeric_traits definition
This changes the __numeric_traits primary template to assume its
argument is an integer type. For the three floating point types that are
supported by __numeric_traits_floating an explicit specialization of
__numeric_traits chooses the right base class.

This improves the failure mode for using __numeric_traits with an
unsupported type. Previously it would use __numeric_traits_floating as
the base class, and give somewhat obscure errors for trying to access
the static data members. Now it will use __numeric_traits_integer which
has a static_assert to check for supported types.

As a side effect of this change there is no need to instantiate
__conditional_type to decide which base class to use.

libstdc++-v3/ChangeLog:

	* include/ext/numeric_traits.h (__numeric_traits): Change
	primary template to always derive from __numeric_traits_integer.
	(__numeric_traits<float>, __numeric_traits<double>)
	(__numeric_traits<long double>): Add explicit specializations.
2020-11-12 14:36:39 +00:00
Jonathan Wakely 7f851c3341 libstdc++: Fix __numeric_traits_integer<__int20> [PR 97798]
The expression used to calculate the maximum value for an integer type
assumes that the number of bits in the value representation is always
sizeof(T) * CHAR_BIT. This is not true for the __int20 type on msp430,
which has only 20 bits in the value representation but 32 bits in the
object representation. This causes an integer overflow in a constant
expression, which is ill-formed.

This problem was already solved by DJ for std::numeric_limits<__int20>
by generalizing the helper macros to use a specified number of bits
instead of assuming sizeof(T) * CHAR_BIT. Then the INT_N_n types can
specify the number of bits using the __GLIBCXX_BITSIZE_INT_N_n macros
that the compiler defines.

I'm using a slightly different approach here. I've replaced the helper
macros entirely, and just expanded the calculations in the initializers
for the static data members. By reordering the data members we can reuse
__is_signed and __digits in the other initializers. This removes the
repetition of expanding __glibcxx_signed(T) and __glibcxx_digits(T)
multiple times in each initializer.

The __is_integer_nonstrict trait now defines a new constant, __width,
which is sizeof(T) * CHAR_BIT by default (defined as an enumerator so
that no storage is needed for a static data member). By specializing
__is_integer_nonstrict for the INT_N types that have padding bits, we
can provide the correct width via the __GLIBCXX_BITSIZE_INT_N_n macros.

libstdc++-v3/ChangeLog:

	PR libstdc++/97798
	* include/ext/numeric_traits.h (__glibcxx_signed)
	(__glibcxx_digits, __glibcxx_min, __glibcxx_max): Remove
	macros.
	(__is_integer_nonstrict::__width): Define new constant.
	(__numeric_traits_integer): Define constants in terms of each
	other and __is_integer_nonstrict::__width, rather than the
	removed macros.
	(_GLIBCXX_INT_N_TRAITS): Macro to define explicit
	specializations for non-standard integer types.
2020-11-12 12:10:10 +00:00
GCC Administrator 0f5f9ed5e5 Daily bump. 2020-11-12 00:16:39 +00:00
Jonathan Yong 505ea90904 libstdc++: Exclude cygwin and mingw from linker relro support
PE format does not have ELF style relro linker support, exclude
from checking. If the host linker supports ELF format, configure
may get confused.

libstdc++-v3/ChangeLog:

	* acinclude.m4 (GLIBCXX_CHECK_LINKER_FEATURES): Exclude
	cygwin and mingw from relro linker test.
	* configure: Regenerate.
2020-11-11 15:36:05 +00:00
Paul Scharnofske 0ebaea3b66 libstdc++: Assigning to a joinable std::jthread calls std::terminate
Move assigning to a std::jthread that represents a thread of execution
needs to send a stop request and join that running thread. Otherwise the
std::thread data member will terminate in its assignment operator.

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

libstdc++-v3/ChangeLog:

	* include/std/thread (jthread::operator=(jthread&&)): Transfer
	any existing state to a temporary that will request a stop and
	then join.
	* testsuite/30_threads/jthread/jthread.cc: Test move assignment.
2020-11-11 11:13:31 +00:00
Jonathan Wakely 43f9e5aff0 libstdc++: Use helper type for checking thread ID
This encapsulates the storing and checking of the thread ID into a class
type, so that the macro _GLIBCXX_HAS_GTHREADS is only checked in one
place. The code doing the checks just calls member functions of the new
type, without caring whether that really does any work or not.

libstdc++-v3/ChangeLog:

	* include/std/stop_token (_Stop_state_t::_M_requester): Define
	new struct with members to store and check the thread ID.
	(_Stop_state_t::_M_request_stop()): Use _M_requester._M_set().
	(_Stop_state_t::_M_remove_callback(_Stop_cb*)): Use
	_M_requester._M_is_current_thread().
2020-11-11 11:13:31 +00:00
Jonathan Wakely ecba8547dd libstdc++: Implement std::emit_on_flush etc.
This adds the manipulators for use with basic_osyncstream. In order to
detect when an arbitrary basic_ostream<C,T> is the base class of a
basic_syncbuf<C,T,A> object, introduce a new intermediate base class
that stores the data members. The new base class stores a pointer and
two bools, which wastes (sizeof(void*) - 2) bytes of padding. It would
be possible to use the two least significant bits of the pointer for the
two bools, at least for targets where alignof(basic_streambuf) > 2, but
that's left as a possible change for a future date.

Also define basic_syncbuf::overflow to override the virtual function in
the base class, so that single characters can be inserted into the
stream buffer. Previously the default basic_streambuf::overflow
implementation was used, which drops the character on the floor.

libstdc++-v3/ChangeLog:

	* include/std/ostream (__syncbuf_base): New class template.
	(emit_on_flush, noemit_on_flush, flush_emit): New manipulators.
	* include/std/syncstream (basic_syncbuf): Derive from
	__syncbuf_base instead of basic_streambuf.
	(basic_syncbuf::operator=): Remove self-assignment check.
	(basic_syncbuf::swap): Remove self-swap check.
	(basic_syncbuf::emit): Do not skip pubsync() call if sequence
	is empty.
	(basic_syncbuf::sync): Remove no-op pubsync on stringbuf.
	(basic_syncbuf::overflow): Define override.
	* testsuite/27_io/basic_syncstream/basic_ops/1.cc: Test
	basic_osyncstream::put(char_type).
	* testsuite/27_io/basic_ostream/emit/1.cc: New test.
2020-11-11 00:19:40 +00:00
GCC Administrator bb6226419f Daily bump. 2020-11-11 00:16:36 +00:00
Jonathan Wakely 5dfbc52264 libstdc++: Avoid bad_alloc exceptions when changing locales
For the --enable-clocale=generic configuration, the current code can
fail with a bad_alloc exception. This patch uses the nothrow version of
operator new and reports allocation failures by setting failbit in the
iostate variable.

	* config/locale/generic/c_locale.cc (__set_C_locale()): New function
	to set the "C" locale and return the name of the previous locale.
	(__convert_to_v<float>, __convert_to_v<double>)
	(__convert_to_v<long double>): Use __set_C_locale and set failbit on
	error.
2020-11-10 20:33:29 +00:00
Jonathan Wakely f7c41c572b libstdc++: Reorder constructors in <sstream>
This groups all the constructors together, consistent with the synopses
in the C++20 standard.

libstdc++-v3/ChangeLog:

	* include/std/sstream (basic_stringbug, basic_istringstream)
	(basic_ostringstream, basic_stringstream): Reorder C++20
	constructors to be declared next to other constructors.
2020-11-10 19:22:48 +00:00
Jonathan Wakely 95cb0fc8c5 libstdc++: Add remaining C++20 additions to <sstream> [P0408R7]
This adds the new overloads of basic_stringbuf::str, and the
corresponding overloads to basic_istringstream, basic_ostringstream and
basic_stringstream.

libstdc++-v3/ChangeLog:

	* config/abi/pre/gnu.ver (GLIBCXX_3.4.21): Tighten patterns.
	(GLIBCXX_3.4.29): Export new symbols.
	* include/bits/alloc_traits.h (__allocator_like): New concept.
	* include/std/sstream (basic_stringbuf::swap): Add exception
	specification.
	(basic_stringbuf::str() const): Add ref-qualifier. Use new
	_M_high_mark function.
	(basic_stringbuf::str(const SAlloc&) const): Define new function.
	(basic_stringbuf::str() &&): Likewise.
	(basic_stringbuf::str(const basic_string<C,T,SAlloc>&)):
	Likewise.
	(basic_stringbuf::str(basic_string<C,T,Alloc>&&)): Likewise.
	(basic_stringbuf::view() const): Use _M_high_mark.
	(basic_istringstream::str, basic_ostringstream::str)
	(basic_stringstream::str): Define new overloads.
	* src/c++20/sstream-inst.cc (basic_stringbuf::str)
	(basic_istringstream::str, basic_ostringstream::str)
	(basic_stringstream::str): Explicit instantiation definitions
	for new overloads.
	* testsuite/27_io/basic_istringstream/view/char/1.cc: Add more
	checks.
	* testsuite/27_io/basic_istringstream/view/wchar_t/1.cc:
	Likewise.
	* testsuite/27_io/basic_ostringstream/view/char/1.cc:
	Likewise.
	* testsuite/27_io/basic_ostringstream/view/wchar_t/1.cc:
	Likewise.
	* testsuite/27_io/basic_stringstream/view/char/1.cc:
	Likewise.
	* testsuite/27_io/basic_stringstream/view/wchar_t/1.cc:
	Likewise.
	* testsuite/27_io/basic_istringstream/str/char/2.cc: New test.
	* testsuite/27_io/basic_istringstream/str/wchar_t/2.cc: New test.
	* testsuite/27_io/basic_ostringstream/str/char/3.cc: New test.
	* testsuite/27_io/basic_ostringstream/str/wchar_t/3.cc: New test.
	* testsuite/27_io/basic_stringbuf/str/char/4.cc: New test.
	* testsuite/27_io/basic_stringbuf/str/wchar_t/4.cc: New test.
	* testsuite/27_io/basic_stringstream/str/char/5.cc: New test.
	* testsuite/27_io/basic_stringstream/str/wchar_t/5.cc.cc: New test.
2020-11-10 19:22:47 +00:00
Jonathan Wakely ced70ebaa3 libstdc++: Fix more unspecified comparisons to null pointer [PR 97415]
This adds some more null checks to avoid a relational comparison with a
null pointer, similar to 78198b6021.

libstdc++-v3/ChangeLog:

	PR libstdc++/97415
	* include/std/sstream (basic_stringbuf::_M_update_egptr)
	(basic_stringbuf::__xfer_bufptrs::__xfer_bufptrs): Check for
	null before comparing pointers.
2020-11-10 19:21:55 +00:00
GCC Administrator 2bee28dd41 Daily bump. 2020-11-10 00:16:24 +00:00
François Dumont 6db082477a libstdc++: Remove <debug/array>
Add _GLIBCXX_ASSERTIONS assert in normal std::array and remove __gnu_debug::array
implementation.

libstdc++-v3/ChangeLog:

	* include/debug/array: Remove.
	* include/Makefile.am: Remove <debug/array>.
	* include/Makefile.in: Regenerate.
	* include/experimental/functional: Adapt.
	* include/std/array: Move to _GLIBCXX_INLINE_VERSION namespace.
	* include/std/functional: Adapt.
	* include/std/span: Adapt.
	* testsuite/23_containers/array/debug/back1_neg.cc:
	Remove dg-require-debug-mode. Add -D_GLIBCXX_ASSERTIONS option.
	* testsuite/23_containers/array/debug/back2_neg.cc: Likewise.
	* testsuite/23_containers/array/debug/front1_neg.cc: Likewise.
	* testsuite/23_containers/array/debug/front2_neg.cc: Likewise.
	* testsuite/23_containers/array/debug/square_brackets_operator1_neg.cc:
	Likewise.
	* testsuite/23_containers/array/debug/square_brackets_operator2_neg.cc:
	Likewise.
	* testsuite/23_containers/array/element_access/60497.cc
	* testsuite/23_containers/array/tuple_interface/get_debug_neg.cc:
	Remove.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc
	* testsuite/23_containers/array/tuple_interface/tuple_element_debug_neg.cc
	* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
2020-11-09 21:20:01 +01:00
François Dumont 38b17c27ce libstdc++: Make _GLIBCXX_DEBUG checks constexpr compatible
libstdc++-v3/ChangeLog:

	* include/debug/assertions.h (__glibcxx_requires_non_empty_range):
	Remove __builtin_expect.
	(__glibcxx_requires_subscript): Likewise.
	(__glibcxx_requires_nonempty): Likewise.
	* include/debug/formatter.h (__check_singular): Add C++11 constexpr
	qualification.
	* include/debug/helper_functions.h (__check_singular): Likewise. Skip
	check if constant evaluated.
	(__valid_range): Do not skip check if constant evaluated.
	* include/debug/macros.h (_GLIBCXX_DEBUG_VERIFY_COND_AT): Add
	__builtin_expect.
	(_GLIBCXX_DEBUG_VERIFY_AT_F): Use __glibcxx_assert_1.
	* testsuite/21_strings/basic_string_view/element_access/char/back_constexpr_neg.cc:
	New test.
	* testsuite/21_strings/basic_string_view/element_access/char/constexpr.cc: New test.
	* testsuite/21_strings/basic_string_view/element_access/char/constexpr_neg.cc: New test.
	* testsuite/21_strings/basic_string_view/element_access/char/front_back_constexpr.cc:
	New test.
	* testsuite/21_strings/basic_string_view/element_access/char/front_constexpr_neg.cc:
	New test.
	* testsuite/21_strings/basic_string_view/element_access/wchar_t/back_constexpr_neg.cc:
	New test.
	* testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr.cc: New test.
	* testsuite/21_strings/basic_string_view/element_access/wchar_t/constexpr_neg.cc: New test.
	* testsuite/21_strings/basic_string_view/element_access/wchar_t/front_constexpr_neg.cc:
	New test.
	* testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_neg.cc: New test.
	* testsuite/25_algorithms/lower_bound/debug/constexpr_partitioned_pred_neg.cc: New test.
	* testsuite/25_algorithms/lower_bound/debug/constexpr_valid_range_neg.cc: New test.
	* testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc: New test.
	* testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc: New test.
	* testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_neg.cc: New test.
	* testsuite/25_algorithms/upper_bound/debug/constexpr_partitioned_pred_neg.cc: New test.
	* testsuite/25_algorithms/upper_bound/debug/constexpr_valid_range_neg.cc: New test.
	* testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc: New test.
	* testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc: New test.
2020-11-09 21:11:13 +01:00
Jonathan Wakely b2b8516373 libstdc++: Improve comment on _Power_of_2 helper function
libstdc++-v3/ChangeLog:

	* include/bits/uniform_int_dist.h (__detail::_Power_of_2):
	Document that true result for zero is intentional.
2020-11-09 14:54:29 +00:00
Jonathan Wakely ff4bfb1553 libstdc++: Remove redundant check for zero in std::__popcount
The popcount built-ins work fine for zero, so there's no need to check
for it.

libstdc++-v3/ChangeLog:

	* include/std/bit (__popcount): Remove redundant check for zero.
2020-11-09 14:54:29 +00:00
Jonathan Wakely 0af3930a49 libstdc++: Use 'inline' consistently in std::exception_ptr [PR 97729]
With PR c++/67453 fixed we can rely on the 'used' attribute to emit
inline constructors and destructors in libsupc++/eh_ptr.cc. This means
we don't need to suppress the 'inline' keyword on them in that file, and
don't need to force 'always_inline' on them in other files.

libstdc++-v3/ChangeLog:

	PR libstdc++/97729
	* libsupc++/exception_ptr.h (exception_ptr::exception_ptr())
	(exception_ptr::exception_ptr(const exception_ptr&))
	(exception_ptr::~exception_ptr()): Remove 'always_inline'
	attributes. Use 'inline' unconditionally.
2020-11-09 14:28:38 +00:00
Jonathan Wakely 99bf3a817b libstdc++: Include <typeinfo> even for -fno-rtti [PR 97758]
The std::function code now uses std::type_info* even when RTTI is
disabled, so it should include <typeinfo> unconditionally. Without this,
Clang can't compile <functional> with -fno-rtti (it works with GCC
because std::type_info gets declared automatically by the compiler).

libstdc++-v3/ChangeLog:

	PR libstdc++/97758
	* include/bits/std_function.h [!__cpp_rtti]: Include <typeinfo>.
2020-11-09 14:28:37 +00:00
GCC Administrator 2da7ee050c Daily bump. 2020-11-08 00:16:31 +00:00
Liu Hao 7fc0f78c3f libsupc++: Make the destructor parameter to `__cxa_thread_atexit()` use the `__thiscall` calling convention for i686-w64-mingw32
The mingw-w64 implementations of `__cxa_thread_atexit()` and `__cxa_atexit()` have been
using `__thiscall` since two years ago. Using the default calling convention (which is
`__cdecl`) causes crashes as explained in PR83562.

Calling conventions have no effect on x86_64-w64-mingw32.

Reference: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83562
Reference: https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/crt/cxa_thread_atexit.c
Reference: f3e0fbb40c/
Reference: https://github.com/msys2/MINGW-packages/issues/7071
Signed-off-by: Liu Hao <lh_mouse@126.com>

    2020-10-08  Liu Hao  <lh_mouse@126.com>

	libstdc++-v3:
	* libsupc++/cxxabi.h: (__cxa_atexit): mark with _GLIBCXX_CDTOR_CALLABI
	(__cxa_thread_atexit): ditto
	* libsupc++/atexit_thread.cc: (__cxa_atexit): mark with
	_GLIBCXX_CDTOR_CALLABI
	(__cxa_thread_atexit): ditto
	(elt): ditto
2020-11-07 02:50:43 +00:00
GCC Administrator 44cab2d8fd Daily bump. 2020-11-07 00:16:39 +00:00
Jonathan Wakely 887515acd2 libstdc++: Fix symbol version conflict in linker script
The change in r11-4748-50b840ac5e1d6534e345c3fee9a97ae45ced6bc7 causes
a build error on Solaris, due to the new explicit instantiation matching
patterns for two different symbol versions.

libstdc++-v3/ChangeLog:

	* config/abi/pre/gnu.ver (GLIBCXX_3.4.21): Tighten up patterns
	for basic_stringbuf that refer to __xfer_bufptrs.
2020-11-06 20:14:03 +00:00
GCC Administrator 0cfd9109e5 Daily bump. 2020-11-06 00:16:34 +00:00
Marek Polacek 5b2003105b c++: Implement -Wvexing-parse [PR25814]
This patch implements the -Wvexing-parse warning to warn about the
sneaky most vexing parse rule in C++: the cases when a declaration
looks like a variable definition, but the C++ language requires it
to be interpreted as a function declaration.  This warning is on by
default (like clang++).  From the docs:

  void f(double a) {
    int i();        // extern int i (void);
    int n(int(a));  // extern int n (int);
  }

  Another example:

  struct S { S(int); };
  void f(double a) {
    S x(int(a));   // extern struct S x (int);
    S y(int());    // extern struct S y (int (*) (void));
    S z();         // extern struct S z (void);
  }

You can find more on this in [dcl.ambig.res].

I spent a fair amount of time on fix-it hints so that GCC can recommend
various ways to resolve such an ambiguity.  Sometimes that's tricky.
E.g., suggesting default-initialization when the class doesn't have
a default constructor would not be optimal.  Suggesting {}-init is also
not trivial because it can use an initializer-list constructor if no
default constructor is available (which ()-init wouldn't do).  And of
course, pre-C++11, we shouldn't be recommending {}-init at all.

I also uncovered a bug in cp_parser_declarator, where we were setting
*parenthesized_p to true despite the comment saying the exact opposite.

gcc/c-family/ChangeLog:

	PR c++/25814
	* c.opt (Wvexing-parse): New option.

gcc/cp/ChangeLog:

	PR c++/25814
	* cp-tree.h (enum cp_tree_index): Add CPTI_EXPLICIT_VOID_LIST.
	(explicit_void_list_node): Define.
	(PARENTHESIZED_LIST_P): New macro.
	(struct cp_declarator): Add function::parens_loc.
	* decl.c (cxx_init_decl_processing): Initialize explicit_void_list_node.
	(grokparms): Also break when explicit_void_list_node.
	* parser.c (make_call_declarator): New location_t parameter.  Use it
	to set declarator->u.function.parens_loc.
	(cp_parser_lambda_declarator_opt): Pass UNKNOWN_LOCATION to
	make_call_declarator.
	(warn_about_ambiguous_parse): New function.
	(cp_parser_init_declarator): Call warn_about_ambiguous_parse.
	(cp_parser_declarator): Set *parenthesized_p to false rather than to
	true.
	(cp_parser_direct_declarator): Create a location for the function's
	parentheses and pass it to make_call_declarator.
	(cp_parser_parameter_declaration_clause): Return explicit_void_list_node
	for (void).
	(cp_parser_parameter_declaration_list): Set PARENTHESIZED_LIST_P
	in the parameters tree.

gcc/ChangeLog:

	PR c++/25814
	* doc/invoke.texi: Document -Wvexing-parse.

gcc/testsuite/ChangeLog:

	PR c++/25814
	* g++.dg/cpp2a/fn-template16.C: Add a dg-warning.
	* g++.dg/cpp2a/fn-template7.C: Likewise.
	* g++.dg/lookup/pr80891-5.C: Likewise.
	* g++.dg/lto/pr79050_0.C: Add extern.
	* g++.dg/lto/pr84805_0.C: Likewise.
	* g++.dg/parse/pr58898.C: Add a dg-warning.
	* g++.dg/template/scope5.C: Likewise.
	* g++.old-deja/g++.brendan/recurse.C: Likewise.
	* g++.old-deja/g++.jason/template4.C: Likewise.
	* g++.old-deja/g++.law/arm4.C: Likewise.
	* g++.old-deja/g++.mike/for2.C: Likewise.
	* g++.old-deja/g++.other/local4.C: Likewise.
	* g++.old-deja/g++.pt/crash3.C: Likewise.
	* g++.dg/warn/Wvexing-parse.C: New test.
	* g++.dg/warn/Wvexing-parse2.C: New test.
	* g++.dg/warn/Wvexing-parse3.C: New test.
	* g++.dg/warn/Wvexing-parse4.C: New test.
	* g++.dg/warn/Wvexing-parse5.C: New test.
	* g++.dg/warn/Wvexing-parse6.C: New test.
	* g++.dg/warn/Wvexing-parse7.C: New test.

libstdc++-v3/ChangeLog:

	PR c++/25814
	* testsuite/20_util/reference_wrapper/lwg2993.cc: Add a dg-warning.
	* testsuite/25_algorithms/generate_n/87982_neg.cc: Likewise.
2020-11-05 15:55:14 -05:00
Jonathan Wakely d16d45655d libstdc++: Fix typo in ChangeLog entry 2020-11-05 19:35:34 +00:00
Jonathan Wakely cdd2d448d8 libstdc++: Fix constraints on std::optional comparisons [PR 96269]
The relational operators for std::optional were using the wrong types
in the declval expressions used to constrain them. Instead of using
const lvalues they were using non-const rvalues, which meant that a type
might satisfy the constraints but then give an error when the function
body was instantiated.

libstdc++-v3/ChangeLog:

	PR libstdc++/96269
	* include/std/optional (operator==, operator!=, operator<)
	(operator>, operator<=, operator>=): Fix types used in
	SFINAE constraints.
	* testsuite/20_util/optional/relops/96269.cc: New test.
2020-11-05 19:09:22 +00:00