1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-25 22:18:30 +02:00

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.)
This commit is contained in:
Misa 2022-12-29 12:10:25 -08:00
parent c177f456f4
commit 7a48d0a53e

View File

@ -0,0 +1,17 @@
#ifndef UNREACHABLE_H
#define UNREACHABLE_H
#include <SDL_stdinc.h>
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 */