Strict prototype are better for portability and to avoid bugs because it
makes sure the compiler has the information to properly validate the
arguments given when a function is called.

This flag however need special care when checking for it, because the
declaration for 'main' generated by autoconf cannot be a strict prototype
so the detection would always see the flag as failing.

This patch handles this by creating a dedicated macro for this detection
which uses a good prototype because we're in a case where it is possible,
so the detection will not always fail; it also makes sure to add the flag
to CFLAG only at the end, to avoid falsely crashing any further test done
in the configure script.

Signed-off-by: Christophe CURIS <christophe.cu...@free.fr>
---
 configure.ac          |  5 +++++
 m4/wm_cflags_check.m4 | 40 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index cc3e046..d46923e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -129,6 +129,11 @@ AS_IF([test "x$debug" = "xyes"],
      dnl more difficult, so try to avoid it
      AX_CFLAGS_GCC_OPTION([-Wredundant-decls])
      dnl
+     dnl Prototype of function must be explicit, no deprecated K&R syntax
+     dnl and no empty argument list which prevents compiler from doing
+     dnl type checking when using the function
+     WM_CFLAGS_GCC_OPTION_STRICTPROTO
+     dnl
      dnl Proper attributes helps the compiler to produce better code
      WM_CFLAGS_CHECK_FIRST([format attribute suggest],
          [-Wsuggest-attribute=format  dnl new gcc syntax
diff --git a/m4/wm_cflags_check.m4 b/m4/wm_cflags_check.m4
index 26745b6..b61b16d 100644
--- a/m4/wm_cflags_check.m4
+++ b/m4/wm_cflags_check.m4
@@ -122,6 +122,44 @@ _ACEOF
 ])
 
 
+# WM_CFLAGS_GCC_OPTION_STRICTPROTO
+# --------------------------------
+#
+# Check if the compiler support '-Wstrict-prototypes'. This would be done
+# traditionally using AX_CFLAGS_GCC_OPTION(...), but in the present case it
+# always fail because the test program generated by autoconf always use a
+# non-compliant prototype (this is needed for portability to avoid possible
+# function declaration mismatch on 'main').
+#
+# This macro works around this by providing a strict prototype for 'main'
+# in this case because we are in a very known case.
+AC_DEFUN([WM_CFLAGS_GCC_OPTION_STRICTPROTO],
+[AC_CACHE_CHECK([CFLAGS for gcc -Wstrict-prototypes], 
[wm_cv_c_checks_compopt_Wstrict_prototypes],
+  [AC_LANG_COMPILER_REQUIRE()dnl
+   AC_LANG_PUSH([C])
+   wm_save_CFLAGS="$CFLAGS"
+   CFLAGS="$wm_save_CFLAGS -pedantic -Werror -Wstrict-prototypes"
+   AC_COMPILE_IFELSE(
+     [AC_LANG_SOURCE([dnl
+int
+main(int argc, char **argv)
+{
+       /* to avoid failing on -Wunused-parameter */
+       (void) argc;
+       (void) argv;
+
+       return 0;
+}])],
+     [wm_cv_c_checks_compopt_Wstrict_prototypes="-Wstrict-prototypes"],
+     [wm_cv_c_checks_compopt_Wstrict_prototypes="no"])
+   CFLAGS="$wm_save_CFLAGS"
+   AC_LANG_POP([C]) ])
+# We cannot add this flag now to CFLAGS because it could break all further
+# detections done by configure, the flag will be added at the end by the
+# macro WM_CFLAGS_GCC_OPTION_POSTPONED
+])
+
+
 # WM_CFLAGS_GCC_OPTION_POSTPONED
 # ------------------------------
 #
@@ -133,7 +171,7 @@ _ACEOF
 AC_DEFUN_ONCE([WM_CFLAGS_GCC_OPTION_POSTPONED],
 [# WM_CFLAGS_GCC_OPTION_POSTPONED: add compiler flags at the end of the 
autoconf flow
 AS_IF([test "x$debug" = "xyes"],
-  [m4_foreach([arg_name], [[Wunused_macros]],
+  [m4_foreach([arg_name], [[Wstrict_prototypes], [Wunused_macros]],
     [AS_CASE([$wm_cv_c_checks_compopt_[]arg_name],
       [no*], [],
       [AS_IF([echo " $CFLAGS " | grep " $wm_cv_c_checks_compopt_[]arg_name " 
2>&1 > /dev/null],
-- 
2.1.3


-- 
To unsubscribe, send mail to wmaker-dev-unsubscr...@lists.windowmaker.org.

Reply via email to