[common] debug: add DEBUG_UNREACHABLE() macro

This lets us mark code as unreachable and signals the compiler that this
is the case with __builtin_unreachable().

We also mark DEBUG_FATAL as unreachable.
This commit is contained in:
Quantum 2021-08-13 19:40:43 -04:00 committed by Geoffrey McRae
parent be1306f91a
commit a9241f6710

View File

@ -57,6 +57,14 @@ void printBacktrace(void);
#define DIRECTORY_SEPARATOR '/' #define DIRECTORY_SEPARATOR '/'
#endif #endif
#ifdef __GNUC__
#define DEBUG_UNREACHABLE_MARKER() __builtin_unreachable()
#elif __defined__(_MSC_VER)
#define DEBUG_UNREACHABLE_MARKER() __assume(0)
#else
#define DEBUG_UNREACHABLE_MARKER()
#endif
#define STRIPPATH(s) ( \ #define STRIPPATH(s) ( \
sizeof(s) > 2 && (s)[sizeof(s)- 3] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 2 : \ sizeof(s) > 2 && (s)[sizeof(s)- 3] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 2 : \
sizeof(s) > 3 && (s)[sizeof(s)- 4] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 3 : \ sizeof(s) > 3 && (s)[sizeof(s)- 4] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 3 : \
@ -95,6 +103,7 @@ void printBacktrace(void);
DEBUG_PRINT(DEBUG_LEVEL_FATAL, fmt, ##__VA_ARGS__); \ DEBUG_PRINT(DEBUG_LEVEL_FATAL, fmt, ##__VA_ARGS__); \
DEBUG_PRINT_BACKTRACE(); \ DEBUG_PRINT_BACKTRACE(); \
abort(); \ abort(); \
DEBUG_UNREACHABLE_MARKER(); \
} while(0) } while(0)
#define DEBUG_ASSERT_PRINT(...) DEBUG_ERROR("Assertion failed: %s", #__VA_ARGS__) #define DEBUG_ASSERT_PRINT(...) DEBUG_ERROR("Assertion failed: %s", #__VA_ARGS__)
@ -114,6 +123,8 @@ void printBacktrace(void);
} while (0) } while (0)
#endif #endif
#define DEBUG_UNREACHABLE() DEBUG_FATAL("Unreachable code reached")
#if defined(DEBUG_SPICE) | defined(DEBUG_IVSHMEM) #if defined(DEBUG_SPICE) | defined(DEBUG_IVSHMEM)
#define DEBUG_PROTO(fmt, args...) DEBUG_PRINT("[P]", fmt, ##args) #define DEBUG_PROTO(fmt, args...) DEBUG_PRINT("[P]", fmt, ##args)
#else #else