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