manywarnings: Add support for C++.
[gnulib/ericb.git] / m4 / manywarnings-c++.m4
blobc023f88cca2475442554fbb3a2a5fabeda0782d4
1 # manywarnings-c++.m4 serial 1
2 dnl Copyright (C) 2008-2017 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 # Implementation of the specialization of gl_MANYWARN_ALL_GCC
8 # for _AC_LANG = C++.
9 AC_DEFUN([gl_MANYWARN_ALL_GCC_CXX_IMPL],
11   AC_LANG_PUSH([C++])
13   dnl First, check for some issues that only occur when combining multiple
14   dnl gcc warning categories.
15   AC_REQUIRE([AC_PROG_CXX])
16   if test -n "$GXX"; then
18     dnl Check if -W -Werror -Wno-missing-field-initializers is supported
19     dnl with the current $CXX $CXXFLAGS $CPPFLAGS.
20     AC_MSG_CHECKING([whether -Wno-missing-field-initializers is supported])
21     AC_CACHE_VAL([gl_cv_cxx_nomfi_supported], [
22       gl_save_CXXFLAGS="$CXXFLAGS"
23       CXXFLAGS="$CXXFLAGS -W -Werror -Wno-missing-field-initializers"
24       AC_COMPILE_IFELSE(
25         [AC_LANG_PROGRAM([[]], [[]])],
26         [gl_cv_cxx_nomfi_supported=yes],
27         [gl_cv_cxx_nomfi_supported=no])
28       CXXFLAGS="$gl_save_CXXFLAGS"])
29     AC_MSG_RESULT([$gl_cv_cxx_nomfi_supported])
31     if test "$gl_cv_cxx_nomfi_supported" = yes; then
32       dnl Now check whether -Wno-missing-field-initializers is needed
33       dnl for the { 0, } construct.
34       AC_MSG_CHECKING([whether -Wno-missing-field-initializers is needed])
35       AC_CACHE_VAL([gl_cv_cxx_nomfi_needed], [
36         gl_save_CXXFLAGS="$CXXFLAGS"
37         CXXFLAGS="$CXXFLAGS -W -Werror"
38         AC_COMPILE_IFELSE(
39           [AC_LANG_PROGRAM(
40              [[int f (void)
41                {
42                  typedef struct { int a; int b; } s_t;
43                  s_t s1 = { 0, };
44                  return s1.b;
45                }
46              ]],
47              [[]])],
48           [gl_cv_cxx_nomfi_needed=no],
49           [gl_cv_cxx_nomfi_needed=yes])
50         CXXFLAGS="$gl_save_CXXFLAGS"
51       ])
52       AC_MSG_RESULT([$gl_cv_cxx_nomfi_needed])
53     fi
55     dnl Next, check if -Werror -Wuninitialized is useful with the
56     dnl user's choice of $CXXFLAGS; some versions of gcc warn that it
57     dnl has no effect if -O is not also used
58     AC_MSG_CHECKING([whether -Wuninitialized is supported])
59     AC_CACHE_VAL([gl_cv_cxx_uninitialized_supported], [
60       gl_save_CXXFLAGS="$CXXFLAGS"
61       CXXFLAGS="$CXXFLAGS -Werror -Wuninitialized"
62       AC_COMPILE_IFELSE(
63         [AC_LANG_PROGRAM([[]], [[]])],
64         [gl_cv_cxx_uninitialized_supported=yes],
65         [gl_cv_cxx_uninitialized_supported=no])
66       CXXFLAGS="$gl_save_CXXFLAGS"])
67     AC_MSG_RESULT([$gl_cv_cxx_uninitialized_supported])
69   fi
71   # List all gcc warning categories.
72   # To compare this list to your installed GCC's, run this Bash command:
73   #
74   # comm -3 \
75   #  <(sed -n 's/^  *\(-[^ ]*\) .*/\1/p' manywarnings-c++.m4 | sort) \
76   #  <(gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort |
77   #      grep -v -x -f <(
78   #         awk '/^[^#]/ {print $1}' ../build-aux/g++-warning.spec))
80   gl_manywarn_set=
81   for gl_manywarn_item in \
82     -W \
83     -Wabi \
84     -Wabi-tag \
85     -Waddress \
86     -Waggressive-loop-optimizations \
87     -Wall \
88     -Wattributes \
89     -Wbool-compare \
90     -Wbuiltin-macro-redefined \
91     -Wcast-align \
92     -Wchar-subscripts \
93     -Wchkp \
94     -Wclobbered \
95     -Wcomment \
96     -Wcomments \
97     -Wconditionally-supported \
98     -Wconversion-null \
99     -Wcoverage-mismatch \
100     -Wcpp \
101     -Wctor-dtor-privacy \
102     -Wdate-time \
103     -Wdelete-incomplete \
104     -Wdelete-non-virtual-dtor \
105     -Wdeprecated \
106     -Wdeprecated-declarations \
107     -Wdisabled-optimization \
108     -Wdiv-by-zero \
109     -Wdouble-promotion \
110     -Weffc++ \
111     -Wempty-body \
112     -Wendif-labels \
113     -Wenum-compare \
114     -Wextra \
115     -Wformat-contains-nul \
116     -Wformat-extra-args \
117     -Wformat-nonliteral \
118     -Wformat-security \
119     -Wformat-signedness \
120     -Wformat-y2k \
121     -Wformat-zero-length \
122     -Wfree-nonheap-object \
123     -Wignored-qualifiers \
124     -Winherited-variadic-ctor \
125     -Winit-self \
126     -Winline \
127     -Wint-to-pointer-cast \
128     -Winvalid-memory-model \
129     -Winvalid-offsetof \
130     -Winvalid-pch \
131     -Wliteral-suffix \
132     -Wlogical-not-parentheses \
133     -Wlogical-op \
134     -Wmain \
135     -Wmaybe-uninitialized \
136     -Wmemset-transposed-args \
137     -Wmissing-braces \
138     -Wmissing-declarations \
139     -Wmissing-field-initializers \
140     -Wmissing-include-dirs \
141     -Wmultichar \
142     -Wnarrowing \
143     -Wnoexcept \
144     -Wnon-template-friend \
145     -Wnon-virtual-dtor \
146     -Wnonnull \
147     -Wodr \
148     -Wold-style-cast \
149     -Wopenmp-simd \
150     -Woverflow \
151     -Woverlength-strings \
152     -Woverloaded-virtual \
153     -Wpacked \
154     -Wpacked-bitfield-compat \
155     -Wparentheses \
156     -Wpmf-conversions \
157     -Wpointer-arith \
158     -Wpragmas \
159     -Wreorder \
160     -Wreturn-local-addr \
161     -Wreturn-type \
162     -Wsequence-point \
163     -Wshadow \
164     -Wshift-count-negative \
165     -Wshift-count-overflow \
166     -Wsign-promo \
167     -Wsized-deallocation \
168     -Wsizeof-array-argument \
169     -Wsizeof-pointer-memaccess \
170     -Wstack-protector \
171     -Wstrict-aliasing \
172     -Wstrict-null-sentinel \
173     -Wstrict-overflow \
174     -Wsuggest-attribute=const \
175     -Wsuggest-attribute=format \
176     -Wsuggest-attribute=noreturn \
177     -Wsuggest-attribute=pure \
178     -Wsuggest-final-methods \
179     -Wsuggest-final-types \
180     -Wsuggest-override \
181     -Wswitch \
182     -Wswitch-bool \
183     -Wswitch-default \
184     -Wsync-nand \
185     -Wsystem-headers \
186     -Wtrampolines \
187     -Wtrigraphs \
188     -Wtype-limits \
189     -Wuninitialized \
190     -Wunknown-pragmas \
191     -Wunsafe-loop-optimizations \
192     -Wunused \
193     -Wunused-but-set-parameter \
194     -Wunused-but-set-variable \
195     -Wunused-function \
196     -Wunused-label \
197     -Wunused-local-typedefs \
198     -Wunused-macros \
199     -Wunused-parameter \
200     -Wunused-result \
201     -Wunused-value \
202     -Wunused-variable \
203     -Wuseless-cast \
204     -Wvarargs \
205     -Wvariadic-macros \
206     -Wvector-operation-performance \
207     -Wvirtual-move-assign \
208     -Wvla \
209     -Wvolatile-register-var \
210     -Wwrite-strings \
211     -Wzero-as-null-pointer-constant \
212     \
213     ; do
214     gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
215   done
217   # gcc --help=warnings outputs an unusual form for these options; list
218   # them here so that the above 'comm' command doesn't report a false match.
219   gl_manywarn_set="$gl_manywarn_set -Warray-bounds=2"
220   gl_manywarn_set="$gl_manywarn_set -Wnormalized=nfc"
221   gl_manywarn_set="$gl_manywarn_set -Wshift-overflow=2"
222   gl_manywarn_set="$gl_manywarn_set -Wunused-const-variable=2"
224   # These are needed for older GCC versions.
225   if test -n "$GXX"; then
226     case `($CXX --version) 2>/dev/null` in
227       'g++ (GCC) '[[0-3]].* | \
228       'g++ (GCC) '4.[[0-7]].*)
229         gl_manywarn_set="$gl_manywarn_set -fdiagnostics-show-option"
230         gl_manywarn_set="$gl_manywarn_set -funit-at-a-time"
231           ;;
232     esac
233   fi
235   # Disable specific options as needed.
236   if test "$gl_cv_cxx_nomfi_needed" = yes; then
237     gl_manywarn_set="$gl_manywarn_set -Wno-missing-field-initializers"
238   fi
240   if test "$gl_cv_cxx_uninitialized_supported" = no; then
241     gl_manywarn_set="$gl_manywarn_set -Wno-uninitialized"
242   fi
244   $1=$gl_manywarn_set
246   AC_LANG_POP([C++])