[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-trivial] [PULL 12/23] use _Static_assert in QEMU_BUILD_BUG_ON
From: |
Michael Tokarev |
Subject: |
[Qemu-trivial] [PULL 12/23] use _Static_assert in QEMU_BUILD_BUG_ON |
Date: |
Sun, 7 May 2017 10:02:15 +0300 |
From: Andreas Grapentin <address@hidden>
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
> ‘<anonymous>’
> 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);
> ^~~~~~~~~~~~~~
Signed-off-by: Andreas Grapentin <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Reviewed-by: Richard Henderson <address@hidden>
Signed-off-by: Michael Tokarev <address@hidden>
---
configure | 18 ++++++++++++++++++
include/qemu/compiler.h | 4 +++-
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 48a9370cc6..39aadba813 100755
--- a/configure
+++ b/configure
@@ -4853,6 +4853,20 @@ EOF
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
@@ -5848,6 +5862,10 @@ if test "$have_sysmacros" = "yes" ; then
echo "CONFIG_SYSMACROS=y" >> $config_host_mak
fi
+if test "$have_static_assert" = "yes" ; then
+ echo "CONFIG_STATIC_ASSERT=y" >> $config_host_mak
+fi
+
# Hold two types of flag:
# CONFIG_THREAD_SETNAME_BYTHREAD - we've got a way of setting the name on
# a thread we have a handle to
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index 18e610083a..340e5fdc09 100644
--- a/include/qemu/compiler.h
+++ b/include/qemu/compiler.h
@@ -82,7 +82,9 @@
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__) __attribute__((unused))
#else
--
2.11.0
- [Qemu-trivial] [PULL 00/23] Trivial patches for 2017-05-07, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 08/23] qga: fix compiler warnings (clang 5), Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 12/23] use _Static_assert in QEMU_BUILD_BUG_ON,
Michael Tokarev <=
- [Qemu-trivial] [PULL 11/23] channel-file: fix wrong parameter comments, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 01/23] Remove reduntant qemu: from error functions, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 09/23] util: Use g_malloc/g_free in envlist.c, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 19/23] tests: Ignore another built executable (test-hmp), Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 14/23] virtio-blk: Remove useless condition around g_free(), Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 06/23] usb-ccid: make ccid_write_data_block() cope with null buffers, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 15/23] hw/core/generic-loader: Fix crash when running without CPU, Michael Tokarev, 2017/05/07
- [Qemu-trivial] [PULL 23/23] tests: Remove redundant assignment, Michael Tokarev, 2017/05/07