public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [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 16:46 ` [Bug c++/18016] " ejb at ql dot org ` (3 more replies) 0 siblings, 4 replies; 5+ 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] 5+ messages in thread
* [Bug c++/18016] -Winit-self misses member variables initialized after : in ctor 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 16:46 ` 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 subsequent siblings) 3 siblings, 0 replies; 5+ messages in thread From: ejb at ql dot org @ 2004-10-15 16:46 UTC (permalink / raw) To: gcc-bugs ------- Additional Comments From ejb at ql dot org 2004-10-15 16:46 ------- Created an attachment (id=7358) --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=7358&action=view) source file that reproduces problem This attachment contains the code that is also inlined in the bug report. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself 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 16:46 ` [Bug c++/18016] " ejb at ql dot org @ 2004-10-15 17:05 ` 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 3 siblings, 0 replies; 5+ messages in thread From: pinskia at gcc dot gnu dot org @ 2004-10-15 17:05 UTC (permalink / raw) To: gcc-bugs ------- Additional Comments From pinskia at gcc dot gnu dot org 2004-10-15 17:05 ------- -Winit-self has nothing to do with this problem really. in this case :a(a) is equivalent to this->a = this->a; We should warn about this case even without -Winit-self or even -Wuninitialize as we can warn without optimization turned on. Note I added -Winit-self so I know what it was designed to do. -- What |Removed |Added ---------------------------------------------------------------------------- Severity|normal |enhancement Status|UNCONFIRMED |NEW Ever Confirmed| |1 Keywords| |diagnostic Last reconfirmed|0000-00-00 00:00:00 |2004-10-15 17:05:57 date| | Summary|-Winit-self misses member |Warn about member variables |variables initialized after |initialized with itself |: in ctor | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself 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 16:46 ` [Bug c++/18016] " 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 3 siblings, 0 replies; 5+ messages in thread From: giovannibajo at libero dot it @ 2004-10-28 3:35 UTC (permalink / raw) To: gcc-bugs ------- Additional Comments From giovannibajo at libero dot it 2004-10-28 03:35 ------- > -Winit-self has nothing to do with this problem really. > in this case :a(a) is equivalent to this->a = this->a; Not really. The member-list syntax is used to *initialize* the members, not to assign a value to them after a default initialization. I think it makes sense to warn only with -Winit-self. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/18016] Warn about member variables initialized with itself 2004-10-15 16:45 [Bug c++/18016] New: -Winit-self misses member variables initialized after : in ctor ejb at ql dot org ` (2 preceding siblings ...) 2004-10-28 3:35 ` giovannibajo at libero dot it @ 2004-10-28 13:08 ` bangerth at dealii dot org 3 siblings, 0 replies; 5+ messages in thread From: bangerth at dealii dot org @ 2004-10-28 13:08 UTC (permalink / raw) To: gcc-bugs ------- Additional Comments From bangerth at dealii dot org 2004-10-28 13:08 ------- That is my view, too. It's an initializer, not an assignment. W. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18016 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-10-28 13:08 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 16:46 ` [Bug c++/18016] " 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).