From 2a0c243edc731d9e3009e11fa481a515118684d4 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Sat, 6 Jan 2024 23:39:36 -0300 Subject: [PATCH] utility: Add function to signal code is unreachable --- meson.build | 2 ++ substrate/internal/defs | 8 ++++++++ substrate/utility | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index b38fabb5..90a6ec00 100644 --- a/meson.build +++ b/meson.build @@ -60,6 +60,8 @@ extended_warnings = [ '-Wstack-protector', '-Wunsuffixed-float-constant', '-Wimplicit-fallthrough', + '-Werror=switch', + '-we4061' # enumerator 'identifier' in switch of enum 'enumeration' is not explicitly handled by a case label ] add_project_arguments( diff --git a/substrate/internal/defs b/substrate/internal/defs index 53fe64d6..79e677f3 100644 --- a/substrate/internal/defs +++ b/substrate/internal/defs @@ -137,5 +137,13 @@ #define SUBSTRATE_INTERFACE #endif +#if defined(_MSC_VER) && !defined(__clang__) +#define SUBSTRATE_ALWAYS_INLINE __forceinline +#elif defined(__has_attribute) && __has_attribute(always_inline) +#define SUBSTRATE_ALWAYS_INLINE inline __attribute__((always_inline)) +#else +#define SUBSTRATE_ALWAYS_INLINE inline +#endif + #endif /* SUBSTRATE_INTERNAL_DEFS */ /* vim: set ft=cpp ts=4 sw=4 noexpandtab: */ diff --git a/substrate/utility b/substrate/utility index b3adbe86..98f6666c 100644 --- a/substrate/utility +++ b/substrate/utility @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -784,7 +783,26 @@ namespace substrate template SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR auto data(const C &c)) -> decltype(c.data()) { return c.data(); } template SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR T *data(T (&array)[N]) noexcept) { return array; } template SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR const E *data(std::initializer_list il) noexcept) { return il.begin(); } +#endif + +#if defined(_MSC_VER) +# pragma warning(disable:4702) // abort is unreachable code +#endif +[[noreturn]] SUBSTRATE_ALWAYS_INLINE void unreachable() +{ +#if defined(_MSC_VER) && !defined(__clang__) + __assume(false); +#else + __builtin_unreachable(); #endif +#ifdef _DEBUG + abort(); +#endif +} +#if defined(_MSC_VER) +# pragma warning(default:4702) +#endif + } // namespace substrate #endif /* SUBSTRATE_UTILITIES */