pthread-cond: Fix compilation error on native Windows.
[gnulib.git] / m4 / manywarnings.m4
blob14bc5041eaa87569a51cb6ad6e7a753cd8a5885d
1 # manywarnings.m4
2 # serial 26
3 dnl Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl From Simon Josefsson
10 AC_PREREQ([2.64])
12 # gl_MANYWARN_COMPLEMENT(OUTVAR, LISTVAR, REMOVEVAR)
13 # --------------------------------------------------
14 # Copy LISTVAR to OUTVAR except for the entries in REMOVEVAR.
15 # Elements separated by whitespace.  In set logic terms, the function
16 # does OUTVAR = LISTVAR \ REMOVEVAR.
17 AC_DEFUN([gl_MANYWARN_COMPLEMENT],
19   gl_warn_set=
20   set x $2; shift
21   for gl_warn_item
22   do
23     case " $3 " in
24       *" $gl_warn_item "*)
25         ;;
26       *)
27         AS_VAR_APPEND([gl_warn_set], [" $gl_warn_item"])
28         ;;
29     esac
30   done
31   $1=$gl_warn_set
34 # gl_MANYWARN_ALL_GCC(VARIABLE)
35 # -----------------------------
36 # Add all documented GCC warning parameters to variable VARIABLE.
37 # Note that you need to test them using gl_WARN_ADD if you want to
38 # make sure your gcc understands it.
40 # The effects of this macro depend on the current language (_AC_LANG).
41 AC_DEFUN([gl_MANYWARN_ALL_GCC],
42 [_AC_LANG_DISPATCH([$0], _AC_LANG, $@)])
44 # Specialization for _AC_LANG = C.
45 AC_DEFUN([gl_MANYWARN_ALL_GCC(C)],
47   AC_LANG_PUSH([C])
49   dnl First, check for some issues that only occur when combining multiple
50   dnl gcc warning categories.
51   AC_REQUIRE([AC_PROG_CC])
52   AS_IF([test -n "$GCC"], [
53     AC_CACHE_CHECK([whether -Wno-missing-field-initializers is needed],
54       [gl_cv_cc_nomfi_needed],
55       [gl_cv_cc_nomfi_needed=no
56        gl_saved_CFLAGS="$CFLAGS"
57        CFLAGS="$CFLAGS -Wextra -Werror"
58        AC_COMPILE_IFELSE(
59          [AC_LANG_PROGRAM(
60             [[struct file_data { int desc, name; };
61               struct cmp { struct file_data file[1]; };
62               void f (struct cmp *r)
63               {
64                 typedef struct { int a; int b; } s_t;
65                 s_t s1 = { 0, };
66                 struct cmp cmp = { .file[0].desc = r->file[0].desc + s1.a };
67                 *r = cmp;
68               }
69             ]],
70             [[]])],
71          [],
72          [CFLAGS="$CFLAGS -Wno-missing-field-initializers"
73           AC_COMPILE_IFELSE([],
74             [gl_cv_cc_nomfi_needed=yes])])
75        CFLAGS="$gl_saved_CFLAGS"
76     ])
78     dnl Next, check if -Werror -Wuninitialized is useful with the
79     dnl user's choice of $CFLAGS; some versions of gcc warn that it
80     dnl has no effect if -O is not also used
81     AC_CACHE_CHECK([whether -Wuninitialized is supported],
82       [gl_cv_cc_uninitialized_supported],
83       [gl_saved_CFLAGS="$CFLAGS"
84        CFLAGS="$CFLAGS -Werror -Wuninitialized"
85        AC_COMPILE_IFELSE(
86          [AC_LANG_PROGRAM([[]], [[]])],
87          [gl_cv_cc_uninitialized_supported=yes],
88          [gl_cv_cc_uninitialized_supported=no])
89        CFLAGS="$gl_saved_CFLAGS"
90       ])
91   ])
93   # List all gcc warning categories.
94   # To compare this list to your installed GCC's, run this Bash command:
95   #
96   # comm -3 \
97   #  <((sed -n 's/^  *\(-[^ 0-9][^ ]*\).*/\1/p' manywarnings.m4; \
98   #     awk '/^[^#]/ {print $1}' ../build-aux/gcc-warning.spec) | sort) \
99   #  <(LC_ALL=C gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort)
101   $1=
102   for gl_manywarn_item in -fanalyzer -fstrict-flex-arrays \
103     -Wall \
104     -Warith-conversion \
105     -Wbad-function-cast \
106     -Wcast-align=strict \
107     -Wdate-time \
108     -Wdisabled-optimization \
109     -Wdouble-promotion \
110     -Wduplicated-branches \
111     -Wduplicated-cond \
112     -Wextra \
113     -Wformat-signedness \
114     -Wflex-array-member-not-at-end \
115     -Winit-self \
116     -Winline \
117     -Winvalid-pch \
118     -Wlogical-op \
119     -Wmissing-declarations \
120     -Wmissing-include-dirs \
121     -Wmissing-prototypes \
122     -Wmissing-variable-declarations \
123     -Wnested-externs \
124     -Wnull-dereference \
125     -Wold-style-definition \
126     -Wopenmp-simd \
127     -Woverlength-strings \
128     -Wpacked \
129     -Wpointer-arith \
130     -Wshadow \
131     -Wstack-protector \
132     -Wstrict-flex-arrays \
133     -Wstrict-overflow \
134     -Wstrict-prototypes \
135     -Wsuggest-attribute=cold \
136     -Wsuggest-attribute=const \
137     -Wsuggest-attribute=format \
138     -Wsuggest-attribute=malloc \
139     -Wsuggest-attribute=noreturn \
140     -Wsuggest-attribute=pure \
141     -Wsuggest-final-methods \
142     -Wsuggest-final-types \
143     -Wsync-nand \
144     -Wsystem-headers \
145     -Wtrampolines \
146     -Wuninitialized \
147     -Wunknown-pragmas \
148     -Wunsafe-loop-optimizations \
149     -Wunused-macros \
150     -Wvariadic-macros \
151     -Wvector-operation-performance \
152     -Wvla \
153     -Wwrite-strings \
154     \
155     ; do
156     AS_VAR_APPEND([$1], [" $gl_manywarn_item"])
157   done
159   # gcc --help=warnings outputs an unusual form for these options; list
160   # them here so that the above 'comm' command doesn't report a false match.
161   AS_VAR_APPEND([$1], [' -Warray-bounds=2'])
162   AS_VAR_APPEND([$1], [' -Wattribute-alias=2'])
163   AS_VAR_APPEND([$1], [' -Wbidi-chars=any,ucn'])
164   AS_VAR_APPEND([$1], [' -Wformat-overflow=2'])
165   AS_VAR_APPEND([$1], [' -Wformat=2'])
166   AS_VAR_APPEND([$1], [' -Wformat-truncation=2'])
167   AS_VAR_APPEND([$1], [' -Wimplicit-fallthrough=5'])
168   AS_VAR_APPEND([$1], [' -Wshift-overflow=2'])
169   AS_VAR_APPEND([$1], [' -Wuse-after-free=3'])
170   AS_VAR_APPEND([$1], [' -Wunused-const-variable=2'])
171   AS_VAR_APPEND([$1], [' -Wvla-larger-than=4031'])
173   # These are needed for older GCC versions.
174   if test -n "$GCC" && gl_gcc_version=`($CC --version) 2>/dev/null`; then
175     case $gl_gcc_version in
176       'gcc (GCC) '[[0-3]].* | \
177       'gcc (GCC) '4.[[0-7]].*)
178         AS_VAR_APPEND([$1], [' -fdiagnostics-show-option'])
179         AS_VAR_APPEND([$1], [' -funit-at-a-time'])
180           ;;
181     esac
182     case $gl_gcc_version in
183       'gcc (GCC) '[[0-9]].*)
184         AS_VAR_APPEND([$1], [' -fno-common'])
185           ;;
186     esac
187   fi
189   # Disable specific options as needed.
190   if test "$gl_cv_cc_nomfi_needed" = yes; then
191     AS_VAR_APPEND([$1], [' -Wno-missing-field-initializers'])
192   fi
194   if test "$gl_cv_cc_uninitialized_supported" = no; then
195     AS_VAR_APPEND([$1], [' -Wno-uninitialized'])
196   fi
198   # This warning have too many false alarms in GCC 11.2.1.
199   # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101713
200   AS_VAR_APPEND([$1], [' -Wno-analyzer-malloc-leak'])
202   AC_LANG_POP([C])
205 # Specialization for _AC_LANG = C++.
206 AC_DEFUN([gl_MANYWARN_ALL_GCC(C++)],
208   gl_MANYWARN_ALL_GCC_CXX_IMPL([$1])