public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3046] c++: Disallow jumps into statement expressions Date: Mon, 3 Oct 2022 16:05:29 +0000 (GMT) [thread overview] Message-ID: <20221003160529.3E80F3858D38@sourceware.org> (raw) https://gcc.gnu.org/g:c823366fdb9069a54a37a3b18b65a4fa69feabfd commit r13-3046-gc823366fdb9069a54a37a3b18b65a4fa69feabfd Author: Jakub Jelinek <jakub@redhat.com> Date: Mon Oct 3 18:04:37 2022 +0200 c++: Disallow jumps into statement expressions 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. 2022-10-03 Jakub Jelinek <jakub@redhat.com> * 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. Diff: --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/decl.cc | 19 ++++++++++++++++--- gcc/cp/name-lookup.cc | 1 + gcc/cp/name-lookup.h | 1 + gcc/cp/parser.cc | 2 +- gcc/cp/semantics.cc | 2 ++ gcc/testsuite/g++.dg/ext/stmtexpr24.C | 27 +++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 5 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 67aea9653e3..e734bf43437 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -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); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index fb85564a191..07148b9fd4e 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -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_entry **slot, cp_binding_level *bl) 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_binding_level* level, tree names, 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_binding_level* level, tree names, 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 %<constexpr if%> statement"); else if (ent->in_consteval_if) inform (input_location, " enters %<consteval if%> statement"); + else if (ent->in_stmt_expr) + inform (input_location, " enters statement expression"); } if (ent->in_omp_scope) diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 69d555ddf1f..d6757d1fe5d 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -4296,6 +4296,7 @@ begin_scope (scope_kind kind, tree entity) case sk_scoped_enum: case sk_transaction: case sk_omp: + case sk_stmt_expr: scope->keep = keep_next_level_flag; break; diff --git a/gcc/cp/name-lookup.h b/gcc/cp/name-lookup.h index 999db33a7e0..7201ae8ead8 100644 --- a/gcc/cp/name-lookup.h +++ b/gcc/cp/name-lookup.h @@ -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 diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 8d0e0fa1b87..6c4f840f9d7 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -5272,7 +5272,7 @@ cp_parser_statement_expr (cp_parser *parser) /* 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 ')'. */ diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 5cc5a001498..58b5b433692 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -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); } diff --git a/gcc/testsuite/g++.dg/ext/stmtexpr24.C b/gcc/testsuite/g++.dg/ext/stmtexpr24.C new file mode 100644 index 00000000000..907d027c6ed --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/stmtexpr24.C @@ -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 } +}
reply other threads:[~2022-10-03 16:05 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20221003160529.3E80F3858D38@sourceware.org \ --to=jakub@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).