This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: unused variable warning


On Mon, 2005-08-29 at 01:00 -0700, Ian Lance Taylor wrote:
> skaller <skaller@users.sourceforge.net> writes:
> 
> > In gcc/g++ version 4.0 there is no way to turn off the 
> > unused variable warning enabled by -Wall with a command
> > line switch.
> 
> How about -Wno-unused-variable?

Hmmm.. that seems to work .. Thanks!

Actually the manual says:

"Many options have long names starting with -f or with -Wâfor example,
-fforce-mem, -fstrength-reduce, -Wformat and so on. Most of these have
both positive and negative forms; the negative form of -ffoo would be
-fno-foo. This manual documents only one of these two forms, whichever
one is not the default."

however this is just sloppy: the document should be explicit
about which options are supported -- I grepped for 'unused'
and didn't find a 'no' form.. I didn't see that section of
the manual (even though I'm aware it is the convention).
I was misled by the text which says 

"To suppress this warning use the unused attribute (see Variable
Attributes)."


which should have said "To suppress this warning on a per variable
basis .."

> > I think this is a bug: it should be possible to selectively
> > turn on or off all warnings (on the command line).
> 
> There is work on progress to address that.

Yes, gcc 4.0 is quite good now.

> > The advice in the documentation is to attach 
> > 
> > __attribute__((unused))
> > 
> > to the selected variable. This may be inappropriate for
> > three distinct reasons:
> > 
> > (a) It is a GNU extension
> > (b) it clutters the code
> > (c) It isn't always easy to tell if a variable is unused
> 
> While (a) and (b) are undeniable, (c) is not an issue; the attribute
> suppresses the unused warning, but does not cause any trouble if the
> variable is in fact used.

I found that out experimentally, and, this behaviour is in fact
documented .. however it isn't all that readable to see 'unused' 
and be expected to guess it actually means 'possibly unused' :)

> To address (a) and (b), the gcc sources themselves do this:
> 
> #ifndef ATTRIBUTE_UNUSED
> #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
> #endif /* ATTRIBUTE_UNUSED */
> 
> int fn (int arg ATTRIBUTE_UNUSED)

Yes, I am also doing that too. I was mainly concerned that
I wish my scripts to use -Wall, which wasn't possible prior
to 4.0 due to the invalid-offsetof warning not being suppressible,
but in 4.0 it is, so I have switched to -Wall -Wno-invalid-offsetof
for checking my code .. for production scripts I want most of the
warnings to be on, however unused variables are just too common
it seems.

Anyhow thanks!

-- 
John Skaller <skaller at users dot sourceforge dot net>

Attachment: signature.asc
Description: This is a digitally signed message part


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]