Patchwork fix c++/18016 - warn about self-initialization in constructor init-list

login
register
mail settings
Submitter Jonathan Wakely
Date May 22, 2011, 9:11 p.m.
Message ID <BANLkTi=EObVZh36cjdxL-MHmqd5fiokm=A@mail.gmail.com>
Download mbox | patch
Permalink /patch/96779/
State New
Headers show

Comments

Jonathan Wakely - May 22, 2011, 9:11 p.m.
I didn't get this reviewed previously so I'll try again for 4.7 ...


---------- Forwarded message ----------


Here's my attempt to fix PR c++/18016 so that we get a warning from

struct S {
 int i;
 S() : i(i) { }
};

As I said in the audit trail, -Winit-self is broken for C++ (PR
c++/34772) so I made this warn with
-Wuninitialized and not affected by -Winit-self.

Even if -Winit-self wasn't broken I don't think it applies here,
there's no valid reason to initialize a member with itself, if you
really want to leave a member uninitialized then don't use a
mem-initializer in the constructor (which will work at least until
someone fixes PR c++/2972)

cp/ChangeLog:

       PR c++/18016
       * init.c (perform_member_init): Check for self-initialization.


testsuite/ChangeLog:

       PR c++/18016
       * g++.dg/warn/pr18016.C: New.


tested x86_64-linux with no regressions, ok for trunk?
Jason Merrill - May 23, 2011, 2:07 a.m.
On 05/22/2011 05:11 PM, Jonathan Wakely wrote:
> As I said in the audit trail, -Winit-self is broken for C++ (PR
> c++/34772)

Not anymore.  :)

> Even if -Winit-self wasn't broken I don't think it applies here,
> there's no valid reason to initialize a member with itself, if you
> really want to leave a member uninitialized then don't use a
> mem-initializer in the constructor (which will work at least until
> someone fixes PR c++/2972)

...at which point we would want it to be controlled by -Winit-self, so 
let's use -Winit-self now.

OK with that change.

Jason

Patch

Index: cp/init.c
===================================================================
--- cp/init.c	(revision 174044)
+++ cp/init.c	(working copy)
@@ -501,6 +501,17 @@ 
   if (decl == error_mark_node)
     return;
 
+  if (warn_uninitialized && init && TREE_CODE (init) == TREE_LIST
+      && TREE_CHAIN (init) == NULL_TREE)
+    {
+      tree val = TREE_VALUE (init);
+      if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
+	  && TREE_OPERAND (val, 0) == current_class_ref)
+	warning_at (DECL_SOURCE_LOCATION (current_function_decl),
+		    OPT_Wuninitialized, "%qD is initialized with itself",
+		    member);
+    }
+
   if (init == void_type_node)
     {
       /* mem() means value-initialization.  */
Index: testsuite/g++.dg/warn/pr18016.C
===================================================================
--- testsuite/g++.dg/warn/pr18016.C	(revision 0)
+++ testsuite/g++.dg/warn/pr18016.C	(revision 0)
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized" } */
+
+class X {
+  int i;
+  X() : i(i) { }   // { dg-warning "initialized with itself" }
+  X(int i) : i(i) { }
+  X(const X& x) : i(x.i) { }
+};
+
+// { dg-prune-output "In constructor" }