From 5f17edea5bd4f92efb35a2facaaacd0042f03359 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 8 Oct 2021 15:17:43 +0200 Subject: [PATCH] std-aux: work around "-Wunused-but-set-variable" warning with clang in nm_auto() We use the cleanup attribute heavily. It's useful for deferring deallocation. For example, we have code like: gs_unref_object NMBluezManager *self_keep_alive = g_object_ref(self); where we don't use the variable otherwise, except for owning (and freeing) the reference. This already lead to a compiler warning about unused variable, which we would workaround with _nm_unused gs_unref_object NMBluezManager *self_keep_alive = g_object_ref(self); With clang 13.0.0~rc1-1.fc35, this got worse. Now for example also static inline void nm_strvarray_set_strv(GArray **array, const char *const *strv) { gs_unref_array GArray *array_old = NULL; array_old = g_steal_pointer(array); if (!strv || !strv[0]) return; nm_strvarray_ensure(array); for (; strv[0]; strv++) nm_strvarray_add(*array, strv[0]); } leads to a warning ./src/libnm-glib-aux/nm-shared-utils.h:3078:28: error: variable array_old set but not used [-Werror,-Wunused-but-set-variable] gs_unref_array GArray *array_old = NULL; ^ This is really annoying. We don't want to plaster our code with _nm_unused, because that might hide actual issues. But we also want to keep using this pattern and need to avoid the warning. A problem is also that GCC usually does not warn about truly unused variables with cleanup attribute. Clang was very useful here to flag such variables. But now clang warns about cases which are no bugs, which is a problem. So this does loose some useful warnings. On the other hand, a truly unused variable (with cleanup attribute) is ugly, but not an actual problem. Now, with clang 13, automatically mark nm_auto() variables as _nm_unused as workaround. (cherry picked from commit 1c85bc5ead2cd2fe7d38c4fb84ebf97ae70ab7c4) --- src/libnm-std-aux/nm-std-aux.h | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index 17b268816d..ca85e47e07 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -13,17 +13,26 @@ /*****************************************************************************/ -#define _nm_packed __attribute__((__packed__)) -#define _nm_unused __attribute__((__unused__)) -#define _nm_used __attribute__((__used__)) -#define _nm_pure __attribute__((__pure__)) -#define _nm_const __attribute__((__const__)) -#define _nm_printf(a, b) __attribute__((__format__(__printf__, a, b))) -#define _nm_align(s) __attribute__((__aligned__(s))) -#define _nm_section(s) __attribute__((__section__(s))) -#define _nm_alignof(type) __alignof(type) -#define _nm_alignas(type) _nm_align(_nm_alignof(type)) -#define nm_auto(fcn) __attribute__((__cleanup__(fcn))) +#define _nm_packed __attribute__((__packed__)) +#define _nm_unused __attribute__((__unused__)) +#define _nm_used __attribute__((__used__)) +#define _nm_pure __attribute__((__pure__)) +#define _nm_const __attribute__((__const__)) +#define _nm_printf(a, b) __attribute__((__format__(__printf__, a, b))) +#define _nm_align(s) __attribute__((__aligned__(s))) +#define _nm_section(s) __attribute__((__section__(s))) +#define _nm_alignof(type) __alignof(type) +#define _nm_alignas(type) _nm_align(_nm_alignof(type)) + +#if defined(__clang__) && __clang_major__ == 13 +/* Clang 13 can emit -Wunused-but-set-variable warning for cleanup variables + * that are only assigned (never used otherwise). Hack around */ +#define _nm_auto_extra _nm_unused +#else +#define _nm_auto_extra +#endif + +#define nm_auto(fcn) _nm_auto_extra __attribute__((__cleanup__(fcn))) /* This is required to make LTO working. *