manywarnings.m4 6.85 KB
Newer Older
Paul Eggert's avatar
Paul Eggert committed
1
# manywarnings.m4 serial 20
Paul Eggert's avatar
Paul Eggert committed
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
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 "*)
        ;;
      *)
Paul Eggert's avatar
Paul Eggert committed
24
        gl_AS_VAR_APPEND([gl_warn_set], [" $gl_warn_item"])
25 26 27 28 29 30 31 32 33 34 35
        ;;
    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 -Wextra -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
    AC_CACHE_CHECK([whether -Wno-missing-field-initializers is supported],
      [gl_cv_cc_nomfi_supported],
      [gl_save_CFLAGS="$CFLAGS"
Paul Eggert's avatar
Paul Eggert committed
57
       CFLAGS="$CFLAGS -Wextra -Werror -Wno-missing-field-initializers"
Paul Eggert's avatar
Paul Eggert committed
58 59 60 61 62 63
       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
      AC_CACHE_CHECK([whether -Wno-missing-field-initializers is needed],
        [gl_cv_cc_nomfi_needed],
        [gl_save_CFLAGS="$CFLAGS"
Paul Eggert's avatar
Paul Eggert committed
71
         CFLAGS="$CFLAGS -Wextra -Werror"
Paul Eggert's avatar
Paul Eggert committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
         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
  #  <((sed -n 's/^  *\(-[^ 0-9][^ ]*\).*/\1/p' manywarnings.m4; \
Paul Eggert's avatar
Paul Eggert committed
109
  #     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

Paul Eggert's avatar
Paul Eggert committed
112 113
  $1=
  for gl_manywarn_item in -fanalyzer -fno-common \
Paul Eggert's avatar
Paul Eggert committed
114
    -Wall \
Paul Eggert's avatar
Paul Eggert committed
115
    -Warith-conversion \
116
    -Wbad-function-cast \
Paul Eggert's avatar
Paul Eggert committed
117
    -Wcast-align=strict \
118
    -Wdate-time \
Paul Eggert's avatar
Paul Eggert committed
119
    -Wdisabled-optimization \
120
    -Wdouble-promotion \
121
    -Wduplicated-branches \
Paul Eggert's avatar
Paul Eggert committed
122
    -Wduplicated-cond \
123
    -Wextra \
Paul Eggert's avatar
Paul Eggert committed
124
    -Wformat-signedness \
Paul Eggert's avatar
Paul Eggert committed
125 126 127 128 129 130 131 132
    -Winit-self \
    -Winline \
    -Winvalid-pch \
    -Wlogical-op \
    -Wmissing-declarations \
    -Wmissing-include-dirs \
    -Wmissing-prototypes \
    -Wnested-externs \
Paul Eggert's avatar
Paul Eggert committed
133
    -Wnull-dereference \
Paul Eggert's avatar
Paul Eggert committed
134
    -Wold-style-definition \
135
    -Wopenmp-simd \
Paul Eggert's avatar
Paul Eggert committed
136 137 138 139 140 141 142
    -Woverlength-strings \
    -Wpacked \
    -Wpointer-arith \
    -Wshadow \
    -Wstack-protector \
    -Wstrict-overflow \
    -Wstrict-prototypes \
Paul Eggert's avatar
Paul Eggert committed
143
    -Wsuggest-attribute=cold \
144
    -Wsuggest-attribute=const \
Paul Eggert's avatar
Paul Eggert committed
145
    -Wsuggest-attribute=format \
Paul Eggert's avatar
Paul Eggert committed
146
    -Wsuggest-attribute=malloc \
147 148
    -Wsuggest-attribute=noreturn \
    -Wsuggest-attribute=pure \
Paul Eggert's avatar
Paul Eggert committed
149 150
    -Wsuggest-final-methods \
    -Wsuggest-final-types \
Paul Eggert's avatar
Paul Eggert committed
151 152
    -Wsync-nand \
    -Wsystem-headers \
153
    -Wtrampolines \
Paul Eggert's avatar
Paul Eggert committed
154 155 156 157 158 159 160 161 162
    -Wuninitialized \
    -Wunknown-pragmas \
    -Wunsafe-loop-optimizations \
    -Wunused-macros \
    -Wvariadic-macros \
    -Wvector-operation-performance \
    -Wvla \
    -Wwrite-strings \
    \
163
    ; do
Paul Eggert's avatar
Paul Eggert committed
164
    gl_AS_VAR_APPEND([$1], [" $gl_manywarn_item"])
165 166
  done

Paul Eggert's avatar
Paul Eggert committed
167 168
  # 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
169 170 171 172 173 174 175 176 177
  gl_AS_VAR_APPEND([$1], [' -Warray-bounds=2'])
  gl_AS_VAR_APPEND([$1], [' -Wattribute-alias=2'])
  gl_AS_VAR_APPEND([$1], [' -Wformat-overflow=2'])
  gl_AS_VAR_APPEND([$1], [' -Wformat=2'])
  gl_AS_VAR_APPEND([$1], [' -Wformat-truncation=2'])
  gl_AS_VAR_APPEND([$1], [' -Wimplicit-fallthrough=5'])
  gl_AS_VAR_APPEND([$1], [' -Wshift-overflow=2'])
  gl_AS_VAR_APPEND([$1], [' -Wunused-const-variable=2'])
  gl_AS_VAR_APPEND([$1], [' -Wvla-larger-than=4031'])
178 179 180 181 182 183

  # 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]].*)
Paul Eggert's avatar
Paul Eggert committed
184 185
        gl_AS_VAR_APPEND([$1], [' -fdiagnostics-show-option'])
        gl_AS_VAR_APPEND([$1], [' -funit-at-a-time'])
186 187 188 189
          ;;
    esac
  fi

Paul Eggert's avatar
Paul Eggert committed
190
  # Disable specific options as needed.
191
  if test "$gl_cv_cc_nomfi_needed" = yes; then
Paul Eggert's avatar
Paul Eggert committed
192
    gl_AS_VAR_APPEND([$1], [' -Wno-missing-field-initializers'])
193 194
  fi

Paul Eggert's avatar
Paul Eggert committed
195
  if test "$gl_cv_cc_uninitialized_supported" = no; then
Paul Eggert's avatar
Paul Eggert committed
196
    gl_AS_VAR_APPEND([$1], [' -Wno-uninitialized'])
Paul Eggert's avatar
Paul Eggert committed
197 198
  fi

Paul Eggert's avatar
Paul Eggert committed
199 200 201 202 203 204 205 206 207
  # Some warnings have too many false alarms in GCC 10.1.
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93695
  gl_AS_VAR_APPEND([$1], [' -Wno-analyzer-double-free'])
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94458
  gl_AS_VAR_APPEND([$1], [' -Wno-analyzer-malloc-leak'])
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94851
  gl_AS_VAR_APPEND([$1], [' -Wno-analyzer-null-dereference'])
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95758
  gl_AS_VAR_APPEND([$1], [' -Wno-analyzer-use-after-free'])
Paul Eggert's avatar
Paul Eggert committed
208 209 210 211 212

  AC_LANG_POP([C])
])

# Specialization for _AC_LANG = C++.
Paul Eggert's avatar
Paul Eggert committed
213 214
# 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
215 216
[
  gl_MANYWARN_ALL_GCC_CXX_IMPL([$1])
217
])