public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-4@http.gcc.gnu.org/bugzilla/>
@ 2012-08-20 11:50 ` paolo.carlini at oracle dot com
  2012-08-20 17:39 ` paolo at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-20 11:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|gcc-bugs at gcc dot gnu.org |
         AssignedTo|unassigned at gcc dot       |paolo.carlini at oracle dot
                   |gnu.org                     |com
   Target Milestone|---                         |4.8.0

--- Comment #11 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-20 11:50:38 UTC ---
For the new Wunused_but_set_variable warning, handled nearby, we actually
check:

           && (!CLASS_TYPE_P (TREE_TYPE (decl))
               || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))

Doing the same for Wunused_variable too looks like progress to me.


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-4@http.gcc.gnu.org/bugzilla/>
  2012-08-20 11:50 ` [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects paolo.carlini at oracle dot com
@ 2012-08-20 17:39 ` paolo at gcc dot gnu.org
  2012-08-20 17:40 ` paolo.carlini at oracle dot com
  2012-11-08 15:06 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 13+ messages in thread
From: paolo at gcc dot gnu.org @ 2012-08-20 17:39 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416

--- Comment #12 from paolo at gcc dot gnu.org <paolo at gcc dot gnu.org> 2012-08-20 17:39:00 UTC ---
Author: paolo
Date: Mon Aug 20 17:38:53 2012
New Revision: 190538

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190538
Log:
/cp
2012-08-20  Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/10416
    * decl.c (poplevel): Check TYPE_HAS_NONTRIVIAL_DESTRUCTOR for
    Wunused_variable too.

/testsuite
2012-08-20  Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/10416
    * g++.dg/warn/Wunused-var-17.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wunused-var-17.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-4@http.gcc.gnu.org/bugzilla/>
  2012-08-20 11:50 ` [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects paolo.carlini at oracle dot com
  2012-08-20 17:39 ` paolo at gcc dot gnu.org
@ 2012-08-20 17:40 ` paolo.carlini at oracle dot com
  2012-11-08 15:06 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-08-20 17:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #13 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-08-20 17:40:25 UTC ---
Fixed for 4.8.0.


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2012-08-20 17:40 ` paolo.carlini at oracle dot com
@ 2012-11-08 15:06 ` paolo.carlini at oracle dot com
  3 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-11-08 15:06 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bisqwit at iki dot fi

--- Comment #14 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-11-08 15:06:14 UTC ---
*** Bug 55239 has been marked as a duplicate of this bug. ***


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-5473@http.gcc.gnu.org/bugzilla/>
  2005-12-23  0:57 ` pinskia at gcc dot gnu dot org
  2006-01-22 10:44 ` gdr at gcc dot gnu dot org
@ 2006-01-23 22:23 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-01-23 22:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from pinskia at gcc dot gnu dot org  2006-01-23 22:23 -------
(In reply to comment #9)
> Of course, the compiler (more precisely the middle-end) knows that the
> destructor has a side-effect.  But GCC middle-end tends to have little
> knowledge of language specific idioms.  Notice also that a is "used"
> from a very high level abstraction point of view -- not from byte-fiddling
> point of view.  This is another instance of diagnostic PR better handed off
> to the front-end.

Actually the diagnostic is done in the front-end in cp/decl.c in poplevel.

  /* Before we remove the declarations first check for unused variables.  */
  if (warn_unused_variable
      && !processing_template_decl)
    for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
      if (TREE_CODE (decl) == VAR_DECL
          && ! TREE_USED (decl)
          && ! DECL_IN_SYSTEM_HEADER (decl)
          && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
        warning (0, "unused variable %q+D", decl);


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-5473@http.gcc.gnu.org/bugzilla/>
  2005-12-23  0:57 ` pinskia at gcc dot gnu dot org
@ 2006-01-22 10:44 ` gdr at gcc dot gnu dot org
  2006-01-23 22:23 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 13+ messages in thread
From: gdr at gcc dot gnu dot org @ 2006-01-22 10:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from gdr at gcc dot gnu dot org  2006-01-22 10:44 -------
(In reply to comment #0)
> Sometimes a variable is created only for the side-effects of its
> constructor/destructor. For example, in the following code:
> 
> struct Closer { ~Closer() { CloseClipboard(); } } closer;
> 
> the closer variable is only used to make sure the clipboard is closed when the
> function leaves (normally or exceptionally).
> 
> G++ apparently does not think this is valid use of a variable, and warns:
> "warning: unused variable".
> 
> Release:
> gcc 3.2
> 
> How-To-Repeat:
> Compile:
> 
> void f () { struct atend { ~atend () { std::cout << "leaving f\n"; } } a; }
> 
> with -Wall.
> 

The construct reported in this PR is a widely known and used C++ idiom:
Resource Acquisition Is Initialization (RAII).  It is inappropriate for
-Wall to warn about it.  However, the mechanism underpining it is so broad
that we would need more accurate measure to fix it.

Of course, the compiler (more precisely the middle-end) knows that the
destructor has a side-effect.  But GCC middle-end tends to have little
knowledge of language specific idioms.  Notice also that a is "used"
from a very high level abstraction point of view -- not from byte-fiddling
point of view.  This is another instance of diagnostic PR better handed off
to the front-end.

-- Gaby


-- 

gdr at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gdr at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <bug-10416-5473@http.gcc.gnu.org/bugzilla/>
@ 2005-12-23  0:57 ` pinskia at gcc dot gnu dot org
  2006-01-22 10:44 ` gdr at gcc dot gnu dot org
  2006-01-23 22:23 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-12-23  0:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from pinskia at gcc dot gnu dot org  2005-12-23 00:57 -------
*** Bug 25541 has been marked as a duplicate of this bug. ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |drepper at redhat dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
                   ` (4 preceding siblings ...)
  2004-08-16 18:56 ` bangerth at dealii dot org
@ 2005-09-07 17:44 ` pinskia at gcc dot gnu dot org
  5 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-09-07 17:44 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|missed-optimization         |diagnostic
   Last reconfirmed|2005-05-16 02:30:28         |2005-09-07 17:43:18
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
                   ` (3 preceding siblings ...)
  2004-08-11  0:02 ` mbp at sourcefrog dot net
@ 2004-08-16 18:56 ` bangerth at dealii dot org
  2005-09-07 17:44 ` pinskia at gcc dot gnu dot org
  5 siblings, 0 replies; 13+ messages in thread
From: bangerth at dealii dot org @ 2004-08-16 18:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-08-16 18:56 -------
Still there with the testcase in #2. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|0000-00-00 00:00:00         |2004-08-16 18:56:18
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
                   ` (2 preceding siblings ...)
  2004-06-10 19:43 ` pinskia at gcc dot gnu dot org
@ 2004-08-11  0:02 ` mbp at sourcefrog dot net
  2004-08-16 18:56 ` bangerth at dealii dot org
  2005-09-07 17:44 ` pinskia at gcc dot gnu dot org
  5 siblings, 0 replies; 13+ messages in thread
From: mbp at sourcefrog dot net @ 2004-08-11  0:02 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From mbp at sourcefrog dot net  2004-08-11 00:02 -------
Workaround: Adding an empty constructor suppresses the warning.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
  2004-06-10 19:31 ` mutz at kde dot org
  2004-06-10 19:41 ` pinskia at gcc dot gnu dot org
@ 2004-06-10 19:43 ` pinskia at gcc dot gnu dot org
  2004-08-11  0:02 ` mbp at sourcefrog dot net
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-10 19:43 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-10 19:43 -------
Confirmed.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |NEW


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
  2004-06-10 19:31 ` mutz at kde dot org
@ 2004-06-10 19:41 ` pinskia at gcc dot gnu dot org
  2004-06-10 19:43 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-10 19:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-10 19:41 -------
Acutally the compiler knows that the variable is used and not unused, why it warns is beyond me.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

* [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
       [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
@ 2004-06-10 19:31 ` mutz at kde dot org
  2004-06-10 19:41 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: mutz at kde dot org @ 2004-06-10 19:31 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From mutz at kde dot org  2004-06-10 19:31 -------
(In reply to comment #1)   
> State-Changed-From-To: open->closed   
> State-Changed-Why: The compiler cannot know what you want. If you don't like   
>     the warning, switch it off, or follow what the manual   
<snip>   
   
Sorry, but this reasoning doesn't hold.   
   
See e.g. (I'll attach this file in a minute):   
   
---begin broken-warning.cpp---   
extern void foo();   
   
class AtTheEndOfMain {   
public:   
  ~AtTheEndOfMain() {   
    foo();   
  }   
};   
   
class AtTheStartOfMain {   
public:   
  AtTheStartOfMain() {   
    foo();   
  }   
};   
   
int main() {   
  AtTheEndOfMain ateom;   
  AtTheStartOfMain atsom;   
   
  return 0;   
}   
---end broken-warning.cpp---   
   
$ g++ -Wall -c -o broken-warning.{o,cpp}   
broken-warning.cpp: In function `int main()':   
broken-warning.cpp:19: warning: unused variable `AtTheEndOfMain ateom'   
   
What's the difference between dtor side effects (which are ignored) and ctor   
side effect (which are honoured w.r.t. the unused-warning)?   
   
The difference is probably that you yourself are so used to using   
resource-allocation-is-initialization that you don't want to be warned about   
every use of it, so the ctor side effects are honoured. It simply seems that   
no-one checked for dtor-only side effects, and although I agree that they're   
not very often used in normal C++, they're often used when you do AOP in C++.   
   
This warning is also not at all useful. Warnings are there for the case where   
there is potential discrepancy between what the user writes and what he wants.   
   
This warning should only be emitted when the compiler can be sure that removing   
the variable has no side-effects. Yes, there are cornercases where the removal   
of an automatic variable can trigger a bufferoverflow to become visible etc,   
but let's not get hypothetical.   
   
This warning is taken by users to mean "you can remove this variable, it is not   
used". And it's dangerous if you can't rely on it. A warning that you can't do   
anything about except setting a proprietary attribute is wrong.   
  
Thinking of it, the variable isn't at all unused: you need it to pass it as the  
this pointer when invoking the dtor...   

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416


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

end of thread, other threads:[~2012-11-08 15:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-10416-4@http.gcc.gnu.org/bugzilla/>
2012-08-20 11:50 ` [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects paolo.carlini at oracle dot com
2012-08-20 17:39 ` paolo at gcc dot gnu.org
2012-08-20 17:40 ` paolo.carlini at oracle dot com
2012-11-08 15:06 ` paolo.carlini at oracle dot com
     [not found] <bug-10416-5473@http.gcc.gnu.org/bugzilla/>
2005-12-23  0:57 ` pinskia at gcc dot gnu dot org
2006-01-22 10:44 ` gdr at gcc dot gnu dot org
2006-01-23 22:23 ` pinskia at gcc dot gnu dot org
     [not found] <20030415185600.10416.gccbugs@contacts.eelis.net>
2004-06-10 19:31 ` mutz at kde dot org
2004-06-10 19:41 ` pinskia at gcc dot gnu dot org
2004-06-10 19:43 ` pinskia at gcc dot gnu dot org
2004-08-11  0:02 ` mbp at sourcefrog dot net
2004-08-16 18:56 ` bangerth at dealii dot org
2005-09-07 17:44 ` pinskia at gcc dot gnu dot org

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