]> The Tcpdump Group git mirrors - libpcap/commitdiff
Avoid some warnings from Sun C.
authorGuy Harris <[email protected]>
Sat, 22 Jun 2013 21:04:43 +0000 (14:04 -0700)
committerGuy Harris <[email protected]>
Sat, 22 Jun 2013 21:04:43 +0000 (14:04 -0700)
Some versions of Sun C support __attribute__ but don't support the
"unused" or "format" attributes - they don't fail, but they whine a lot.
Check whether they can be used without warnings, and use them only if
they can.

aclocal.m4
config.h.in
configure
configure.in
gencode.h

index 5462eb0b1db50ee825bed41565fe08c9718fce25..509f757b25c78116b38ad2569d33d98d7e909a8c 100644 (file)
@@ -112,12 +112,25 @@ AC_DEFUN(AC_LBL_C_INIT,
                            $1="-O2"
                    fi
            fi
+
+           #
+           # -Werror forces warnings to be errors.
+           #
+           ac_lbl_cc_force_warning_errors=-Werror
     else
            $2="$$2 -I/usr/local/include"
            LDFLAGS="$LDFLAGS -L/usr/local/lib"
 
            case "$host_os" in
 
+           darwin*)
+                   #
+                   # This is assumed either to be GCC or clang, both
+                   # of which use -Werror to force warnings to be errors.
+                   #
+                   ac_lbl_cc_force_warning_errors=-Werror
+                   ;;
+
            hpux*)
                    #
                    # HP C, which is what we presume we're using, doesn't
@@ -156,6 +169,14 @@ AC_DEFUN(AC_LBL_C_INIT,
                    $1="$$1 -g3"
                    ;;
 
+           solaris*)
+                   #
+                   # Assumed to be Sun C, which requires -errwarn to force
+                   # warnings to be treated as errors.
+                   #
+                   ac_lbl_cc_force_warning_errors=-errwarn
+                   ;;
+
            ultrix*)
                    AC_MSG_CHECKING(that Ultrix $CC hacks const in prototypes)
                    AC_CACHE_VAL(ac_cv_lbl_cc_const_proto,
@@ -1094,7 +1115,7 @@ dnl
 AC_DEFUN(AC_C___ATTRIBUTE__, [
 AC_MSG_CHECKING(for __attribute__)
 AC_CACHE_VAL(ac_cv___attribute__, [
-AC_COMPILE_IFELSE(
+AC_COMPILE_IFELSE([
   AC_LANG_SOURCE([[
 #include <stdlib.h>
 
@@ -1111,18 +1132,85 @@ main(int argc, char **argv)
 {
   foo();
 }
-  ]]),
+  ]])],
 ac_cv___attribute__=yes,
 ac_cv___attribute__=no)])
 if test "$ac_cv___attribute__" = "yes"; then
   AC_DEFINE(HAVE___ATTRIBUTE__, 1, [define if your compiler has __attribute__])
-  V_DEFS="$V_DEFS -D_U_=\"__attribute__((unused))\""
 else
+  #
+  # We can't use __attribute__, so we can't use __attribute__((unused)),
+  # so we define _U_ to an empty string.
+  #
   V_DEFS="$V_DEFS -D_U_=\"\""
 fi
 AC_MSG_RESULT($ac_cv___attribute__)
 ])
 
+dnl
+dnl Test whether __attribute__((unused)) can be used without warnings
+dnl
+
+AC_DEFUN(AC_C___ATTRIBUTE___UNUSED, [
+AC_MSG_CHECKING([whether __attribute__((unused)) can be used without warnings])
+AC_CACHE_VAL(ac_cv___attribute___unused, [
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+AC_COMPILE_IFELSE([
+  AC_LANG_SOURCE([[
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(int argc  __attribute((unused)), char **argv __attribute((unused)))
+{
+  printf("Hello, world!\n");
+  return 0;
+}
+  ]])],
+ac_cv___attribute___unused=yes,
+ac_cv___attribute___unused=no)])
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___unused" = "yes"; then
+  V_DEFS="$V_DEFS -D_U_=\"__attribute__((unused))\""
+else
+  V_DEFS="$V_DEFS -D_U_=\"\""
+fi
+AC_MSG_RESULT($ac_cv___attribute___unused)
+])
+
+dnl
+dnl Test whether __attribute__((format)) can be used without warnings
+dnl
+
+AC_DEFUN(AC_C___ATTRIBUTE___FORMAT, [
+AC_MSG_CHECKING([whether __attribute__((format)) can be used without warnings])
+AC_CACHE_VAL(ac_cv___attribute___format, [
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+AC_COMPILE_IFELSE([
+  AC_LANG_SOURCE([[
+#include <stdlib.h>
+
+extern int foo(const char *fmt, ...)
+                 __attribute__ ((format (printf, 1, 2)));
+
+int
+main(int argc, char **argv)
+{
+  foo("%s", "test");
+}
+  ]])],
+ac_cv___attribute___format=yes,
+ac_cv___attribute___format=no)])
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___format" = "yes"; then
+  AC_DEFINE(__ATTRIBUTE___FORMAT_OK, 1,
+    [define if your compiler allows __attribute__((format)) without a warning])
+fi
+AC_MSG_RESULT($ac_cv___attribute___format)
+])
+
 dnl
 dnl Checks to see if tpacket_stats is defined in linux/if_packet.h
 dnl If so then pcap-linux.c can use this to report proper statistics.
index 2debbcfb5b468c38bc34d8881899a59d99fa2394..1351a79cf1101092c62f4cd95acf2bc7fc1ecc7d 100644 (file)
 /* define on AIX to get certain functions */
 #undef _SUN
 
+/* define if your compiler allows __attribute__((format)) without a warning */
+#undef __ATTRIBUTE___FORMAT_OK
+
 /* to handle Ultrix compilers that don't support const in prototypes */
 #undef const
 
index d00d1db7de43b520d286098e33453172b2488a8c..ab808d3acf3b101c20ed6a95200ffe7761e0333a 100755 (executable)
--- a/configure
+++ b/configure
@@ -2948,12 +2948,25 @@ echo "${ECHO_T}$ac_cv_lbl_gcc_vers" >&6; }
                            V_CCOPT="-O2"
                    fi
            fi
+
+           #
+           # -Werror forces warnings to be errors.
+           #
+           ac_lbl_cc_force_warning_errors=-Werror
     else
            V_INCLS="$V_INCLS -I/usr/local/include"
            LDFLAGS="$LDFLAGS -L/usr/local/lib"
 
            case "$host_os" in
 
+           darwin*)
+                   #
+                   # This is assumed either to be GCC or clang, both
+                   # of which use -Werror to force warnings to be errors.
+                   #
+                   ac_lbl_cc_force_warning_errors=-Werror
+                   ;;
+
            hpux*)
                    #
                    # HP C, which is what we presume we're using, doesn't
@@ -2992,6 +3005,14 @@ echo "${ECHO_T}$ac_cv_lbl_gcc_vers" >&6; }
                    V_CCOPT="$V_CCOPT -g3"
                    ;;
 
+           solaris*)
+                   #
+                   # Assumed to be Sun C, which requires -errwarn to force
+                   # warnings to be treated as errors.
+                   #
+                   ac_lbl_cc_force_warning_errors=-errwarn
+                   ;;
+
            ultrix*)
                    { echo "$as_me:$LINENO: checking that Ultrix $CC hacks const in prototypes" >&5
 echo $ECHO_N "checking that Ultrix $CC hacks const in prototypes... $ECHO_C" >&6; }
@@ -3299,7 +3320,8 @@ if test "${ac_cv___attribute__+set}" = set; then
 else
 
 cat >conftest.$ac_ext <<_ACEOF
-/* confdefs.h.  */
+
+  /* confdefs.h.  */
 _ACEOF
 cat confdefs.h >>conftest.$ac_ext
 cat >>conftest.$ac_ext <<_ACEOF
@@ -3356,13 +3378,151 @@ cat >>confdefs.h <<\_ACEOF
 #define HAVE___ATTRIBUTE__ 1
 _ACEOF
 
-  V_DEFS="$V_DEFS -D_U_=\"__attribute__((unused))\""
 else
+  #
+  # We can't use __attribute__, so we can't use __attribute__((unused)),
+  # so we define _U_ to an empty string.
+  #
   V_DEFS="$V_DEFS -D_U_=\"\""
 fi
 { echo "$as_me:$LINENO: result: $ac_cv___attribute__" >&5
 echo "${ECHO_T}$ac_cv___attribute__" >&6; }
 
+if test "$ac_cv___attribute__" = "yes"; then
+
+{ echo "$as_me:$LINENO: checking whether __attribute__((unused)) can be used without warnings" >&5
+echo $ECHO_N "checking whether __attribute__((unused)) can be used without warnings... $ECHO_C" >&6; }
+if test "${ac_cv___attribute___unused+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+cat >conftest.$ac_ext <<_ACEOF
+
+  /* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(int argc  __attribute((unused)), char **argv __attribute((unused)))
+{
+  printf("Hello, world!\n");
+  return 0;
+}
+
+_ACEOF
+rm -f conftest.$ac_objext
+if { (ac_try="$ac_compile"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_compile") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+        test -z "$ac_c_werror_flag" ||
+        test ! -s conftest.err
+       } && test -s conftest.$ac_objext; then
+  ac_cv___attribute___unused=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+       ac_cv___attribute___unused=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___unused" = "yes"; then
+  V_DEFS="$V_DEFS -D_U_=\"__attribute__((unused))\""
+else
+  V_DEFS="$V_DEFS -D_U_=\"\""
+fi
+{ echo "$as_me:$LINENO: result: $ac_cv___attribute___unused" >&5
+echo "${ECHO_T}$ac_cv___attribute___unused" >&6; }
+
+
+{ echo "$as_me:$LINENO: checking whether __attribute__((format)) can be used without warnings" >&5
+echo $ECHO_N "checking whether __attribute__((format)) can be used without warnings... $ECHO_C" >&6; }
+if test "${ac_cv___attribute___format+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+cat >conftest.$ac_ext <<_ACEOF
+
+  /* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+#include <stdlib.h>
+
+extern int foo(const char *fmt, ...)
+                 __attribute__ ((format (printf, 1, 2)));
+
+int
+main(int argc, char **argv)
+{
+  foo("%s", "test");
+}
+
+_ACEOF
+rm -f conftest.$ac_objext
+if { (ac_try="$ac_compile"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_compile") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+        test -z "$ac_c_werror_flag" ||
+        test ! -s conftest.err
+       } && test -s conftest.$ac_objext; then
+  ac_cv___attribute___format=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+       ac_cv___attribute___format=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___format" = "yes"; then
+
+cat >>confdefs.h <<\_ACEOF
+#define __ATTRIBUTE___FORMAT_OK 1
+_ACEOF
+
+fi
+{ echo "$as_me:$LINENO: result: $ac_cv___attribute___format" >&5
+echo "${ECHO_T}$ac_cv___attribute___format" >&6; }
+
+fi
 
 ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
index 5ecc97aca769754d53e8d07458137a40de0d25f2..b7924129114dd337d2974e689e6757b540447c8f 100644 (file)
@@ -26,6 +26,10 @@ AC_LBL_C_INIT(V_CCOPT, V_INCLS)
 AC_LBL_SHLIBS_INIT
 AC_LBL_C_INLINE
 AC_C___ATTRIBUTE__
+if test "$ac_cv___attribute__" = "yes"; then
+       AC_C___ATTRIBUTE___UNUSED
+       AC_C___ATTRIBUTE___FORMAT
+fi
 
 AC_CHECK_HEADERS(sys/bitypes.h)
 
index c7f5a381919c6f4a474f6e553be7840f2b8a6617..afb69338fb5296a8a74aacc95fc596840e78fdb5 100644 (file)
--- a/gencode.h
+++ b/gencode.h
@@ -336,7 +336,11 @@ struct block *gen_p80211_fcdir(int);
 
 void bpf_optimize(struct block **);
 void bpf_error(const char *, ...)
-    __attribute__((noreturn, format (printf, 1, 2)));
+    __attribute__((noreturn))
+#ifdef __ATTRIBUTE___FORMAT_OK
+    __attribute__((format (printf, 1, 2)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+    ;
 
 void finish_parse(struct block *);
 char *sdup(const char *);