]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Avoid some warnings from Sun C.
authorGuy Harris <[email protected]>
Sat, 22 Jun 2013 21:06:33 +0000 (14:06 -0700)
committerGuy Harris <[email protected]>
Sat, 22 Jun 2013 21:06:33 +0000 (14:06 -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.
They also support the "noreturn" attribute, but don't allow it to be
applied to function pointers, only to functions.

Check whether they can be used without warnings, and use them only if
they can.

aclocal.m4
config.h.in
configure
configure.in
interface.h
machdep.c
netdissect.h

index 7bdfd81bc6a7aba06a0ef90a4c8bd3533c1d4510..a2da5119dccd7271380edf2988b0cefa5a98727a 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 -O"
                    ;;
 
+           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,
@@ -1299,14 +1320,81 @@ 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 Test whether __attribute__((format)) can be applied to function
 dnl pointers
@@ -1337,6 +1425,34 @@ fi
 AC_MSG_RESULT($ac_cv___attribute___format_function_pointer)
 ])
 
+AC_DEFUN(AC_C___ATTRIBUTE___NORETURN_FUNCTION_POINTER, [
+AC_MSG_CHECKING([whether __attribute__((noreturn)) can be applied to function pointers without warnings])
+AC_CACHE_VAL(ac_cv___attribute___noreturn_function_pointer, [
+save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS $ac_lbl_cc_force_warning_errors"
+AC_COMPILE_IFELSE([
+  AC_LANG_SOURCE([[
+#include <stdlib.h>
+
+extern int (*foo)(int i)
+                 __attribute__ ((noreturn));
+
+int
+main(int argc, char **argv)
+{
+  (*foo)(1);
+}
+  ]])],
+ac_cv___attribute___noreturn_function_pointer=yes,
+ac_cv___attribute___noreturn_function_pointer=no)])
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___noreturn_function_pointer" = "yes"; then
+  AC_DEFINE(__ATTRIBUTE___NORETURN_OK_FOR_FUNCTION_POINTERS, 1,
+    [define if your compiler allows __attribute__((noreturn)) to be applied to function pointers])
+fi
+AC_MSG_RESULT($ac_cv___attribute___noreturn_function_pointer)
+])
+
 AC_DEFUN(AC_LBL_SSLEAY,
     [
        #
index 4c48e6fb0fea76db1d2eb8779759f667446a6c50..3a2da984944eb547681b58b42a8177a07368f993 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
+
 /* define if your compiler allows __attribute__((format)) to be applied to
    function pointers */
 #undef __ATTRIBUTE___FORMAT_OK_FOR_FUNCTION_POINTERS
 
+/* define if your compiler allows __attribute__((noreturn)) to be applied to
+   function pointers */
+#undef __ATTRIBUTE___NORETURN_OK_FOR_FUNCTION_POINTERS
+
 /* to handle Ultrix compilers that don't support const in prototypes */
 #undef const
 
index 5fde04c99bd7d38f00f8459aa46a6ad80948bdbf..a56eb7b073095a3ed0f1140bab363ac82ba1cd8f 100755 (executable)
--- a/configure
+++ b/configure
@@ -2851,12 +2851,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
@@ -2895,6 +2908,14 @@ echo "${ECHO_T}$ac_cv_lbl_gcc_vers" >&6; }
                    V_CCOPT="$V_CCOPT -O"
                    ;;
 
+           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; }
@@ -3113,8 +3134,11 @@ 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
@@ -3122,6 +3146,208 @@ 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__((noreturn)) can be applied to function pointers without warnings" >&5
+echo $ECHO_N "checking whether __attribute__((noreturn)) can be applied to function pointers without warnings... $ECHO_C" >&6; }
+if test "${ac_cv___attribute___noreturn_function_pointer+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)(int i)
+                 __attribute__ ((noreturn));
+
+int
+main(int argc, char **argv)
+{
+  (*foo)(1);
+}
+
+_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___noreturn_function_pointer=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+       ac_cv___attribute___noreturn_function_pointer=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+
+CFLAGS="$save_CFLAGS"
+if test "$ac_cv___attribute___noreturn_function_pointer" = "yes"; then
+
+cat >>confdefs.h <<\_ACEOF
+#define __ATTRIBUTE___NORETURN_OK_FOR_FUNCTION_POINTERS 1
+_ACEOF
+
+fi
+{ echo "$as_me:$LINENO: result: $ac_cv___attribute___noreturn_function_pointer" >&5
+echo "${ECHO_T}$ac_cv___attribute___noreturn_function_pointer" >&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; }
+
+       if test "$ac_cv___attribute___format" = "yes"; then
+
 { echo "$as_me:$LINENO: checking whether __attribute__((format)) can be applied to function pointers" >&5
 echo $ECHO_N "checking whether __attribute__((format)) can be applied to function pointers... $ECHO_C" >&6; }
 if test "${ac_cv___attribute___format_function_pointer+set}" = set; then
@@ -3186,6 +3412,7 @@ fi
 { echo "$as_me:$LINENO: result: $ac_cv___attribute___format_function_pointer" >&5
 echo "${ECHO_T}$ac_cv___attribute___format_function_pointer" >&6; }
 
+       fi
 fi
 ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
index 570fd5816f74cda2e78071b08df4695ea9df6d2b..138828e6182fb7878993339779cc9dfb937f6d08 100644 (file)
@@ -26,7 +26,12 @@ AC_LBL_C_INIT(V_CCOPT, V_INCLS)
 AC_LBL_C_INLINE
 AC_C___ATTRIBUTE__
 if test "$ac_cv___attribute__" = "yes"; then
-       AC_C___ATTRIBUTE___FORMAT_FUNCTION_POINTER
+       AC_C___ATTRIBUTE___UNUSED
+       AC_C___ATTRIBUTE___NORETURN_FUNCTION_POINTER
+       AC_C___ATTRIBUTE___FORMAT
+       if test "$ac_cv___attribute___format" = "yes"; then
+               AC_C___ATTRIBUTE___FORMAT_FUNCTION_POINTER
+       fi
 fi
 AC_CHECK_HEADERS(fcntl.h rpc/rpc.h rpc/rpcent.h netdnet/dnetdb.h)
 AC_CHECK_HEADERS(net/pfvar.h, , , [#include <sys/types.h>
index bb79414977d70a2963fb6a4144a572c6ff64fd11..88a17147f11dc28444e02871c2e80071bdeea252 100644 (file)
 
 #if !defined(HAVE_SNPRINTF)
 int snprintf(char *, size_t, const char *, ...)
-     __attribute__((format(printf, 3, 4)));
-#endif
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format(printf, 3, 4)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
+#endif /* !defined(HAVE_SNPRINTF) */
 
 #if !defined(HAVE_VSNPRINTF)
 int vsnprintf(char *, size_t, const char *, va_list)
-     __attribute__((format(printf, 3, 0)));
-#endif
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format(printf, 3, 0)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
+#endif /* !defined(HAVE_VSNPRINTF) */
 
 #ifndef HAVE_STRLCAT
 extern size_t strlcat(char *, const char *, size_t);
@@ -136,8 +142,16 @@ extern const char *tok2strary_internal(const char **, int, const char *, int);
 extern const char *dnaddr_string(u_short);
 
 extern void error(const char *, ...)
-    __attribute__((noreturn, format (printf, 1, 2)));
-extern void warning(const char *, ...) __attribute__ ((format (printf, 1, 2)));
+     __attribute__((noreturn))
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format (printf, 1, 2)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
+extern void warning(const char *, ...)
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format (printf, 1, 2)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
 
 extern char *read_infile(char *);
 extern char *copy_argv(char **);
index 2bcf317470622099aa0968a329190ff694c98c87..6b3d473ebade6811cfa4dfd999173dba9d3a0f9a 100644 (file)
--- a/machdep.c
+++ b/machdep.c
@@ -46,7 +46,10 @@ static const char rcsid[] _U_ =
 
 #if !defined(HAVE_SNPRINTF)
 int snprintf(char *, size_t, const char *, ...)
-     __attribute__((format(printf, 3, 4)));
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format(printf, 3, 4)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
 #endif /* !defined(HAVE_SNPRINTF) */
 #endif /* __osf__ */
 
index f1ce79596d74a0b48972ffe979edf29787e00085..fec5a6b6b4c61f547de9131d40250f75d7ba0c0a 100644 (file)
 
 #if !defined(HAVE_SNPRINTF)
 int snprintf (char *str, size_t sz, const char *format, ...)
-     __attribute__ ((format (printf, 3, 4)));
-#endif
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format (printf, 3, 4)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
+#endif /* !defined(HAVE_SNPRINTF) */
 
 #if !defined(HAVE_VSNPRINTF)
 int vsnprintf (char *str, size_t sz, const char *format, va_list ap)
-     __attribute__((format (printf, 3, 0)));
-#endif
+#ifdef __ATTRIBUTE___FORMAT_OK
+     __attribute__((format (printf, 3, 0)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+     ;
+#endif /* !defined(HAVE_SNPRINTF) */
 
 #ifndef HAVE_STRLCAT
 extern size_t strlcat (char *, const char *, size_t);
@@ -158,9 +164,12 @@ struct netdissect_options {
                     ;
   void (*ndo_error)(netdissect_options *,
                    const char *fmt, ...)
+#ifdef __ATTRIBUTE___NORETURN_OK_FOR_FUNCTION_POINTERS
+                    __attribute__ ((noreturn))
+#endif /* __ATTRIBUTE___NORETURN_OK_FOR_FUNCTION_POINTERS */
 #ifdef __ATTRIBUTE___FORMAT_OK_FOR_FUNCTION_POINTERS
-                    __attribute__ ((noreturn, format (printf, 2, 3)))
-#endif
+                    __attribute__ ((format (printf, 2, 3)))
+#endif /* __ATTRIBUTE___FORMAT_OK_FOR_FUNCTION_POINTERS */
                     ;
   void (*ndo_warning)(netdissect_options *,
                      const char *fmt, ...)