public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC/clang warning incompatibility with unused private member variables
@ 2021-06-11 19:37 Markus Faehling
  2021-06-11 19:59 ` Gabriel Ravier
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Markus Faehling @ 2021-06-11 19:37 UTC (permalink / raw)
  To: gcc

Hello,

I'm currently facing a problem where I cannot get both gcc and clang 
warning-free simultaneously in my project. My code looks somewhat like this:

class Test {
     int a_;
     void b() {};
};

This code gives me the(usually very useful) "-Wunused-private-field" 
warning on clang. But because I have the unused member on purpose, I 
would like to add the [[maybe_unused]] attribute to it:

class Test {
     [[maybe_unused]] int a_;
     void b() {};
};

While this version is warning-free in clang, gcc has a "-Wattributes" 
warning because it ignores the [[maybe_unused]] warning. But I do not 
want to disable either of these warnings because they are still very 
useful in other situations.

Would it be possible to ignore the "-Wattributes" warning for 
[[maybe_unused]] in places where other compilers might use the attribute?

Demonstration on godbolt.org: https://godbolt.org/z/8oT4Kr5eM

Regards,

Markus Faehling


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GCC/clang warning incompatibility with unused private member variables
  2021-06-11 19:37 GCC/clang warning incompatibility with unused private member variables Markus Faehling
@ 2021-06-11 19:59 ` Gabriel Ravier
  2021-06-11 20:03 ` Jason Merrill
  2021-06-11 22:14 ` Jonathan Wakely
  2 siblings, 0 replies; 6+ messages in thread
From: Gabriel Ravier @ 2021-06-11 19:59 UTC (permalink / raw)
  To: Markus Faehling, gcc

On 6/11/21 9:37 PM, Markus Faehling wrote:
> Hello,
>
> I'm currently facing a problem where I cannot get both gcc and clang 
> warning-free simultaneously in my project. My code looks somewhat like 
> this:
>
> class Test {
>     int a_;
>     void b() {};
> };
>
> This code gives me the(usually very useful) "-Wunused-private-field" 
> warning on clang. But because I have the unused member on purpose, I 
> would like to add the [[maybe_unused]] attribute to it:
>
> class Test {
>     [[maybe_unused]] int a_;
>     void b() {};
> };
>
> While this version is warning-free in clang, gcc has a "-Wattributes" 
> warning because it ignores the [[maybe_unused]] warning. But I do not 
> want to disable either of these warnings because they are still very 
> useful in other situations.
>
> Would it be possible to ignore the "-Wattributes" warning for 
> [[maybe_unused]] in places where other compilers might use the attribute?
>
> Demonstration on godbolt.org: https://godbolt.org/z/8oT4Kr5eM
>
> Regards,
>
> Markus Faehling
>
It should be easy to solve this with some application of `#pragma GCC 
diagnostic` pragmas to temporarily disable `-Wattributes` on that 
specific line. I suppose it may look ugly, but it works.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GCC/clang warning incompatibility with unused private member variables
  2021-06-11 19:37 GCC/clang warning incompatibility with unused private member variables Markus Faehling
  2021-06-11 19:59 ` Gabriel Ravier
@ 2021-06-11 20:03 ` Jason Merrill
  2021-06-11 20:35   ` Jakub Jelinek
  2021-06-13 17:19   ` Jason Merrill
  2021-06-11 22:14 ` Jonathan Wakely
  2 siblings, 2 replies; 6+ messages in thread
From: Jason Merrill @ 2021-06-11 20:03 UTC (permalink / raw)
  To: Markus Faehling, gcc

On 6/11/21 3:37 PM, Markus Faehling wrote:
> Hello,
> 
> I'm currently facing a problem where I cannot get both gcc and clang 
> warning-free simultaneously in my project. My code looks somewhat like 
> this:
> 
> class Test {
>      int a_;
>      void b() {};
> };
> 
> This code gives me the(usually very useful) "-Wunused-private-field" 
> warning on clang. But because I have the unused member on purpose, I 
> would like to add the [[maybe_unused]] attribute to it:
> 
> class Test {
>      [[maybe_unused]] int a_;
>      void b() {};
> };
> 
> While this version is warning-free in clang, gcc has a "-Wattributes" 
> warning because it ignores the [[maybe_unused]] warning. But I do not 
> want to disable either of these warnings because they are still very 
> useful in other situations.
> 
> Would it be possible to ignore the "-Wattributes" warning for 
> [[maybe_unused]] in places where other compilers might use the attribute?
> 
> Demonstration on godbolt.org: https://godbolt.org/z/8oT4Kr5eM

You can use #pragma to disable a warning for a particular section of code:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
class Test {
      [[maybe_unused]] int a_;
      void b() {};
};
#pragma GCC diagnostic pop

But I also agree that GCC shouldn't warn here.

Jason


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GCC/clang warning incompatibility with unused private member variables
  2021-06-11 20:03 ` Jason Merrill
@ 2021-06-11 20:35   ` Jakub Jelinek
  2021-06-13 17:19   ` Jason Merrill
  1 sibling, 0 replies; 6+ messages in thread
From: Jakub Jelinek @ 2021-06-11 20:35 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Markus Faehling, gcc

On Fri, Jun 11, 2021 at 04:03:34PM -0400, Jason Merrill via Gcc wrote:
> You can use #pragma to disable a warning for a particular section of code:
> 
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wattributes"
> class Test {
>      [[maybe_unused]] int a_;
>      void b() {};
> };
> #pragma GCC diagnostic pop
> 
> But I also agree that GCC shouldn't warn here.

We could do that by using a wrapper around handle_unused_attribute
for the maybe_unused attribute, that way warn on unused attribute on
FIELD_DECLs, but not for maybe_unused (until we actually implement some
warning that uses it).

	Jakub


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GCC/clang warning incompatibility with unused private member variables
  2021-06-11 19:37 GCC/clang warning incompatibility with unused private member variables Markus Faehling
  2021-06-11 19:59 ` Gabriel Ravier
  2021-06-11 20:03 ` Jason Merrill
@ 2021-06-11 22:14 ` Jonathan Wakely
  2 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2021-06-11 22:14 UTC (permalink / raw)
  To: markus; +Cc: gcc

Here's another #pragma solution to your problem:

class Test {
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpragmas" // so GCC ignores next line
# pragma GCC diagnostic ignored "-Wunused-private-field"
     int a_;
# pragma GCC diagnostic pop
     void b() {};
};

First tell GCC not to warn about the warning option that it doesn't
recognize, then use that option to tell clang to not warn about the
unused member.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: GCC/clang warning incompatibility with unused private member variables
  2021-06-11 20:03 ` Jason Merrill
  2021-06-11 20:35   ` Jakub Jelinek
@ 2021-06-13 17:19   ` Jason Merrill
  1 sibling, 0 replies; 6+ messages in thread
From: Jason Merrill @ 2021-06-13 17:19 UTC (permalink / raw)
  To: Markus Faehling, gcc Mailing List

On Fri, Jun 11, 2021 at 4:03 PM Jason Merrill <jason@redhat.com> wrote:

> On 6/11/21 3:37 PM, Markus Faehling wrote:
> > Hello,
> >
> > I'm currently facing a problem where I cannot get both gcc and clang
> > warning-free simultaneously in my project. My code looks somewhat like
> > this:
> >
> > class Test {
> >      int a_;
> >      void b() {};
> > };
> >
> > This code gives me the(usually very useful) "-Wunused-private-field"
> > warning on clang. But because I have the unused member on purpose, I
> > would like to add the [[maybe_unused]] attribute to it:
> >
> > class Test {
> >      [[maybe_unused]] int a_;
> >      void b() {};
> > };
> >
> > While this version is warning-free in clang, gcc has a "-Wattributes"
> > warning because it ignores the [[maybe_unused]] warning. But I do not
> > want to disable either of these warnings because they are still very
> > useful in other situations.
> >
> > Would it be possible to ignore the "-Wattributes" warning for
> > [[maybe_unused]] in places where other compilers might use the attribute?
> >
> > Demonstration on godbolt.org: https://godbolt.org/z/8oT4Kr5eM
>
> You can use #pragma to disable a warning for a particular section of code:
>
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wattributes"
> class Test {
>       [[maybe_unused]] int a_;
>       void b() {};
> };
> #pragma GCC diagnostic pop
>
> But I also agree that GCC shouldn't warn here.


I've pushed a change to trunk to stop warning about this case.

Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-06-13 17:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11 19:37 GCC/clang warning incompatibility with unused private member variables Markus Faehling
2021-06-11 19:59 ` Gabriel Ravier
2021-06-11 20:03 ` Jason Merrill
2021-06-11 20:35   ` Jakub Jelinek
2021-06-13 17:19   ` Jason Merrill
2021-06-11 22:14 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).