diff --git a/core/include/attributes.h b/core/include/attributes.h index e0ae7fbd7a..b1efec6dbf 100644 --- a/core/include/attributes.h +++ b/core/include/attributes.h @@ -56,6 +56,18 @@ #define PURE #endif +/** + * @def UNREACHABLE() + * @brief Tell the compiler that this line of code cannot be reached. + * @details Most useful in junction with #NORETURN. + * Use this if the compiler cannot tell that e.g. + * an assembler instruction causes a longjmp, or a write causes a reboot. + */ +#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) +#define UNREACHABLE() __builtin_unreachable() +#else +#define UNREACHABLE() do { /* nothing */ } while (1) +#endif + #endif /* ATTRIBUTES_H_ */ /** @} */ - diff --git a/cpu/arm_common/crash.c b/cpu/arm_common/crash.c index 366e3f8393..70671cd297 100644 --- a/cpu/arm_common/crash.c +++ b/cpu/arm_common/crash.c @@ -63,11 +63,5 @@ NORETURN void core_panic(int crash_code, const char *message) /* tell the compiler that we won't return from this function (even if we actually won't even get here...) */ -#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) - __builtin_unreachable(); -#else - while (1) { - /* do nothing, but do it often */ - } -#endif + UNREACHABLE(); } diff --git a/cpu/lpc1768/crash.c b/cpu/lpc1768/crash.c index 9c26664b45..9a449d9e97 100644 --- a/cpu/lpc1768/crash.c +++ b/cpu/lpc1768/crash.c @@ -64,11 +64,5 @@ NORETURN void core_panic(int crash_code, const char *message) /* tell the compiler that we won't return from this function (even if we actually won't even get here...) */ -#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) - __builtin_unreachable(); -#else - while (1) { - /* do nothing, but do it often */ - } -#endif + UNREACHABLE(); } diff --git a/cpu/msp430-common/crash.c b/cpu/msp430-common/crash.c index 365dd91ad6..3a45e62331 100644 --- a/cpu/msp430-common/crash.c +++ b/cpu/msp430-common/crash.c @@ -63,11 +63,5 @@ NORETURN void core_panic(int crash_code, const char *message) /* tell the compiler that we won't return from this function (even if we actually won't even get here...) */ -#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) - __builtin_unreachable(); -#else - while (1) { - /* do nothing, but do it often */ - } -#endif + UNREACHABLE(); } diff --git a/cpu/native/crash.c b/cpu/native/crash.c index dfcd6be18a..b6f0532352 100644 --- a/cpu/native/crash.c +++ b/cpu/native/crash.c @@ -60,11 +60,5 @@ NORETURN void core_panic(int crash_code, const char *message) /* tell the compiler that we won't return from this function (even if we actually won't even get here...) */ -#if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5) - __builtin_unreachable(); -#else - while (1) { - /* do nothing, but do it often */ - } -#endif + UNREACHABLE(); }