xgetcwd: Improve documentation.
[gnulib.git] / m4 / manywarnings.m4
blob719bafb2909417518dda1b34638859a6787b8be1
1 # manywarnings.m4 serial 19
2 dnl Copyright (C) 2008-2020 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.
7 dnl From Simon Josefsson
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],
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
31 # gl_MANYWARN_ALL_GCC(VARIABLE)
32 # -----------------------------
33 # Add all documented GCC warning parameters to variable VARIABLE.
34 # Note that you need to test them using gl_WARN_ADD if you want to
35 # make sure your gcc understands it.
37 # The effects of this macro depend on the current language (_AC_LANG).
38 AC_DEFUN([gl_MANYWARN_ALL_GCC],
39 [_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])
41 # Specialization for _AC_LANG = C.
42 # Use of m4_defun rather than AC_DEFUN works around a bug in autoconf < 2.63b.
43 m4_defun([gl_MANYWARN_ALL_GCC(C)],
45   AC_LANG_PUSH([C])
47   dnl First, check for some issues that only occur when combining multiple
48   dnl gcc warning categories.
49   AC_REQUIRE([AC_PROG_CC])
50   if test -n "$GCC"; then
52     dnl Check if -W -Werror -Wno-missing-field-initializers is supported
53     dnl with the current $CC $CFLAGS $CPPFLAGS.
54     AC_CACHE_CHECK([whether -Wno-missing-field-initializers is supported],
55       [gl_cv_cc_nomfi_supported],
56       [gl_save_CFLAGS="$CFLAGS"
57        CFLAGS="$CFLAGS -W -Werror -Wno-missing-field-initializers"
58        AC_COMPILE_IFELSE(
59          [AC_LANG_PROGRAM([[]], [[]])],
60          [gl_cv_cc_nomfi_supported=yes],
61          [gl_cv_cc_nomfi_supported=no])
62        CFLAGS="$gl_save_CFLAGS"
63       ])
65     if test "$gl_cv_cc_nomfi_supported" = yes; then
66       dnl Now check whether -Wno-missing-field-initializers is needed
67       dnl for the { 0, } construct.
68       AC_CACHE_CHECK([whether -Wno-missing-field-initializers is needed],
69         [gl_cv_cc_nomfi_needed],
70         [gl_save_CFLAGS="$CFLAGS"
71          CFLAGS="$CFLAGS -W -Werror"
72          AC_COMPILE_IFELSE(
73            [AC_LANG_PROGRAM(
74               [[int f (void)
75                 {
76                   typedef struct { int a; int b; } s_t;
77                   s_t s1 = { 0, };
78                   return s1.b;
79                 }
80               ]],
81               [[]])],
82            [gl_cv_cc_nomfi_needed=no],
83            [gl_cv_cc_nomfi_needed=yes])
84          CFLAGS="$gl_save_CFLAGS"
85         ])
86     fi
88     dnl Next, check if -Werror -Wuninitialized is useful with the
89     dnl user's choice of $CFLAGS; some versions of gcc warn that it
90     dnl has no effect if -O is not also used
91     AC_CACHE_CHECK([whether -Wuninitialized is supported],
92       [gl_cv_cc_uninitialized_supported],
93       [gl_save_CFLAGS="$CFLAGS"
94        CFLAGS="$CFLAGS -Werror -Wuninitialized"
95        AC_COMPILE_IFELSE(
96          [AC_LANG_PROGRAM([[]], [[]])],
97          [gl_cv_cc_uninitialized_supported=yes],
98          [gl_cv_cc_uninitialized_supported=no])
99        CFLAGS="$gl_save_CFLAGS"
100       ])
102   fi
104   # List all gcc warning categories.
105   # To compare this list to your installed GCC's, run this Bash command:
106   #
107   # comm -3 \
108   #  <((sed -n 's/^  *\(-[^ 0-9][^ ]*\) .*/\1/p' manywarnings.m4; \
109   #     awk '/^[^#]/ {print $1}' ../build-aux/gcc-warning.spec) | sort) \
110   #  <(LC_ALL=C gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort)
112   gl_manywarn_set=
113   for gl_manywarn_item in -fno-common \
114     -W \
115     -Wabsolute-value \
116     -Waddress \
117     -Waddress-of-packed-member \
118     -Waggressive-loop-optimizations \
119     -Wall \
120     -Wanalyzer-double-fclose \
121     -Wanalyzer-double-free \
122     -Wanalyzer-exposure-through-output-file \
123     -Wanalyzer-file-leak \
124     -Wanalyzer-free-of-non-heap \
125     -Wanalyzer-malloc-leak \
126     -Wanalyzer-null-argument \
127     -Wanalyzer-null-dereference \
128     -Wanalyzer-possible-null-argument \
129     -Wanalyzer-possible-null-dereference \
130     -Wanalyzer-stale-setjmp-buffer \
131     -Wanalyzer-tainted-array-index \
132     -Wanalyzer-too-complex \
133     -Wanalyzer-unsafe-call-within-signal-handler \
134     -Wanalyzer-use-after-free \
135     -Wanalyzer-use-of-pointer-in-stale-stack-frame \
136     -Warith-conversion \
137     -Wattribute-warning \
138     -Wattributes \
139     -Wbad-function-cast \
140     -Wbool-compare \
141     -Wbool-operation \
142     -Wbuiltin-declaration-mismatch \
143     -Wbuiltin-macro-redefined \
144     -Wcannot-profile \
145     -Wcast-align \
146     -Wcast-align=strict \
147     -Wcast-function-type \
148     -Wchar-subscripts \
149     -Wclobbered \
150     -Wcomment \
151     -Wcomments \
152     -Wcoverage-mismatch \
153     -Wcpp \
154     -Wdangling-else \
155     -Wdate-time \
156     -Wdeprecated \
157     -Wdeprecated-declarations \
158     -Wdesignated-init \
159     -Wdisabled-optimization \
160     -Wdiscarded-array-qualifiers \
161     -Wdiscarded-qualifiers \
162     -Wdiv-by-zero \
163     -Wdouble-promotion \
164     -Wduplicated-branches \
165     -Wduplicated-cond \
166     -Wduplicate-decl-specifier \
167     -Wempty-body \
168     -Wendif-labels \
169     -Wenum-compare \
170     -Wenum-conversion \
171     -Wexpansion-to-defined \
172     -Wextra \
173     -Wformat-contains-nul \
174     -Wformat-diag \
175     -Wformat-extra-args \
176     -Wformat-nonliteral \
177     -Wformat-security \
178     -Wformat-signedness \
179     -Wformat-y2k \
180     -Wformat-zero-length \
181     -Wframe-address \
182     -Wfree-nonheap-object \
183     -Whsa \
184     -Wif-not-aligned \
185     -Wignored-attributes \
186     -Wignored-qualifiers \
187     -Wimplicit \
188     -Wimplicit-function-declaration \
189     -Wimplicit-int \
190     -Wincompatible-pointer-types \
191     -Winit-self \
192     -Winline \
193     -Wint-conversion \
194     -Wint-in-bool-context \
195     -Wint-to-pointer-cast \
196     -Winvalid-memory-model \
197     -Winvalid-pch \
198     -Wlogical-not-parentheses \
199     -Wlogical-op \
200     -Wmain \
201     -Wmaybe-uninitialized \
202     -Wmemset-elt-size \
203     -Wmemset-transposed-args \
204     -Wmisleading-indentation \
205     -Wmissing-attributes \
206     -Wmissing-braces \
207     -Wmissing-declarations \
208     -Wmissing-field-initializers \
209     -Wmissing-include-dirs \
210     -Wmissing-parameter-type \
211     -Wmissing-profile \
212     -Wmissing-prototypes \
213     -Wmultichar \
214     -Wmultistatement-macros \
215     -Wnarrowing \
216     -Wnested-externs \
217     -Wnonnull \
218     -Wnonnull-compare \
219     -Wnull-dereference \
220     -Wodr \
221     -Wold-style-declaration \
222     -Wold-style-definition \
223     -Wopenmp-simd \
224     -Woverflow \
225     -Woverlength-strings \
226     -Woverride-init \
227     -Wpacked \
228     -Wpacked-bitfield-compat \
229     -Wpacked-not-aligned \
230     -Wparentheses \
231     -Wpointer-arith \
232     -Wpointer-compare \
233     -Wpointer-sign \
234     -Wpointer-to-int-cast \
235     -Wpragmas \
236     -Wpsabi \
237     -Wrestrict \
238     -Wreturn-local-addr \
239     -Wreturn-type \
240     -Wscalar-storage-order \
241     -Wsequence-point \
242     -Wshadow \
243     -Wshift-count-negative \
244     -Wshift-count-overflow \
245     -Wshift-negative-value \
246     -Wsizeof-array-argument \
247     -Wsizeof-pointer-div \
248     -Wsizeof-pointer-memaccess \
249     -Wstack-protector \
250     -Wstrict-aliasing \
251     -Wstrict-overflow \
252     -Wstrict-prototypes \
253     -Wstring-compare \
254     -Wstringop-truncation \
255     -Wsuggest-attribute=cold \
256     -Wsuggest-attribute=const \
257     -Wsuggest-attribute=format \
258     -Wsuggest-attribute=malloc \
259     -Wsuggest-attribute=noreturn \
260     -Wsuggest-attribute=pure \
261     -Wsuggest-final-methods \
262     -Wsuggest-final-types \
263     -Wswitch \
264     -Wswitch-bool \
265     -Wswitch-outside-range \
266     -Wswitch-unreachable \
267     -Wsync-nand \
268     -Wsystem-headers \
269     -Wtautological-compare \
270     -Wtrampolines \
271     -Wtrigraphs \
272     -Wtype-limits \
273     -Wuninitialized \
274     -Wunknown-pragmas \
275     -Wunsafe-loop-optimizations \
276     -Wunused \
277     -Wunused-but-set-parameter \
278     -Wunused-but-set-variable \
279     -Wunused-function \
280     -Wunused-label \
281     -Wunused-local-typedefs \
282     -Wunused-macros \
283     -Wunused-parameter \
284     -Wunused-result \
285     -Wunused-value \
286     -Wunused-variable \
287     -Wvarargs \
288     -Wvariadic-macros \
289     -Wvector-operation-performance \
290     -Wvla \
291     -Wvolatile-register-var \
292     -Wwrite-strings \
293     -Wzero-length-bounds \
294     \
295     ; do
296     gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
297   done
299   # gcc --help=warnings outputs an unusual form for these options; list
300   # them here so that the above 'comm' command doesn't report a false match.
301   # Would prefer "min (PTRDIFF_MAX, SIZE_MAX)", but it must be a literal.
302   # Also, AC_COMPUTE_INT requires it to fit in a long; it is 2**63 on
303   # the only platforms where it does not fit in a long, so make that
304   # a special case.
305   AC_MSG_CHECKING([max safe object size])
306   AC_COMPUTE_INT([gl_alloc_max],
307     [LONG_MAX < (PTRDIFF_MAX < (size_t) -1 ? PTRDIFF_MAX : (size_t) -1)
308      ? -1
309      : PTRDIFF_MAX < (size_t) -1 ? (long) PTRDIFF_MAX : (long) (size_t) -1],
310     [[#include <limits.h>
311       #include <stddef.h>
312       #include <stdint.h>
313     ]],
314     [gl_alloc_max=2147483647])
315   case $gl_alloc_max in
316     -1) gl_alloc_max=9223372036854775807;;
317   esac
318   AC_MSG_RESULT([$gl_alloc_max])
319   gl_manywarn_set="$gl_manywarn_set -Walloc-size-larger-than=$gl_alloc_max"
320   gl_manywarn_set="$gl_manywarn_set -Warray-bounds=2"
321   gl_manywarn_set="$gl_manywarn_set -Wattribute-alias=2"
322   gl_manywarn_set="$gl_manywarn_set -Wformat-overflow=2"
323   gl_manywarn_set="$gl_manywarn_set -Wformat-truncation=2"
324   gl_manywarn_set="$gl_manywarn_set -Wimplicit-fallthrough=5"
325   gl_manywarn_set="$gl_manywarn_set -Wnormalized=nfc"
326   gl_manywarn_set="$gl_manywarn_set -Wshift-overflow=2"
327   gl_manywarn_set="$gl_manywarn_set -Wstringop-overflow=2"
328   gl_manywarn_set="$gl_manywarn_set -Wunused-const-variable=2"
329   gl_manywarn_set="$gl_manywarn_set -Wvla-larger-than=4031"
331   # These are needed for older GCC versions.
332   if test -n "$GCC"; then
333     case `($CC --version) 2>/dev/null` in
334       'gcc (GCC) '[[0-3]].* | \
335       'gcc (GCC) '4.[[0-7]].*)
336         gl_manywarn_set="$gl_manywarn_set -fdiagnostics-show-option"
337         gl_manywarn_set="$gl_manywarn_set -funit-at-a-time"
338           ;;
339     esac
340   fi
342   # Disable specific options as needed.
343   if test "$gl_cv_cc_nomfi_needed" = yes; then
344     gl_manywarn_set="$gl_manywarn_set -Wno-missing-field-initializers"
345   fi
347   if test "$gl_cv_cc_uninitialized_supported" = no; then
348     gl_manywarn_set="$gl_manywarn_set -Wno-uninitialized"
349   fi
351   $1=$gl_manywarn_set
353   AC_LANG_POP([C])
356 # Specialization for _AC_LANG = C++.
357 # Use of m4_defun rather than AC_DEFUN works around a bug in autoconf < 2.63b.
358 m4_defun([gl_MANYWARN_ALL_GCC(C++)],
360   gl_MANYWARN_ALL_GCC_CXX_IMPL([$1])