From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Andreas Grapentin <andreas@grapentin.org>
Subject: [Qemu-devel] [PULL 05/21] use _Static_assert in QEMU_BUILD_BUG_ON
Date: Fri, 5 May 2017 12:13:21 +0200 [thread overview]
Message-ID: <20170505101337.4650-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20170505101337.4650-1-pbonzini@redhat.com>
From: Andreas Grapentin <andreas@grapentin.org>
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 <andreas@grapentin.org>
Message-Id: <20170314165953.18506-1-andreas@grapentin.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
configure | 18 ++++++++++++++++++
include/qemu/compiler.h | 4 +++-
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index ff2c81f2c7..7c020c076b 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.12.2
next prev parent reply other threads:[~2017-05-05 10:13 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-05 10:13 [Qemu-devel] [PULL 00/21] Misc patches for 2017-05-05 Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 01/21] hw/i386: Use Rev3 FADT (ACPI 2.0) instead of Rev1 to improve guest OS support Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 02/21] hw/i386: Build-time assertion on pc/q35 reset register being identical Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 03/21] char: Fix removing wrong GSource that be found by fd_in_tag Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 04/21] target/i386: Add GDB XML register description support Paolo Bonzini
2017-05-05 10:13 ` Paolo Bonzini [this message]
2017-05-05 10:13 ` [Qemu-devel] [PULL 06/21] vl: deprecate the "-hdachs" option Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 07/21] scsi: avoid an off-by-one error in megasas_mmio_write Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 08/21] sgabios: update for "fix wrong video attrs for int 10h, ah==13h" Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 09/21] vmw_pvscsi: check message ring page count at initialisation Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 10/21] trace: add qemu mutex lock and unlock trace events Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 11/21] checkpatch: Disallow glib asserts in main code Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 12/21] hax: Fix memory mapping de-duplication logic Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 13/21] dump: Acquire BQL around vm_start() in dump thread Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 14/21] Fix the -accel parameter and the documentation for 'hax' Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 15/21] MAINTAINERS: Add "R:" tag for self-appointed reviewers Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 16/21] get_maintainer: Teach get_maintainer.pl about the new "R:" tag Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 17/21] get_maintainer: it's '--pattern-depth', not '-pattern-depth' Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 18/21] get_maintainer: --r (list reviewer) is on by default Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 19/21] get_maintainer: add subsystem to reviewer output Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 20/21] libvhost-user: replace vasprintf() to fix build Paolo Bonzini
2017-05-05 10:13 ` [Qemu-devel] [PULL 21/21] vhost-scsi: create a vhost-scsi-common abstraction Paolo Bonzini
2017-05-30 13:11 ` Stefan Hajnoczi
2017-05-30 14:06 ` Felipe Franciosi
2017-05-30 14:16 ` Paolo Bonzini
2017-05-30 14:21 ` Felipe Franciosi
2017-05-30 14:29 ` Paolo Bonzini
2017-05-30 14:34 ` Felipe Franciosi
2017-05-08 17:02 ` [Qemu-devel] [PULL 00/21] Misc patches for 2017-05-05 Stefan Hajnoczi
2017-05-30 13:13 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170505101337.4650-6-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=andreas@grapentin.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).