Skip to contentSkip to conversation

build: disable -Wmissing-braces on older compilers

PATCH In review Paolo Bonzini 3 participants 10 messages Oct 20, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 build: disable -Wmissing-braces on older compilers

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Detect the lack of support for the idiom, and disable the warning in that case.

---
Of course it's always possible to use "memset", but {0} is neater in my opinion.

 configure | 13 +++++++++++++
 1 file changed, 13 insertions(+)
configure +13 -0
index 7766e74125..0b2d595c6e 100755
@@ -1651,6 +1651,19 @@ EOF
   fi
 fi
 
+# Disable -Wmissing-braces on older compilers that warn even for
+# the "universal" C zero initializer {0}.
+cat > $TMPC << EOF
+struct {
+  int a[2];
+} x = {0};
+EOF
+if compile_object "-Werror" "" ; then
+  :
+else
+  QEMU_CFLAGS="$QEMU_CFLAGS -Wno-missing-braces"
+fi
+
 # Workaround for http://gcc.gnu.org/PR55489.  Happens with -fPIE/-fPIC and
 # large functions that use global variables.  The bug is in all releases of
 # GCC, but it became particularly acute in 4.6.x and 4.7.x.  It is fixed in
Signature · 1 line
2.14.2
#1 lore raw
Quoted 2 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked

s/sneaked/snuck/

Hmmm, no, wait, both forms are valid!

https://en.wiktionary.org/wiki/sneaked
https://en.wiktionary.org/wiki/snuck

The irregular form snuck originated by analogy with struck for the past of strike. Snuck was originally limited to a few dialects, but is now very widespread (especially in American English) and is recognized by most dictionaries. The word is now one of the best examples of irregularization of a regular verb, along with dove.

Quoted 10 lines

into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Detect the lack of support for the idiom, and disable the warning in that case.

---
Of course it's always possible to use "memset", but {0} is neater in my opinion.

Strongly agree.

Quoted 12 lines
 configure | 13 +++++++++++++
 1 file changed, 13 insertions(+)
configure +0 -0
index 7766e74125..0b2d595c6e 100755
@@ -1651,6 +1651,19 @@ EOF
   fi
 fi
 

configure EOF · line 1663

Quoted 10 lines
configure +10 -0
+# Disable -Wmissing-braces on older compilers that warn even for
+# the "universal" C zero initializer {0}.
+cat > $TMPC << EOF
+struct {
+  int a[2];
+} x = {0};
+EOF
+if compile_object "-Werror" "" ; then
+  :
+else

Is this an established idiom for the configure script, in place of:

if ! compile_object "-Werror" "" ; then

?

Looks good to me otherwise.

Thanks!
Laszlo

Quoted 7 lines
configure +3 -0
+  QEMU_CFLAGS="$QEMU_CFLAGS -Wno-missing-braces"
+fi
+
 # Workaround for http://gcc.gnu.org/PR55489.  Happens with -fPIE/-fPIC and
 # large functions that use global variables.  The bug is in all releases of
 # GCC, but it became particularly acute in 4.6.x and 4.7.x.  It is fixed in
#2 lore raw
Quoted 10 lines · Laszlo Ersek

configure EOF · line 1663

Quoted 3 lines
configure +3 -0
+if compile_object "-Werror" "" ; then
+  :
+else

Is this an established idiom for the configure script, in place of:

if ! compile_object "-Werror" "" ; then

?

Looks good to me otherwise.

I tend not use "if !". In general that's just me, but in this case I think it's useful to point out that a successful compile does nothing; generally it's the successful compile that adjusts command line arguments.

Paolo

Reviewed-by
#3 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Laszlo Ersek

configure EOF · line 1663

Quoted 3 lines
configure +3 -0
+if compile_object "-Werror" "" ; then
+  :
+else

Is this an established idiom for the configure script, in place of:

if ! compile_object "-Werror" "" ; then

Quoted 7 lines
Quoted 3 lines

?

Looks good to me otherwise.

I tend not use "if !". In general that's just me, but in this case I think it's useful to point out that a successful compile does nothing; generally it's the successful compile that adjusts command line arguments.

Thanks for the explanation.

Cheers
Laszlo

#4 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Detect the lack of support for the idiom, and disable the warning in that case.

AFAIK "{}" will work and not be warned about anywhere (we use it extensively already) so you don't need to fall back to memset...

thanks
-- PMM

#5 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Quoted 8 lines
Quoted 4 lines

Detect the lack of support for the idiom, and disable the warning in that case.

AFAIK "{}" will work and not be warned about anywhere (we use it extensively already) so you don't need to fall back to memset...

Doh, of course that will work in scsi/qemu-pr-helper.c. In general you can use {0} but not {} to initialize a scalar, like

IDontKnowIfItsAnArrayOrPointer x = {}; //might fail
IDontKnowIfItsAnArrayOrPointer x = {0}; //always works

I'm not sure if that matters.

Paolo

#6 lore raw
Quoted 16 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Quoted 8 lines
Quoted 4 lines

Detect the lack of support for the idiom, and disable the warning in that case.

AFAIK "{}" will work and not be warned about anywhere (we use it extensively already) so you don't need to fall back to memset...

Quoted 7 lines

Doh, of course that will work in scsi/qemu-pr-helper.c. In general you can use {0} but not {} to initialize a scalar, like

IDontKnowIfItsAnArrayOrPointer x = {}; //might fail
IDontKnowIfItsAnArrayOrPointer x = {0}; //always works

I'm not sure if that matters.

My remark below might matter even less, but:

I'd find it regrettable if we suppressed a wrong gcc warning about a valid C construct by replacing the construct with a GNU-ism that is *not* standard C. :/

Laszlo

#7 lore raw
Quoted 23 lines · Laszlo Ersek
Quoted 22 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Quoted 8 lines
Quoted 4 lines

Detect the lack of support for the idiom, and disable the warning in that case.

AFAIK "{}" will work and not be warned about anywhere (we use it extensively already) so you don't need to fall back to memset...

Doh, of course that will work in scsi/qemu-pr-helper.c. In general you can use {0} but not {} to initialize a scalar, like

IDontKnowIfItsAnArrayOrPointer x = {}; //might fail
IDontKnowIfItsAnArrayOrPointer x = {0}; //always works

Quoted 7 lines
Quoted 1 line

I'm not sure if that matters.

My remark below might matter even less, but:

I'd find it regrettable if we suppressed a wrong gcc warning about a valid C construct by replacing the construct with a GNU-ism that is *not* standard C. :/

Is {} to initialize structs a GNUism? (Initializing a scalar with {} is a hard error with GCC).

Paolo

#8 lore raw
Quoted 27 lines · Paolo Bonzini
Quoted 26 lines · Laszlo Ersek
Quoted 24 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell
Quoted 5 lines · Paolo Bonzini

GCC 4.9 and newer stopped warning for missing braces around the "universal" C zero initializer {0}. One such initializer sneaked into scsi/qemu-pr-helper.c and is breaking the build with such older GCC versions.

Quoted 8 lines
Quoted 4 lines

Detect the lack of support for the idiom, and disable the warning in that case.

AFAIK "{}" will work and not be warned about anywhere (we use it extensively already) so you don't need to fall back to memset...

Doh, of course that will work in scsi/qemu-pr-helper.c. In general you can use {0} but not {} to initialize a scalar, like

IDontKnowIfItsAnArrayOrPointer x = {}; //might fail
IDontKnowIfItsAnArrayOrPointer x = {0}; //always works

I'm not sure if that matters.

Quoted 7 lines
Quoted 5 lines

My remark below might matter even less, but:

I'd find it regrettable if we suppressed a wrong gcc warning about a valid C construct by replacing the construct with a GNU-ism that is *not* standard C. :/

Is {} to initialize structs a GNUism?

It is;

Quoted 3 lines

struct {
int a, b;
} x = {};

$ gcc -fsyntax-only -std=c99 -pedantic -Wall -Wextra x.c
Quoted 9 lines

x.c:3:7: warning: ISO C forbids empty initializer braces [-Wpedantic]

 } x = {};
       ^

x.c:3:1: warning: missing initializer for field 'a' of 'struct <anonymous>' [-Wmissing-field-initializers]
} x = {};
^
x.c:2:7: note: 'a' declared here

   int a, b;
       ^

If you replace "{}" with "{ 0 }", the same command line stays quiet.

(I used: gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16).)

Quoted 1 line

(Initializing a scalar with {} is a hard error with GCC).

Thanks,
Laszlo

#9 lore raw
Quoted 28 lines · Laszlo Ersek
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Laszlo Ersek

My remark below might matter even less, but:

I'd find it regrettable if we suppressed a wrong gcc warning about a valid C construct by replacing the construct with a GNU-ism that is *not* standard C. :/

Is {} to initialize structs a GNUism?

It is;

Quoted 3 lines

struct {
int a, b;
} x = {};

$ gcc -fsyntax-only -std=c99 -pedantic -Wall -Wextra x.c
Quoted 9 lines

x.c:3:7: warning: ISO C forbids empty initializer braces [-Wpedantic]

 } x = {};
       ^

x.c:3:1: warning: missing initializer for field 'a' of 'struct <anonymous>' [-Wmissing-field-initializers]
} x = {};
^
x.c:2:7: note: 'a' declared here

   int a, b;
       ^
Quoted 3 lines

If you replace "{}" with "{ 0 }", the same command line stays quiet.

(I used: gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16).)

Oh. So there _was_ some logic to my original configure patch. Currently we have 82 occurrences of empty initializers and 92 occurrences of {0}, so I think it makes sense to go ahead with it.

On the other hand, without a more precise option than -Wpedantic, empty initializers are going to creep in again.

Paolo