Skip to content

Commit

Permalink
utility: Add function to signal code is unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark committed Jan 7, 2024
1 parent 2e09985 commit 2a0c243
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions substrate/internal/defs
Original file line number Diff line number Diff line change
Expand Up @@ -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: */
20 changes: 19 additions & 1 deletion substrate/utility
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <array>
#include <memory>
#include <string>
#include <limits>
#include <vector>
#include <chrono>
#include <utility>
Expand Down Expand Up @@ -784,7 +783,26 @@ namespace substrate
template<typename C> SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR auto data(const C &c)) -> decltype(c.data()) { return c.data(); }
template<typename T, std::size_t N> SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR T *data(T (&array)[N]) noexcept) { return array; }
template<typename E> SUBSTRATE_NO_DISCARD(inline SUBSTRATE_CXX14_CONSTEXPR const E *data(std::initializer_list<E> 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 */
Expand Down

0 comments on commit 2a0c243

Please sign in to comment.