manywarnings: add -Wno-missing-field-initializers if needed
[gnulib.git] / m4 / manywarnings.m4
1 # manywarnings.m4 serial 1
2 dnl Copyright (C) 2008-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Simon Josefsson
8
9 # gl_MANYWARN_COMPLEMENT(OUTVAR, LISTVAR, REMOVEVAR)
10 # --------------------------------------------------
11 # Copy LISTVAR to OUTVAR except for the entries in REMOVEVAR.
12 # Elements separated by whitespace.  In set logic terms, the function
13 # does OUTVAR = LISTVAR \ REMOVEVAR.
14 AC_DEFUN([gl_MANYWARN_COMPLEMENT],
15 [
16   gl_warn_set=
17   set x $2; shift
18   for gl_warn_item
19   do
20     case " $3 " in
21       *" $gl_warn_item "*)
22         ;;
23       *)
24         gl_warn_set="$gl_warn_set $gl_warn_item"
25         ;;
26     esac
27   done
28   $1=$gl_warn_set
29 ])
30
31 # gl_MANYWARN_ALL_GCC(VARIABLE)
32 # -----------------------------
33 # Add all documented GCC (currently as per version 4.4) warning
34 # parameters to variable VARIABLE.  Note that you need to test them
35 # using gl_WARN_ADD if you want to make sure your gcc understands it.
36 AC_DEFUN([gl_MANYWARN_ALL_GCC],
37 [
38  dnl First, check if -Wno-missing-field-initializers is needed.
39  dnl -Wmissing-field-initializers is implied by -W, but that issues
40  dnl warnings with GCC version before 4.7, for the common idiom
41  dnl of initializing types on the stack to zero, using { 0, }
42  AC_REQUIRE([AC_PROG_CC])
43  if test -n "$GCC"; then
44
45    dnl First, check -W -Werror -Wno-missing-field-initializers is supported
46    dnl with the current $CC $CFLAGS $CPPFLAGS.
47    AC_MSG_CHECKING([whether -Wno-missing-field-initializers is supported])
48    AC_CACHE_VAL([gl_cv_cc_nomfi_supported], [
49      gl_save_CFLAGS="$CFLAGS"
50      CFLAGS="$CFLAGS -W -Werror -Wno-missing-field-initializers"
51      AC_COMPILE_IFELSE(
52        [AC_LANG_PROGRAM([[]], [[]])],
53        [gl_cv_cc_nomfi_supported=yes],
54        [gl_cv_cc_nomfi_supported=no])
55      CFLAGS="$gl_save_CFLAGS"])
56    AC_MSG_RESULT([$gl_cv_cc_nomfi_supported])
57
58    if test "$gl_cv_cc_nomfi_supported" = yes; then
59      dnl Now check whether -Wno-missing-field-initializers is needed
60      dnl for the { 0, } construct.
61      AC_MSG_CHECKING([whether -Wno-missing-field-initializers is needed])
62      AC_CACHE_VAL([gl_cv_cc_nomfi_needed], [
63        gl_save_CFLAGS="$CFLAGS"
64        CFLAGS="$CFLAGS -W -Werror"
65        AC_COMPILE_IFELSE(
66          [AC_LANG_PROGRAM(
67             [[void f (void)
68               {
69                 typedef struct { int a; int b; } s_t;
70                 s_t s1 = { 0, };
71               }
72             ]],
73             [[]])],
74          [gl_cv_cc_nomfi_needed=no],
75          [gl_cv_cc_nomfi_needed=yes])
76        CFLAGS="$gl_save_CFLAGS"])
77      AC_MSG_RESULT([$gl_cv_cc_nomfi_needed])
78    fi
79  fi
80
81  gl_manywarn_set=
82  for gl_manywarn_item in \
83    -Wall \
84    -W \
85    -Wformat-y2k \
86    -Wformat-nonliteral \
87    -Wformat-security \
88    -Winit-self \
89    -Wmissing-include-dirs \
90    -Wswitch-default \
91    -Wswitch-enum \
92    -Wunused \
93    -Wunknown-pragmas \
94    -Wstrict-aliasing \
95    -Wstrict-overflow \
96    -Wsystem-headers \
97    -Wfloat-equal \
98    -Wtraditional \
99    -Wtraditional-conversion \
100    -Wdeclaration-after-statement \
101    -Wundef \
102    -Wshadow \
103    -Wunsafe-loop-optimizations \
104    -Wpointer-arith \
105    -Wbad-function-cast \
106    -Wc++-compat \
107    -Wcast-qual \
108    -Wcast-align \
109    -Wwrite-strings \
110    -Wconversion \
111    -Wsign-conversion \
112    -Wlogical-op \
113    -Waggregate-return \
114    -Wstrict-prototypes \
115    -Wold-style-definition \
116    -Wmissing-prototypes \
117    -Wmissing-declarations \
118    -Wmissing-noreturn \
119    -Wmissing-format-attribute \
120    -Wpacked \
121    -Wpadded \
122    -Wredundant-decls \
123    -Wnested-externs \
124    -Wunreachable-code \
125    -Winline \
126    -Winvalid-pch \
127    -Wlong-long \
128    -Wvla \
129    -Wvolatile-register-var \
130    -Wdisabled-optimization \
131    -Wstack-protector \
132    -Woverlength-strings \
133    -Wbuiltin-macro-redefined \
134    -Wmudflap \
135    -Wpacked-bitfield-compat \
136    -Wsync-nand \
137   ; do
138     gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
139   done
140  # The following are not documented in the manual but are included in
141  # output from gcc --help=warnings.
142  for gl_manywarn_item in \
143    -Wattributes \
144    -Wcoverage-mismatch \
145    -Wmultichar \
146    -Wunused-macros \
147   ; do
148     gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
149   done
150
151  # Disable the missing-field-initializers warning if needed
152  if test "$gl_cv_cc_nomfi_needed" = yes; then
153    gl_manywarn_set="$gl_manywarn_set -Wno-missing-field-initializers"
154  fi
155
156  $1=$gl_manywarn_set
157 ])