From patchwork Wed Jun 7 01:30:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1791490 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=urQcwrvg; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QbVDX0lhGz20Q8 for ; Wed, 7 Jun 2023 11:31:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57FDD3857735 for ; Wed, 7 Jun 2023 01:31:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57FDD3857735 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686101481; bh=aHWbTem/+3S5BP7T6JUwUqLp1XOGdVuVyktDt/Anoy0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=urQcwrvgNetTaQsoZ9szp5Qm9yIpGqzMsU7yd4zlx6zMYf26LXkafzP/d/E0FKZag Sq5URZrns+8MbfsiTMNVTRP5LNQcf1n9QWPqqJXZYgN/Jut7vYhAz7ZY9BZ9+G2/F5 QWho1UAbkMeMYuK/M8fKp6/8jTRMit0P+n9gxcjI= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 297AD3857346 for ; Wed, 7 Jun 2023 01:31:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 297AD3857346 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-574-ZGoaAncYPqWfPONAPgUXgg-1; Tue, 06 Jun 2023 21:30:59 -0400 X-MC-Unique: ZGoaAncYPqWfPONAPgUXgg-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6261a25e981so76335256d6.0 for ; Tue, 06 Jun 2023 18:30:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686101458; x=1688693458; 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=aHWbTem/+3S5BP7T6JUwUqLp1XOGdVuVyktDt/Anoy0=; b=OjkjP3r/XYQGExU4FZz0315XpQZNQgK3VC1iOWgTXoj8/IED44ZuADFRdPpiCr5Una ZEwkq9hlNZmP1UGB/hGjLBsSnsFb0qcUp1tif/sU050LILzblNEhuMXRZZutLK34DX0E 3QFMdLGeVyNTMD8lwEk9zKipJdL1wVxfGUUrHQOUSIbNJ3FLiF+7XPK6MUpdA3VgKHa3 0c2QYzBgO72PsPE7tz+m54619gbzqfZwkNbmL1W1cph44W9P+qWV/p32LURQ1/nx0g3E gDTmc6JuRDK5cRIVJ1edkXi1Hpz61KzgLMFyrOebxSUUgrkNiboCJeS3OHW6XYrbJWnY vTtQ== X-Gm-Message-State: AC+VfDwHpq726HXn9Qj4NCXci7XzfXRO6ETeU9LqiYa6te9nSBWDDGSA IgjUIMVh3WBbWh2Eotglovl90DBr+kHL30Wg8bujWV40nAK2lKjLhe8NLPCpLIDu9oZj6wgJAdf q8QWb7xdJpL57Z5Z0A0IbGH8i0OgUW1FnMtF7Hs7u2qdnMrviB1n+CfmR9X98Fk5DSgC7fOC9bg == X-Received: by 2002:a05:6214:f2f:b0:625:b517:6dcb with SMTP id iw15-20020a0562140f2f00b00625b5176dcbmr2333834qvb.5.1686101458472; Tue, 06 Jun 2023 18:30:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7LUa+b0PH9k/jkrXVBJqzXAe0aFJCnjy5RPlvXVtOwEVRMtYugrQZdKzwJPETOcfUrDlI3/w== X-Received: by 2002:a05:6214:f2f:b0:625:b517:6dcb with SMTP id iw15-20020a0562140f2f00b00625b5176dcbmr2333812qvb.5.1686101458058; Tue, 06 Jun 2023 18:30:58 -0700 (PDT) Received: from jason.com (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 t9-20020a05622a01c900b003f4e18bfa06sm5977461qtw.60.2023.06.06.18.30.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 18:30:55 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: fix throwing cleanup with label Date: Tue, 6 Jun 2023 21:30:53 -0400 Message-Id: <20230607013053.2770663-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- While looking at PR92407 I noticed that the expectations of maybe_splice_retval_cleanup weren't being met; an sk_cleanup level was confusing its attempt to recognize the outer block of the function. And even if I fixed the detection, it failed to actually wrap the body of the function because the STATEMENT_LIST it got only had the label, not anything after it. So I moved the call after poplevel does pop_stmt_list on all the sk_cleanup levels. PR c++/33799 gcc/cp/ChangeLog: * except.cc (maybe_splice_retval_cleanup): Change recognition of function body and try scopes. * semantics.cc (do_poplevel): Call it after poplevel. (at_try_scope): New. * cp-tree.h (maybe_splice_retval_cleanup): Adjust. gcc/testsuite/ChangeLog: * g++.dg/eh/return1.C: Add label cases. --- gcc/cp/cp-tree.h | 2 +- gcc/cp/except.cc | 25 ++++++++++++------------- gcc/cp/semantics.cc | 16 +++++++++++++++- gcc/testsuite/g++.dg/eh/return1.C | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 15 deletions(-) base-commit: 29c82c6ca929e0f5eccfe038dea71177d814c6b7 prerequisite-patch-id: aed53cdac161144c31cb1433282e1ad1d49d3770 prerequisite-patch-id: 1098cb4457a5eff90fa8176f9b0d8d2e9477596e diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 101da35d322..834fdd12ef8 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7047,7 +7047,7 @@ extern tree begin_eh_spec_block (void); extern void finish_eh_spec_block (tree, tree); extern tree build_eh_type_type (tree); extern tree cp_protect_cleanup_actions (void); -extern void maybe_splice_retval_cleanup (tree); +extern void maybe_splice_retval_cleanup (tree, bool); extern tree maybe_set_retval_sentinel (void); extern tree template_parms_to_args (tree); diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc index b04eb00d220..28106dadf1e 100644 --- a/gcc/cp/except.cc +++ b/gcc/cp/except.cc @@ -1312,21 +1312,20 @@ maybe_set_retval_sentinel () on throw. */ void -maybe_splice_retval_cleanup (tree compound_stmt) +maybe_splice_retval_cleanup (tree compound_stmt, bool is_try) { - /* If we need a cleanup for the return value, add it in at the same level as - pushdecl_outermost_localscope. And also in try blocks. */ - const bool function_body - = (current_binding_level->level_chain - && current_binding_level->level_chain->kind == sk_function_parms - /* When we're processing a default argument, c_f_d may not have been - set. */ - && current_function_decl); + if (!current_function_decl || !cfun + || DECL_CONSTRUCTOR_P (current_function_decl) + || DECL_DESTRUCTOR_P (current_function_decl) + || !current_retval_sentinel) + return; - if ((function_body || current_binding_level->kind == sk_try) - && !DECL_CONSTRUCTOR_P (current_function_decl) - && !DECL_DESTRUCTOR_P (current_function_decl) - && current_retval_sentinel) + /* if we need a cleanup for the return value, add it in at the same level as + pushdecl_outermost_localscope. And also in try blocks. */ + cp_binding_level *b = current_binding_level; + const bool function_body = b->kind == sk_function_parms; + + if (function_body || is_try) { location_t loc = DECL_SOURCE_LOCATION (current_function_decl); tree_stmt_iterator iter = tsi_start (compound_stmt); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index c94ea090a99..a13c16f34a3 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -628,6 +628,17 @@ set_cleanup_locs (tree stmts, location_t loc) set_cleanup_locs (stmt, loc); } +/* True iff the innermost block scope is a try block. */ + +static bool +at_try_scope () +{ + cp_binding_level *b = current_binding_level; + while (b && b->kind == sk_cleanup) + b = b->level_chain; + return b && b->kind == sk_try; +} + /* Finish a scope. */ tree @@ -635,11 +646,14 @@ do_poplevel (tree stmt_list) { tree block = NULL; - maybe_splice_retval_cleanup (stmt_list); + bool was_try = at_try_scope (); if (stmts_are_full_exprs_p ()) block = poplevel (kept_level_p (), 1, 0); + /* This needs to come after poplevel merges sk_cleanup statement_lists. */ + maybe_splice_retval_cleanup (stmt_list, was_try); + stmt_list = pop_stmt_list (stmt_list); /* input_location is the last token of the scope, usually a }. */ diff --git a/gcc/testsuite/g++.dg/eh/return1.C b/gcc/testsuite/g++.dg/eh/return1.C index ac2225405da..e22d674ae9a 100644 --- a/gcc/testsuite/g++.dg/eh/return1.C +++ b/gcc/testsuite/g++.dg/eh/return1.C @@ -33,6 +33,13 @@ X f() return X(false); } +X f2() +{ + foo: + X x(true); + return X(false); +} + X g() { return X(true),X(false); @@ -54,6 +61,16 @@ X i() return X(false); } +X i2() +{ + try { + foo: + X x(true); + return X(false); + } catch(...) {} + return X(false); +} + X j() { try { @@ -84,6 +101,8 @@ int main() try { f(); } catch (...) {} + try { f2(); } catch (...) {} + try { g(); } catch (...) {} @@ -93,6 +112,8 @@ int main() try { i(); } catch (...) {} + try { i2(); } catch (...) {} + try { j(); } catch (...) {} try { k(); } catch (...) {}