From patchwork Sun Nov 13 05:10:10 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 125379 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 7328BB70C5 for ; Sun, 13 Nov 2011 16:10:40 +1100 (EST) Received: (qmail 1552 invoked by alias); 13 Nov 2011 05:10:34 -0000 Received: (qmail 1543 invoked by uid 22791); 13 Nov 2011 05:10:31 -0000 X-SWARE-Spam-Status: No, hits=-7.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS 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; Sun, 13 Nov 2011 05:10:13 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pAD5ADB5020598 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 13 Nov 2011 00:10:13 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pAD5ACYf013572 for ; Sun, 13 Nov 2011 00:10:12 -0500 Received: from [0.0.0.0] (ovpn-113-23.phx2.redhat.com [10.3.113.23]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id pAD5AB3Q005829 for ; Sun, 13 Nov 2011 00:10:12 -0500 Message-ID: <4EBF5132.4060407@redhat.com> Date: Sun, 13 Nov 2011 00:10:10 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20111001 Thunderbird/7.0.1 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/986 (warn about temporary bound to reference in constructor) 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 My recent work on fixing lifetime extension for temporaries bound to references makes it trivial to add this warning. Tested x86_64-pc-linux-gnu, applied to trunk. commit 1db25121594ff9405adeb5bd6892d72679bf2ba1 Author: Jason Merrill Date: Sat Nov 12 20:42:21 2011 -0500 PR c++/986 * call.c (set_up_extended_ref_temp): Warn about references bound to non-static reference members. * init.c (perform_member_init): Pass in the member. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index dd3026d..181d9e8 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -8609,6 +8609,14 @@ set_up_extended_ref_temp (tree decl, tree expr, VEC(tree,gc) **cleanups, if (TREE_CODE (expr) != TARGET_EXPR) expr = get_target_expr (expr); + if (TREE_CODE (decl) == FIELD_DECL + && extra_warnings && !TREE_NO_WARNING (decl)) + { + warning (OPT_Wextra, "a temporary bound to %qD only persists " + "until the constructor exits", decl); + TREE_NO_WARNING (decl) = true; + } + /* Recursively extend temps in this initializer. */ TARGET_EXPR_INITIAL (expr) = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups); diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 888c151..73f2b67 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -623,7 +623,7 @@ perform_member_init (tree member, tree init) if (init == error_mark_node) return; /* Use 'this' as the decl, as it has the lifetime we want. */ - init = extend_ref_init_temps (current_class_ptr, init, &cleanups); + init = extend_ref_init_temps (member, init, &cleanups); if (TREE_CODE (type) == ARRAY_TYPE && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type))) init = build_vec_init_expr (type, init, tf_warning_or_error); diff --git a/gcc/testsuite/g++.dg/warn/ref-temp1.C b/gcc/testsuite/g++.dg/warn/ref-temp1.C new file mode 100644 index 0000000..26f1ca5 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/ref-temp1.C @@ -0,0 +1,11 @@ +// PR c++/986 +// { dg-options "-Wall -Wextra" } + +struct X { X (int); }; + +struct Y { + Y (); + const X &x; // note the ampersand +}; + +Y::Y () : x(1) {} // { dg-warning "temporary" }