diff --git a/libcxx/docs/Status/Cxx2bPapers.csv b/libcxx/docs/Status/Cxx2bPapers.csv --- a/libcxx/docs/Status/Cxx2bPapers.csv +++ b/libcxx/docs/Status/Cxx2bPapers.csv @@ -38,3 +38,5 @@ "`P2393R1 `__","LWG","Cleaning up ``integer``-class types","October 2021","","" "`P2401R0 `__","LWG","Add a conditional ``noexcept`` specification to ``std::exchange``","October 2021","|Complete|","14.0" "","","","","","" +"`P0627R6 `__","LWG","Function to mark unreachable code","February 2022","|Complete|"","15.0" +"","","","","","" diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -397,6 +397,7 @@ __utility/swap.h __utility/to_underlying.h __utility/transaction.h + __utility/unreachable.h __variant/monostate.h algorithm any diff --git a/libcxx/include/__filesystem/directory_entry.h b/libcxx/include/__filesystem/directory_entry.h --- a/libcxx/include/__filesystem/directory_entry.h +++ b/libcxx/include/__filesystem/directory_entry.h @@ -20,6 +20,7 @@ #include <__filesystem/operations.h> #include <__filesystem/path.h> #include <__filesystem/perms.h> +#include <__utility/unreachable.h> #include #include #include @@ -362,7 +363,7 @@ __ec->clear(); return __data_.__type_; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -383,7 +384,7 @@ return __data_.__type_; } } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -398,7 +399,7 @@ case _RefreshSymlink: return file_status(__get_ft(__ec), __data_.__non_sym_perms_); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -414,7 +415,7 @@ case _RefreshSymlinkUnresolved: return file_status(__get_sym_ft(__ec), __data_.__sym_perms_); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -439,7 +440,7 @@ return __data_.__size_; } } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -458,7 +459,7 @@ return __data_.__nlink_; } } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY @@ -481,7 +482,7 @@ return __data_.__write_time_; } } - _LIBCPP_UNREACHABLE(); + __unreachable(); } private: diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h --- a/libcxx/include/__format/format_arg.h +++ b/libcxx/include/__format/format_arg.h @@ -17,6 +17,7 @@ #include <__format/format_parse_context.h> #include <__functional_base> #include <__memory/addressof.h> +#include <__utility/unreachable.h> #include <__variant/monostate.h> #include #include @@ -81,7 +82,7 @@ #ifndef _LIBCPP_HAS_NO_INT128 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__i128); #else - _LIBCPP_UNREACHABLE(); + __unreachable(); #endif case __format::__arg_t::__unsigned: return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__unsigned); @@ -92,7 +93,7 @@ #ifndef _LIBCPP_HAS_NO_INT128 return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__u128); #else - _LIBCPP_UNREACHABLE(); + __unreachable(); #endif case __format::__arg_t::__float: return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__float); @@ -110,7 +111,7 @@ case __format::__arg_t::__handle: return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__handle); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } template diff --git a/libcxx/include/__format/formatter.h b/libcxx/include/__format/formatter.h --- a/libcxx/include/__format/formatter.h +++ b/libcxx/include/__format/formatter.h @@ -18,6 +18,7 @@ #include <__format/format_fwd.h> #include <__format/format_string.h> #include <__format/parser_std_format_spec.h> +#include <__utility/unreachable.h> #include #include @@ -120,7 +121,7 @@ size_t __fill = __width - __size; switch (__align) { case __format_spec::_Flags::_Alignment::__default: - _LIBCPP_UNREACHABLE(); + __unreachable(); case __format_spec::_Flags::_Alignment::__left: return {0, __fill}; @@ -136,7 +137,7 @@ case __format_spec::_Flags::_Alignment::__right: return {__fill, 0}; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } /** diff --git a/libcxx/include/__format/formatter_floating_point.h b/libcxx/include/__format/formatter_floating_point.h --- a/libcxx/include/__format/formatter_floating_point.h +++ b/libcxx/include/__format/formatter_floating_point.h @@ -27,6 +27,7 @@ #include <__format/formatter_integral.h> #include <__format/parser_std_format_spec.h> #include <__utility/move.h> +#include <__utility/unreachable.h> #include #include @@ -689,7 +690,7 @@ default: _LIBCPP_ASSERT(false, "The parser should have validated the type"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } } }; diff --git a/libcxx/include/__format/formatter_integral.h b/libcxx/include/__format/formatter_integral.h --- a/libcxx/include/__format/formatter_integral.h +++ b/libcxx/include/__format/formatter_integral.h @@ -19,6 +19,7 @@ #include <__format/format_fwd.h> #include <__format/formatter.h> #include <__format/parser_std_format_spec.h> +#include <__utility/unreachable.h> #include #include #include @@ -176,7 +177,7 @@ } } - _LIBCPP_UNREACHABLE(); + __unreachable(); } template @@ -292,7 +293,7 @@ } default: _LIBCPP_ASSERT(false, "The parser should have validated the type"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } } diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h --- a/libcxx/include/__iterator/advance.h +++ b/libcxx/include/__iterator/advance.h @@ -16,6 +16,7 @@ #include <__iterator/incrementable_traits.h> #include <__iterator/iterator_traits.h> #include <__utility/move.h> +#include <__utility/unreachable.h> #include #include #include @@ -181,7 +182,7 @@ return __n; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } }; diff --git a/libcxx/include/__utility/unreachable.h b/libcxx/include/__utility/unreachable.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__utility/unreachable.h @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___UTILITY_UNREACHABLE_H +#define _LIBCPP___UTILITY_UNREACHABLE_H + +#include <__config> + +_LIBCPP_BEGIN_NAMESPACE_STD + +_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI inline void __unreachable() +{ +#ifdef __GNUC__ + __builtin_unreachable(); +#else + std::abort(); +#endif +} + +#if _LIBCPP_STD_VER > 20 + +[[noreturn]] _LIBCPP_HIDE_FROM_ABI inline void unreachable() { __unreachable(); } + +#endif // _LIBCPP_STD_VER > 20 + +_LIBCPP_END_NAMESPACE_STD + +#endif diff --git a/libcxx/include/array b/libcxx/include/array --- a/libcxx/include/array +++ b/libcxx/include/array @@ -111,8 +111,8 @@ #include <__config> #include <__debug> #include <__tuple> +#include <__utility/unreachable.h> #include -#include // for _LIBCPP_UNREACHABLE #include #include #include @@ -309,54 +309,54 @@ _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference operator[](size_type) _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::operator[] on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference operator[](size_type) const _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::operator[] on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type) { __throw_out_of_range("array::at"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type) const { __throw_out_of_range("array::at"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::front() on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::front() on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::back() on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT { _LIBCPP_ASSERT(false, "cannot call array::back() on a zero-sized array"); - _LIBCPP_UNREACHABLE(); + __unreachable(); } }; -#if _LIBCPP_STD_VER >= 17 +#if _LIBCPP_STD_VER > 14 template::value...>::value> > diff --git a/libcxx/include/charconv b/libcxx/include/charconv --- a/libcxx/include/charconv +++ b/libcxx/include/charconv @@ -84,6 +84,7 @@ #include <__charconv/to_chars_result.h> #include <__config> #include <__errc> +#include <__utility/unreachable.h> #include // for log2f #include #include // for _LIBCPP_UNREACHABLE @@ -341,7 +342,7 @@ __r += 4; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } template diff --git a/libcxx/include/cstdlib b/libcxx/include/cstdlib --- a/libcxx/include/cstdlib +++ b/libcxx/include/cstdlib @@ -88,12 +88,6 @@ # pragma GCC system_header #endif -#ifdef __GNUC__ -#define _LIBCPP_UNREACHABLE() __builtin_unreachable() -#else -#define _LIBCPP_UNREACHABLE() _VSTD::abort() -#endif - _LIBCPP_BEGIN_NAMESPACE_STD using ::size_t _LIBCPP_USING_IF_EXISTS; diff --git a/libcxx/include/fstream b/libcxx/include/fstream --- a/libcxx/include/fstream +++ b/libcxx/include/fstream @@ -183,6 +183,7 @@ #include <__config> #include <__debug> #include <__locale> +#include <__utility/unreachable.h> #include #include #include @@ -538,7 +539,7 @@ default: return nullptr; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } template diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -953,6 +953,7 @@ module swap { private header "__utility/swap.h" } module to_underlying { private header "__utility/to_underlying.h" } module transaction { private header "__utility/transaction.h" } + module unreachable { private header "__utility/unreachable.h" } } } module valarray { diff --git a/libcxx/include/utility b/libcxx/include/utility --- a/libcxx/include/utility +++ b/libcxx/include/utility @@ -239,6 +239,7 @@ #include <__utility/swap.h> #include <__utility/to_underlying.h> #include <__utility/transaction.h> +#include <__utility/unreachable.h> #include #include #include diff --git a/libcxx/src/filesystem/filesystem_common.h b/libcxx/src/filesystem/filesystem_common.h --- a/libcxx/src/filesystem/filesystem_common.h +++ b/libcxx/src/filesystem/filesystem_common.h @@ -10,6 +10,7 @@ #define FILESYSTEM_COMMON_H #include "__config" +#include <__utility/unreachable.h> #include "array" #include "chrono" #include "climits" @@ -178,7 +179,7 @@ case 2: __throw_filesystem_error(what, *p1_, *p2_, ec); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 0) @@ -197,7 +198,7 @@ case 2: __throw_filesystem_error(what, *p1_, *p2_, ec); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp --- a/libcxx/src/filesystem/operations.cpp +++ b/libcxx/src/filesystem/operations.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include <__utility/unreachable.h> #include "filesystem" #include "array" #include "iterator" @@ -154,7 +155,7 @@ return makeState(PS_AtEnd); case PS_AtEnd: - _LIBCPP_UNREACHABLE(); + __unreachable(); } } @@ -202,7 +203,7 @@ return makeState(PS_InRootName, Path.data(), RStart + 1); case PS_InRootName: case PS_BeforeBegin: - _LIBCPP_UNREACHABLE(); + __unreachable(); } } @@ -224,7 +225,7 @@ case PS_InFilenames: return RawEntry; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } explicit operator bool() const noexcept { @@ -285,7 +286,7 @@ case PS_AtEnd: return getAfterBack(); } - _LIBCPP_UNREACHABLE(); + __unreachable(); } /// \brief Return a pointer to the first character in the currently lexed @@ -302,7 +303,7 @@ case PS_AtEnd: return &Path.back() + 1; } - _LIBCPP_UNREACHABLE(); + __unreachable(); } // Consume all consecutive separators. @@ -681,7 +682,7 @@ return detail::format_string("filesystem error: %s [" PATH_CSTR_FMT "] [" PATH_CSTR_FMT "]", derived_what, path1().c_str(), path2().c_str()); } - _LIBCPP_UNREACHABLE(); + __unreachable(); }(); } @@ -1188,7 +1189,7 @@ } else if (is_regular_file(st)) return static_cast(pst.st_size) == 0; - _LIBCPP_UNREACHABLE(); + __unreachable(); } static file_time_type __extract_last_write_time(const path& p, const StatT& st, @@ -1801,7 +1802,7 @@ break; } case PK_None: - _LIBCPP_UNREACHABLE(); + __unreachable(); } } // [fs.path.generic]p6.8: If the path is empty, add a dot. diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -12,6 +12,7 @@ #define _LCONV_C99 #endif +#include <__utility/unreachable.h> #include "algorithm" #include "clocale" #include "codecvt" @@ -4623,7 +4624,7 @@ return false; #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS - _LIBCPP_UNREACHABLE(); + __unreachable(); } diff --git a/libcxx/src/strstream.cpp b/libcxx/src/strstream.cpp --- a/libcxx/src/strstream.cpp +++ b/libcxx/src/strstream.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include <__utility/unreachable.h> #include "strstream" #include "algorithm" #include "climits" @@ -268,7 +269,7 @@ newoff = seekhigh - eback(); break; default: - _LIBCPP_UNREACHABLE(); + __unreachable(); } newoff += __off; if (0 <= newoff && newoff <= seekhigh - eback())