util: avoid calling UNREACHABLE(str) macro without arguments

After commit 2dcd6bed6a ("util: enforce unreachable()'s argument being
a literal string", 2023-04-17) the compiler effectively emit errors when
an argument that is not a string literal is passed to UNREACHABLE(str),
however the compiler still allows the macro to be called without
arguments, which can be confusing.

Implement the type check outside of the assert() call so that we have
two types of errors:

1. The compiler will error out when the argument passed to the macro is
   not a string literal because the concatenation with an empty string
   will not be allowed.

2. The compiler will error out when no arguments are passed to the macro
   because the invocation of assert() will be invalid.

This also has the nice side-effect of removing the extra empty string
printed in the assert() messages; after the changes the messages will
look like:

  Assertion `!"Invalid type"' failed.

instead of:

  Assertion `!"" "Invalid type"' failed.

Fixes: 2dcd6bed6a ("util: enforce unreachable()'s argument being a literal string", 2023-04-17)

Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
This commit is contained in:
Antonio Ospite 2025-07-30 10:33:33 +02:00 committed by Marge Bot
parent ddf2aa3a4d
commit 9ced3148ca

View file

@ -122,17 +122,23 @@
#if defined(HAVE___BUILTIN_UNREACHABLE) || __has_builtin(__builtin_unreachable)
#define UNREACHABLE(str) \
do { \
assert(!"" str); \
(void)"" str; /* str must be a string literal */ \
assert(!str); \
__builtin_unreachable(); \
} while (0)
#elif defined (_MSC_VER)
#define UNREACHABLE(str) \
do { \
assert(!"" str); \
(void)"" str; /* str must be a string literal */ \
assert(!str); \
__assume(0); \
} while (0)
#else
#define UNREACHABLE(str) assert(!"" str)
#define UNREACHABLE(str) \
do { \
(void)"" str; /* str must be a string literal */ \
assert(!str); \
} while (0)
#endif
/**