From ce37198bcbc6e9cdec268e8279c95520f902cbbc Mon Sep 17 00:00:00 2001 From: Yong Gyu Lee Date: Tue, 7 Jan 2025 17:14:15 +0900 Subject: [PATCH] Implement unreachable (#69) * Implement unreachable * Fix test --- README.md | 24 ++++----- include/preview/__core/noreturn.h | 10 ++++ include/preview/__utility/unreachable.h | 65 +++++++++++++++++++++++++ include/preview/core.h | 1 + include/preview/utility.h | 1 + test/CMakeLists.txt | 1 - test/utility.cc | 8 +++ 7 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 include/preview/__core/noreturn.h create mode 100644 include/preview/__utility/unreachable.h diff --git a/README.md b/README.md index 62f7f1c1..5c844054 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Implementation available in C++14 ~ : | Header | Total | | C++17 | C++20 | C++23 | C++26 | |-------------------------------------|---------------------------------------------------------|---|--------------------------------------------------------|---------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------| -| | ![](https://img.shields.io/badge/314/694-grey)![][p045] | | ![](https://img.shields.io/badge/44/113-grey)![][p039] | ![](https://img.shields.io/badge/233/429-grey)![][p054] | ![](https://img.shields.io/badge/59/144-grey)![][p041] | ![](https://img.shields.io/badge/18/135-grey)![][p013] | +| | ![](https://img.shields.io/badge/315/694-grey)![][p045] | | ![](https://img.shields.io/badge/44/113-grey)![][p039] | ![](https://img.shields.io/badge/233/429-grey)![][p054] | ![](https://img.shields.io/badge/60/144-grey)![][p042] | ![](https://img.shields.io/badge/18/135-grey)![][p013] | | | | | | | | | | [algorithm](#algorithm) | ![](https://img.shields.io/badge/55/115-grey)![][p048] | | ![](https://img.shields.io/badge/2/4-grey)![][p050] | ![](https://img.shields.io/badge/47/96-grey)![][p049] | ![](https://img.shields.io/badge/8/18-grey)![][p044] | ![](https://img.shields.io/badge/7/23-grey)![][p030] | | [any](#any) | ![](https://img.shields.io/badge/5/5-grey)![][p100] | | ![](https://img.shields.io/badge/5/5-grey)![][p100] | | | | @@ -129,7 +129,7 @@ Implementation available in C++14 ~ : | [string_view](#string_view) | ![](https://img.shields.io/badge/7/7-grey)![][p100] | | ![](https://img.shields.io/badge/4/4-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | ![](https://img.shields.io/badge/3/3-grey)![][p100] * | | [tuple](#tuple) | ![](https://img.shields.io/badge/7/8-grey)![][p088] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/5/6-grey)![][p083] | | [type_traits](#type_traits) | ![](https://img.shields.io/badge/17/26-grey)![][p065] | | ![](https://img.shields.io/badge/8/10-grey)![][p080] | ![](https://img.shields.io/badge/7/13-grey)![][p054] | ![](https://img.shields.io/badge/2/6-grey)![][p033] | ![](https://img.shields.io/badge/0/2-grey)![][p000] | -| [utility](#utility) | ![](https://img.shields.io/badge/7/8-grey)![][p088] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/3-grey)![][p067] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | +| [utility](#utility) | ![](https://img.shields.io/badge/8/8-grey)![][p100] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/3/3-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | | [variant](#variant) | ![](https://img.shields.io/badge/9/9-grey)![][p100] | | ![](https://img.shields.io/badge/9/9-grey)![][p100] | | | ![](https://img.shields.io/badge/1/1-grey)![][p100] | | | | | | | | | | [atomic](#atomic) | ![](https://img.shields.io/badge/0/17-grey)![][p000] | | | ![](https://img.shields.io/badge/0/13-grey)![][p000] | | ![](https://img.shields.io/badge/0/4-grey)![][p000] | @@ -1230,16 +1230,16 @@ Description #### `` - | | Introduced | Revision | - |-----------------------------|----------------|----------| - | `as_const` | ![][c17ok] | | - | `in_place`
`in_place_t` | ![][c17ok] | | - | `cmp_equal`... | ![][c20ok] | | - | `in_range` | ![][c20ok] | | - | `forward_like` | ![][c23ok] | | - | `to_underlying` | ![][c23ok] | | - | `unreachable` | ![][c23no] N/A | | - | `nontype`
`nontype_t` | ![][c26ok] | | + | | Introduced | Revision | + |-----------------------------|------------|----------| + | `as_const` | ![][c17ok] | | + | `in_place`
`in_place_t` | ![][c17ok] | | + | `cmp_equal`... | ![][c20ok] | | + | `in_range` | ![][c20ok] | | + | `forward_like` | ![][c23ok] | | + | `to_underlying` | ![][c23ok] | | + | `unreachable` | ![][c23ok] | | + | `nontype`
`nontype_t` | ![][c26ok] | | #### `` diff --git a/include/preview/__core/noreturn.h b/include/preview/__core/noreturn.h new file mode 100644 index 00000000..565f6fdb --- /dev/null +++ b/include/preview/__core/noreturn.h @@ -0,0 +1,10 @@ +// +// Created by yonggyulee on 06/01/2025 +// + +#ifndef PREVIEW_CORE_NORETURN_H_ +#define PREVIEW_CORE_NORETURN_H_ + +#define PREVIEW_NORETURN [[noreturn]] + +#endif // PREVIEW_CORE_NORETURN_H_ diff --git a/include/preview/__utility/unreachable.h b/include/preview/__utility/unreachable.h new file mode 100644 index 00000000..98c68256 --- /dev/null +++ b/include/preview/__utility/unreachable.h @@ -0,0 +1,65 @@ +// +// Created by yonggyulee on 18/11/2024 +// + +#ifndef PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_ +#define PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_ + +#include "preview/__core/std_version.h" + +#if PREVIEW_CXX_VERSION >= 23 + +#include + +namespace preview { +using std::unreachable; +} // namespace preview + +#else + +#include "preview/__core/noreturn.h" + +# if defined(_MSC_VER) + +namespace preview { +PREVIEW_NORETURN inline void unreachable() { + __assume(0); +} +} // namespace preview + +# else + +#include +#include + +#include "preview/__type_traits/void_t.h" + + +namespace preview { +namespace detail_unreachable { + +template +struct adl_unreachable : std::false_type {}; +template<> +struct adl_unreachable> : std::true_type {}; + +PREVIEW_NORETURN [[maybe_unused]] inline void unreachable_impl(std::true_type) { + __builtin_unreachable(); +} + +PREVIEW_NORETURN [[maybe_unused]] inline void unreachable_impl(std::false_type) { + abort(); +} + +} // namespace detail_unreachable + +PREVIEW_NORETURN inline void unreachable() { + preview::detail_unreachable::unreachable_impl(detail_unreachable::adl_unreachable<>{}); +} + +} // namespace preview + +# endif // defined(_MSC_VER) +#endif // PREVIEW_CXX_VERSION >= 23 + +#endif // PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_ diff --git a/include/preview/core.h b/include/preview/core.h index 7398bf0b..51e19252 100644 --- a/include/preview/core.h +++ b/include/preview/core.h @@ -8,6 +8,7 @@ #include "preview/__core/inline_variable.h" #include "preview/__core/no_unique_address.h" #include "preview/__core/nodiscard.h" +#include "preview/__core/noreturn.h" #include "preview/__core/std_version.h" #endif // PREVIEW_CORE_H_ diff --git a/include/preview/utility.h b/include/preview/utility.h index cc7fe0a2..4dbac279 100644 --- a/include/preview/utility.h +++ b/include/preview/utility.h @@ -20,5 +20,6 @@ #include "preview/__utility/to_underlying.h" #include "preview/__utility/to_unsigned_like.h" #include "preview/__utility/type_sequence.h" +#include "preview/__utility/unreachable.h" #endif // PREVIEW_UTILITY_H_ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1ed61d9d..1aef7426 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -153,7 +153,6 @@ function(preview_add_multi_version_test name_prefix test_src) target_compile_options(${target_name} PUBLIC "-fexceptions" ) - gtest_discover_tests(${target_name} DISCOVERY_TIMEOUT 60) elseif (MSVC) target_compile_options(${target_name} PRIVATE "/bigobj" diff --git a/test/utility.cc b/test/utility.cc index 899eca0f..ac7153c3 100644 --- a/test/utility.cc +++ b/test/utility.cc @@ -101,3 +101,11 @@ TEST(VERSIONED(Utility), force_forward_like) { EXPECT_EQ(preview::force_forward_like(cref_my_state.from_opt())(), kTypeLvalueReference); EXPECT_EQ(preview::force_forward_like(cref_my_state[0])(), kTypeLvalueReference); } + +#ifdef GTEST_HAS_DEATH_TEST +TEST(VERSIONED(Utility), unreachable) { +//#ifndef NDEBUG +// EXPECT_DEATH(preview::unreachable(), ""); +//#endif +} +#endif