diff mbox

C++ PATCH for c++/34772 (warning about self-initialization without -Winit-self)

Message ID 4DC824D2.2060203@redhat.com
State New
Headers show

Commit Message

Jason Merrill May 9, 2011, 5:30 p.m. UTC
In this testcase -Wuninitialized was warning about 'int i = i' without 
-Winit-self because the C++ front end always uses separate code for 
non-constant initialization.  But for simple initialization, it makes 
sense to use DECL_INITIAL.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit b105bfbee01e9183e7fc100f3a33c7c109db7fae
Author: Jason Merrill <jason@redhat.com>
Date:   Sat May 7 17:31:09 2011 -0400

    	PR c++/34772
    	* decl.c (initialize_local_var): Use DECL_INITIAL for simple
    	initialization.

Comments

H.J. Lu May 21, 2011, 5:31 p.m. UTC | #1
On Mon, May 9, 2011 at 10:30 AM, Jason Merrill <jason@redhat.com> wrote:
> In this testcase -Wuninitialized was warning about 'int i = i' without
> -Winit-self because the C++ front end always uses separate code for
> non-constant initialization.  But for simple initialization, it makes sense
> to use DECL_INITIAL.
>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
> commit b105bfbee01e9183e7fc100f3a33c7c109db7fae
> Author: Jason Merrill <jason@redhat.com>
> Date:   Sat May 7 17:31:09 2011 -0400
>
>        PR c++/34772
>        * decl.c (initialize_local_var): Use DECL_INITIAL for simple
>        initialization.
>

This caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49092
diff mbox

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index c139f3f..c255e16 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5689,21 +5689,32 @@  initialize_local_var (tree decl, tree init)
   /* Perform the initialization.  */
   if (init)
     {
-      int saved_stmts_are_full_exprs_p;
+      if (TREE_CODE (init) == INIT_EXPR
+	  && !TREE_SIDE_EFFECTS (TREE_OPERAND (init, 1)))
+	{
+	  /* Stick simple initializers in DECL_INITIAL so that
+	     -Wno-init-self works (c++/34772).  */
+	  gcc_assert (TREE_OPERAND (init, 0) == decl);
+	  DECL_INITIAL (decl) = TREE_OPERAND (init, 1);
+	}
+      else
+	{
+	  int saved_stmts_are_full_exprs_p;
 
-      /* If we're only initializing a single object, guard the destructors
-	 of any temporaries used in its initializer with its destructor.
-	 This isn't right for arrays because each element initialization is
-	 a full-expression.  */
-      if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
-	wrap_temporary_cleanups (init, cleanup);
+	  /* If we're only initializing a single object, guard the
+	     destructors of any temporaries used in its initializer with
+	     its destructor.  This isn't right for arrays because each
+	     element initialization is a full-expression.  */
+	  if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
+	    wrap_temporary_cleanups (init, cleanup);
 
-      gcc_assert (building_stmt_tree ());
-      saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
-      current_stmt_tree ()->stmts_are_full_exprs_p = 1;
-      finish_expr_stmt (init);
-      current_stmt_tree ()->stmts_are_full_exprs_p =
-	saved_stmts_are_full_exprs_p;
+	  gcc_assert (building_stmt_tree ());
+	  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
+	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
+	  finish_expr_stmt (init);
+	  current_stmt_tree ()->stmts_are_full_exprs_p =
+	    saved_stmts_are_full_exprs_p;
+	}
     }
 
   /* Set this to 0 so we can tell whether an aggregate which was
diff --git a/gcc/testsuite/c-c++-common/uninit-D-O0.c b/gcc/testsuite/c-c++-common/uninit-D-O0.c
new file mode 100644
index 0000000..e63cb80
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-D-O0.c
@@ -0,0 +1,9 @@ 
+/* Test we do not warn about initializing variable with self. */
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized" } */
+
+int f()
+{
+  int i = i;
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-D.c b/gcc/testsuite/c-c++-common/uninit-D.c
new file mode 100644
index 0000000..ea957e4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-D.c
@@ -0,0 +1,9 @@ 
+/* Test we do not warn about initializing variable with self. */
+/* { dg-do compile } */
+/* { dg-options "-O -Wuninitialized" } */
+
+int f()
+{
+  int i = i;
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-E-O0.c b/gcc/testsuite/c-c++-common/uninit-E-O0.c
new file mode 100644
index 0000000..2cc2459
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-E-O0.c
@@ -0,0 +1,9 @@ 
+/* Test we do warn about initializing variable with self when -Winit-self is supplied. */
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized -Winit-self" } */
+
+int f()
+{
+  int i = i; /* { dg-warning "i" "uninitialized variable warning" }  */
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-E.c b/gcc/testsuite/c-c++-common/uninit-E.c
new file mode 100644
index 0000000..eb356c3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-E.c
@@ -0,0 +1,9 @@ 
+/* Test we do warn about initializing variable with self when -Winit-self is supplied. */
+/* { dg-do compile } */
+/* { dg-options "-O -Wuninitialized -Winit-self" } */
+
+int f()
+{
+  int i = i; /* { dg-warning "i" "uninitialized variable warning" }  */
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-F-O0.c b/gcc/testsuite/c-c++-common/uninit-F-O0.c
new file mode 100644
index 0000000..737cc65
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-F-O0.c
@@ -0,0 +1,9 @@ 
+/* Test we do warn about initializing variable with self in the initialization. */
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized" } */
+
+int f()
+{
+  int i = i + 1; /* { dg-warning "i" "uninitialized variable warning" }  */
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-F.c b/gcc/testsuite/c-c++-common/uninit-F.c
new file mode 100644
index 0000000..1dbb365
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-F.c
@@ -0,0 +1,9 @@ 
+/* Test we do warn about initializing variable with self in the initialization. */
+/* { dg-do compile } */
+/* { dg-options "-O -Wuninitialized" } */
+
+int f()
+{
+  int i = i + 1; /* { dg-warning "i" "uninitialized variable warning" }  */
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-G-O0.c b/gcc/testsuite/c-c++-common/uninit-G-O0.c
new file mode 100644
index 0000000..d6edffe
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-G-O0.c
@@ -0,0 +1,9 @@ 
+/* Test we do not warn about initializing variable with address of self in the initialization. */
+/* { dg-do compile } */
+/* { dg-options "-Wuninitialized" } */
+
+void *f()
+{
+  void *i = &i;
+  return i;
+}
diff --git a/gcc/testsuite/c-c++-common/uninit-G.c b/gcc/testsuite/c-c++-common/uninit-G.c
new file mode 100644
index 0000000..08f5f53
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/uninit-G.c
@@ -0,0 +1,9 @@ 
+/* Test we do not warn about initializing variable with address of self in the initialization. */
+/* { dg-do compile } */
+/* { dg-options "-O -Wuninitialized" } */
+
+void *f()
+{
+  void *i = &i;
+  return i;
+}
diff --git a/gcc/testsuite/gcc.dg/uninit-D-O0.c b/gcc/testsuite/gcc.dg/uninit-D-O0.c
deleted file mode 100644
index e63cb80..0000000
--- a/gcc/testsuite/gcc.dg/uninit-D-O0.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do not warn about initializing variable with self. */
-/* { dg-do compile } */
-/* { dg-options "-Wuninitialized" } */
-
-int f()
-{
-  int i = i;
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-D.c b/gcc/testsuite/gcc.dg/uninit-D.c
deleted file mode 100644
index ea957e4..0000000
--- a/gcc/testsuite/gcc.dg/uninit-D.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do not warn about initializing variable with self. */
-/* { dg-do compile } */
-/* { dg-options "-O -Wuninitialized" } */
-
-int f()
-{
-  int i = i;
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-E-O0.c b/gcc/testsuite/gcc.dg/uninit-E-O0.c
deleted file mode 100644
index 2cc2459..0000000
--- a/gcc/testsuite/gcc.dg/uninit-E-O0.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do warn about initializing variable with self when -Winit-self is supplied. */
-/* { dg-do compile } */
-/* { dg-options "-Wuninitialized -Winit-self" } */
-
-int f()
-{
-  int i = i; /* { dg-warning "i" "uninitialized variable warning" }  */
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-E.c b/gcc/testsuite/gcc.dg/uninit-E.c
deleted file mode 100644
index eb356c3..0000000
--- a/gcc/testsuite/gcc.dg/uninit-E.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do warn about initializing variable with self when -Winit-self is supplied. */
-/* { dg-do compile } */
-/* { dg-options "-O -Wuninitialized -Winit-self" } */
-
-int f()
-{
-  int i = i; /* { dg-warning "i" "uninitialized variable warning" }  */
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-F-O0.c b/gcc/testsuite/gcc.dg/uninit-F-O0.c
deleted file mode 100644
index 737cc65..0000000
--- a/gcc/testsuite/gcc.dg/uninit-F-O0.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do warn about initializing variable with self in the initialization. */
-/* { dg-do compile } */
-/* { dg-options "-Wuninitialized" } */
-
-int f()
-{
-  int i = i + 1; /* { dg-warning "i" "uninitialized variable warning" }  */
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-F.c b/gcc/testsuite/gcc.dg/uninit-F.c
deleted file mode 100644
index 1dbb365..0000000
--- a/gcc/testsuite/gcc.dg/uninit-F.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do warn about initializing variable with self in the initialization. */
-/* { dg-do compile } */
-/* { dg-options "-O -Wuninitialized" } */
-
-int f()
-{
-  int i = i + 1; /* { dg-warning "i" "uninitialized variable warning" }  */
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-G-O0.c b/gcc/testsuite/gcc.dg/uninit-G-O0.c
deleted file mode 100644
index d6edffe..0000000
--- a/gcc/testsuite/gcc.dg/uninit-G-O0.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do not warn about initializing variable with address of self in the initialization. */
-/* { dg-do compile } */
-/* { dg-options "-Wuninitialized" } */
-
-void *f()
-{
-  void *i = &i;
-  return i;
-}
diff --git a/gcc/testsuite/gcc.dg/uninit-G.c b/gcc/testsuite/gcc.dg/uninit-G.c
deleted file mode 100644
index 08f5f53..0000000
--- a/gcc/testsuite/gcc.dg/uninit-G.c
+++ /dev/null
@@ -1,9 +0,0 @@ 
-/* Test we do not warn about initializing variable with address of self in the initialization. */
-/* { dg-do compile } */
-/* { dg-options "-O -Wuninitialized" } */
-
-void *f()
-{
-  void *i = &i;
-  return i;
-}