From patchwork Mon Nov 24 08:50:23 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 413576 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 288C414003E for ; Mon, 24 Nov 2014 19:50:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=RqQRhSaP6J57TegVo5fzgUMcXIHWR5OKp3uOPmkwCx1 n9KOt6656jK9U69aNV9rT5Aw43YI+1GiWLSgtQUxSC0DC10HGwVBFuX7AiZxpzeN tBXrSdaJx23PfF7cRsCoVya/F4gY+Yg+yAgJYGayF4FVziVCDOwjHegL/hysn7bU = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=XFsMNe2ZfP+6MTLpCCM3EOzbiVo=; b=ESDQMKgfO3TvfiaxV J/Ol7yuEuC2m6bsMXGpBwF5PPFMw1CvwHHBKJzT/541tgds62/vQDCLm2vtmevj/ yJH4vGWkikPaxBt8UGjfjG6sPIZ3xPS7hSWMBdw9fKVu52mQCPN8WWQTupqK7OnK elRDeULkZ0sJW1AnKbqOYhRZFA= Received: (qmail 2519 invoked by alias); 24 Nov 2014 08:50:33 -0000 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 Received: (qmail 2509 invoked by uid 89); 24 Nov 2014 08:50:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL, BAYES_00, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: aserp1040.oracle.com Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Mon, 24 Nov 2014 08:50:31 +0000 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id sAO8oRGa013369 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 24 Nov 2014 08:50:28 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id sAO8oQnW007466 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Mon, 24 Nov 2014 08:50:26 GMT Received: from abhmp0013.oracle.com (abhmp0013.oracle.com [141.146.116.19]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id sAO8oPGn019624; Mon, 24 Nov 2014 08:50:25 GMT Received: from [192.168.1.4] (/79.53.236.236) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 24 Nov 2014 00:50:25 -0800 Message-ID: <5472F14F.9010301@oracle.com> Date: Mon, 24 Nov 2014 09:50:23 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch] PR 63203 X-IsSubscribed: yes Hi, in the audit trail Jon argued that we should have code specifically checking for references initialized with themselves (because one can't rebind references). I added to his draft a STRIP_NOPS, which manages to get back to the decl on the right hand side which we are looking for + minor tweaks. To make sure that the patch is safe from the false positives point of view, I also ran the testsuite with -Winit-self enabled by default and only g++.dg/init/ref6.C, correctly, showed up. Tested x86_64-linux. Thanks, Paolo. //////////////////// /cp 2014-11-24 Jonathan Wakely Paolo Carlini PR c++/63203 * decl.c (initialize_local_var): Add -Winit-self warning for references initialized with themselves. /testsuite 2014-11-24 Jonathan Wakely Paolo Carlini PR c++/63203 * g++.dg/warn/Winit-self-2.C: New. Index: cp/decl.c =================================================================== --- cp/decl.c (revision 217980) +++ cp/decl.c (working copy) @@ -6132,13 +6132,23 @@ initialize_local_var (tree decl, tree init) /* Perform the initialization. */ if (init) { - if (TREE_CODE (init) == INIT_EXPR - && !TREE_SIDE_EFFECTS (TREE_OPERAND (init, 1))) + tree rinit = (TREE_CODE (init) == INIT_EXPR + ? TREE_OPERAND (init, 1) : NULL_TREE); + if (rinit && !TREE_SIDE_EFFECTS (rinit)) { /* 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); + DECL_INITIAL (decl) = rinit; + + if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE) + { + STRIP_NOPS (rinit); + if (rinit == decl) + warning_at (DECL_SOURCE_LOCATION (decl), + OPT_Winit_self, + "reference %qD is initialized with itself", decl); + } } else { Index: testsuite/g++.dg/warn/Winit-self-2.C =================================================================== --- testsuite/g++.dg/warn/Winit-self-2.C (revision 0) +++ testsuite/g++.dg/warn/Winit-self-2.C (working copy) @@ -0,0 +1,12 @@ +// PR c++/63203 +// { dg-options "-Winit-self" } + +struct string { }; + +int main() +{ + for (int ii = 0; ii < 1; ++ii) + { + const string& str = str; // { dg-warning "is initialized with itself" } + } +}