public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: C++ PATCH for c++/34772 (warning about self-initialization without -Winit-self)
Date: Mon, 09 May 2011 18:37:00 -0000	[thread overview]
Message-ID: <4DC824D2.2060203@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 287 bytes --]

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.

[-- Attachment #2: 34772.patch --]
[-- Type: text/plain, Size: 9179 bytes --]

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.

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;
-}

             reply	other threads:[~2011-05-09 17:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-09 18:37 Jason Merrill [this message]
2011-05-21 22:07 ` H.J. Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DC824D2.2060203@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).