device: renew dhcp leases on awake for software devices
[NetworkManager.git] / m4 / compiler_warnings.m4
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], [
4         CFLAGS_SAVED="$CFLAGS"
5         CFLAGS="$CFLAGS -Werror -W$1"
6         AC_MSG_CHECKING(whether -W$1 works)
7
8         AC_COMPILE_IFELSE([AC_LANG_SOURCE([[]])], [
9                 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[$2]])], [
10                         AC_MSG_RESULT(yes)
11                         CFLAGS="$CFLAGS_SAVED -W$1"
12                 ],[
13                         AC_MSG_RESULT(no)
14                         CFLAGS="$CFLAGS_SAVED -Wno-$1"
15                 ])
16         ],[
17                 AC_MSG_RESULT(not supported)
18                 CFLAGS="$CFLAGS_SAVED"
19         ])
20 ])
21
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
28         AC_MSG_RESULT(yes)
29
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")
35         unset CFLAGS_SAVED
36
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], [])
42
43         CFLAGS_SAVED="$CFLAGS"
44         CFLAGS_MORE_WARNINGS="-Wall -std=gnu89"
45
46         if test "x$set_more_warnings" = xerror; then
47                 CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS -Werror"
48         fi
49
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 \
54                       -Wstrict-prototypes \
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([], [],
63                         has_option=yes,
64                         has_option=no,)
65                 if test $has_option != no; then
66                         CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS $option"
67                 fi
68                 AC_MSG_RESULT($has_option)
69                 unset has_option
70         done
71         unset option
72
73         CFLAGS="$CFLAGS_SAVED"
74         unset CFLAGS_SAVED
75
76         dnl Disable warnings triggered by known compiler problems
77
78         dnl https://bugzilla.gnome.org/show_bug.cgi?id=745821
79         NM_COMPILER_WARNING([unknown-attributes], [#include <glib.h>])
80
81         dnl https://bugzilla.gnome.org/show_bug.cgi?id=744473
82         NM_COMPILER_WARNING([typedef-redefinition], [#include <gio/gio.h>])
83
84         dnl https://llvm.org/bugs/show_bug.cgi?id=21614
85         NM_COMPILER_WARNING([array-bounds],
86                 [#include <string.h>]
87                 [void f () { strcmp ("something", "0"); }]
88         )
89
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; }]
94         )
95
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; }]
101         )
102
103         CFLAGS="$CFLAGS_MORE_WARNINGS $CFLAGS"
104 else
105         AC_MSG_RESULT(no)
106 fi
107 ])