OSDN Git Service

2011-05-23 Jonathan Wakely <jwakely.gcc@gmail.com>
authorredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 23 May 2011 08:15:16 +0000 (08:15 +0000)
committerredi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 23 May 2011 08:15:16 +0000 (08:15 +0000)
PR c++/18016
* init.c (perform_member_init): Check for self-initialization.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@174058 138bc75d-0d04-0410-961f-82ee72b054a4

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

index e6399d3..20c9808 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 7d7adbe..5f30275 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 b1446bb..b87cb2a 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" }