From patchwork Sun Oct 2 11:35:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp547698wrs; Sun, 2 Oct 2022 04:35:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5HWM7krtvKprZxTZ6NHvhE4xCqvC/A7lzBuVGOgC+v6ekDZVvIdsvmSeNpSsnF28cGOG/b X-Received: by 2002:a05:6402:1910:b0:458:8b32:4752 with SMTP id e16-20020a056402191000b004588b324752mr8530309edz.302.1664710558423; Sun, 02 Oct 2022 04:35:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664710558; cv=none; d=google.com; s=arc-20160816; b=m3rCBYMbRigJ4PseCM8zN+vvFfwXV+8fq3k2xbioY9GrhdqyTH4qGWrPDH4U7lt5G2 JASze5FLGsr4l+rf8bRudaIANqhC6iTMDqR1TiuEKvaA9wL1UjPrI3F8PRt/j6DTL/DM HHAhWXifNAUHRrcOQFa8vLKBXcfd52t3XrZnaydhghM8PF2CjqBU8xh3xN8vV4EViEBO WG4nmQDUaP6TEz7/xEUI1ZRrDQGUHTzqXfL2lkXiSOUUx1sHeNcmYnzEQcmPzeWNyhOx ut4vOGOJRrKWMmWuzXhIX4FBCDbkSY0sBOtwDH78QVbL7fzss5Vyj7en4d45kAuobWMj UyxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=ILCfWgmzcJYcGtT19DkGiqUZrH+XyIEpf4JbsAbcSUY=; b=L+qe+osJyMouuL/LbUDwrnP0pJa6MLf29EgFOWBevXfatno1F+cwJBjv9m3oUkb25I iBW1y2WTN9RLggp7R3SS+u1wIL3WOdj3fHSyTTrXE/ypoi4mknmsDF0UOdoUJ1PaVjoT GhykNfnX0F10yrfNbMpDXxBUWDINfULrpcgdnym73BAzMSQdtPVfN/cGwRU/hm2pAD6x KDqIo7f/q3if6zbfdn3ZEfnq5omBIwoTzSrygpxAo4P3WFmUYm5S18G5tt4EcsGjaL5x yg6+Ak+dOVUzcQrOb8f+9iE5U8Z6sxeM3920TrP5rSQOSMSwwmMbYsA9w0uRB9sgjL5X ajRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BRQISgaS; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id k14-20020a508ace000000b00458239a54basi5430694edk.192.2022.10.02.04.35.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 04:35:58 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BRQISgaS; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 530FA3857354 for ; Sun, 2 Oct 2022 11:35:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 530FA3857354 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664710557; bh=ILCfWgmzcJYcGtT19DkGiqUZrH+XyIEpf4JbsAbcSUY=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=BRQISgaSpPUTUhtm0UWeL3akmhnuk/+mfRCqs0sWAXYXa73mnf3iXINPVWJFIcgfi hYKZqBEp8eRIgHPcs74OoxzFW4SYY9Ya+Hu1D1+cCfZijcgopOf5NC41Ls8L0WgUwk u99SnzaxmAgVRNB9wlRBsxi7f4OjRhtAlaFlsUz4= 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 C242A3858D32 for ; Sun, 2 Oct 2022 11:35:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C242A3858D32 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-361-ao8KORoMNN-JD91tOahVrw-1; Sun, 02 Oct 2022 07:35:10 -0400 X-MC-Unique: ao8KORoMNN-JD91tOahVrw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4DC233C0219F for ; Sun, 2 Oct 2022 11:35:10 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.194]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0569F477F55; Sun, 2 Oct 2022 11:35:08 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 292BZ6DA1929688 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sun, 2 Oct 2022 13:35:06 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 292BZ5Pu1929687; Sun, 2 Oct 2022 13:35:05 +0200 Date: Sun, 2 Oct 2022 13:35:05 +0200 To: Jason Merrill Subject: [PATCH] c++: Disallow jumps into statement expressions Message-ID: References: <8b360a15-5255-3fbe-16f4-84eaf0fa9612@redhat.com> MIME-Version: 1.0 In-Reply-To: <8b360a15-5255-3fbe-16f4-84eaf0fa9612@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FUZZY_PRICES, RCVD_IN_DNSWL_LOW, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745575538539589575?= X-GMAIL-MSGID: =?utf-8?q?1745575538539589575?= On Fri, Sep 30, 2022 at 04:39:25PM -0400, Jason Merrill wrote: > > --- gcc/cp/decl.cc.jj 2022-09-22 00:14:55.478599363 +0200 > > +++ gcc/cp/decl.cc 2022-09-22 00:24:01.121178256 +0200 > > @@ -223,6 +223,7 @@ struct GTY((for_user)) named_label_entry > > bool in_transaction_scope; > > bool in_constexpr_if; > > bool in_consteval_if; > > + bool in_assume; > > I think it would be better to reject jumps into statement-expressions like > the C front-end. Ok, here is a self-contained patch that does that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-10-01 Jakub Jelinek * cp-tree.h (BCS_STMT_EXPR): New enumerator. * name-lookup.h (enum scope_kind): Add sk_stmt_expr. * name-lookup.cc (begin_scope): Handle sk_stmt_expr like sk_block. * semantics.cc (begin_compound_stmt): For BCS_STMT_EXPR use sk_stmt_expr. * parser.cc (cp_parser_statement_expr): Use BCS_STMT_EXPR instead of BCS_NORMAL. * decl.cc (struct named_label_entry): Add in_stmt_expr. (poplevel_named_label_1): Handle sk_stmt_expr. (check_previous_goto_1): Diagnose entering of statement expression. (check_goto): Likewise. * g++.dg/ext/stmtexpr24.C: New test. Jakub --- gcc/cp/cp-tree.h.jj 2022-09-30 18:38:55.351607176 +0200 +++ gcc/cp/cp-tree.h 2022-10-01 13:06:20.731720730 +0200 @@ -7599,7 +7599,8 @@ enum { BCS_NO_SCOPE = 1, BCS_TRY_BLOCK = 2, BCS_FN_BODY = 4, - BCS_TRANSACTION = 8 + BCS_TRANSACTION = 8, + BCS_STMT_EXPR = 16 }; extern tree begin_compound_stmt (unsigned int); --- gcc/cp/name-lookup.h.jj 2022-09-23 09:02:31.103668514 +0200 +++ gcc/cp/name-lookup.h 2022-10-01 13:37:50.158404107 +0200 @@ -200,6 +200,7 @@ enum scope_kind { init-statement. */ sk_cond, /* The scope of the variable declared in the condition of an if or switch statement. */ + sk_stmt_expr, /* GNU statement expression block. */ sk_function_parms, /* The scope containing function parameters. */ sk_class, /* The scope containing the members of a class. */ sk_scoped_enum, /* The scope containing the enumerators of a C++11 --- gcc/cp/name-lookup.cc.jj 2022-09-13 09:21:28.123540623 +0200 +++ gcc/cp/name-lookup.cc 2022-10-01 13:37:26.383732959 +0200 @@ -4296,6 +4296,7 @@ begin_scope (scope_kind kind, tree entit case sk_scoped_enum: case sk_transaction: case sk_omp: + case sk_stmt_expr: scope->keep = keep_next_level_flag; break; --- gcc/cp/semantics.cc.jj 2022-09-30 18:38:50.337675080 +0200 +++ gcc/cp/semantics.cc 2022-10-01 13:09:34.958970367 +0200 @@ -1761,6 +1761,8 @@ begin_compound_stmt (unsigned int flags) sk = sk_try; else if (flags & BCS_TRANSACTION) sk = sk_transaction; + else if (flags & BCS_STMT_EXPR) + sk = sk_stmt_expr; r = do_pushlevel (sk); } --- gcc/cp/parser.cc.jj 2022-09-30 18:38:55.374606864 +0200 +++ gcc/cp/parser.cc 2022-10-01 13:08:27.367927479 +0200 @@ -5272,7 +5272,7 @@ cp_parser_statement_expr (cp_parser *par /* Start the statement-expression. */ tree expr = begin_stmt_expr (); /* Parse the compound-statement. */ - cp_parser_compound_statement (parser, expr, BCS_NORMAL, false); + cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false); /* Finish up. */ expr = finish_stmt_expr (expr, false); /* Consume the ')'. */ --- gcc/cp/decl.cc.jj 2022-09-27 08:27:47.671428567 +0200 +++ gcc/cp/decl.cc 2022-10-01 13:14:57.990434730 +0200 @@ -223,6 +223,7 @@ struct GTY((for_user)) named_label_entry bool in_transaction_scope; bool in_constexpr_if; bool in_consteval_if; + bool in_stmt_expr; }; #define named_labels cp_function_chain->x_named_labels @@ -538,6 +539,9 @@ poplevel_named_label_1 (named_label_entr case sk_transaction: ent->in_transaction_scope = true; break; + case sk_stmt_expr: + ent->in_stmt_expr = true; + break; case sk_block: if (level_for_constexpr_if (bl->level_chain)) ent->in_constexpr_if = true; @@ -3487,7 +3491,7 @@ check_previous_goto_1 (tree decl, cp_bin bool complained = false; int identified = 0; bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false; - bool saw_ceif = false; + bool saw_ceif = false, saw_se = false; if (exited_omp) { @@ -3560,6 +3564,12 @@ check_previous_goto_1 (tree decl, cp_bin saw_tm = true; break; + case sk_stmt_expr: + if (!saw_se) + inf = G_(" enters statement expression"); + saw_se = true; + break; + case sk_block: if (!saw_cxif && level_for_constexpr_if (b->level_chain)) { @@ -3650,12 +3660,13 @@ check_goto (tree decl) if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope || ent->in_constexpr_if || ent->in_consteval_if - || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls)) + || ent->in_omp_scope || ent->in_stmt_expr + || !vec_safe_is_empty (ent->bad_decls)) { diagnostic_t diag_kind = DK_PERMERROR; if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if || ent->in_consteval_if || ent->in_transaction_scope - || ent->in_omp_scope) + || ent->in_omp_scope || ent->in_stmt_expr) diag_kind = DK_ERROR; complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), &input_location, diag_kind); @@ -3703,6 +3714,8 @@ check_goto (tree decl) inform (input_location, " enters % statement"); else if (ent->in_consteval_if) inform (input_location, " enters % statement"); + else if (ent->in_stmt_expr) + inform (input_location, " enters statement expression"); } if (ent->in_omp_scope) --- gcc/testsuite/g++.dg/ext/stmtexpr24.C.jj 2022-10-01 13:34:01.471565458 +0200 +++ gcc/testsuite/g++.dg/ext/stmtexpr24.C 2022-10-01 13:39:56.078662362 +0200 @@ -0,0 +1,27 @@ +// { dg-do compile } +// { dg-options "" } + +void +foo (int x) +{ + bool a = false; + if (x == 1) + goto l1; // { dg-message "from here" } + a = ({ l0:; if (x == 0) goto l0; true; }); + a = ({ if (x == 0) throw 1; true; }); + a = ({ l1:; true; }); // { dg-error "jump to label 'l1'" } + // { dg-message "enters statement expression" "" { target *-*-* } .-1 } + a = ({ l2:; true; }); // { dg-error "jump to label 'l2'" } + switch (x) + { + case 2: + a = ({ case 3:; true; }); // { dg-error "jump to case label" } + // { dg-message "enters statement expression" "" { target *-*-* } .-1 } + a = ({ default:; true; }); // { dg-error "jump to case label" } + // { dg-message "enters statement expression" "" { target *-*-* } .-1 } + break; + } + if (x == 4) + goto l2; // { dg-message "from here" } + // { dg-message "enters statement expression" "" { target *-*-* } .-1 } +}