From patchwork Fri May 3 20:00:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1931181 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ETCdcBd1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VWMBR0K5fz1ymc for ; Sat, 4 May 2024 06:01:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3EDD33844769 for ; Fri, 3 May 2024 20:01:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A4344384AB58 for ; Fri, 3 May 2024 20:00:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A4344384AB58 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A4344384AB58 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714766459; cv=none; b=OGasAvbea/OWVTUgFZLnspmQrcirs4LuuYig+2vzfzOFfvYT5Nl+v8LEaDzGpWaScmaOQGwPwM9z8K7BwhDhkA+g3q1rQn+5ZYKb1tNMe7Ch+XqB9dCUUlb4ZkMDD4eq2oCYDSqLxMykJ1eupM3jirq1X2dBkZLf14gCKZkJ0pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714766459; c=relaxed/simple; bh=iiOC0Y9rQkqwh3oajseEvXP3HX18Jve/nxdhB9MSpVk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=X0qevYTOJq5vhgdizBCJ5NWSaiMgFhwwpZMJx2ZcbXsCSvBe8u3e76C1S1gso7CHWFOzJKRZKWuY3Cl2v6e2mqTL4XWU4PRJ8+xgpI8Qd89usDbS45PSETfVWlpx9gi0qNZB6557xdiV27rQrXEiFXk+TXJ+VKKFBuGjsMG33eE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714766456; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+vhjwEtvhoiRKOpMtZflyW7UIM5Iqomf7fPlO1cgUkk=; b=ETCdcBd1nAlT2LoldUOVm3VYRwVTi2N1LauSMTxbE2gQ6QsfKLpFXdB45/KR/aOsYxnK8f 1NlpqajbrrmqJdT7vgKMwEF3D6PyVRtZRRxjJ/eekvzGEnmwIrOQ9gzl0AOu3wXQDCVPuC BBbBdsKQMFPKRhsY9tAGURM6/rLhh5E= Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-se118S_6MPq1GZdd0Z5ObA-1; Fri, 03 May 2024 16:00:54 -0400 X-MC-Unique: se118S_6MPq1GZdd0Z5ObA-1 Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-3c61104bebfso18713b6e.1 for ; Fri, 03 May 2024 13:00:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714766453; x=1715371253; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+vhjwEtvhoiRKOpMtZflyW7UIM5Iqomf7fPlO1cgUkk=; b=wpUFyw+SP/dVdQ83oWjy4C1etOY6AJSQLCna65Qbi5i7q/uEziClM9ysHFLAsAb1qG UzLtEWkz5eQ+8P7iU7GvrPqwfuzQbRHeN5rDs08uaRTkRxLChtDJk8nMPrcF72CFu9rr snIy1bkDCvNuMFi3N+a41O6IoLwti/4SGrrmvom0icbZCF2/R/hFvozwNCKAi7SXMJfi 4ZE9FvMWQ+RQ02HMfawdNKSB9hUiRRvolh7Cl+VaUUOXXY1Q3l1BrDgArvDj+GLdXzpP f6CZD2xDGAVfGB8+VNZUXKNbiaWGjaSDRgBMvuE6lidxJQQoqYC3r72yXmPK3t3A0gZj aDWA== X-Gm-Message-State: AOJu0YyPWd95ENs0TcBdFsFXwEuwk6BFOGDZreJC5nva2UVQqi1sGxnn rcuFRNEaDQygJpkv8PvyJ8IZtbLOZTcmU4a0mKgv+m/Se+w4pWqqnYYmL2yQfdV0xU+8LYmSYoe 4d87a47N+0eah/7GExyl+4EwjyJPtEVgappCbYqI1HjQvHbUNzqf6hEY7m2haGQEF9MLLdKzUs7 yOzl2Xfgf5nW1SG+HsG4jN4jedVhMTajJy110= X-Received: by 2002:a05:6808:10c9:b0:3c8:61a9:629 with SMTP id s9-20020a05680810c900b003c861a90629mr5197487ois.1.1714766453528; Fri, 03 May 2024 13:00:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqtOw0v8DUBUBwRRS3PInQvcvcnP1JecrsoTtTlt7opp3jSFXttEFs/1qXggH3zU5TLiknZg== X-Received: by 2002:a05:6808:10c9:b0:3c8:61a9:629 with SMTP id s9-20020a05680810c900b003c861a90629mr5197455ois.1.1714766453150; Fri, 03 May 2024 13:00:53 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id p17-20020a0cf691000000b006a0cc19f870sm1482640qvn.9.2024.05.03.13.00.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 13:00:52 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: initializer_list and EH [PR114935] Date: Fri, 3 May 2024 16:00:37 -0400 Message-ID: <20240503200050.914147-1-jason@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk and 14. -- 8< -- When we initialize an array of a type with a non-trivial destructor, such as the backing array for the initializer_list, we have a cleanup to destroy any constructed elements if a later constructor throws. When the array being created is a variable, the end of that EH region naturally coincides with the beginning of the EH region for the cleanup for the variable as a whole. But if the array is a temporary, or a subobject of one, the array cleanup region lasts for the rest of the full-expression, along with the normal cleanup for the TARGET_EXPR. As a result, when tata throws we clean it up twice. Before r14-1705 we avoided this by disabling the array cleanup in split_nonconstant_init, but after that we don't go through split_nonconstant_init, so let's handle it in cp_genericize_target_expr. PR c++/114935 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_genericize_init): Add flags parm. (cp_genericize_init_expr): Pass nullptr. (cp_genericize_target_expr): Handle cleanup flags. * typeck2.cc (build_disable_temp_cleanup): Factor out of... (split_nonconstant_init): ...here. * cp-tree.h (build_disable_temp_cleanup): Declare. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-eh1.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/cp-gimplify.cc | 18 +++++++++--- gcc/cp/typeck2.cc | 34 +++++++++++++---------- gcc/testsuite/g++.dg/cpp0x/initlist-eh1.C | 25 +++++++++++++++++ 4 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-eh1.C base-commit: c943d7b5c40f447b12431df9ad27a47dad95026d diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 1ba7054f8bc..52d6841559c 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8411,6 +8411,7 @@ extern int abstract_virtuals_error (abstract_class_use, tree, tsubst_flags_t = tf_warning_or_error); extern tree store_init_value (tree, tree, vec**, int); +extern tree build_disable_temp_cleanup (tree); extern tree split_nonconstant_init (tree, tree); extern bool check_narrowing (tree, tree, tsubst_flags_t, bool = false); diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index ab5acd18c99..5cbdf0ea498 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1063,11 +1063,11 @@ any_non_eliding_target_exprs (tree ctor) the result. */ static void -cp_genericize_init (tree *replace, tree from, tree to) +cp_genericize_init (tree *replace, tree from, tree to, vec** flags) { tree init = NULL_TREE; if (TREE_CODE (from) == VEC_INIT_EXPR) - init = expand_vec_init_expr (to, from, tf_warning_or_error); + init = expand_vec_init_expr (to, from, tf_warning_or_error, flags); else if (TREE_CODE (from) == CONSTRUCTOR && TREE_SIDE_EFFECTS (from) && ((flag_exceptions @@ -1101,7 +1101,7 @@ cp_genericize_init_expr (tree *stmt_p) /* Return gets confused if we clobber its INIT_EXPR this soon. */ && TREE_CODE (to) != RESULT_DECL) from = TARGET_EXPR_INITIAL (from); - cp_genericize_init (stmt_p, from, to); + cp_genericize_init (stmt_p, from, to, nullptr); } /* For a TARGET_EXPR, change the TARGET_EXPR_INITIAL. We will need to use @@ -1112,9 +1112,19 @@ cp_genericize_target_expr (tree *stmt_p) { iloc_sentinel ils = EXPR_LOCATION (*stmt_p); tree slot = TARGET_EXPR_SLOT (*stmt_p); + vec *flags = make_tree_vector (); cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p), - TARGET_EXPR_INITIAL (*stmt_p), slot); + TARGET_EXPR_INITIAL (*stmt_p), slot, &flags); gcc_assert (!DECL_INITIAL (slot)); + for (tree f : flags) + { + /* Once initialization is complete TARGET_EXPR_CLEANUP becomes active, so + disable any subobject cleanups. */ + tree d = build_disable_temp_cleanup (f); + auto &r = TARGET_EXPR_INITIAL (*stmt_p); + r = add_stmt_to_compound (r, d); + } + release_tree_vector (flags); } /* Similar to if (target_expr_needs_replace) replace_decl, but TP is the diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index 2985bfdf9ec..06bad4d3303 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -466,6 +466,25 @@ maybe_push_temp_cleanup (tree sub, vec **flags) } } +/* F is something added to a cleanup flags vec by maybe_push_temp_cleanup or + build_vec_init. Return the code to disable the cleanup it controls. */ + +tree +build_disable_temp_cleanup (tree f) +{ + tree d = f; + tree i = boolean_false_node; + if (TREE_CODE (f) == TREE_LIST) + { + /* To disable a build_vec_init cleanup, set + iterator = maxindex. */ + d = TREE_PURPOSE (f); + i = TREE_VALUE (f); + ggc_free (f); + } + return build2 (MODIFY_EXPR, TREE_TYPE (d), d, i); +} + /* The recursive part of split_nonconstant_init. DEST is an lvalue expression to which INIT should be assigned. INIT is a CONSTRUCTOR. Return true if the whole of the value was initialized by the @@ -737,20 +756,7 @@ split_nonconstant_init (tree dest, tree init) init = NULL_TREE; for (tree f : flags) - { - /* See maybe_push_temp_cleanup. */ - tree d = f; - tree i = boolean_false_node; - if (TREE_CODE (f) == TREE_LIST) - { - /* To disable a build_vec_init cleanup, set - iterator = maxindex. */ - d = TREE_PURPOSE (f); - i = TREE_VALUE (f); - ggc_free (f); - } - add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (d), d, i)); - } + add_stmt (build_disable_temp_cleanup (f)); release_tree_vector (flags); code = pop_stmt_list (code); diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-eh1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-eh1.C new file mode 100644 index 00000000000..93baf1c2abc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-eh1.C @@ -0,0 +1,25 @@ +// { dg-do run { target c++11 } } + +#include + +int as; +struct A { + A(const char *) { ++as; } + A(const A&) { ++as; } + ~A() { --as; } +}; + +void __attribute__((noipa)) +tata(std::initializer_list init) +{ + throw 1; +} + +int +main() +{ + try { tata({ "foo","bar" }); } + catch (...) { } + + if (as != 0) __builtin_abort (); +}