This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: pragma GCC diagnostic
- From: "Manuel López-Ibáñez" <lopezibanez at gmail dot com>
- To: "Bruno Haible" <bruno at clisp dot org>
- Cc: "Jim Meyering" <jim at meyering dot net>, "Paolo Bonzini" <bonzini at gnu dot org>, bug-gnulib at gnu dot org, gcc at gcc dot gnu dot org
- Date: Fri, 17 Oct 2008 14:26:24 +0200
- Subject: Re: pragma GCC diagnostic
- References: <8763nriv8w.fsf@rho.meyering.net> <48F8504A.1000500@gnu.org> <87abd3habz.fsf@rho.meyering.net> <200810171329.17618.bruno@clisp.org>
2008/10/17 Bruno Haible <bruno@clisp.org>:
> "#pragma GCC diagnostic" has a few limitations, which make it unusable to
> resolve warnings like this one:
>
> Jim Meyering wrote in [1]:
>> $ cat in.c
>> int f (void) __attribute__ ((__warn_unused_result__));
>> void g (void) { (void) f (); }
>> $ gcc -Werror -c in.c
>> cc1: warnings being treated as errors
>> in.c: In function 'g':
>> in.c:2: error: ignoring return value of 'f', declared with attribute warn_unused_result
>
> It is a pity that "#pragma GCC diagnostic" [2] cannot be used to get rid of
> this warning, for three reasons:
>
> - gcc's warning is not triggered by a particular option. It's on by
> default. It's not clear what should by the second argument of
> "#pragma GCC diagnostic". An empty string does not work.
We want most warnings to be controlled by an option. This is easy to fix.
> - It would be needed to restore the previous setting after the line.
> But there's no #pragma GCC push-diagnostic/pop-diagnostic.
>
> - The restriction that the "#pragma GCC diagnostic" should not occur
> inside functions does not lead to maintainable code. We want to have
> the annotation to inhibit the warning to be just one line away from the
> code that triggers the warning.
These are known limitations of pragma diagnostics. They are not going
to be fixed for GCC 4.4.
However, in this particular case, there is an easy workaround:
int f (void) __attribute__ ((__warn_unused_result__));
void g (void) { int i; i = f (); }
This silences the warning and makes it obvious that you are doing
something fishy.
Cheers,
Manuel