Commit 4f68a88f5519e1bbc8b339dd48882f7f8c613edf

Guillem Jover 2021-02-19T06:55:17

build: Add compiler warnings support Detect as many warnings as possible during configure and enable them if the user did not supply any, so that any such problem can be spotted and fixed.

diff --git a/.gitignore b/.gitignore
index c805925..bece823 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,5 +20,4 @@ build-aux/
 configure
 config.*
 libtool
-m4/
 stamp-h1
diff --git a/configure.ac b/configure.ac
index 09cb310..1591a93 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,9 +52,45 @@ AC_PROG_INSTALL
 AC_PROG_LN_S
 
 # Set default compiler variables
-if test "$user_CFLAGS" = unset && test "$GCC" = yes; then
-  CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter"
-fi
+AS_IF([test "$user_CFLAGS" = unset], [
+  LIBBSD_CHECK_COMPILER_FLAG([-Wall])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wextra])
+
+  LIBBSD_CHECK_COMPILER_FLAG([-Wbad-function-cast])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wcast-align])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wdocumentation])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-branches])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-cond])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wformat=2])
+  LIBBSD_CHECK_COMPILER_FLAG([-Winit-self])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-op])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-declarations])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wnested-externs])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wnull-dereference])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wold-style-definition])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wpointer-arith])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wregister])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wrestrict])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wshadow])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wshift-negative-value])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wswitch-bool])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wvla])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wwrite-strings])
+
+  CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
+])
 
 # Checks for libraries.
 AC_CHECK_LIB([testu01], [unif01_CreateExternGenBits],
diff --git a/m4/.gitignore b/m4/.gitignore
new file mode 100644
index 0000000..10fb5d4
--- /dev/null
+++ b/m4/.gitignore
@@ -0,0 +1,2 @@
+*.m4
+!libbsd*.m4
diff --git a/m4/libbsd-compiler.m4 b/m4/libbsd-compiler.m4
new file mode 100644
index 0000000..b655b83
--- /dev/null
+++ b/m4/libbsd-compiler.m4
@@ -0,0 +1,24 @@
+# Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_CHECK_COMPILER_FLAG
+# -------------------------
+AC_DEFUN([LIBBSD_CHECK_COMPILER_FLAG], [
+  AS_VAR_PUSHDEF([libbsd_varname_cache], [libbsd_cv_cflags_$1])
+  AC_CACHE_CHECK([whether $CC accepts $1], [libbsd_varname_cache], [
+    m4_define([libbsd_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
+    AS_VAR_COPY([libbsd_save_CFLAGS], [CFLAGS])
+    AS_VAR_SET([CFLAGS], ["-Werror libbsd_check_flag"])
+    AC_COMPILE_IFELSE([
+      AC_LANG_SOURCE([[]])
+    ], [
+      AS_VAR_SET([libbsd_varname_cache], [yes])
+    ], [
+      AS_VAR_SET([libbsd_varname_cache], [no])
+    ])
+    AS_VAR_COPY([CFLAGS], [libbsd_save_CFLAGS])
+  ])
+  AS_VAR_IF([libbsd_varname_cache], [yes], [
+    AS_VAR_APPEND([LIBBSD_COMPILER_FLAGS], [" $1"])
+  ])
+  AS_VAR_POPDEF([libbsd_varname_cache])
+])