From patchwork Sat May 18 13:27:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1936721 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=KRAyP0WC; dkim-atps=neutral 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=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 [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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VhPmD56TPz20KF for ; Sat, 18 May 2024 23:28:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE17D3858C52 for ; Sat, 18 May 2024 13:28:26 +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 35D4C3858D20 for ; Sat, 18 May 2024 13:28:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35D4C3858D20 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 35D4C3858D20 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=1716038887; cv=none; b=LBHtKikoixZGz6BwcSrbxC7bk0sDBFoEH7T9cX4zBZPXu45B1T4UsUxKktWD7q6DzsK4RwVVmxZP6DDFkhUKJR8xWCxKrT+GCT6dCB9w0kO9oGfsfxZuMoQpPqqu1Rq53FfH8WIITgJkpVjGTb6261wTIe3ZySXzDGaTm+ZpylA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716038887; c=relaxed/simple; bh=lKP7WKClQlpkCGhPEaQXjXS2rApAaGpW2X03P/tVEkc=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=SwbnMy2IDu4GH0cdleI3nRy9gx5a27Xa0W5E+kKHHmDiA9AKaBb5p1YE8slUdNEw+aP8w+1tsMHFWVTrwn1f6QJ5UhQFTvOfX8EQsvGFdOw06bZCzXo+azy1dmwVoQIAWK1rHGpRMhFRouCPRe+Rcnt7KYIWN5fivra/2gCFNWM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-169-100.tugraz.at (vra-169-100.tugraz.at [129.27.169.100]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4VhPlb62ydz3wM6; Sat, 18 May 2024 15:27:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1716038876; bh=zQCq3EMaL2GYROJd3mrNwKP/RP60FKHjqKYGDKIQGYo=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=KRAyP0WCZ9j1ssEfo7Yk38V6ObCCWY+TGgtyKMLeu7q7SKr8Cemc1LclzZmsqTfFx xHkwwBJiXW1zpwVa7UnOJIZkr4/gMp8oiQ00KmVtph6TyLMgbyPuOLP9RfLoqFEkGm DFmZH3dTP/4udfJXIZsmslMIDbQWYC1z8fsQwCSI= Message-ID: Subject: [C PATCH] Fix for some variably modified types not being recognized [PR114831] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Sat, 18 May 2024 15:27:55 +0200 In-Reply-To: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> References: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.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.117 X-Spam-Status: No, score=-11.5 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 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 We did not propagate C_TYPE_VARIABLY_MODIFIED to pointers in all cases.   I added this directly in two places, but maybe we should check all cases of build_pointer_type or integrate this into  c_build_pointer_type and use this everywhere (but I do not fully  understand the pointer mode logic there). Bootstrapped and regession tested on x86_64. c: Fix for some variably modified types not being recognized [PR114831] We did not evaluate expressions with variably modified types correctly in typeof and did not produce warnings when jumping over declarations using typeof. After addressof or array-to-pointer decay we construct new pointer types that have to be marked variably modified if the pointer target is variably modified. 2024-05-18 Martin Uecker PR c/114831 gcc/c/ * c-typeck.cc (array_to_pointer_conversion, build_unary_op): Propagate flag to pointer target. gcc/testsuite/ * gcc.dg/pr114831-1.c: New test. * gcc.dg/pr114831-2.c: New test. * gcc.dg/gnu23-varmod-1.c: New test. * gcc.dg/gnu23-varmod-2.c: New test. diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 7ecca9f58c6..2d092357e0f 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1891,8 +1891,12 @@ array_to_pointer_conversion (location_t loc, tree exp) copy_warning (exp, orig_exp); + bool varmod = C_TYPE_VARIABLY_MODIFIED (restype); + ptrtype = build_pointer_type (restype); + C_TYPE_VARIABLY_MODIFIED (ptrtype) = varmod; + if (INDIRECT_REF_P (exp)) return convert (ptrtype, TREE_OPERAND (exp, 0)); @@ -4630,6 +4634,7 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, tree eptype = NULL_TREE; const char *invalid_op_diag; bool int_operands; + bool varmod; int_operands = EXPR_INT_CONST_OPERANDS (xarg); if (int_operands) @@ -5113,8 +5118,12 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, gcc_assert (TREE_CODE (arg) != COMPONENT_REF || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1))); + varmod = C_TYPE_VARIABLY_MODIFIED (argtype); + argtype = build_pointer_type (argtype); + C_TYPE_VARIABLY_MODIFIED (argtype) = varmod; + /* ??? Cope with user tricks that amount to offsetof. Delete this when we have proper support for integer constant expressions. */ val = get_base_address (arg); diff --git a/gcc/testsuite/gcc.dg/gnu23-varmod-1.c b/gcc/testsuite/gcc.dg/gnu23-varmod-1.c new file mode 100644 index 00000000000..add10d13573 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gnu23-varmod-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } + * { dg-options "-std=gnu23" } */ + +int foo(int n) +{ + int (*a(void))[n] { return 0; }; + goto err; /* { dg-error "jump into scope" "variably modified" } */ + typeof((n++,a)) b2; +err: + return n; +} + diff --git a/gcc/testsuite/gcc.dg/gnu23-varmod-2.c b/gcc/testsuite/gcc.dg/gnu23-varmod-2.c new file mode 100644 index 00000000000..c36af1d1647 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gnu23-varmod-2.c @@ -0,0 +1,16 @@ +/* { dg-do run } + * { dg-options "-std=gnu23" } */ + +int foo(int n) +{ + int (*a(void))[n] { return 0; }; + typeof((n++,a)) b2; + return n; +} + +int main() +{ + if (2 != foo(1)) + __builtin_abort(); +} + diff --git a/gcc/testsuite/gcc.dg/pr114831-1.c b/gcc/testsuite/gcc.dg/pr114831-1.c new file mode 100644 index 00000000000..ed30a494b3c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr114831-1.c @@ -0,0 +1,27 @@ +/* { dg-do compile } + * { dg-options "-std=c23" } */ + +void f(int n) +{ + int a[n]; + goto foo; /* { dg-error "jump into scope" "variably modified" } */ + typeof(a) b1; +foo: +} + +void g(int n) +{ + int a2[1][n]; + goto foo; /* { dg-error "jump into scope" "variably modified" } */ + typeof((n++,a2)) b2; +foo: +} + +void h(int n) +{ + int a[n]; + typeof(a) b1; + goto foo; /* { dg-error "jump into scope" "variably modified" } */ + typeof(&b1) b; +foo: +} diff --git a/gcc/testsuite/gcc.dg/pr114831-2.c b/gcc/testsuite/gcc.dg/pr114831-2.c new file mode 100644 index 00000000000..ecfd87988c2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr114831-2.c @@ -0,0 +1,16 @@ +/* { dg-do run } + * { dg-options "-std=c23" } */ + +int foo(int n) +{ + int a[1][n]; + typeof((n++,a)) b2; + return n; +} + +int main() +{ + if (2 != foo(1)) + __builtin_abort(); +} +