From patchwork Thu Oct 20 19:17:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 120864 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 71972B6F84 for ; Fri, 21 Oct 2011 06:18:10 +1100 (EST) Received: (qmail 12401 invoked by alias); 20 Oct 2011 19:18:06 -0000 Received: (qmail 12392 invoked by uid 22791); 20 Oct 2011 19:18:05 -0000 X-SWARE-Spam-Status: No, hits=-6.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_TM 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; Thu, 20 Oct 2011 19:17:51 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p9KJHp7o010814 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 20 Oct 2011 15:17:51 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p9KJHoeF032420 for ; Thu, 20 Oct 2011 15:17:50 -0400 Received: from [0.0.0.0] (ovpn-113-169.phx2.redhat.com [10.3.113.169]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p9KJHn0F003860 for ; Thu, 20 Oct 2011 15:17:50 -0400 Message-ID: <4EA073DC.8050406@redhat.com> Date: Thu, 20 Oct 2011 15:17:48 -0400 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++/41449 (EH cleanup of partially-aggregate-initialized objects) 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 The C++ standard says that if an exception is thrown during initialization of a class, any fully-constructed subobjects are destroyed. We already handled that properly for objects initialized via constructor, but we weren't handling it properly for aggregate initialization. This patch adds the necessary EH cleanups for during initialization; conveniently, just using push_eh_cleanup works here because we were already doing push/pop_stmt_list around the initialization as a whole. Tested x86_64-pc-linux-gnu, applying to trunk. commit 7d32708956095f3ddb6698fee9fa092f649d72d4 Author: Jason Merrill Date: Thu Oct 20 15:00:49 2011 -0400 PR c++/41449 * typeck2.c (split_nonconstant_init_1): Handle EH cleanup of initialized subobjects. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 3accab6..580f669 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -567,6 +567,13 @@ split_nonconstant_init_1 (tree dest, tree init) code = build2 (INIT_EXPR, inner_type, sub, value); code = build_stmt (input_location, EXPR_STMT, code); add_stmt (code); + if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type)) + { + code = (build_special_member_call + (sub, complete_dtor_identifier, NULL, inner_type, + LOOKUP_NORMAL, tf_warning_or_error)); + finish_eh_cleanup (code); + } num_split_elts++; } diff --git a/gcc/testsuite/g++.dg/eh/partial1.C b/gcc/testsuite/g++.dg/eh/partial1.C new file mode 100644 index 0000000..db73177 --- /dev/null +++ b/gcc/testsuite/g++.dg/eh/partial1.C @@ -0,0 +1,37 @@ +// PR c++/41449 +// { dg-do run } + +struct A +{ + A() {} + A(const A&) { throw 1; } +}; + +int bs; +struct B +{ + B() { ++bs; } + B(const B&) { ++bs; } + ~B() { --bs; } +}; + +struct C +{ + B b1; + A a; + B b2; +}; + +int main() +{ + { + B b1, b2; + A a; + + try { + C c = { b1, a, b2 }; + } catch (...) {} + } + if (bs != 0) + __builtin_abort (); +}