diff mbox

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

Message ID 20170314144453.5027-2-andreas@grapentin.org
State New
Headers show

Commit Message

Andreas Grapentin March 14, 2017, 2:44 p.m. UTC
---
 configure               | 18 ++++++++++++++++++
 include/qemu/compiler.h |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)

Comments

Eric Blake March 14, 2017, 3:08 p.m. UTC | #1
On 03/14/2017 09:44 AM, Andreas Grapentin wrote:
> ---

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).

>  configure               | 18 ++++++++++++++++++
>  include/qemu/compiler.h |  4 +++-
>  2 files changed, 21 insertions(+), 1 deletion(-)
> 
> diff --git a/configure b/configure
> index 75c7c3526c..e9b33d9cf8 100755
> --- a/configure
> +++ b/configure
> @@ -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.

> +++ 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".
diff mbox

Patch

diff --git a/configure b/configure
index 75c7c3526c..e9b33d9cf8 100755
--- a/configure
+++ b/configure
@@ -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
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index e0ce9ffb28..37a65d4bb7 100644
--- a/include/qemu/compiler.h
+++ 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)
+#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