From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vs1-xe30.google.com (mail-vs1-xe30.google.com [IPv6:2607:f8b0:4864:20::e30]) by sourceware.org (Postfix) with ESMTPS id EA8523858C39 for ; Fri, 1 Oct 2021 11:05:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EA8523858C39 Received: by mail-vs1-xe30.google.com with SMTP id i30so10733966vsj.13 for ; Fri, 01 Oct 2021 04:05:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=l0XiArOFO5LQuS/Xic6f7tiM+4amvaIxKphM4ylFx74=; b=6vjilt7g9QmIT+ZlwNrwAh/zGSl7bGl3zThywyDZkh9WaPX+909wIHnNazzjDDxiBb 6zgYdylWWD7tZMmrgEQeXyoKf0m+v8KnD/0xVNOzaMab/B1TkW0i2e7RNlUORGFSjdHp xJeCRq9M3OJB40FV+ad+7Zdpsg1vT4TtcWwdnWeLmjDdjJwIwfZjH4AqeXBizvuK81o1 KFcFdhmu6q5ES2DcAjG+2OrK6OLNxCTlEoAKkZkCjdcV9A51jF+qDu0Z09kFmBhEtNaT vnwXJxAb/TFXwpmlOvySPTKYkQqunDaU4haVzbCUIG6VtT4mjQU9m+nz/0LrVfPO0gw5 MuEg== X-Gm-Message-State: AOAM532qFv6hInr+FtefXVzyuiIKrYL2fhoMUhC0AN8LF5aKmfIVnkyy v4mTKYLmrKPeLj09ofP5kRKdWChO4X3YUaV7qnY= X-Google-Smtp-Source: ABdhPJzD57/LieYKqyr2p2c0u0nPqGr+usP2IAr6WChH2zYPMCCY3E+SIP0S2dbXBF0lJONXuOUcsuBZgpTaOKpSmpQ= X-Received: by 2002:a67:d912:: with SMTP id t18mr3206704vsj.12.1633086302200; Fri, 01 Oct 2021 04:05:02 -0700 (PDT) MIME-Version: 1.0 References: <1632111261-18790-1-git-send-email-apinski@marvell.com> In-Reply-To: From: Andrew Pinski Date: Fri, 1 Oct 2021 04:04:50 -0700 Message-ID: Subject: Re: [PATCH] c: [PR32122] Require pointer types for computed gotos To: Eric Gallager Cc: Andrew Pinski , gcc-patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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, 01 Oct 2021 11:05:04 -0000 On Fri, Oct 1, 2021 at 4:03 AM Eric Gallager via Gcc-patches wrote: > > On Mon, Sep 20, 2021 at 12:15 AM apinski--- via Gcc-patches > wrote: > > > > From: Andrew Pinski > > > > So GCC has always accepted non-pointer types in computed gotos but > > that was wrong based on the documentation: > > Any expression of type void * is allowed. > > > > So this fixes the problem by requiring the type to > > be a pointer type. > > > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > > > PR c/32122 > > > > gcc/c/ChangeLog: > > > > * c-parser.c (c_parser_statement_after_labels): Pass > > the c_expr instead of the tree to c_finish_goto_ptr. > > * c-typeck.c (c_finish_goto_ptr): Change the second > > argument type to c_expr. > > * c-tree.h (c_finish_goto_ptr): Likewise. > > Error out if the expression was not of a pointer type. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/comp-goto-5.c: New test. > > * gcc.dg/comp-goto-6.c: New test. > > --- > > gcc/c/c-parser.c | 2 +- > > gcc/c/c-tree.h | 2 +- > > gcc/c/c-typeck.c | 11 ++++++++++- > > gcc/testsuite/gcc.dg/comp-goto-5.c | 11 +++++++++++ > > gcc/testsuite/gcc.dg/comp-goto-6.c | 6 ++++++ > > 5 files changed, 29 insertions(+), 3 deletions(-) > > create mode 100644 gcc/testsuite/gcc.dg/comp-goto-5.c > > create mode 100644 gcc/testsuite/gcc.dg/comp-goto-6.c > > > > diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c > > index fb1399e300d..bcd8a05489f 100644 > > --- a/gcc/c/c-parser.c > > +++ b/gcc/c/c-parser.c > > @@ -6141,7 +6141,7 @@ c_parser_statement_after_labels (c_parser *parser, bool *if_p, > > c_parser_consume_token (parser); > > val = c_parser_expression (parser); > > val = convert_lvalue_to_rvalue (loc, val, false, true); > > - stmt = c_finish_goto_ptr (loc, val.value); > > + stmt = c_finish_goto_ptr (loc, val); > > } > > else > > c_parser_error (parser, "expected identifier or %<*%>"); > > diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h > > index d50d0cb7f2d..a046c6b0926 100644 > > --- a/gcc/c/c-tree.h > > +++ b/gcc/c/c-tree.h > > @@ -746,7 +746,7 @@ extern tree c_finish_expr_stmt (location_t, tree); > > extern tree c_finish_return (location_t, tree, tree); > > extern tree c_finish_bc_stmt (location_t, tree, bool); > > extern tree c_finish_goto_label (location_t, tree); > > -extern tree c_finish_goto_ptr (location_t, tree); > > +extern tree c_finish_goto_ptr (location_t, c_expr val); > > extern tree c_expr_to_decl (tree, bool *, bool *); > > extern tree c_finish_omp_construct (location_t, enum tree_code, tree, tree); > > extern tree c_finish_oacc_data (location_t, tree, tree); > > diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c > > index 49d1bb067a0..b472e448011 100644 > > --- a/gcc/c/c-typeck.c > > +++ b/gcc/c/c-typeck.c > > @@ -10783,10 +10783,19 @@ c_finish_goto_label (location_t loc, tree label) > > the GOTO. */ > > > > tree > > -c_finish_goto_ptr (location_t loc, tree expr) > > +c_finish_goto_ptr (location_t loc, c_expr val) > > { > > + tree expr = val.value; > > tree t; > > pedwarn (loc, OPT_Wpedantic, "ISO C forbids %"); > > + if (expr != error_mark_node > > + && !POINTER_TYPE_P (TREE_TYPE (expr)) > > + && !null_pointer_constant_p (expr)) > > + { > > + error_at (val.get_location (), > > + "computed goto must be pointer type"); > > + expr = build_zero_cst (ptr_type_node); > > + } > > expr = c_fully_fold (expr, false, NULL); > > expr = convert (ptr_type_node, expr); > > t = build1 (GOTO_EXPR, void_type_node, expr); > > diff --git a/gcc/testsuite/gcc.dg/comp-goto-5.c b/gcc/testsuite/gcc.dg/comp-goto-5.c > > new file mode 100644 > > index 00000000000..d487729a5d4 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/comp-goto-5.c > > @@ -0,0 +1,11 @@ > > +/* PR c/32122 */ > > +/* { dg-do compile } */ > > +/* { dg-options "" } */ > > + > > +enum {a=1}; > > +void foo() > > +{ > > + goto * > > + a; /* { dg-error "computed goto must be pointer type" } */ > > +} > > + > > diff --git a/gcc/testsuite/gcc.dg/comp-goto-6.c b/gcc/testsuite/gcc.dg/comp-goto-6.c > > new file mode 100644 > > index 00000000000..497f6cd76ca > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/comp-goto-6.c > > @@ -0,0 +1,6 @@ > > +/* PR c/32122 */ > > +/* { dg-do compile } */ > > +/* { dg-options "" } */ > > +void foo(void *a) { goto *10000000; } /* { dg-error "computed goto must be pointer type" } */ > > +void foo1(void *a) { goto *a; } > > + > > -- > > 2.17.1 > > > > Maybe add to one of the testcases a test to ensure that the > cast-to-void workaround works successfully? > e.g. > void foo2(void *a) { goto *(void *)10000000; } /* { dg-bogus "computed > goto must be pointer type" } */ There actually were a few testcases which needed to be fixed up so there is no need for another one :). Thanks, Andrew