public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
@ 2010-12-16 16:05 ` redi at gcc dot gnu.org
2010-12-21 15:28 ` redi at gcc dot gnu.org
` (8 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2010-12-16 16:05 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-12-16 16:05:04 UTC ---
(In reply to comment #0)
> A() : a(a) // <-- should generate a warning
Clang warns about this with -Wuninitialized
My patch for PR 2972 *doesn't* help here, because A::a does have an initializer
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
2010-12-16 16:05 ` [Bug c++/18016] Warn about member variables initialized with itself redi at gcc dot gnu.org
@ 2010-12-21 15:28 ` redi at gcc dot gnu.org
2010-12-21 17:20 ` redi at gcc dot gnu.org
` (7 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2010-12-21 15:28 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |patch
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-12-21 15:28:18 UTC ---
With the patch at http://gcc.gnu.org/ml/gcc-patches/2010-12/msg01622.html
the testcase above gives:
$ g++4x a.cc -c -Wuninitialized
a.cc: In constructor ‘A::A()’:
a.cc:4:5: warning: ‘A::a’ is initialized with itself [-Wuninitialized]
a.cc: In member function ‘int A::getA()’:
a.cc:9:10: warning: ‘b’ is used uninitialized in this function
[-Wuninitialized]
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
2010-12-16 16:05 ` [Bug c++/18016] Warn about member variables initialized with itself redi at gcc dot gnu.org
2010-12-21 15:28 ` redi at gcc dot gnu.org
@ 2010-12-21 17:20 ` redi at gcc dot gnu.org
2010-12-22 9:18 ` manu at gcc dot gnu.org
` (6 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2010-12-21 17:20 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-12-21 17:19:57 UTC ---
my patch doesn't help in these cases (which clang does warn about):
A() : a(this->a) { }
A() : a((int)a) { }
A() : a(a+1) { }
For that we need proper tracking of uninitialized variables, which we don't do
for member variables. But my patch catches the simple typo where you
accidentally use the wrong variable name in a mem-initializer.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (2 preceding siblings ...)
2010-12-21 17:20 ` redi at gcc dot gnu.org
@ 2010-12-22 9:18 ` manu at gcc dot gnu.org
2010-12-22 14:40 ` redi at gcc dot gnu.org
` (5 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: manu at gcc dot gnu.org @ 2010-12-22 9:18 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #10 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2010-12-22 09:17:42 UTC ---
Like others commenting here, I don't understand why a(a) should not warn only
with -Winit-self. On the other hand, I always thought that Winit-self is a bad
idea. Although the patch does not fixes many cases and thus, we shouldn't close
this PR, it is better than nothing.
About the location, don't we have a better location at that point?
I am thinking that
X() :
j(j), #2
i(i) #3
{}
should give warnings in #2 and #3.
Please, resubmit and ping. I think this is small enough to go in GCC 4.6.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (3 preceding siblings ...)
2010-12-22 9:18 ` manu at gcc dot gnu.org
@ 2010-12-22 14:40 ` redi at gcc dot gnu.org
2011-05-23 8:39 ` redi at gcc dot gnu.org
` (4 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2010-12-22 14:40 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-12-22 14:40:07 UTC ---
(In reply to comment #10)
> Like others commenting here, I don't understand why a(a) should not warn only
> with -Winit-self.
I agree with Andrew, the a(a) mistake should always warn, it should be
independent of -Winit-self, which exists so that -Wuninitialized doesn't warn
about the common (but questionable) practice of self-initializing automatic
variables to silence warnings.
As I said in my mail to gcc-patches, if you want to leave a member variable
uninitialized, just don't give it a mem-initializer in the constructor. Giving
it a self-initializing one is just perverse. (The case of automatic variables
is different, you can't just not declare it to leave it uninitialized.)
Also, as -Winit-self is broken I didn't want to tie this bug to a broken
feature that might be changed to not work for C++.
> On the other hand, I always thought that Winit-self is a bad
> idea. Although the patch does not fixes many cases and thus, we shouldn't close
> this PR, it is better than nothing.
>
> About the location, don't we have a better location at that point?
>
> I am thinking that
>
> X() :
> j(j), #2
> i(i) #3
> {}
>
> should give warnings in #2 and #3.
There are various open bugs about that, e.g. PR 43064, I don't think it's
possible at the moment.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (4 preceding siblings ...)
2010-12-22 14:40 ` redi at gcc dot gnu.org
@ 2011-05-23 8:39 ` redi at gcc dot gnu.org
2011-05-23 8:52 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2011-05-23 8:39 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-05-23 08:15:24 UTC ---
Author: redi
Date: Mon May 23 08:15:16 2011
New Revision: 174058
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174058
Log:
2011-05-23 Jonathan Wakely <jwakely.gcc@gmail.com>
PR c++/18016
* init.c (perform_member_init): Check for self-initialization.
Added:
trunk/gcc/testsuite/g++.dg/warn/pr18016.C
Modified:
trunk/gcc/cp/ChangeLog
trunk/gcc/cp/init.c
trunk/gcc/testsuite/ChangeLog
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (5 preceding siblings ...)
2011-05-23 8:39 ` redi at gcc dot gnu.org
@ 2011-05-23 8:52 ` redi at gcc dot gnu.org
2011-06-27 18:07 ` ejb at ql dot org
` (2 subsequent siblings)
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2011-05-23 8:52 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |FIXED
Target Milestone|--- |4.7.0
--- Comment #13 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-05-23 08:19:38 UTC ---
fixed for 4.7.0
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (6 preceding siblings ...)
2011-05-23 8:52 ` redi at gcc dot gnu.org
@ 2011-06-27 18:07 ` ejb at ql dot org
2011-12-13 19:59 ` redi at gcc dot gnu.org
2012-11-14 11:41 ` redi at gcc dot gnu.org
9 siblings, 0 replies; 15+ messages in thread
From: ejb at ql dot org @ 2011-06-27 18:07 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
--- Comment #14 from ejb at ql dot org 2011-06-27 18:06:15 UTC ---
Very nice to see this bug fixed. :-)
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (7 preceding siblings ...)
2011-06-27 18:07 ` ejb at ql dot org
@ 2011-12-13 19:59 ` redi at gcc dot gnu.org
2012-11-14 11:41 ` redi at gcc dot gnu.org
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2011-12-13 19:59 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |tutufan at gmail dot com
--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-12-13 19:55:11 UTC ---
*** Bug 51533 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
` (8 preceding siblings ...)
2011-12-13 19:59 ` redi at gcc dot gnu.org
@ 2012-11-14 11:41 ` redi at gcc dot gnu.org
9 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-14 11:41 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |brunonery+bugzilla at
| |brunonery dot com
--- Comment #16 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-14 11:40:50 UTC ---
*** Bug 55318 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 15+ messages in thread
[parent not found: <bug-18016-4042@http.gcc.gnu.org/bugzilla/>]
* [Bug c++/18016] New: -Winit-self misses member variables initialized after : in ctor
@ 2004-10-15 16:45 ejb at ql dot org
2004-10-15 17:05 ` [Bug c++/18016] Warn about member variables initialized with itself pinskia at gcc dot gnu dot org
` (2 more replies)
0 siblings, 3 replies; 15+ messages in thread
From: ejb at ql dot org @ 2004-10-15 16:45 UTC (permalink / raw)
To: gcc-bugs
First, my gcc version information:
% gcc -v
Reading specs from /usr/lib/gcc/i486-linux/3.4.2/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr
--libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4
--enable-shared --with-system-zlib --enable-nls --without-included-gettext
--program-suffix=-3.4 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt
--enable-clocale=gnu --enable-libstdcxx-debug --enable-java-gc=boehm
--enable-java-awt=gtk --disable-werror i486-linux
Thread model: posix
gcc version 3.4.2 (Debian 3.4.2-2)
This is the gcc-3.4 package in debian, FYI, but this bug is not likely to be
debian-specific.
I'm excited to see the -Winit-self warning, but it fails to catch a common
problem, as illustrated by this code fragment (which I will include inline and
also as an attachment for convenience). This code has two cases of variables
initialized with themselves: one trivial int b = b case, and one more subtle A()
: a(a) case. g++ warns for one but not the other. Use this command:
g++-3.4 -c -Winit-self -Wuninitialized -O1 a.cc
on this file:
class A
{
public:
A() : a(a) // <-- should generate a warning
{
}
int getA()
{
int b = b; // <-- generates warning as it should
return this->a + b;
}
private:
int a;
};
int main()
{
A a;
return a.getA();
}
generates this output:
a.cc: In function `int main()':
a.cc:9: warning: 'b' might be used uninitialized in this function
It fails to warn for A::a being initialized with itself in the constructor.
One might also consider it to be a problem that A::getA's being inlined causes
the warning message to mention main() rather than getA() -- this problem is not
present if getA is not inlined. Anyway, the line number is okay, so this seems
pretty unimportant though still worthy of mention.
It would also be nice if -Winit-self would automatically add -Wuninitialized
instead of just not doing anything unless -Wuninitialized was also specified.
I'll mention that in a separate bug report.
--
Summary: -Winit-self misses member variables initialized after :
in ctor
Product: gcc
Version: 3.4.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ejb at ql dot org
CC: ejb at ql dot org,gcc-bugs at gcc dot gnu dot org
GCC build triplet: i486-linux
GCC host triplet: i486-linux
GCC target triplet: i486-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2012-11-14 11:41 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <bug-18016-4@http.gcc.gnu.org/bugzilla/>
2010-12-16 16:05 ` [Bug c++/18016] Warn about member variables initialized with itself redi at gcc dot gnu.org
2010-12-21 15:28 ` redi at gcc dot gnu.org
2010-12-21 17:20 ` redi at gcc dot gnu.org
2010-12-22 9:18 ` manu at gcc dot gnu.org
2010-12-22 14:40 ` redi at gcc dot gnu.org
2011-05-23 8:39 ` redi at gcc dot gnu.org
2011-05-23 8:52 ` redi at gcc dot gnu.org
2011-06-27 18:07 ` ejb at ql dot org
2011-12-13 19:59 ` redi at gcc dot gnu.org
2012-11-14 11:41 ` redi at gcc dot gnu.org
[not found] <bug-18016-4042@http.gcc.gnu.org/bugzilla/>
2007-04-27 15:45 ` irving at cs dot stanford dot edu
2010-02-21 19:17 ` manu at gcc dot gnu dot org
2004-10-15 16:45 [Bug c++/18016] New: -Winit-self misses member variables initialized after : in ctor ejb at ql dot org
2004-10-15 17:05 ` [Bug c++/18016] Warn about member variables initialized with itself pinskia at gcc dot gnu dot org
2004-10-28 3:35 ` giovannibajo at libero dot it
2004-10-28 13:08 ` bangerth at dealii 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).