Skip to contentSkip to conversation

Developer Conveniences - check for _Static_assert and use in QEMU_BUILD_BUG_ON

PATCH In review Andreas Grapentin 2 participants 4 messages Mar 14, 2017

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 added configure check for _Static_assert and updated QEMU_BUILD_BUG_ON(...) accordingly

In reply to Andreas Grapentin
---
 configure               | 18 ++++++++++++++++++
 include/qemu/compiler.h |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)
configure +18 -0
index 75c7c3526c..e9b33d9cf8 100755
@@ -4725,6 +4725,20 @@ if compile_prog "" "" ; then
 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
 
@@ -5694,6 +5708,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
include/qemu/compiler.h +3 -1
index e0ce9ffb28..37a65d4bb7 100644
@@ -90,7 +90,9 @@
         int:(x) ? -1 : 1; \
     }
 
-#ifdef __COUNTER__
+#if defined(CONFIG_STATIC_ASSERT)
+#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), #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
Signature · 1 line
2.12.0
#0 lore raw

cover Developer Conveniences - check for _Static_assert and use in QEMU_BUILD_BUG_ON

(as taken from here: http://wiki.qemu-project.org/Contribute/BiteSizedTasks)

I added a configure check for C11's _Static_assert, and based on the outcome, QEMU_BUILD_BUG_ON will now produce slightly more readable results on failures.

Systems without C11 conforming compilers 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:

Quoted 4 lines

In file included from /home/andi/research/qemu-shared-memory/qemu/include/qemu/osdep.h:36:0,
from /home/andi/research/qemu-shared-memory/qemu/qga/commands.c:13:
/home/andi/research/qemu-shared-memory/qemu/qga/commands.c: In function ‘qmp_guest_exec_status’:
/home/andi/research/qemu-shared-memory/qemu/include/qemu/compiler.h:89:12: error: negative width in bit-field ‘<anonymous>’

Quoted 14 lines
     struct { \
            ^
/home/andi/research/qemu-shared-memory/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) \
                                      ^~~~~~~~~~~~~~~~~~~~~~~~
/home/andi/research/qemu-shared-memory/qemu/include/qemu/atomic.h:146:5: note: in expansion of macro ‘QEMU_BUILD_BUG_ON’
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));   \
     ^~~~~~~~~~~~~~~~~
/home/andi/research/qemu-shared-memory/qemu/include/qemu/atomic.h:417:5: note: in expansion of macro ‘atomic_load_acquire’
     atomic_load_acquire(ptr)
     ^~~~~~~~~~~~~~~~~~~
/home/andi/research/qemu-shared-memory/qemu/qga/commands.c:160:21: note: in expansion of macro ‘atomic_mb_read’
     bool finished = atomic_mb_read(&gei->finished);
                     ^~~~~~~~~~~~~~

with _Static_assert:

Quoted 4 lines

In file included from /home/andi/research/qemu-shared-memory/qemu/include/qemu/osdep.h:36:0,
from /home/andi/research/qemu-shared-memory/qemu/qga/commands.c:13:
/home/andi/research/qemu-shared-memory/qemu/qga/commands.c: In function ‘qmp_guest_exec_status’:
/home/andi/research/qemu-shared-memory/qemu/include/qemu/compiler.h:94:30: error: static assertion failed: "sizeof(*&gei->finished) > sizeof(void *)"

Quoted 11 lines
 #define QEMU_BUILD_BUG_ON(x) _Static_assert((x), #x)
                              ^
/home/andi/research/qemu-shared-memory/qemu/include/qemu/atomic.h:146:5: note: in expansion of macro ‘QEMU_BUILD_BUG_ON’
     QEMU_BUILD_BUG_ON(sizeof(*ptr) > sizeof(void *));   \
     ^~~~~~~~~~~~~~~~~
/home/andi/research/qemu-shared-memory/qemu/include/qemu/atomic.h:417:5: note: in expansion of macro ‘atomic_load_acquire’
     atomic_load_acquire(ptr)
     ^~~~~~~~~~~~~~~~~~~
/home/andi/research/qemu-shared-memory/qemu/qga/commands.c:160:21: note: in expansion of macro ‘atomic_mb_read’
     bool finished = atomic_mb_read(&gei->finished);
                     ^~~~~~~~~~~~~~

Andreas Grapentin (1):
added configure check for _Static_assert and updated
QEMU_BUILD_BUG_ON(...) accordingly

 configure               | 18 ++++++++++++++++++
 include/qemu/compiler.h |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)
Signature · 1 line
2.12.0
#3 lore raw
Quoted 3 lines · Andreas Grapentin

(as taken from here: http://wiki.qemu-project.org/Contribute/BiteSizedTasks)

A single patch can be sent without a cover letter if desired (0/N cover letters are only mandatory on patch series). In fact, much of the information you give here:

Quoted 12 lines

I added a configure check for C11's _Static_assert, and based on the outcome, QEMU_BUILD_BUG_ON will now produce slightly more readable results on failures.

Systems without C11 conforming compilers 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:

...

Quoted 8 lines

with _Static_assert:

Quoted 6 lines

In file included from /home/andi/research/qemu-shared-memory/qemu/include/qemu/osdep.h:36:0,
from /home/andi/research/qemu-shared-memory/qemu/qga/commands.c:13:
/home/andi/research/qemu-shared-memory/qemu/qga/commands.c: In function ‘qmp_guest_exec_status’:
/home/andi/research/qemu-shared-memory/qemu/include/qemu/compiler.h:94:30: error: static assertion failed: "sizeof(*&gei->finished) > sizeof(void *)"

 #define QEMU_BUILD_BUG_ON(x) _Static_assert((x), #x)
                              ^

should be in the commit message of the actual patch (right now, your 1/1 message has a rather blank commit message).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw

Re: [Qemu-devel] [PATCH 1/1] added configure check for _Static_assert and updated QEMU_BUILD_BUG_ON(...) accordingly

In reply to Andreas Grapentin
Quoted 1 line · Andreas Grapentin

---

Missing a Signed-off-by, so you'll need to send a v2 before we can apply it.

Subject line is long; I suggest shortening it to something like:

build: use _Static_assert in QEMU_BUILD_BUG_ON

then going into details in the rest of the commit body (see also my comments on your 0/1 mail about content that should have been on this commit).

Quoted 9 lines
 configure               | 18 ++++++++++++++++++
 include/qemu/compiler.h |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)
configure +0 -0
index 75c7c3526c..e9b33d9cf8 100755

configure if compile_prog "" "" ; then · line 4732

Quoted 8 lines
configure +5 -0
@@ -4725,6 +4725,20 @@ if compile_prog "" "" ; then
 fi
 
 ##########################################
+# check for _Static_assert()
+
+have_static_assert=no
+cat > $TMPC << EOF
+_Static_assert(1, "success.");

trailing dot looks awkward; the test is the same if you omit it.

configure line 94

Quoted 8 lines
configure +3 -1
+++ b/include/qemu/compiler.h
@@ -90,7 +90,9 @@
         int:(x) ? -1 : 1; \
     }
 
-#ifdef __COUNTER__
+#if defined(CONFIG_STATIC_ASSERT)
+#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), #x)

Should we add any explanatory text in addition to just the expression being tested? For example:

block/raw-format.c: QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);

A message of "static assertion failed: BDRV_SECTOR_SIZE != 512" is
tricky: it can only trip if the sector size was not 512, but normally when I see a _Static_assert message, I assume that I should fix my code to make the expression true, and the expression printed is already true.

Maybe _Static_assert(!(x), "not expecting " #x) is better, because it would give a message of "static assertion failed: not expecting BDRV_SECTOR_SIZE != 512".

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org