Skip to content

Commit

Permalink
[libc++] Implement P0627R6 (Function to mark unreachable code)
Browse files Browse the repository at this point in the history
Reviewed By: ldionne, Quuxplusone, #libc

Spies: arichardson, mstorsjo, libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D119152
  • Loading branch information
philnik777 committed Feb 14, 2022
1 parent b305de1 commit 2a8f9a5
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 76 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/FeatureTestMacroTable.rst
Expand Up @@ -350,6 +350,6 @@ Status
------------------------------------------------- -----------------
``__cpp_lib_to_underlying`` ``202102L``
------------------------------------------------- -----------------
``__cpp_lib_unreachable`` *unimplemented*
``__cpp_lib_unreachable`` ``202202L``
================================================= =================

2 changes: 2 additions & 0 deletions libcxx/docs/ReleaseNotes.rst
Expand Up @@ -38,6 +38,8 @@ What's New in Libc++ 15.0.0?
New Features
------------

- Implemented P0627R6 (Function to mark unreachable code)

API Changes
-----------

Expand Down
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2bPapers.csv
Expand Up @@ -40,7 +40,7 @@
"","","","","",""
"`P0323R12 <https://wg21.link/P0323R12>`__","LWG","``std::expected``","February 2022","",""
"`P0533R9 <https://wg21.link/P0533R9>`__","LWG","``constexpr`` for ``<cmath>`` and ``<cstdlib>``","February 2022","",""
"`P0627R6 <https://wg21.link/P0627R6>`__","LWG","Function to mark unreachable code","February 2022","",""
"`P0627R6 <https://wg21.link/P0627R6>`__","LWG","Function to mark unreachable code","February 2022","|Complete|","15.0"
"`P1206R7 <https://wg21.link/P1206R7>`__","LWG","``ranges::to``: A function to convert any range to a container","February 2022","",""
"`P1413R3 <https://wg21.link/P1413R3>`__","LWG","Deprecate ``std::aligned_storage`` and ``std::aligned_union``","February 2022","",""
"`P2255R3 <https://wg21.link/P2255R3>`__","LWG","A type trait to detect reference binding to temporary","February 2022","",""
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -401,6 +401,7 @@ set(files
__utility/swap.h
__utility/to_underlying.h
__utility/transaction.h
__utility/unreachable.h
__variant/monostate.h
algorithm
any
Expand Down
15 changes: 8 additions & 7 deletions libcxx/include/__filesystem/directory_entry.h
Expand Up @@ -20,6 +20,7 @@
#include <__filesystem/operations.h>
#include <__filesystem/path.h>
#include <__filesystem/perms.h>
#include <__utility/unreachable.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -362,7 +363,7 @@ class directory_entry {
__ec->clear();
return __data_.__type_;
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -383,7 +384,7 @@ class directory_entry {
return __data_.__type_;
}
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -398,7 +399,7 @@ class directory_entry {
case _RefreshSymlink:
return file_status(__get_ft(__ec), __data_.__non_sym_perms_);
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -414,7 +415,7 @@ class directory_entry {
case _RefreshSymlinkUnresolved:
return file_status(__get_sym_ft(__ec), __data_.__sym_perms_);
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -439,7 +440,7 @@ class directory_entry {
return __data_.__size_;
}
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -458,7 +459,7 @@ class directory_entry {
return __data_.__nlink_;
}
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY
Expand All @@ -481,7 +482,7 @@ class directory_entry {
return __data_.__write_time_;
}
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

private:
Expand Down
7 changes: 4 additions & 3 deletions libcxx/include/__format/format_arg.h
Expand Up @@ -16,6 +16,7 @@
#include <__format/format_fwd.h>
#include <__format/format_parse_context.h>
#include <__memory/addressof.h>
#include <__utility/unreachable.h>
#include <__variant/monostate.h>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -77,7 +78,7 @@ visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
#ifndef _LIBCPP_HAS_NO_INT128
return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__i128);
#else
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
#endif
case __format::__arg_t::__unsigned:
return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__unsigned);
Expand All @@ -88,7 +89,7 @@ visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
#ifndef _LIBCPP_HAS_NO_INT128
return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__u128);
#else
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
#endif
case __format::__arg_t::__float:
return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__float);
Expand All @@ -106,7 +107,7 @@ visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
case __format::__arg_t::__handle:
return _VSTD::invoke(_VSTD::forward<_Visitor>(__vis), __arg.__handle);
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

template <class _Context>
Expand Down
5 changes: 3 additions & 2 deletions libcxx/include/__format/formatter.h
Expand Up @@ -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 <string_view>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down Expand Up @@ -116,7 +117,7 @@ __padding_size(size_t __size, size_t __width,
size_t __fill = __width - __size;
switch (__align) {
case __format_spec::_Flags::_Alignment::__default:
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();

case __format_spec::_Flags::_Alignment::__left:
return {0, __fill};
Expand All @@ -132,7 +133,7 @@ __padding_size(size_t __size, size_t __width,
case __format_spec::_Flags::_Alignment::__right:
return {__fill, 0};
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion libcxx/include/__format/formatter_floating_point.h
Expand Up @@ -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 <charconv>
#include <cmath>

Expand Down Expand Up @@ -689,7 +690,7 @@ class _LIBCPP_TEMPLATE_VIS __formatter_floating_point : public __parser_floating

default:
_LIBCPP_ASSERT(false, "The parser should have validated the type");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions libcxx/include/__format/formatter_integral.h
Expand Up @@ -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 <array>
#include <charconv>
#include <concepts>
Expand Down Expand Up @@ -176,7 +177,7 @@ __determine_grouping(ptrdiff_t __size, const string& __grouping) {
}
}

_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

template <class _Parser>
Expand Down Expand Up @@ -292,7 +293,7 @@ class _LIBCPP_TEMPLATE_VIS __formatter_integral : public _Parser {
}
default:
_LIBCPP_ASSERT(false, "The parser should have validated the type");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}
}

Expand Down
3 changes: 2 additions & 1 deletion libcxx/include/__iterator/advance.h
Expand Up @@ -16,6 +16,7 @@
#include <__iterator/incrementable_traits.h>
#include <__iterator/iterator_traits.h>
#include <__utility/move.h>
#include <__utility/unreachable.h>
#include <concepts>
#include <cstdlib>
#include <limits>
Expand Down Expand Up @@ -181,7 +182,7 @@ struct __fn {
return __n;
}

_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}
};

Expand Down
38 changes: 38 additions & 0 deletions libcxx/include/__utility/unreachable.h
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// 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>
#include <cstdlib>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI inline void __libcpp_unreachable()
{
#if __has_builtin(__builtin_unreachable)
__builtin_unreachable();
#else
std::abort();
#endif
}

#if _LIBCPP_STD_VER > 20

[[noreturn]] _LIBCPP_HIDE_FROM_ABI inline void unreachable() { __libcpp_unreachable(); }

#endif // _LIBCPP_STD_VER > 20

_LIBCPP_END_NAMESPACE_STD

#endif
20 changes: 10 additions & 10 deletions libcxx/include/array
Expand Up @@ -111,8 +111,8 @@ template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexce
#include <__config>
#include <__debug>
#include <__tuple>
#include <__utility/unreachable.h>
#include <algorithm>
#include <cstdlib> // for _LIBCPP_UNREACHABLE
#include <iterator>
#include <stdexcept>
#include <type_traits>
Expand Down Expand Up @@ -309,54 +309,54 @@ struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference operator[](size_type) _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference operator[](size_type) const _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference at(size_type) {
__throw_out_of_range("array<T, 0>::at");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference at(size_type) const {
__throw_out_of_range("array<T, 0>::at");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference front() _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference front() const _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
reference back() _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const_reference back() const _NOEXCEPT {
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}
};


#if _LIBCPP_STD_VER >= 17
#if _LIBCPP_STD_VER > 14
template<class _Tp, class... _Args,
class = enable_if_t<__all<_IsSame<_Tp, _Args>::value...>::value>
>
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/charconv
Expand Up @@ -83,16 +83,16 @@ namespace std {
#include <__charconv/from_chars_result.h>
#include <__charconv/to_chars_result.h>
#include <__config>
#include <__debug>
#include <__errc>
#include <__utility/unreachable.h>
#include <cmath> // for log2f
#include <cstdint>
#include <cstdlib> // for _LIBCPP_UNREACHABLE
#include <cstdlib>
#include <cstring>
#include <limits>
#include <type_traits>

#include <__debug>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
Expand Down Expand Up @@ -341,7 +341,7 @@ _LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_INLINE_VISIBILITY int __to_chars_integral_
__r += 4;
}

_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

template <typename _Tp>
Expand Down
6 changes: 0 additions & 6 deletions libcxx/include/cstdlib
Expand Up @@ -88,12 +88,6 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
# 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;
Expand Down
3 changes: 2 additions & 1 deletion libcxx/include/fstream
Expand Up @@ -183,6 +183,7 @@ typedef basic_fstream<wchar_t> wfstream;
#include <__config>
#include <__debug>
#include <__locale>
#include <__utility/unreachable.h>
#include <cstdio>
#include <cstdlib>
#include <istream>
Expand Down Expand Up @@ -538,7 +539,7 @@ const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
default:
return nullptr;
}
_LIBCPP_UNREACHABLE();
__libcpp_unreachable();
}

template <class _CharT, class _Traits>
Expand Down

0 comments on commit 2a8f9a5

Please sign in to comment.