gtestutils: Call __builtin_undefined() from g_assert_not_reached()

Both GCC and Clang treat this as a hint that the code won’t be reached,
which helps in the cases where they might not have automatically
detected it already.

It doesn’t change any behaviour of the compiled code, other than
allowing the compiler to go off into undefined behaviour.

See
https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-03-05 11:00:12 +00:00
parent 57a806b762
commit 8f2365dc8a

View File

@ -149,7 +149,14 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
#endif
#ifdef G_DISABLE_ASSERT
/* https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
* GCC 5 is not a strict lower bound for versions of GCC which provide __builtin_unreachable(). */
#if __GNUC__ >= 5 || g_macro__has_builtin(__builtin_unreachable)
#define g_assert_not_reached() G_STMT_START { (void) 0; __builtin_unreachable (); } G_STMT_END
#else /* if __builtin_unreachable() is not supported: */
#define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
#endif
#define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
#else /* !G_DISABLE_ASSERT */
#define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END