compiler: Add QEMU_BUILD_BUG_MSG() macro

_Static_assert() allows us to specify messages, and that may come in
handy. Even without _Static_assert(), encouraging developers to put a
helpful message next to the QEMU_BUILD_BUG_* may make debugging easier
whenever it breaks.

Backports commit 9139b5672360aaa263da1d96cdfdbe16accb6e3b from qemu
This commit is contained in:
Max Reitz 2018-03-20 10:43:29 -04:00 committed by Lioncash
parent 4f6ba5a2dc
commit 6acf9baa68
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -72,15 +72,21 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}};
int:(x) ? -1 : 1; \
}
/* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is
* supported; otherwise, it will be omitted from the compiler error
* message (but as it remains present in the source code, it can still
* be useful when debugging). */
#if defined(CONFIG_STATIC_ASSERT)
#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x)
#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
#elif defined(__COUNTER__)
#define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
#define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
glue(qemu_build_bug_on__, __COUNTER__) QEMU_UNUSED_VAR
#else
#define QEMU_BUILD_BUG_ON(x)
#define QEMU_BUILD_BUG_MSG(x, msg)
#endif
#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
@ -190,15 +196,21 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}};
int:(x) ? -1 : 1; \
}
/* QEMU_BUILD_BUG_MSG() emits the message given if _Static_assert is
* supported; otherwise, it will be omitted from the compiler error
* message (but as it remains present in the source code, it can still
* be useful when debugging). */
#if defined(CONFIG_STATIC_ASSERT)
#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x)
#define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
#elif defined(__COUNTER__)
#define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
#define QEMU_BUILD_BUG_MSG(x, msg) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \
glue(qemu_build_bug_on__, __COUNTER__) QEMU_UNUSED_VAR
#else
#define QEMU_BUILD_BUG_ON(x)
#define QEMU_BUILD_BUG_MSG(x, msg)
#endif
#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))