From 4f6ba5a2dcfbf8018e0511fdc383a65645b9bfda Mon Sep 17 00:00:00 2001 From: Andreas Grapentin Date: Tue, 20 Mar 2018 10:41:11 -0400 Subject: [PATCH] use _Static_assert in QEMU_BUILD_BUG_ON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QEMU_BUILD_BUG_ON should use C11's _Static_assert, if the compiler supports it, to provide more readable messages on failure. We check for _Static_assert in configure, and set CONFIG_STATIC_ASSERT accordingly. QEMU_BUILD_BUG_ON invokes _Static_assert if CONFIG_STATIC_ASSERT is defined, and reverts to the old way otherwise. That way, systems without C11 conforming compiler will still have the old messages, as verified by intentionally breaking the configure check. the following example output was generated by inverting the condition in QEMU_BUILD_BUG_ON: without _Static_assert: > In file included from /qemu/include/qemu/osdep.h:36:0, > from /qemu/qga/commands.c:13: > /qemu/qga/commands.c: In function ‘qmp_guest_exec_status’: > /qemu/include/qemu/compiler.h:89:12: error: negative width in bit-field ‘’ > struct { \ > ^ > /qemu/include/qemu/compiler.h:96:38: note: in expansion of macro QEMU_BUILD_BUG_ON_STRUCT’ > #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ > ^~~~~~~~~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:146:5: note: in expansion of macro ‘QEMU_BUILD_BUG_ON’ > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro ‘atomic_load_acquire’ > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro ‘atomic_mb_read’ > bool finished = atomic_mb_read(&gei->finished); > ^~~~~~~~~~~~~~ with _Static_assert: > In file included from /qemu/include/qemu/osdep.h:36:0, > from /qemu/qga/commands.c:13: > /qemu/qga/commands.c: In function ‘qmp_guest_exec_status’: > /qemu/include/qemu/compiler.h:94:30: error: static assertion failed: "not expecting: sizeof(*&gei->finished) > sizeof(void *)" > #define QEMU_BUILD_BUG_ON(x) _Static_assert((x), #x) > ^ > /qemu/include/qemu/atomic.h:146:5: note: in expansion of macro ‘QEMU_BUILD_BUG_ON’ > QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *)); \ > ^~~~~~~~~~~~~~~~~ > /qemu/include/qemu/atomic.h:417:5: note: in expansion of macro ‘atomic_load_acquire’ > atomic_load_acquire(ptr) > ^~~~~~~~~~~~~~~~~~~ > /qemu/qga/commands.c:160:21: note: in expansion of macro ‘atomic_mb_read’ > bool finished = atomic_mb_read(&gei->finished); > ^~~~~~~~~~~~~~ Backports commit 49e00a18708e27c815828d9440d5c9300d19547c from qemu --- qemu/configure | 17 +++++++++++++++++ qemu/include/qemu/compiler.h | 8 ++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/qemu/configure b/qemu/configure index c04e4071..57aff38a 100755 --- a/qemu/configure +++ b/qemu/configure @@ -1308,6 +1308,19 @@ if compile_prog "" "" ; then posix_memalign=yes fi +# check for _Static_assert() + +have_static_assert=no +cat > $TMPC << EOF +_Static_assert(1, "success"); +int main(void) { + return 0; +} +EOF +if compile_prog "" "" ; then + have_static_assert=yes +fi + ########################################## # End of CC checks # After here, no more $cc or $ld runs @@ -1488,6 +1501,10 @@ if test "$vector16" = "yes" ; then echo "CONFIG_VECTOR16=y" >> $config_host_mak fi +if test "$have_static_assert" = "yes" ; then + echo "CONFIG_STATIC_ASSERT=y" >> $config_host_mak +fi + if test "$tcg_interpreter" = "yes"; then QEMU_INCLUDES="-I\$(SRC_PATH)/tcg/tci $QEMU_INCLUDES" elif test "$ARCH" = "sparc64" ; then diff --git a/qemu/include/qemu/compiler.h b/qemu/include/qemu/compiler.h index 8b365e86..926854c7 100644 --- a/qemu/include/qemu/compiler.h +++ b/qemu/include/qemu/compiler.h @@ -72,7 +72,9 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}}; int:(x) ? -1 : 1; \ } -#ifdef __COUNTER__ +#if defined(CONFIG_STATIC_ASSERT) +#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x) +#elif defined(__COUNTER__) #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ glue(qemu_build_bug_on__, __COUNTER__) QEMU_UNUSED_VAR #else @@ -188,7 +190,9 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}}; int:(x) ? -1 : 1; \ } -#ifdef __COUNTER__ +#if defined(CONFIG_STATIC_ASSERT) +#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), "not expecting: " #x) +#elif defined(__COUNTER__) #define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ glue(qemu_build_bug_on__, __COUNTER__) QEMU_UNUSED_VAR #else