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

Daniel P. Berrangé berrange at redhat.com
Tue Sep 14 13:47:59 UTC 2021


On Tue, Sep 14, 2021 at 03:28:06PM +0200, Tim Wiederhake wrote:
> On Fri, 2021-09-10 at 14:35 +0200, Ján Tomko wrote:
> > On a Friday in 2021, Tim Wiederhake wrote:
> > > 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(+)
> > > 
> > 
> > https://listman.redhat.com/archives/libvir-list/2021-August/msg00863.html
> > 
> > Jano
> 
> >From the mail you linked:
> 
> > It's our usage that is weird here.
> 
> I disagree. I believe our usage of `g_auto*` (and in extension,
> `__attribute__((cleanup))`) is exactly as this feature is meant to be
> used.
> 
> > These are not needed since in all cases, the G_GNUC_UNUSED can be
> > used unconditionally for both gcc and CLang in the respective macros.
> 
> That would disable unused-variable-checking for other compilers as
> well, e.g. gcc, robbing us of a valuable diagnostic.

I don't think G_GNUC_UNUSED impacts GCC warnings at all.

>From GCC's POV, the attribute((cleanup(freefunc))) annotation gets
expanded into a set of calls to freefunc().  So any variable declared
with a "cleanup" attribute will always appear used to GCC, because of
these auto inserted calls.

$ cat demo.c

#include <stdlib.h>

static void freeit(void *ptrptr)
{
  void *ptr = *(void **)ptrptr;
  free(ptr);
}

void foo(void)
{
  __attribute__((cleanup(freeit))) char *a = malloc(1);
  __attribute__((unused)) __attribute__((cleanup(freeit))) char *b = malloc(1);
  __attribute__((unused)) char *c = malloc(1);
  char *d = malloc(1);
  char *e = malloc(1);

  free(d);
}


$ gcc -c -Wall demo.c
demo.c: In function ‘foo’:
demo.c:13:9: warning: unused variable ‘b’ [-Wunused-variable]
   13 |   char *e = malloc(1);
      |         ^



Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the libvir-list mailing list