From patchwork Sat Dec 9 19:58:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1874082 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=tugraz.at header.i=@tugraz.at header.a=rsa-sha256 header.s=mailrelay header.b=mzIwPrro; 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 4Snf2s3CHnz23nC for ; Sun, 10 Dec 2023 06:58:42 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0EFFB3858434 for ; Sat, 9 Dec 2023 19:58:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 33E643858CD1 for ; Sat, 9 Dec 2023 19:58:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 33E643858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 33E643858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702151909; cv=none; b=F6d86M9HDN3GnFsr28lAl3A06xHgDzgRzbluwQ81h8ZmaxtG+KPO+ul+y0hDvRb5P1XYzQkq0aX4XvVfgd04qYpPS+DuhUVbC/5u01OcIGIzsDm99y1C0RFEp89cRrR3+4NtaaqFoyDWJA3iFHVaebgcOh+AF3Euj3mFsLiPsCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702151909; c=relaxed/simple; bh=tid+QtUjoeULDa9xfrc19hE8pZlOkQBNc/U/xGAZNrc=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=p6t/IKk/yUG8SQ6NfXjLlkxaCu9zBEAcWUr6rcjaJWqmiqtDPNdGx2VfLhq2n2eyYcnoKfewtQDja6oabLxIUGJXV7a4Rhr/yImfrcC+818AFdbY77walyKZj7ExkqFqxZ92DFxOeAQBNgROUv8IZxIkJ9Ztu8keyEOsoM1uV2c= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-168-142.tugraz.at (vra-168-142.tugraz.at [129.27.168.142]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4Snf2D3thjz1LLyW; Sat, 9 Dec 2023 20:58:12 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4Snf2D3thjz1LLyW DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1702151893; bh=ZHjedODv1jTvnQWExe6vyK9WYOl/vujz+QYP99U+hpc=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=mzIwPrrot73KvJG+Pbnf2v6By3shHEg3LqEoml+1M/0QDs/AknsBvevoqSQzGIn6P u+jCAbEOMl8Tb4Rk3olptmFx6sXXTjMW+4AGhU+83B48J1gRM+NKIdkdzdUEyrRE2A gb6tbCCZi5hREtRd1/1ZWvwzhQBz4WcvBo6tT4qA= Message-ID: <7e4a91d16011c4d5ac5ab37412de78118a77fa63.camel@tugraz.at> Subject: v2 [C PATCH] Fix regression causing ICE for structs with VLAs [PR 112488] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , Richard Biener Date: Sat, 09 Dec 2023 20:58:11 +0100 In-Reply-To: <6194d2a421117c3830d2afbe2bf3bb53b4f9565e.camel@tugraz.at> References: <6194d2a421117c3830d2afbe2bf3bb53b4f9565e.camel@tugraz.at> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, 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.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 I revised version which fixes a problem with breaking other callers of finish_rust. Please ignore the previous one. Bootstrapped and regression tested on x86_64 Fix regression causing ICE for structs with VLAs [PR 112488] A previous patch the fixed several ICEs related to size expressions of VM types (PR c/70418, ...) caused a regression for structs where a DECL_EXPR is not generated anymore although reqired. We now call add_decl_expr introduced by the previous patch from finish_struct. The function is revised with a new argument to not set the TYPE_NAME for the type to the DECL_EXPR in this specific case. PR c/112488 gcc/c * c-decl.cc (add_decl_expr): Revise. (finish_struct): Create DECL_EXPR. * c-parser.cc (c_parser_struct_or_union_specifier): Call finish_struct with expression for VLA sizes. * c-tree.h (finish_struct): Add argument. gcc/testsuite * gcc.dg/pr112488-1.c: New test. * gcc.dg/pr112488-2.c: New test. * gcc.dg/pr112898.c: New test. * gcc.misc-tests/gcov-pr85350.c: Adapt. --- gcc/c/c-decl.cc | 33 ++++++++++++++++----- gcc/c/c-parser.cc | 2 +- gcc/c/c-tree.h | 3 +- gcc/testsuite/gcc.dg/pr112488-1.c | 14 +++++++++ gcc/testsuite/gcc.dg/pr112488-2.c | 13 ++++++++ gcc/testsuite/gcc.dg/pr112898.c | 9 ++++++ gcc/testsuite/gcc.misc-tests/gcov-pr85350.c | 2 +- 7 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr112488-1.c create mode 100644 gcc/testsuite/gcc.dg/pr112488-2.c create mode 100644 gcc/testsuite/gcc.dg/pr112898.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 92c83e1bf10..039a66fef09 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -6618,12 +6618,10 @@ smallest_type_quals_location (const location_t *locations, the size evaluation prior to the side effects. We therefore use BIND_EXPRs in TYPENAME contexts too. */ static void -add_decl_expr (location_t loc, enum decl_context decl_context, tree type, - tree *expr) +add_decl_expr (location_t loc, tree type, tree *expr, bool set_name_p) { tree bind = NULL_TREE; - if (decl_context == TYPENAME || decl_context == PARM - || decl_context == FIELD) + if (expr) { bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE, NULL_TREE); @@ -6636,7 +6634,8 @@ add_decl_expr (location_t loc, enum decl_context decl_context, tree type, pushdecl (decl); DECL_ARTIFICIAL (decl) = 1; add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl)); - TYPE_NAME (type) = decl; + if (set_name_p) + TYPE_NAME (type) = decl; if (bind) { @@ -7635,7 +7634,12 @@ grokdeclarator (const struct c_declarator *declarator, type has a name/declaration of it's own, but special attention is required if the type is anonymous. */ if (!TYPE_NAME (type) && c_type_variably_modified_p (type)) - add_decl_expr (loc, decl_context, type, expr); + { + bool bind_p = decl_context == TYPENAME + || decl_context == FIELD + || decl_context == PARM; + add_decl_expr (loc, type, bind_p ? expr : NULL, true); + } type = c_build_pointer_type (type); @@ -7900,7 +7904,12 @@ grokdeclarator (const struct c_declarator *declarator, /* The pointed-to type may need a decl expr (see above). */ if (!TYPE_NAME (type) && c_type_variably_modified_p (type)) - add_decl_expr (loc, decl_context, type, expr); + { + bool bind_p = decl_context == TYPENAME + || decl_context == FIELD + || decl_context == PARM; + add_decl_expr (loc, type, bind_p ? expr : NULL, true); + } type = c_build_pointer_type (type); type_quals = array_ptr_quals; @@ -9257,7 +9266,8 @@ is_flexible_array_member_p (bool is_last_field, tree finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, - class c_struct_parse_info *enclosing_struct_parse_info) + class c_struct_parse_info *enclosing_struct_parse_info, + tree *expr) { tree x; bool toplevel = file_scope == current_scope; @@ -9595,6 +9605,13 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, finish_incomplete_vars (incomplete_vars, toplevel); + /* Make sure a DECL_EXPR is created for structs with VLA members. + Because we do not know the context, we always pass expr + to force creation of a BIND_EXPR which is required in some + contexts. */ + if (c_type_variably_modified_p (t)) + add_decl_expr (loc, t, expr, false); + if (warn_cxx_compat) warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc); diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index df9a07928b5..dcb6c21da41 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -4087,7 +4087,7 @@ c_parser_struct_or_union_specifier (c_parser *parser) ret.spec = finish_struct (struct_loc, type, nreverse (contents), chainon (std_attrs, chainon (attrs, postfix_attrs)), - struct_info); + struct_info, &expr); ret.kind = ctsk_tagdef; ret.expr = expr; ret.expr_const_operands = true; diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index d0bdc3df2c2..b325723a734 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -656,7 +656,8 @@ extern void finish_decl (tree, location_t, tree, tree, tree); extern tree finish_enum (tree, tree, tree); extern void finish_function (location_t = input_location); extern tree finish_struct (location_t, tree, tree, tree, - class c_struct_parse_info *); + class c_struct_parse_info *, + tree *expr = NULL); extern tree c_simulate_enum_decl (location_t, const char *, vec *); extern tree c_simulate_record_decl (location_t, const char *, diff --git a/gcc/testsuite/gcc.dg/pr112488-1.c b/gcc/testsuite/gcc.dg/pr112488-1.c new file mode 100644 index 00000000000..b53295c4366 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr112488-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile } + * { dg-options "-O1" } */ + +extern void abort(void); + +int test(int *n) { + struct T { char a[*n], b[*n]; }; + return sizeof(struct T) - sizeof(struct T); +} + +void f1(int *p) { + if (!test(p)) abort(); +} + diff --git a/gcc/testsuite/gcc.dg/pr112488-2.c b/gcc/testsuite/gcc.dg/pr112488-2.c new file mode 100644 index 00000000000..b5e5fbd3f2b --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr112488-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile } + * { dg-options "-std=gnu23 -O1" } */ + +extern void abort(void); + +int test(int *n, struct T { char a[*n], b[*n]; }*) { + return sizeof(struct T) - sizeof(struct T); +} + +void f1(int *p) { + if (test(p, 0)) abort(); +} + diff --git a/gcc/testsuite/gcc.dg/pr112898.c b/gcc/testsuite/gcc.dg/pr112898.c new file mode 100644 index 00000000000..395f3afaf2e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr112898.c @@ -0,0 +1,9 @@ +/* { dg-do compile } + { dg-options "-O2 -finstrument-functions-once" } */ + +void func(int n) +{ + struct T { int x[n]; }; + struct T *t = __builtin_malloc(sizeof *t); +} + diff --git a/gcc/testsuite/gcc.misc-tests/gcov-pr85350.c b/gcc/testsuite/gcc.misc-tests/gcov-pr85350.c index a42bf1282b2..0383b81fdfb 100644 --- a/gcc/testsuite/gcc.misc-tests/gcov-pr85350.c +++ b/gcc/testsuite/gcc.misc-tests/gcov-pr85350.c @@ -4,7 +4,7 @@ int main (void) { const int t = 2; /* count(1) */ - struct s1 { /* count(-) */ + struct s1 { /* count(1) */ int x; int g[t]; };