libstdc++: Implement std::unreachable() for C++23 (P0627R6)
authorJonathan Wakely <jwakely@redhat.com>
Fri, 1 Apr 2022 11:25:02 +0000 (12:25 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 1 Apr 2022 12:17:18 +0000 (13:17 +0100)
commitbabaabbcc8346758be0051b32272da18d54f5eea
tree1a63a98ad862f3fa05fd8e12882a37795f4db7ce
parent944da70a5d1cdc5bd4327b2d32420f57b6883985
libstdc++: Implement std::unreachable() for C++23 (P0627R6)

This defines std::unreachable as an assertion for debug mode, a trap
when _GLIBCXX_ASSERTIONS is defined, and __builtin_unreachable()
otherwise.

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

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

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

libstdc++-v3/ChangeLog:

* include/std/utility (unreachable): Define for C++23.
* include/std/version (__cpp_lib_unreachable): Define.
* src/c++11/debug.cc (__glibcxx_assert_fail): Check for valid
arguments. Handle only the function being given.
* testsuite/20_util/unreachable/1.cc: New test.
* testsuite/20_util/unreachable/version.cc: New test.
libstdc++-v3/include/std/utility
libstdc++-v3/include/std/version
libstdc++-v3/src/c++11/debug.cc
libstdc++-v3/testsuite/20_util/unreachable/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/unreachable/version.cc [new file with mode: 0644]