From: Guy Harris Date: Sat, 22 Jun 2013 21:06:33 +0000 (-0700) Subject: Avoid some warnings from Sun C. X-Git-Tag: tcpdump-4.5.0~74 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/94bf5a24bf7c9c959ad90821655d2b68c92ef32f Avoid some warnings from Sun C. 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. --- diff --git a/aclocal.m4 b/aclocal.m4 index 7bdfd81b..a2da5119 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -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 +#include + +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 + +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 + +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, [ # diff --git a/config.h.in b/config.h.in index 4c48e6fb..3a2da984 100644 --- a/config.h.in +++ b/config.h.in @@ -283,10 +283,17 @@ /* 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 diff --git a/configure b/configure index 5fde04c9..a56eb7b0 100755 --- 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 +#include + +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 + +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 + +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' diff --git a/configure.in b/configure.in index 570fd581..138828e6 100644 --- a/configure.in +++ b/configure.in @@ -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 diff --git a/interface.h b/interface.h index bb794149..88a17147 100644 --- a/interface.h +++ b/interface.h @@ -38,13 +38,19 @@ #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 **); diff --git a/machdep.c b/machdep.c index 2bcf3174..6b3d473e 100644 --- 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__ */ diff --git a/netdissect.h b/netdissect.h index f1ce7959..fec5a6b6 100644 --- a/netdissect.h +++ b/netdissect.h @@ -42,13 +42,19 @@ #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, ...)