[libvirt PATCH v2 01/12] glibcompat: Add G_GNUC_UNUSED to g_auto* definitions for clang

Tim Wiederhake twiederh at redhat.com
Fri Sep 10 10:45:43 UTC 2021


Workaround for a bug in clang. Clang emits an unused-variable warning
if the variable is only accessed on scope exit by a destructor function.
Note that gcc does not exhibit this behavior.

See https://bugs.llvm.org/show_bug.cgi?id=3888 and
https://bugs.llvm.org/show_bug.cgi?id=43482.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/util/glibcompat.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/util/glibcompat.h b/src/util/glibcompat.h
index 697687b967..ea486e32d5 100644
--- a/src/util/glibcompat.h
+++ b/src/util/glibcompat.h
@@ -86,3 +86,22 @@ char *vir_g_strdup_vprintf(const char *msg, va_list args)
 #define g_fsync vir_g_fsync
 
 void vir_g_source_unref(GSource *src, GMainContext *ctx);
+
+/*
+ * Workaround for a bug in clang. Clang emits an unused-variable warning if the
+ * variable is only accessed on scope exit by a destructor function. See
+ * https://bugs.llvm.org/show_bug.cgi?id=3888 and
+ * https://bugs.llvm.org/show_bug.cgi?id=43482.
+ */
+#if defined(__clang__)
+# undef g_autoptr
+# define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName) G_GNUC_UNUSED
+# undef g_autolist
+# define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) G_GNUC_UNUSED
+# undef g_autoslist
+# define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) G_GNUC_UNUSED
+# undef g_autoqueue
+# define g_autoqueue(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName) G_GNUC_UNUSED
+# undef g_auto
+# define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName G_GNUC_UNUSED
+#endif /* __clang__ */
-- 
2.31.1




More information about the libvir-list mailing list