public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: No warning on unused variable
@ 2006-10-24 17:29 Kaz Kylheku
  0 siblings, 0 replies; 5+ messages in thread
From: Kaz Kylheku @ 2006-10-24 17:29 UTC (permalink / raw)
  To: Andrew Haley, Dave Williss; +Cc: gcc-help

Andrew Haley wrote:
> Dave Williss writes:
>  > It might be nice if g++ could add an __attribute__ setting 
> so you could 
>  > tag a class with something like ctor_has_no_useful_side_effects.  
>  > Classes with this attribute could then be allowed to raise 
> the "declared 
>  > but not used" warning.  It would be most useful on things like 
>  > std::string, which you also wouldn't want just laying around.
> 
> It's an interesting idea, for sure.  I can't immediately think of any
> big downside, but I'm not sure if such a patch would be accepted.

This attribute could generalize to things other than constructors.

The name is wrong, because the side effect of initializing an object
is usually useful. It's only not useful when there is no subsequent use
of the object.

The general attribute would assert that the function's only effect is
to write to the object(s) that are passed into it. In other words,
it would promise that the function does not cause the pointer to
escape.

And in fact, this attribute would be better on a parameter than on the
function. Of course, in C++ constructors you don't have an explicit
parameter, so the attribute would go on the function, but be understood
to be on the object.

Examples:

  // 1
  void foo(int *p __attribute__((noescape)), int *q);

  // 2: noescape refers to ``bar *this'' parameter.
  bar::bar(int *q) __attribute__ ((noescape));

If you know that the pointer does not escape (the function does not
cause it to be remembered after it returns) that is exactly what you
need to know to determine whether the referent object has a next use.

This could have other uses besides generating warnings:

  {
    int i, j;
    noescape_arg(&i);
    i = 3;
    external_function();

    /* i can still be cached in a register
       because we know that external_function
       has no access to i! */

    noescape_arg(&j);
    /* Elided call, j has no next use */
  }

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

* Re: No warning on unused variable
  2006-10-24 14:34   ` Dave Williss
@ 2006-10-24 17:08     ` Andrew Haley
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Haley @ 2006-10-24 17:08 UTC (permalink / raw)
  To: Dave Williss; +Cc: gcc-help

Dave Williss writes:
 > 
 > >  > 
 > >  > Why did this not warn me about the unused stringstream?
 > > [snip]
 > > > it is concievable that declaring this object
 > >  > might be sufficient for some action to be taken by the code, thus
 > >  > rendering it incorrect for -Wall to report it.
 > >
 > > You got it in one.  For example, you might see
 > >
 > >   {
 > >     lock myLock;
 > >     ...  
 > >   }
 > >
 > > where the lock constructor locks memory and the destructor releases
 > > it.  
 > >   
 > It might be nice if g++ could add an __attribute__ setting so you could 
 > tag a class with something like ctor_has_no_useful_side_effects.  
 > Classes with this attribute could then be allowed to raise the "declared 
 > but not used" warning.  It would be most useful on things like 
 > std::string, which you also wouldn't want just laying around.

It's an interesting idea, for sure.  I can't immediately think of any
big downside, but I'm not sure if such a patch would be accepted.

Andrew.

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

* Re: No warning on unused variable
  2006-10-24  5:43 ` Andrew Haley
@ 2006-10-24 14:34   ` Dave Williss
  2006-10-24 17:08     ` Andrew Haley
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Williss @ 2006-10-24 14:34 UTC (permalink / raw)
  To: gcc-help


>  > 
>  > Why did this not warn me about the unused stringstream?
> [snip]
> > it is concievable that declaring this object
>  > might be sufficient for some action to be taken by the code, thus
>  > rendering it incorrect for -Wall to report it.
>
> You got it in one.  For example, you might see
>
>   {
>     lock myLock;
>     ...  
>   }
>
> where the lock constructor locks memory and the destructor releases
> it.  
>   
It might be nice if g++ could add an __attribute__ setting so you could 
tag a class with something like ctor_has_no_useful_side_effects.  
Classes with this attribute could then be allowed to raise the "declared 
but not used" warning.  It would be most useful on things like 
std::string, which you also wouldn't want just laying around.


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

* Re: No warning on unused variable
  2006-10-24  5:33 D Haley
@ 2006-10-24  5:43 ` Andrew Haley
  2006-10-24 14:34   ` Dave Williss
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Haley @ 2006-10-24  5:43 UTC (permalink / raw)
  To: D Haley; +Cc: gcc-help

D Haley writes:

Ah, perhaps a distant cousin.  Hi!

 > I came across this situation recently
 > 
 > Consider the following code
 > 
 > #include <sstream>
 > 
 > using std::stringstream;
 > 
 > int main()
 > {
 >         int i;
 >         stringstream ss;
 > }
 > 
 > I the compiled it with
 > 
 > gcc main.cpp -Wall -lstdc++
 > >main.cpp: In function 'int main()':
 > >main.cpp:7: warning: unused variable 'i'
 > 
 > Why did this not warn me about the unused stringstream?
 > Can wall only catch unused primitive types?

No, it's not that, it's ...

 > Is it because the constructor is called and therefore it is not unused?

Yes.

 > It seems that -Wall should pick this up from the point of view of "i
 > declared it then did nothing, this is incorrect" (also i used no params
 > in the constructor), but it is concievable that declaring this object
 > might be sufficient for some action to be taken by the code, thus
 > rendering it incorrect for -Wall to report it.

You got it in one.  For example, you might see

  {
    lock myLock;
    ...  
  }

where the lock constructor locks memory and the destructor releases
it.  

 > Is this desirable behaviour by GCC or not?

Sure is.

Andrew.

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

* No warning on unused variable
@ 2006-10-24  5:33 D Haley
  2006-10-24  5:43 ` Andrew Haley
  0 siblings, 1 reply; 5+ messages in thread
From: D Haley @ 2006-10-24  5:33 UTC (permalink / raw)
  To: gcc-help

Hello,

I came across this situation recently

Consider the following code

#include <sstream>

using std::stringstream;

int main()
{
        int i;
        stringstream ss;
}

I the compiled it with

gcc main.cpp -Wall -lstdc++
>main.cpp: In function 'int main()':
>main.cpp:7: warning: unused variable 'i'

Why did this not warn me about the unused stringstream?
Can wall only catch unused primitive types?

Is it because the constructor is called and therefore it is not unused?
It seems that -Wall should pick this up from the point of view of "i
declared it then did nothing, this is incorrect" (also i used no params
in the constructor), but it is concievable that declaring this object
might be sufficient for some action to be taken by the code, thus
rendering it incorrect for -Wall to report it.

Is this desirable behaviour by GCC or not?

Thankyou.

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

end of thread, other threads:[~2006-10-24 17:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-24 17:29 No warning on unused variable Kaz Kylheku
  -- strict thread matches above, loose matches on Subject: below --
2006-10-24  5:33 D Haley
2006-10-24  5:43 ` Andrew Haley
2006-10-24 14:34   ` Dave Williss
2006-10-24 17:08     ` Andrew Haley

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).