From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D004D3858436 for ; Fri, 7 Jan 2022 00:22:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D004D3858436 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-444-zmbwYnQwOdCDFbsn3O6Ccw-1; Thu, 06 Jan 2022 19:22:07 -0500 X-MC-Unique: zmbwYnQwOdCDFbsn3O6Ccw-1 Received: by mail-qt1-f197.google.com with SMTP id h13-20020ac87d4d000000b002af9c496444so3315586qtb.22 for ; Thu, 06 Jan 2022 16:22:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Abr6rjzx1R85GhQPSpK/XGOjQBE/OYq0hXStGhYGDNE=; b=tUj59Jvm5hRkOg+7LleKavKqjToQFYKOVIPDSjuiFHGkb7rordMMT5WS45NuzHWx4W UX1+EviwmHC9nnhmX552Yi9zZO8Nu40PnTerelVHHg6SBTuLaTdyW9Xol9py7XCSr5qw TlhDIIlnoplmG8EXgFMjFfBJTKHPdjRv0pVCYcUqx7VJRXJtR4tP/50wGrz1RTTwVPyG i3iaTQp8BUpTa1FQr2dID6wavDKyUCCaIsBoYRjAstrhEujyLhbjuK2k2CR/yNF8bDOc b5cIhjewjMRcJK0pqY/HCsoSEqF77FWgvQrrKlIYt6TR2D5dEdWXLzKcrpwMufI4NC5d RYLQ== X-Gm-Message-State: AOAM533UhIwx0xVZe2RVd/JfhiehYt+P8lZwZWzEm2eMkkmGkMuFrYoz mxUwUgfe6eeArBEk8+07WZd8dD2soczcjLE3mSI+Mpw0txjeVQnWGjJJJLtnI5/R719xgDuYXvo ckcirRGSYgMdnbirVsf5z0KVwx5nyK0PLbvUNKPHKqJLXXk6VSSit6/xrnFmBo4Gvpw== X-Received: by 2002:a05:6214:2a88:: with SMTP id jr8mr56117275qvb.118.1641514926681; Thu, 06 Jan 2022 16:22:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJyb+KUbEmIcV6y4sVxmO2+TPPdMi6RavoaglU+xVShO5vXFXDFkNtQRUvzSi2C0FcSx5e13Mg== X-Received: by 2002:a05:6214:2a88:: with SMTP id jr8mr56117257qvb.118.1641514926263; Thu, 06 Jan 2022 16:22:06 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id s3sm2323785qkp.93.2022.01.06.16.22.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 16:22:05 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed 07/11] c++: keep destroying array after one dtor throws [PR66451] Date: Thu, 6 Jan 2022 19:21:52 -0500 Message-Id: <20220107002156.2992278-7-jason@redhat.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220107002156.2992278-1-jason@redhat.com> References: <20220107002156.2992278-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Jan 2022 00:22:10 -0000 When we're cleaning up an array, if one destructor throws, we should still try to clean up the rest of the array. We can use TRY_CATCH_EXPR for this, instead of a TARGET_EXPR like my other recent patches, because a destructor call can't involve any temporaries that need to live longer. I thought about only doing this when we call build_vec_delete_1 from build_vec_init, but it seems appropriate for delete-expressions as well; we've said that the array's lifetime is over, it makes sense to keep trying to destroy it. The standard isn't clear, but clang seems to agree with me. PR c++/66451 gcc/cp/ChangeLog: * init.c (build_vec_delete_1): Handle throwing dtor. (build_vec_init): Tell it we're in a cleanup already. gcc/testsuite/ChangeLog: * g++.dg/eh/array3.C: New test. --- gcc/cp/init.c | 18 +++++++++++-- gcc/testsuite/g++.dg/eh/array1.C | 8 +++++- gcc/testsuite/g++.dg/eh/array3.C | 40 ++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/eh/delete1.C | 2 +- gcc/testsuite/g++.dg/ipa/devirt-40.C | 10 ++++++- gcc/testsuite/g++.dg/warn/pr83054.C | 9 ++++++- 6 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/eh/array3.C diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 7c7b8104026..df63e618394 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -4006,7 +4006,8 @@ build_new (location_t loc, vec **placement, tree type, static tree build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type, special_function_kind auto_delete_vec, - int use_global_delete, tsubst_flags_t complain) + int use_global_delete, tsubst_flags_t complain, + bool in_cleanup = false) { tree virtual_size; tree ptype = build_pointer_type (type = complete_type (type)); @@ -4109,6 +4110,18 @@ build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type, body = build_compound_expr (loc, body, tmp); loop = build1 (LOOP_EXPR, void_type_node, body); + + /* If one destructor throws, keep trying to clean up the rest, unless we're + already in a build_vec_init cleanup. */ + if (flag_exceptions && !in_cleanup && !expr_noexcept_p (tmp, tf_none)) + { + loop = build2 (TRY_CATCH_EXPR, void_type_node, loop, + unshare_expr (loop)); + /* Tell honor_protect_cleanup_actions to discard this on the + exceptional path. */ + TRY_CATCH_IS_CLEANUP (loop) = true; + } + loop = build_compound_expr (loc, tbase_init, loop); no_destructor: @@ -4490,7 +4503,8 @@ build_vec_init (tree base, tree maxindex, tree init, e = build_vec_delete_1 (input_location, rval, m, inner_elt_type, sfk_complete_destructor, - /*use_global_delete=*/0, complain); + /*use_global_delete=*/0, complain, + /*in_cleanup*/true); if (e == error_mark_node) errors = true; TARGET_EXPR_CLEANUP (iterator_targ) = e; diff --git a/gcc/testsuite/g++.dg/eh/array1.C b/gcc/testsuite/g++.dg/eh/array1.C index 30b035cfc52..79d62ad5058 100644 --- a/gcc/testsuite/g++.dg/eh/array1.C +++ b/gcc/testsuite/g++.dg/eh/array1.C @@ -2,10 +2,16 @@ // rather than one for each element. // { dg-options "-fdump-tree-gimple" } +#if __cplusplus < 201100L +#define NOTHROW throw() +#else +#define NOTHROW noexcept +#endif + struct A { A(); - ~A(); + ~A() NOTHROW; }; void f() diff --git a/gcc/testsuite/g++.dg/eh/array3.C b/gcc/testsuite/g++.dg/eh/array3.C new file mode 100644 index 00000000000..547541b5dc3 --- /dev/null +++ b/gcc/testsuite/g++.dg/eh/array3.C @@ -0,0 +1,40 @@ +// PR c++/66451 +// { dg-do run } + +#if __cplusplus > 201100L +#define THROWING noexcept(false) +#else +#define THROWING +#endif + +extern "C" void abort(); + +int c; +struct A +{ + int a; + + A(int new_a) : a(new_a) { ++c; } + A(const A&); // not defined + ~A() THROWING + { + --c; + if(a==4) + throw a; + } +}; + +struct B +{ + A a[2]; + ~B() { } +}; + +int sink; +int main() +{ + try { + B b = {3,4}; + } catch(...) { } + if (c != 0) abort(); +} diff --git a/gcc/testsuite/g++.dg/eh/delete1.C b/gcc/testsuite/g++.dg/eh/delete1.C index 1727a74ff36..92ed646ef2c 100644 --- a/gcc/testsuite/g++.dg/eh/delete1.C +++ b/gcc/testsuite/g++.dg/eh/delete1.C @@ -69,7 +69,7 @@ int ary () { deleted = 0; - Baz *p = new Baz[5]; + Baz *p = new Baz[1]; try { delete[] p; } catch (...) { return deleted != 1;} return 1; diff --git a/gcc/testsuite/g++.dg/ipa/devirt-40.C b/gcc/testsuite/g++.dg/ipa/devirt-40.C index 32e0d22c0e7..31fe1504cb8 100644 --- a/gcc/testsuite/g++.dg/ipa/devirt-40.C +++ b/gcc/testsuite/g++.dg/ipa/devirt-40.C @@ -1,4 +1,12 @@ /* { dg-options "-O2 -fdump-tree-fre3-details" } */ + +// A throwing dtor in C++98 mode changes the results. +#if __cplusplus < 201100L +#define NOTHROW throw() +#else +#define NOTHROW noexcept +#endif + typedef enum { } UErrorCode; @@ -6,7 +14,7 @@ class UnicodeString { public: UnicodeString (); - virtual ~UnicodeString (); + virtual ~UnicodeString () NOTHROW; }; class A { diff --git a/gcc/testsuite/g++.dg/warn/pr83054.C b/gcc/testsuite/g++.dg/warn/pr83054.C index 506c9609b90..5285f94acee 100644 --- a/gcc/testsuite/g++.dg/warn/pr83054.C +++ b/gcc/testsuite/g++.dg/warn/pr83054.C @@ -2,6 +2,13 @@ // { dg-options "-O3 -Wsuggest-final-types" } // { dg-do compile } +// A throwing dtor in C++98 mode changes the warning. +#if __cplusplus < 201100L +#define NOTHROW throw() +#else +#define NOTHROW noexcept +#endif + extern "C" int printf (const char *, ...); struct foo // { dg-warning "final would enable devirtualization of 5 calls" } { @@ -12,7 +19,7 @@ struct foo // { dg-warning "final would enable devirtualization of 5 calls" } x = count++; printf("this %d = %x\n", x, (void *)this); } - virtual ~foo () { + virtual ~foo () NOTHROW { printf("this %d = %x\n", x, (void *)this); --count; } -- 2.27.0