From patchwork Mon May 9 17:30:58 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 94829 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 01B39B6F12 for ; Tue, 10 May 2011 03:31:17 +1000 (EST) Received: (qmail 7665 invoked by alias); 9 May 2011 17:31:15 -0000 Received: (qmail 7653 invoked by uid 22791); 9 May 2011 17:31:14 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 09 May 2011 17:31:00 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p49HUxtD023093 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 9 May 2011 13:30:59 -0400 Received: from [127.0.0.1] (ovpn-113-60.phx2.redhat.com [10.3.113.60]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p49HUxP1031204 for ; Mon, 9 May 2011 13:30:59 -0400 Message-ID: <4DC824D2.2060203@redhat.com> Date: Mon, 09 May 2011 13:30:58 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/34772 (warning about self-initialization without -Winit-self) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 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; -}