1 dnl Check whether a particular compiler flag works with code provided,
2 dnl disable it in CFLAGS if the check fails.
3 AC_DEFUN([NM_COMPILER_WARNING], [
5 CFLAGS="$CFLAGS -Werror -W$1"
6 AC_MSG_CHECKING(whether -W$1 works)
8 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[]])], [
9 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[$2]])], [
11 CFLAGS="$CFLAGS_SAVED -W$1"
14 CFLAGS="$CFLAGS_SAVED -Wno-$1"
17 AC_MSG_RESULT(not supported)
18 CFLAGS="$CFLAGS_SAVED"
22 AC_DEFUN([NM_COMPILER_WARNINGS],
23 [AC_ARG_ENABLE(more-warnings,
24 AS_HELP_STRING([--enable-more-warnings], [Possible values: no/yes/error]),
25 set_more_warnings="$enableval",set_more_warnings=yes)
26 AC_MSG_CHECKING(for more warnings)
27 if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
30 dnl This is enabled in clang by default, makes little sense,
31 dnl and causes the build to abort with -Werror.
32 CFLAGS_SAVED="$CFLAGS"
33 CFLAGS="$CFLAGS -Qunused-arguments"
34 AC_COMPILE_IFELSE([AC_LANG_SOURCE([])], [], CFLAGS="$CFLAGS_SAVED")
37 dnl clang only warns about unknown warnings, unless
38 dnl called with "-Werror=unknown-warning-option"
39 dnl Test if the compiler supports that, and if it does
40 dnl attach it to the CFLAGS.
41 NM_COMPILER_WARNING([unknown-warning-option], [])
43 CFLAGS_SAVED="$CFLAGS"
44 CFLAGS_MORE_WARNINGS="-Wall -std=gnu89"
46 if test "x$set_more_warnings" = xerror; then
47 CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS -Werror"
50 for option in -Wshadow -Wmissing-declarations -Wmissing-prototypes \
51 -Wdeclaration-after-statement -Wformat-security \
52 -Wfloat-equal -Wno-unused-parameter -Wno-sign-compare \
53 -Wno-duplicate-decl-specifier \
55 -fno-strict-aliasing -Wno-unused-but-set-variable \
56 -Wundef -Wimplicit-function-declaration \
57 -Wpointer-arith -Winit-self \
58 -Wmissing-include-dirs -Wno-pragmas; do
59 dnl GCC 4.4 does not warn when checking for -Wno-* flags (https://gcc.gnu.org/wiki/FAQ#wnowarning)
60 CFLAGS="-Werror $CFLAGS_MORE_WARNINGS $(printf '%s' "$option" | sed 's/^-Wno-/-W/') $CFLAGS_SAVED"
61 AC_MSG_CHECKING([whether compiler understands $option])
62 AC_TRY_COMPILE([], [],
65 if test $has_option != no; then
66 CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS $option"
68 AC_MSG_RESULT($has_option)
73 CFLAGS="$CFLAGS_SAVED"
76 dnl Disable warnings triggered by known compiler problems
78 dnl https://bugzilla.gnome.org/show_bug.cgi?id=745821
79 NM_COMPILER_WARNING([unknown-attributes], [#include <glib.h>])
81 dnl https://bugzilla.gnome.org/show_bug.cgi?id=744473
82 NM_COMPILER_WARNING([typedef-redefinition], [#include <gio/gio.h>])
84 dnl https://llvm.org/bugs/show_bug.cgi?id=21614
85 NM_COMPILER_WARNING([array-bounds],
87 [void f () { strcmp ("something", "0"); }]
90 dnl https://llvm.org/bugs/show_bug.cgi?id=22949
91 NM_COMPILER_WARNING([parentheses-equality],
92 [#include <sys/wait.h>]
93 [void f () { if (WIFCONTINUED(0)) return; }]
96 dnl systemd-dhcp's log_internal macro and our handle_warn are sometimes
97 dnl used in void context,u sometimes in int. Makes clang unhappy.
98 NM_COMPILER_WARNING([unused-value],
99 [#define yolo ({ (666 + 666); })]
100 [int f () { int i = yolo; yolo; return i; }]
103 CFLAGS="$CFLAGS_MORE_WARNINGS $CFLAGS"