[libvirt PATCH] meson: avoid bogus warnings from clang and g_autoptr

Daniel P. Berrangé posted 1 patch 2 years, 7 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20210831140819.126155-1-berrange@redhat.com
There is a newer version of this series
meson.build | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
[libvirt PATCH] meson: avoid bogus warnings from clang and g_autoptr
Posted by Daniel P. Berrangé 2 years, 7 months ago
Clang has previously had trouble with G_DEFINE_AUTOPTR_CLEANUP_FUNC
generated code, thinking it was unused. We turn off -Wunused-function
to avoid tripping up on that with CLang.

New CLang has started having trouble with g_autoptr now too. In usage
scenarios where the variable is set, but never again read, it thinks
it is unused not realizing the destructor has useful side effects.
For this we have to skip -Wunused-but-set-variable on CLang.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 meson.build | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/meson.build b/meson.build
index 5af09d319a..dbd70b6483 100644
--- a/meson.build
+++ b/meson.build
@@ -470,6 +470,26 @@ if get_option('warning_level') == '2'
     supported_cc_flags += [ '-Wno-unused-function' ]
   endif
 
+  # Clang complains about unused variables in many scenarios arelated
+  # to attribute((cleanup)) aka g_auto*
+  w_unused_but_set_var_args = [ '-Wunused-but-set-variable', '-Werror' ]
+  w_unused_but_set_var_code = '''
+    static inline void free_pointer (void *p) {
+      void **pp = (void**)p;
+      free (*pp);
+    }
+
+    int main(void) {
+      __attribute__((cleanup(free_pointer))) char *buffer = 0x0;
+      buffer = 0x1;
+    }
+  '''
+  # We previously turned on unused-but-set-variable, so we must turn
+  # it off again explicitly now.
+  if not cc.compiles(w_unused_but_set_var_code, args: w_unused_but_set_var_args)
+    supported_cc_flags += [ '-Wno-unused-but-set-variable' ]
+  endif
+
 endif
 add_project_arguments(supported_cc_flags, language: 'c')
 
-- 
2.31.1

Re: [libvirt PATCH] meson: avoid bogus warnings from clang and g_autoptr
Posted by Pavel Hrdina 2 years, 7 months ago
On Tue, Aug 31, 2021 at 03:08:19PM +0100, Daniel P. Berrangé wrote:
> Clang has previously had trouble with G_DEFINE_AUTOPTR_CLEANUP_FUNC
> generated code, thinking it was unused. We turn off -Wunused-function
> to avoid tripping up on that with CLang.
> 
> New CLang has started having trouble with g_autoptr now too. In usage
> scenarios where the variable is set, but never again read, it thinks
> it is unused not realizing the destructor has useful side effects.
> For this we have to skip -Wunused-but-set-variable on CLang.

s/CLang/Clang/

> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  meson.build | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/meson.build b/meson.build
> index 5af09d319a..dbd70b6483 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -470,6 +470,26 @@ if get_option('warning_level') == '2'
>      supported_cc_flags += [ '-Wno-unused-function' ]
>    endif
>  
> +  # Clang complains about unused variables in many scenarios arelated

s/arelated/related/

> +  # to attribute((cleanup)) aka g_auto*
> +  w_unused_but_set_var_args = [ '-Wunused-but-set-variable', '-Werror' ]
> +  w_unused_but_set_var_code = '''
> +    static inline void free_pointer (void *p) {
> +      void **pp = (void**)p;
> +      free (*pp);
> +    }
> +
> +    int main(void) {
> +      __attribute__((cleanup(free_pointer))) char *buffer = 0x0;
> +      buffer = 0x1;
> +    }
> +  '''
> +  # We previously turned on unused-but-set-variable, so we must turn
> +  # it off again explicitly now.
> +  if not cc.compiles(w_unused_but_set_var_code, args: w_unused_but_set_var_args)
> +    supported_cc_flags += [ '-Wno-unused-but-set-variable' ]
> +  endif
> +
>  endif
>  add_project_arguments(supported_cc_flags, language: 'c')

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Re: [libvirt PATCH] meson: avoid bogus warnings from clang and g_autoptr
Posted by Daniel P. Berrangé 2 years, 7 months ago
On Tue, Aug 31, 2021 at 05:23:08PM +0200, Pavel Hrdina wrote:
> On Tue, Aug 31, 2021 at 03:08:19PM +0100, Daniel P. Berrangé wrote:
> > Clang has previously had trouble with G_DEFINE_AUTOPTR_CLEANUP_FUNC
> > generated code, thinking it was unused. We turn off -Wunused-function
> > to avoid tripping up on that with CLang.
> > 
> > New CLang has started having trouble with g_autoptr now too. In usage
> > scenarios where the variable is set, but never again read, it thinks
> > it is unused not realizing the destructor has useful side effects.
> > For this we have to skip -Wunused-but-set-variable on CLang.
> 
> s/CLang/Clang/
> 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >  meson.build | 20 ++++++++++++++++++++
> >  1 file changed, 20 insertions(+)
> > 
> > diff --git a/meson.build b/meson.build
> > index 5af09d319a..dbd70b6483 100644
> > --- a/meson.build
> > +++ b/meson.build
> > @@ -470,6 +470,26 @@ if get_option('warning_level') == '2'
> >      supported_cc_flags += [ '-Wno-unused-function' ]
> >    endif
> >  
> > +  # Clang complains about unused variables in many scenarios arelated
> 
> s/arelated/related/
> 
> > +  # to attribute((cleanup)) aka g_auto*
> > +  w_unused_but_set_var_args = [ '-Wunused-but-set-variable', '-Werror' ]
> > +  w_unused_but_set_var_code = '''
> > +    static inline void free_pointer (void *p) {
> > +      void **pp = (void**)p;
> > +      free (*pp);
> > +    }
> > +
> > +    int main(void) {
> > +      __attribute__((cleanup(free_pointer))) char *buffer = 0x0;
> > +      buffer = 0x1;
> > +    }
> > +  '''
> > +  # We previously turned on unused-but-set-variable, so we must turn
> > +  # it off again explicitly now.
> > +  if not cc.compiles(w_unused_but_set_var_code, args: w_unused_but_set_var_args)
> > +    supported_cc_flags += [ '-Wno-unused-but-set-variable' ]
> > +  endif
> > +
> >  endif
> >  add_project_arguments(supported_cc_flags, language: 'c')
> 
> Reviewed-by: Pavel Hrdina <phrdina@redhat.com>

Turns out this broke on macOS / FreeBSD with older CLang so will need
a v2.



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 :|