From 7a48d0a53e35536ad36fe0c651783c81a91c61a3 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 29 Dec 2022 12:10:25 -0800 Subject: [PATCH] Add Unreachable.h This is to indicate when a code path is absolutely, for certain, 100% unreachable. Useful as the default case inside a case-switch that is for sure 100% exhaustive because it's inside the case of another case-switch (and the default case is there to suppress compiler warnings about the case-switch not being exhaustive), which is a situation coming up in my scriptclass::startgamemode refactor. It does this by deliberately invoking undefined behavior, either using a compiler builtin that does the same thing or being a noreturn function that returns. (And undefined behavior is not undefined behavior if it is not executed in a code path, otherwise all NULL checks would be useless because it'd dereference something that could be NULL in another code path.) --- desktop_version/src/Unreachable.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 desktop_version/src/Unreachable.h diff --git a/desktop_version/src/Unreachable.h b/desktop_version/src/Unreachable.h new file mode 100644 index 00000000..d37db95d --- /dev/null +++ b/desktop_version/src/Unreachable.h @@ -0,0 +1,17 @@ +#ifndef UNREACHABLE_H +#define UNREACHABLE_H + +#include + +SDL_NORETURN SDL_INLINE void VVV_unreachable(void) +{ + /* __builtin_unreachable() and __assume(0) execute undefined behavior. + * Otherwise, a noreturn function returning is also undefined behavior. */ +#if defined(__has_builtin) && __has_builtin(__builtin_unreachable) + __builtin_unreachable(); +#elif defined(_MSC_VER) + __assume(0); +#endif +} + +#endif /* UNREACHABLE_H */