[linux-nfc] [neard][PATCH v2 67/73] build: add more compiler warnings

Krzysztof Kozlowski <[email protected]> Mon, 19 Jul 2021 13:08:13 +0200
Newsgroups org.01.lists.linux-nfc,dev.linux.lists.oe-linux-nfc
Message-ID <[email protected]>
Use the AX_CHECK_COMPILE_FLAG() macro from autoconf-archive to check for
support for more compiler warning flags (not enabled via -Wall and
-Wextra) and enable these which pass.

These flags are warning for:
1. Code bugs like dereferencing NULL or truncating string operations
   functions.

2. Minor code quality or portability issues like using undefined
   defines, wrong casts of functions, missing global function
   declarations, skipping variable initializations, duplicated if
   conditions or branches, usage of variable-length arrays.

3. Portability issues like implicit promoting float to double,
   allocating zero bytes or depending of size of void *.

Keep more warnings for later, because the code does not build with them.

The AX_CHECK_COMPILE_FLAG() macro tries to compile C code with given
flag, so move the code from acinclude.m4 (executed early, before AC_LANG
and others) to separate accflags.m4.

Signed-off-by: Krzysztof Kozlowski <[email protected]>
---
 HACKING      |  1 +
 accflags.m4  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 acinclude.m4 | 17 -----------------
 configure.ac |  5 +++--
 4 files changed, 49 insertions(+), 19 deletions(-)
 create mode 100644 accflags.m4

diff --git a/HACKING b/HACKING
index c1eb6d6189a6..7326242506a6 100644
--- a/HACKING
+++ b/HACKING
@@ -7,6 +7,7 @@ Build tools requirements
 
 Required packages and tools:
   autoconf
+  autoconf-archive
   automake
   dbus
   gcc/clang
diff --git a/accflags.m4 b/accflags.m4
new file mode 100644
index 000000000000..b1bb1500324f
--- /dev/null
+++ b/accflags.m4
@@ -0,0 +1,45 @@
+AC_DEFUN([NEARD_COMPILER_FLAGS], [
+	# AX_CHECK_COMPILE_FLAG comes from autoconf-archive
+	AC_REQUIRE([AC_PROG_CC])
+	AC_REQUIRE([AX_CHECK_COMPILE_FLAG])
+
+	if (test "${CFLAGS}" = ""); then
+		CFLAGS="-Wall -O2 -D_FORTIFY_SOURCE=2"
+	fi
+	if (test "$USE_MAINTAINER_MODE" = "yes"); then
+		CFLAGS="$CFLAGS -Werror -Wextra"
+		CFLAGS="$CFLAGS -Wno-unused-parameter"
+		CFLAGS="$CFLAGS -Wno-missing-field-initializers"
+		CFLAGS="$CFLAGS -Wdeclaration-after-statement"
+		CFLAGS="$CFLAGS -Wmissing-declarations"
+		CFLAGS="$CFLAGS -Wredundant-decls"
+		CFLAGS="$CFLAGS -Wcast-align"
+		CFLAGS="$CFLAGS -DG_DISABLE_DEPRECATED"
+
+		AX_CHECK_COMPILE_FLAG([-Wdouble-promotion], [CFLAGS="$CFLAGS -Wdouble-promotion"])
+		AX_CHECK_COMPILE_FLAG([-Wundef], [CFLAGS="$CFLAGS -Wundef"])
+		AX_CHECK_COMPILE_FLAG([-Wbad-function-cast], [CFLAGS="$CFLAGS -Wbad-function-cast"])
+		AX_CHECK_COMPILE_FLAG([-Wmissing-prototypes], [CFLAGS="$CFLAGS -Wmissing-prototypes"])
+		AX_CHECK_COMPILE_FLAG([-Wjump-misses-init], [CFLAGS="$CFLAGS -Wjump-misses-init"])
+		AX_CHECK_COMPILE_FLAG([-Wpointer-arith], [CFLAGS="$CFLAGS -Wpointer-arith"])
+
+		# GCC v6.0
+		AX_CHECK_COMPILE_FLAG([-Wnull-dereference], [CFLAGS="$CFLAGS -Wnull-dereference"])
+		AX_CHECK_COMPILE_FLAG([-Wduplicated-cond], [CFLAGS="$CFLAGS -Wduplicated-cond"])
+		# GCC v7.0
+		AX_CHECK_COMPILE_FLAG([-Wduplicated-branches], [CFLAGS="$CFLAGS -Wduplicated-branches"])
+		AX_CHECK_COMPILE_FLAG([-Wvla-larger-than=1], [CFLAGS="$CFLAGS -Wvla-larger-than=1"])
+		AX_CHECK_COMPILE_FLAG([-Walloc-zero], [CFLAGS="$CFLAGS -Walloc-zero"])
+		# GCC v8.0
+		AX_CHECK_COMPILE_FLAG([-Wstringop-truncation], [CFLAGS="$CFLAGS -Wstringop-truncation"])
+	fi
+	if (test "$USE_MAINTAINER_MODE" = "pedantic"); then
+		AX_CHECK_COMPILE_FLAG([-Wstrict-overflow=3], [CFLAGS="$CFLAGS -Wstrict-overflow=3"])
+		AX_CHECK_COMPILE_FLAG([-Wshadow], [CFLAGS="$CFLAGS -Wshadow"])
+		AX_CHECK_COMPILE_FLAG([-Wcast-qual], [CFLAGS="$CFLAGS -Wcast-qual"])
+		AX_CHECK_COMPILE_FLAG([-Wunsafe-loop-optimizations], [CFLAGS="$CFLAGS -Wunsafe-loop-optimizations"])
+
+		# GCC v5.0
+		AX_CHECK_COMPILE_FLAG([-Wformat-signedness], [CFLAGS="$CFLAGS -Wformat-signedness"])
+	fi
+])
diff --git a/acinclude.m4 b/acinclude.m4
index 027ed69977db..a12b93972cfa 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -9,20 +9,3 @@ AC_DEFUN([NEARD_PROG_CC_PIE], [
 		rm -rf conftest*
 	])
 ])
-
-AC_DEFUN([NEARD_COMPILER_FLAGS], [
-	if (test "${CFLAGS}" = ""); then
-		CFLAGS="-Wall -O2 -D_FORTIFY_SOURCE=2"
-	fi
-	if (test "$USE_MAINTAINER_MODE" = "yes"); then
-		CFLAGS="$CFLAGS -Werror -Wextra"
-		CFLAGS="$CFLAGS -Werror -Wextra"
-		CFLAGS="$CFLAGS -Wno-unused-parameter"
-		CFLAGS="$CFLAGS -Wno-missing-field-initializers"
-		CFLAGS="$CFLAGS -Wdeclaration-after-statement"
-		CFLAGS="$CFLAGS -Wmissing-declarations"
-		CFLAGS="$CFLAGS -Wredundant-decls"
-		CFLAGS="$CFLAGS -Wcast-align"
-		CFLAGS="$CFLAGS -DG_DISABLE_DEPRECATED"
-	fi
-])
diff --git a/configure.ac b/configure.ac
index c4e8993305b1..b9419c1fad74 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,8 +22,6 @@ AC_SUBST(se_plugindir)
 
 PKG_PROG_PKG_CONFIG
 
-NEARD_COMPILER_FLAGS
-
 AC_LANG([C])
 
 AC_PROG_CC
@@ -34,6 +32,9 @@ AC_PROG_MKDIR_P
 m4_define([_LT_AC_TAGCONFIG], [])
 m4_ifdef([AC_LIBTOOL_TAGS], [AC_LIBTOOL_TAGS([])])
 
+m4_include([accflags.m4])
+NEARD_COMPILER_FLAGS
+
 LT_INIT([disable-static])
 
 AM_CONDITIONAL(READLINE, test "${enable_readline}" = "yes")
-- 
2.27.0
_______________________________________________
Linux-nfc mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s