openMSX
unreachable.hh
Go to the documentation of this file.
1#ifndef UNREACHABLE_HH
2#define UNREACHABLE_HH
3
4// TODO use c++23 std::unreachable()
5
6// Clang has a very convenient way of testing features, unfortunately (for now)
7// it's clang-only so add a fallback for non-clang compilers.
8#ifndef __has_builtin
9 #define __has_builtin(x) 0
10#endif
11
12#if defined(NDEBUG)
13 // clang
14 #if __has_builtin(__builtin_unreachable)
15 #define UNREACHABLE __builtin_unreachable()
16
17 // __builtin_unreachable() was introduced in gcc-4.5, but 4.5 and 4.6 contain
18 // bugs on architectures with delay slots (e.g. MIPS). Look at the git
19 // history of this file for more details.
20 #elif ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)))
21 // gcc-4.7 or above
22 #define UNREACHABLE __builtin_unreachable()
23
24 #elif defined(_MSC_VER)
25 // visual studio
26 #define UNREACHABLE __assume(0)
27
28 #else
29 // fall-back
30 #define UNREACHABLE /*nothing*/
31
32 #endif
33
34#else
35 // asserts enabled
36 // One some platforms, like MinGW, the compiler cannot determine that
37 // assert(false) will never return, so we help it by wrapping the assert
38 // in an infinite loop.
39 #include <cassert>
40 #define UNREACHABLE while (1) assert(false)
41
42#endif
43
44#endif // UNREACHABLE_HH