manywarnings.m4 9.85 KB
Newer Older
Paul Eggert's avatar
Paul Eggert committed
1
# manywarnings.m4 serial 18
2
dnl Copyright (C) 2008-2020 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl From Simon Josefsson

# gl_MANYWARN_COMPLEMENT(OUTVAR, LISTVAR, REMOVEVAR)
# --------------------------------------------------
# Copy LISTVAR to OUTVAR except for the entries in REMOVEVAR.
# Elements separated by whitespace.  In set logic terms, the function
# does OUTVAR = LISTVAR \ REMOVEVAR.
AC_DEFUN([gl_MANYWARN_COMPLEMENT],
[
  gl_warn_set=
  set x $2; shift
  for gl_warn_item
  do
    case " $3 " in
      *" $gl_warn_item "*)
        ;;
      *)
        gl_warn_set="$gl_warn_set $gl_warn_item"
        ;;
    esac
  done
  $1=$gl_warn_set
])

# gl_MANYWARN_ALL_GCC(VARIABLE)
# -----------------------------
# Add all documented GCC warning parameters to variable VARIABLE.
# Note that you need to test them using gl_WARN_ADD if you want to
# make sure your gcc understands it.
Paul Eggert's avatar
Paul Eggert committed
36 37
#
# The effects of this macro depend on the current language (_AC_LANG).
38
AC_DEFUN([gl_MANYWARN_ALL_GCC],
Paul Eggert's avatar
Paul Eggert committed
39 40 41
[_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])

# Specialization for _AC_LANG = C.
Paul Eggert's avatar
Paul Eggert committed
42 43
# Use of m4_defun rather than AC_DEFUN works around a bug in autoconf < 2.63b.
m4_defun([gl_MANYWARN_ALL_GCC(C)],
44
[
Paul Eggert's avatar
Paul Eggert committed
45 46
  AC_LANG_PUSH([C])

Paul Eggert's avatar
Paul Eggert committed
47 48
  dnl First, check for some issues that only occur when combining multiple
  dnl gcc warning categories.
49 50 51
  AC_REQUIRE([AC_PROG_CC])
  if test -n "$GCC"; then

Paul Eggert's avatar
Paul Eggert committed
52
    dnl Check if -W -Werror -Wno-missing-field-initializers is supported
53
    dnl with the current $CC $CFLAGS $CPPFLAGS.
Paul Eggert's avatar
Paul Eggert committed
54 55 56 57 58 59 60 61 62 63
    AC_CACHE_CHECK([whether -Wno-missing-field-initializers is supported],
      [gl_cv_cc_nomfi_supported],
      [gl_save_CFLAGS="$CFLAGS"
       CFLAGS="$CFLAGS -W -Werror -Wno-missing-field-initializers"
       AC_COMPILE_IFELSE(
         [AC_LANG_PROGRAM([[]], [[]])],
         [gl_cv_cc_nomfi_supported=yes],
         [gl_cv_cc_nomfi_supported=no])
       CFLAGS="$gl_save_CFLAGS"
      ])
64 65 66 67

    if test "$gl_cv_cc_nomfi_supported" = yes; then
      dnl Now check whether -Wno-missing-field-initializers is needed
      dnl for the { 0, } construct.
Paul Eggert's avatar
Paul Eggert committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      AC_CACHE_CHECK([whether -Wno-missing-field-initializers is needed],
        [gl_cv_cc_nomfi_needed],
        [gl_save_CFLAGS="$CFLAGS"
         CFLAGS="$CFLAGS -W -Werror"
         AC_COMPILE_IFELSE(
           [AC_LANG_PROGRAM(
              [[int f (void)
                {
                  typedef struct { int a; int b; } s_t;
                  s_t s1 = { 0, };
                  return s1.b;
                }
              ]],
              [[]])],
           [gl_cv_cc_nomfi_needed=no],
           [gl_cv_cc_nomfi_needed=yes])
         CFLAGS="$gl_save_CFLAGS"
        ])
86
    fi
Paul Eggert's avatar
Paul Eggert committed
87 88 89 90

    dnl Next, check if -Werror -Wuninitialized is useful with the
    dnl user's choice of $CFLAGS; some versions of gcc warn that it
    dnl has no effect if -O is not also used
Paul Eggert's avatar
Paul Eggert committed
91 92 93 94 95 96 97 98 99 100
    AC_CACHE_CHECK([whether -Wuninitialized is supported],
      [gl_cv_cc_uninitialized_supported],
      [gl_save_CFLAGS="$CFLAGS"
       CFLAGS="$CFLAGS -Werror -Wuninitialized"
       AC_COMPILE_IFELSE(
         [AC_LANG_PROGRAM([[]], [[]])],
         [gl_cv_cc_uninitialized_supported=yes],
         [gl_cv_cc_uninitialized_supported=no])
       CFLAGS="$gl_save_CFLAGS"
      ])
Paul Eggert's avatar
Paul Eggert committed
101

102 103
  fi

Paul Eggert's avatar
Paul Eggert committed
104
  # List all gcc warning categories.
105 106 107
  # To compare this list to your installed GCC's, run this Bash command:
  #
  # comm -3 \
Paul Eggert's avatar
Paul Eggert committed
108 109
  #  <((sed -n 's/^  *\(-[^ 0-9][^ ]*\) .*/\1/p' manywarnings.m4; \
  #     awk '/^[^#]/ {print $1}' ../build-aux/gcc-warning.spec) | sort) \
Paul Eggert's avatar
Paul Eggert committed
110
  #  <(LC_ALL=C gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort)
111

112
  gl_manywarn_set=
113
  for gl_manywarn_item in -fno-common \
114
    -W \
Paul Eggert's avatar
Paul Eggert committed
115
    -Wabsolute-value \
Paul Eggert's avatar
Paul Eggert committed
116
    -Waddress \
Paul Eggert's avatar
Paul Eggert committed
117
    -Waddress-of-packed-member \
Paul Eggert's avatar
Paul Eggert committed
118
    -Waggressive-loop-optimizations \
Paul Eggert's avatar
Paul Eggert committed
119
    -Wall \
Paul Eggert's avatar
Paul Eggert committed
120
    -Wattribute-warning \
Paul Eggert's avatar
Paul Eggert committed
121
    -Wattributes \
122
    -Wbad-function-cast \
Paul Eggert's avatar
Paul Eggert committed
123
    -Wbool-compare \
124 125
    -Wbool-operation \
    -Wbuiltin-declaration-mismatch \
126
    -Wbuiltin-macro-redefined \
Paul Eggert's avatar
Paul Eggert committed
127
    -Wcannot-profile \
Paul Eggert's avatar
Paul Eggert committed
128
    -Wcast-align \
Paul Eggert's avatar
Paul Eggert committed
129 130
    -Wcast-align=strict \
    -Wcast-function-type \
Paul Eggert's avatar
Paul Eggert committed
131 132 133 134
    -Wchar-subscripts \
    -Wclobbered \
    -Wcomment \
    -Wcomments \
135 136
    -Wcoverage-mismatch \
    -Wcpp \
137
    -Wdangling-else \
138
    -Wdate-time \
139 140
    -Wdeprecated \
    -Wdeprecated-declarations \
Paul Eggert's avatar
Paul Eggert committed
141
    -Wdesignated-init \
Paul Eggert's avatar
Paul Eggert committed
142
    -Wdisabled-optimization \
Paul Eggert's avatar
Paul Eggert committed
143 144
    -Wdiscarded-array-qualifiers \
    -Wdiscarded-qualifiers \
145 146
    -Wdiv-by-zero \
    -Wdouble-promotion \
147
    -Wduplicated-branches \
Paul Eggert's avatar
Paul Eggert committed
148
    -Wduplicated-cond \
149
    -Wduplicate-decl-specifier \
Paul Eggert's avatar
Paul Eggert committed
150
    -Wempty-body \
151
    -Wendif-labels \
Paul Eggert's avatar
Paul Eggert committed
152
    -Wenum-compare \
153
    -Wexpansion-to-defined \
154 155 156
    -Wextra \
    -Wformat-contains-nul \
    -Wformat-extra-args \
Paul Eggert's avatar
Paul Eggert committed
157 158
    -Wformat-nonliteral \
    -Wformat-security \
Paul Eggert's avatar
Paul Eggert committed
159
    -Wformat-signedness \
Paul Eggert's avatar
Paul Eggert committed
160
    -Wformat-y2k \
161
    -Wformat-zero-length \
Paul Eggert's avatar
Paul Eggert committed
162
    -Wframe-address \
Paul Eggert's avatar
Paul Eggert committed
163
    -Wfree-nonheap-object \
Paul Eggert's avatar
Paul Eggert committed
164
    -Whsa \
Paul Eggert's avatar
Paul Eggert committed
165
    -Wif-not-aligned \
Paul Eggert's avatar
Paul Eggert committed
166
    -Wignored-attributes \
Paul Eggert's avatar
Paul Eggert committed
167 168 169 170
    -Wignored-qualifiers \
    -Wimplicit \
    -Wimplicit-function-declaration \
    -Wimplicit-int \
Paul Eggert's avatar
Paul Eggert committed
171
    -Wincompatible-pointer-types \
Paul Eggert's avatar
Paul Eggert committed
172 173
    -Winit-self \
    -Winline \
Paul Eggert's avatar
Paul Eggert committed
174
    -Wint-conversion \
175
    -Wint-in-bool-context \
Paul Eggert's avatar
Paul Eggert committed
176 177 178
    -Wint-to-pointer-cast \
    -Winvalid-memory-model \
    -Winvalid-pch \
Paul Eggert's avatar
Paul Eggert committed
179
    -Wlogical-not-parentheses \
Paul Eggert's avatar
Paul Eggert committed
180 181 182
    -Wlogical-op \
    -Wmain \
    -Wmaybe-uninitialized \
183
    -Wmemset-elt-size \
Paul Eggert's avatar
Paul Eggert committed
184
    -Wmemset-transposed-args \
Paul Eggert's avatar
Paul Eggert committed
185
    -Wmisleading-indentation \
Paul Eggert's avatar
Paul Eggert committed
186
    -Wmissing-attributes \
Paul Eggert's avatar
Paul Eggert committed
187 188 189 190 191
    -Wmissing-braces \
    -Wmissing-declarations \
    -Wmissing-field-initializers \
    -Wmissing-include-dirs \
    -Wmissing-parameter-type \
Paul Eggert's avatar
Paul Eggert committed
192
    -Wmissing-profile \
Paul Eggert's avatar
Paul Eggert committed
193
    -Wmissing-prototypes \
194
    -Wmultichar \
Paul Eggert's avatar
Paul Eggert committed
195
    -Wmultistatement-macros \
Paul Eggert's avatar
Paul Eggert committed
196 197 198
    -Wnarrowing \
    -Wnested-externs \
    -Wnonnull \
Paul Eggert's avatar
Paul Eggert committed
199 200
    -Wnonnull-compare \
    -Wnull-dereference \
Paul Eggert's avatar
Paul Eggert committed
201
    -Wodr \
Paul Eggert's avatar
Paul Eggert committed
202 203
    -Wold-style-declaration \
    -Wold-style-definition \
204
    -Wopenmp-simd \
205
    -Woverflow \
Paul Eggert's avatar
Paul Eggert committed
206 207 208 209
    -Woverlength-strings \
    -Woverride-init \
    -Wpacked \
    -Wpacked-bitfield-compat \
Paul Eggert's avatar
Paul Eggert committed
210
    -Wpacked-not-aligned \
Paul Eggert's avatar
Paul Eggert committed
211 212
    -Wparentheses \
    -Wpointer-arith \
213
    -Wpointer-compare \
Paul Eggert's avatar
Paul Eggert committed
214
    -Wpointer-sign \
215 216
    -Wpointer-to-int-cast \
    -Wpragmas \
217 218
    -Wpsabi \
    -Wrestrict \
Paul Eggert's avatar
Paul Eggert committed
219
    -Wreturn-local-addr \
Paul Eggert's avatar
Paul Eggert committed
220
    -Wreturn-type \
Paul Eggert's avatar
Paul Eggert committed
221
    -Wscalar-storage-order \
Paul Eggert's avatar
Paul Eggert committed
222 223
    -Wsequence-point \
    -Wshadow \
Paul Eggert's avatar
Paul Eggert committed
224 225
    -Wshift-count-negative \
    -Wshift-count-overflow \
Paul Eggert's avatar
Paul Eggert committed
226
    -Wshift-negative-value \
Paul Eggert's avatar
Paul Eggert committed
227
    -Wsizeof-array-argument \
Paul Eggert's avatar
Paul Eggert committed
228
    -Wsizeof-pointer-div \
Paul Eggert's avatar
Paul Eggert committed
229 230 231 232 233
    -Wsizeof-pointer-memaccess \
    -Wstack-protector \
    -Wstrict-aliasing \
    -Wstrict-overflow \
    -Wstrict-prototypes \
Paul Eggert's avatar
Paul Eggert committed
234 235
    -Wstringop-truncation \
    -Wsuggest-attribute=cold \
236
    -Wsuggest-attribute=const \
Paul Eggert's avatar
Paul Eggert committed
237
    -Wsuggest-attribute=format \
Paul Eggert's avatar
Paul Eggert committed
238
    -Wsuggest-attribute=malloc \
239 240
    -Wsuggest-attribute=noreturn \
    -Wsuggest-attribute=pure \
Paul Eggert's avatar
Paul Eggert committed
241 242
    -Wsuggest-final-methods \
    -Wsuggest-final-types \
Paul Eggert's avatar
Paul Eggert committed
243
    -Wswitch \
Paul Eggert's avatar
Paul Eggert committed
244
    -Wswitch-bool \
245
    -Wswitch-unreachable \
Paul Eggert's avatar
Paul Eggert committed
246 247
    -Wsync-nand \
    -Wsystem-headers \
Paul Eggert's avatar
Paul Eggert committed
248
    -Wtautological-compare \
249
    -Wtrampolines \
Paul Eggert's avatar
Paul Eggert committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    -Wtrigraphs \
    -Wtype-limits \
    -Wuninitialized \
    -Wunknown-pragmas \
    -Wunsafe-loop-optimizations \
    -Wunused \
    -Wunused-but-set-parameter \
    -Wunused-but-set-variable \
    -Wunused-function \
    -Wunused-label \
    -Wunused-local-typedefs \
    -Wunused-macros \
    -Wunused-parameter \
    -Wunused-result \
    -Wunused-value \
    -Wunused-variable \
    -Wvarargs \
    -Wvariadic-macros \
    -Wvector-operation-performance \
    -Wvla \
    -Wvolatile-register-var \
    -Wwrite-strings \
    \
273 274 275 276
    ; do
    gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
  done

Paul Eggert's avatar
Paul Eggert committed
277 278
  # gcc --help=warnings outputs an unusual form for these options; list
  # them here so that the above 'comm' command doesn't report a false match.
Paul Eggert's avatar
Paul Eggert committed
279 280 281 282
  # Would prefer "min (PTRDIFF_MAX, SIZE_MAX)", but it must be a literal.
  # Also, AC_COMPUTE_INT requires it to fit in a long; it is 2**63 on
  # the only platforms where it does not fit in a long, so make that
  # a special case.
Paul Eggert's avatar
Paul Eggert committed
283 284
  AC_MSG_CHECKING([max safe object size])
  AC_COMPUTE_INT([gl_alloc_max],
Paul Eggert's avatar
Paul Eggert committed
285 286 287
    [LONG_MAX < (PTRDIFF_MAX < (size_t) -1 ? PTRDIFF_MAX : (size_t) -1)
     ? -1
     : PTRDIFF_MAX < (size_t) -1 ? (long) PTRDIFF_MAX : (long) (size_t) -1],
Paul Eggert's avatar
Paul Eggert committed
288 289 290 291 292
    [[#include <limits.h>
      #include <stddef.h>
      #include <stdint.h>
    ]],
    [gl_alloc_max=2147483647])
Paul Eggert's avatar
Paul Eggert committed
293 294 295
  case $gl_alloc_max in
    -1) gl_alloc_max=9223372036854775807;;
  esac
Paul Eggert's avatar
Paul Eggert committed
296 297
  AC_MSG_RESULT([$gl_alloc_max])
  gl_manywarn_set="$gl_manywarn_set -Walloc-size-larger-than=$gl_alloc_max"
Paul Eggert's avatar
Paul Eggert committed
298
  gl_manywarn_set="$gl_manywarn_set -Warray-bounds=2"
Paul Eggert's avatar
Paul Eggert committed
299
  gl_manywarn_set="$gl_manywarn_set -Wattribute-alias=2"
300 301 302
  gl_manywarn_set="$gl_manywarn_set -Wformat-overflow=2"
  gl_manywarn_set="$gl_manywarn_set -Wformat-truncation=2"
  gl_manywarn_set="$gl_manywarn_set -Wimplicit-fallthrough=5"
303
  gl_manywarn_set="$gl_manywarn_set -Wnormalized=nfc"
Paul Eggert's avatar
Paul Eggert committed
304
  gl_manywarn_set="$gl_manywarn_set -Wshift-overflow=2"
305
  gl_manywarn_set="$gl_manywarn_set -Wstringop-overflow=2"
Paul Eggert's avatar
Paul Eggert committed
306
  gl_manywarn_set="$gl_manywarn_set -Wunused-const-variable=2"
307
  gl_manywarn_set="$gl_manywarn_set -Wvla-larger-than=4031"
308 309 310 311 312 313 314 315 316 317 318 319

  # These are needed for older GCC versions.
  if test -n "$GCC"; then
    case `($CC --version) 2>/dev/null` in
      'gcc (GCC) '[[0-3]].* | \
      'gcc (GCC) '4.[[0-7]].*)
        gl_manywarn_set="$gl_manywarn_set -fdiagnostics-show-option"
        gl_manywarn_set="$gl_manywarn_set -funit-at-a-time"
          ;;
    esac
  fi

Paul Eggert's avatar
Paul Eggert committed
320
  # Disable specific options as needed.
321 322 323 324
  if test "$gl_cv_cc_nomfi_needed" = yes; then
    gl_manywarn_set="$gl_manywarn_set -Wno-missing-field-initializers"
  fi

Paul Eggert's avatar
Paul Eggert committed
325 326 327 328
  if test "$gl_cv_cc_uninitialized_supported" = no; then
    gl_manywarn_set="$gl_manywarn_set -Wno-uninitialized"
  fi

329
  $1=$gl_manywarn_set
Paul Eggert's avatar
Paul Eggert committed
330 331 332 333 334

  AC_LANG_POP([C])
])

# Specialization for _AC_LANG = C++.
Paul Eggert's avatar
Paul Eggert committed
335 336
# Use of m4_defun rather than AC_DEFUN works around a bug in autoconf < 2.63b.
m4_defun([gl_MANYWARN_ALL_GCC(C++)],
Paul Eggert's avatar
Paul Eggert committed
337 338
[
  gl_MANYWARN_ALL_GCC_CXX_IMPL([$1])
339
])