From: Guy Harris Date: Fri, 1 Dec 2023 09:05:18 +0000 (-0800) Subject: Have a #define to squelch -Wunused-result warnings. X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/dd821c4f84a9d4956fc39a3a2359cf4ee4652e72 Have a #define to squelch -Wunused-result warnings. In one place, we really *do* legitimately ignore the return value of pcap_set_datalink(). We cast its return value to void, which appears to squelch warnings from Clang, but not from at least some versions of GCC, so provide DIAG_OFF_WARN_UNUSED_RESULT/DIAG_ON_WARN_UNUSED_RESULT and use them to squelch the warning. --- diff --git a/diag-control.h b/diag-control.h index 633e4ed0..73c745d1 100644 --- a/diag-control.h +++ b/diag-control.h @@ -132,6 +132,22 @@ #define DIAG_ON_CAST_QUAL \ DIAG_DO_PRAGMA(GCC diagnostic pop) + #if ND_IS_AT_LEAST_GNUC_VERSION(4,5) + /* + * GCC warns about unused return values if a function is marked as + * "warn about ignoring this function's return value". + * + * Clang appears to let you ignore a result without a warning by + * casting the function result to void, so we don't appear to + * need this for Clang. + */ + #define DIAG_OFF_WARN_UNUSED_RESULT \ + DIAG_DO_PRAGMA(GCC diagnostic push) \ + DIAG_DO_PRAGMA(GCC diagnostic ignored "-Wunused-result") + #define DIAG_ON_WARN_UNUSED_RESULT \ + DIAG_DO_PRAGMA(GCC diagnostic pop) + #endif + /* * Suppress deprecation warnings. */ @@ -172,6 +188,12 @@ #ifndef DIAG_ON_CAST_QUAL #define DIAG_ON_CAST_QUAL #endif +#ifndef DIAG_OFF_WARN_UNUSED_RESULT +#define DIAG_OFF_WARN_UNUSED_RESULT +#endif +#ifndef DIAG_ON_WARN_UNUSED_RESULT +#define DIAG_ON_WARN_UNUSED_RESULT +#endif #ifndef DIAG_OFF_DEPRECATION #define DIAG_OFF_DEPRECATION #endif diff --git a/tcpdump.c b/tcpdump.c index a7756be5..9ec0aaf1 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -2326,8 +2326,11 @@ main(int argc, char **argv) * on; this may be a non-Linux "any" device * that doesn't support DLT_LINUX_SLL2. */ - if (strcmp(device, "any") == 0) + if (strcmp(device, "any") == 0) { +DIAG_OFF_WARN_UNUSED_RESULT (void) pcap_set_datalink(pd, DLT_LINUX_SLL2); +DIAG_ON_WARN_UNUSED_RESULT + } } #endif i = pcap_snapshot(pd);