re PR c++/18016 (Warn about member variables initialized with itself)
authorJonathan Wakely <jwakely.gcc@gmail.com>
Mon, 23 May 2011 08:15:16 +0000 (08:15 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 23 May 2011 08:15:16 +0000 (09:15 +0100)
2011-05-23  Jonathan Wakely  <jwakely.gcc@gmail.com>

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

From-SVN: r174058

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/pr18016.C [new file with mode: 0644]

index e6399d364e9eac69599e1dc057c842b63584fb79..20c98083e9fd18ab8227a1729b3c52b3b9275d46 100644 (file)
@@ -1,3 +1,8 @@
+2011-05-23  Jonathan Wakely  <jwakely.gcc@gmail.com>
+
+       PR c++/18016
+       * init.c (perform_member_init): Check for self-initialization.
+
 2011-05-22  Jason Merrill  <jason@redhat.com>
 
        PR c++/48647
index 7d7adbe04050f310c74790a52ddf50803f868e6b..5f30275ae5bb6d944069b43ff1f77fc072ec778a 100644 (file)
@@ -501,6 +501,17 @@ perform_member_init (tree member, tree init)
   if (decl == error_mark_node)
     return;
 
+  if (warn_init_self && 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 b1446bb7951472ffb89398109272088d2028c08e..b87cb2aa51e5b78076318da99074d48df67219cd 100644 (file)
@@ -1,3 +1,8 @@
+2011-05-23  Jonathan Wakely  <jwakely.gcc@gmail.com>
+
+       PR c++/18016
+       * g++.dg/warn/pr18016.C: New.
+
 2011-05-23  Tom de Vries  <tom@codesourcery.com>
 
        PR target/45098
diff --git a/gcc/testsuite/g++.dg/warn/pr18016.C b/gcc/testsuite/g++.dg/warn/pr18016.C
new file mode 100644 (file)
index 0000000..9cf1ea3
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized -Winit-self" } */
+
+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" }