Fwd: [PATCH] Attribute for unused warning for variables of non-trivial types

Lubos Lunak l.lunak@suse.cz
Sun Jun 30 16:27:00 GMT 2013


 Sorry, this has disappeared off my radar for a while.

On Wednesday 21 of November 2012, Jason Merrill wrote:
> On 11/20/2012 10:39 AM, Lubos Lunak wrote:
> >    if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
> > -    /* Just know that we've seen something for this node.  */
> > -    TREE_USED (exp) = 1;
> > +    {
> > +      /* Just know that we've seen something for this node.
> > +         Merely creating a warn_unused aggregate doesn't make it used
> > though. */ +      if( !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES
> > (type))) +        TREE_USED (exp) = 1;
> > +    }
>
> I don't think we need this either; without this hunk, we should warn
> with -Wunused-but-set-variable, which seems like the behavior we want,
> since a variable with a non-trivial default constructor is set.

 I have fixed the other two pointed out places (updated version attached), but 
if I do also this, then the warning doesn't work. This is because TREE_USED 
is used for detection, and it triggers the first warning in the relevant part 
in gcc/cp/decl.c . The but-set warning would require using DECL_READ_P being 
unset instead, but that one is always set by that point (it's set at least 
twice during building the ctor call).

 Are you sure this should be covered by -Wunused-but-set-variable rather than 
plain -Wunused-variable? While strictly technically speaking the variable is 
set by the ctor, it's conceptually confusing (is "string s;" really set from 
the developer's point of view?), and it's inconsistent with basic types:

$ echo "void f() { int a = 1; } " | g++ -x c++ -fsyntax-only -Wall -
<stdin>: In function ‘void f()’:
<stdin>:1:16: warning: unused variable ‘a’ [-Wunused-variable]

 So while I could try to patch all the places that mark the variable 
DECL_READ_P during ctor/dtor calls, the only difference I see is that it will 
make the handling more complicated, so I myself do not find that worth it.

-- 
 Lubos Lunak
 l.lunak@suse.cz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-implement-warn_unused-attribute-gcc-55203.patch
Type: text/x-diff
Size: 5469 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20130630/df6e9c26/attachment.bin>


More information about the Gcc-patches mailing list