Mark g_auto* variables as G_GNUC_UNUSED on clang
Mark g_auto* variables as G_GNUC_UNUSED on clang
This is a workaround for clang bug https://bugs.llvm.org/show_bug.cgi?id=3888 / https://bugs.llvm.org/show_bug.cgi?id=43482.
Consider the following straw man program:
#include <glib.h>
#include <stdio.h>
typedef struct Tag {
char *name;
} Tag;
Tag *tag_new(char *name) {
printf("<%s>", name);
Tag *tag = g_new0(Tag, 1);
tag->name = name;
return tag;
}
void tag_free(Tag *tag) {
printf("</%s>\n", tag->name);
g_free(tag);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(Tag, tag_free);
int main() {
g_autoptr(Tag) tag = tag_new("foo");
printf("bar");
return 0;
}
$ gcc -o demo demo.c -Wunused `pkg-config --cflags --libs glib-2.0`
$ clang -o demo demo.c -Wunused `pkg-config --cflags --libs glib-2.0`
demo.c:25:17: warning: unused variable 'tag' [-Wunused-variable]
g_autoptr(Tag) tag = tag_new("foo");
^
1 warning generated.
Clang emits an unused-variable warning if the variable is only accessed on scope exit by a cleanup function. Note that gcc does not exhibit this behavior.
This patch adds G_GNUC_UNUSED
to all g_auto* definitions if compiled with clang and supresses the warning.