From patchwork Tue Jul 2 19:44:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1955609 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=anlauf@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=l4Ov5mur; 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 4WDCzH1NcSz1xpc for ; Wed, 3 Jul 2024 05:44:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E40C387084B for ; Tue, 2 Jul 2024 19:44:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) by sourceware.org (Postfix) with ESMTPS id 230D2386103B; Tue, 2 Jul 2024 19:44:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 230D2386103B Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 230D2386103B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.15.18 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719949445; cv=none; b=MUkd8T87xfsYoaXbg8rQ2I34X8u9EZak0F3ZUkQaTi9Zl2p10DvcRUhJOH30a9eoevGXmIjFxQnql1aPXjlbxFjPpOk1MO5srOXRHsWwR32VjUWz/rz3EK0VJALKdx48oRFppEWcZxyKEDBTV0jM9VZgfPiQptIPZdMLOtS8tuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719949445; c=relaxed/simple; bh=I0hEnH55U57keeEoKXZBtGtQm0KitkRohZPYxM6ZPj0=; h=DKIM-Signature:MIME-Version:Message-ID:From:To:Subject:Date; b=Aiba2RMTuURFRT74dBzu2XQ8H5mSiaCudNJN3L6sElfd//kIYUt29vDxyMg+eA5h+Gas9uE8ZFrAZ7Ti5v76QOC0x44I3nXI9KKvlJsJ9gwMejfX0zO/tc+3HOwVPNe63jVEWW2DIrJtahc+kHv0gyR1/4otBzHkiuIM3Iq4aQI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1719949441; x=1720554241; i=anlauf@gmx.de; bh=jluVcWZStvD0zbKtXRazd98IX0PkYWpTeXLoWPShTZA=; h=X-UI-Sender-Class:MIME-Version:Message-ID:From:To:Subject: Content-Type:Date:cc:content-transfer-encoding:content-type:date: from:message-id:mime-version:reply-to:subject:to; b=l4Ov5murerZGlNcmsxHOFbWsgx5e5Q8N60Pthf/Er3QebAponxT1iun6VIklPg2a e1aEniY399zVMsD8uWRmkvapn+iH9T8mxT15YtMgK4RprbAuCBGV+GdBWCM+EsFmY pk/2RyGw4BqAS0hl7khmrXjUVeXoS/SyEr4N31mPVzpDwGsvyM1/BPUHzTVbhAfb0 cYbgDUpJSiex652IK5mGG7Tli2c2w32EyGjzFEn/JmB6vD/XyZLB6oekV/LxcY7HF hfpPktSEfJdSuBTkzMEVnEgZlNUbPElFHtJXl/vPHKPTyTkZ9EC1ALaM7aZB/sbMQ 55o6ugk/gKjGQixu1Q== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.154.60] ([79.232.154.60]) by web-mail.gmx.net (3c-app-gmx-bs32.server.lan [172.19.170.84]) (via HTTP); Tue, 2 Jul 2024 21:44:01 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: fix associate with assumed-length character array [PR115700] Date: Tue, 2 Jul 2024 21:44:01 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:dSRpfPzLsqHCqBEYT4wvt/dz0hX+ROSzfpp4OHdjqsDQpXj9UFGs2eSdd6XBtYtljqvR/ cED6WOgWhoFjGLIMEaY0vff+cEgI52IWdw1jwE4lpmZeuRa8d9yZLV7+18mrRd+oLMMtICP9167Q kszmePTodA+mb1R7DFfxagvKoX/GQlwcla1K4Rs4ZpeqTKJUtKCJLEYC07CFWOZ3hG0uRBXNBbrB 1pnzeJx1GxGXAFaSFuPxo2/Gee4DZOLBcX87vDNnmdwM3MoAWDytLosT0CZI8uaslEp5FG87FJd0 OU= UI-OutboundReport: notjunk:1;M01:P0:4fXU1+mJOOk=;OudoX3WspVdyxuIGlGgftqv/TMb KwkSyJEexxqnozgmNVf6AWXloq/Ir2xELRn0qH54pyjFNS0G/IvHYpKsh4nlDe+aF0EwwEVTy uMsHJl8NkopREXPn4ZB+57iHJrygtANa3zPqmU/+TngdkwZW1gMM1ojsh9ArfiTqLZu/Km51l YysgVHBfz7rGe9dCNCB2CuY5NY7Z9pobSDh3oxqXuDnFSwPZccG0V/ylz7EHzMBxmjfHLccrA SWdclEMfQPHeBwF1COXiJuGhN7IHl92u2vL8H2RJPMvHsJYhTVJo1COVLHZYR0Id+9z1VzEjJ CJffAznLD/+ceDp9PMVBG7M9b5bDrvC91SlAwINu1OoIOhAQZ01uqTZPaoGX4Uehd6KAM/m5m jXI0rCw6evEBPDq5LW/K4uui56bPYbd77UsPqU5eIPn0Bsfilv1zttEn+fgS5N+OlHq2NNoiR pwybxFqeVPp1m0frLbwM15Q04jVUQdMYCGYIpqCImyNz0KfuD2acIt0iyDOP7xQCmInwDyLTu hJbIg14aIGvR2fzvKnbUMwnQNRZcg6HmZGZGA5zjU9jd0MGWGw62L3UfWr2ubfOpp/44zwpim Plt6mnvkEllrZYKDniwnjv1ErpnBW0UH5iFEVgejquo//HP3/KCq0HoCUVJ/823vheebthA1B WUtdwHpYimClEe7n5TWuWtrGJmdJLe1yngIq4iWLIJpOjXm0L+0abUDx1YCbyF0= X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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 Dear all, the attached patch addresses an effectively bogus warning about uninitialized temporary string lengths of associate selectors. The primary reason is that the array descriptor for a character array is created before the corresponding string length is set. Moving the setting of the string length temporary to the beginning of the block solves the issue. The patch does not solve the case for the target containing substring references. This needs to be addressed separately. (So far I could not find a solution that does not regress.) Regtested on x86_64-pc-linux-gnu. OK for mainline? As the PR is marked as a regression, is it also OK for backporting? Thanks, Harald From 930a1be8c623cf03f9b2e6dbddb45d0b69e152dd Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 2 Jul 2024 21:26:05 +0200 Subject: [PATCH] Fortran: fix associate with assumed-length character array [PR115700] gcc/fortran/ChangeLog: PR fortran/115700 * trans-stmt.cc (trans_associate_var): When the associate target is an array-valued character variable, the length is known at entry of the associate block. Move setting of string length of the selector to the initialization part of the block. gcc/testsuite/ChangeLog: PR fortran/115700 * gfortran.dg/associate_69.f90: New test. --- gcc/fortran/trans-stmt.cc | 18 +++++++++--- gcc/testsuite/gfortran.dg/associate_69.f90 | 33 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/associate_69.f90 diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index 60275e18867..703a705e7ca 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -1911,6 +1911,8 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block) gfc_se se; tree desc; bool cst_array_ctor; + stmtblock_t init; + gfc_init_block (&init); desc = sym->backend_decl; cst_array_ctor = e->expr_type == EXPR_ARRAY @@ -1935,10 +1937,17 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block) && !sym->attr.select_type_temporary && sym->ts.u.cl->backend_decl && VAR_P (sym->ts.u.cl->backend_decl) + && se.string_length && se.string_length != sym->ts.u.cl->backend_decl) - gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl, - fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl), - se.string_length)); + { + /* When the target is a variable, its length is already known. */ + tree len = fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl), + se.string_length); + if (e->expr_type == EXPR_VARIABLE) + gfc_add_modify (&init, sym->ts.u.cl->backend_decl, len); + else + gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl, len); + } /* If we didn't already do the pointer assignment, set associate-name descriptor to the one generated for the temporary. */ @@ -1978,7 +1987,8 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block) } /* Done, register stuff as init / cleanup code. */ - gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), + gfc_add_block_to_block (&init, &se.pre); + gfc_add_init_cleanup (block, gfc_finish_block (&init), gfc_finish_block (&se.post)); } diff --git a/gcc/testsuite/gfortran.dg/associate_69.f90 b/gcc/testsuite/gfortran.dg/associate_69.f90 new file mode 100644 index 00000000000..28f488bb274 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/associate_69.f90 @@ -0,0 +1,33 @@ +! { dg-do compile } +! { dg-options "-Og -Wuninitialized -Wmaybe-uninitialized -fdump-tree-optimized" } +! +! PR fortran/115700 - Bogus warning for associate with assumed-length character array +! +subroutine mvce(x) + implicit none + character(len=*), dimension(:), intent(in) :: x + + associate (tmp1 => x) + if (len (tmp1) /= len (x)) stop 1 + end associate + + associate (tmp2 => x(1:)) + if (len (tmp2) /= len (x)) stop 2 + end associate + + associate (tmp3 => x(1:)(:)) + if (len (tmp3) /= len (x)) stop 3 + end associate + +! The following associate blocks still produce bogus warnings: + +! associate (tmp4 => x(:)(1:)) +! if (len (tmp4) /= len (x)) stop 4 +! end associate +! +! associate (tmp5 => x(1:)(1:)) +! if (len (tmp5) /= len (x)) stop 5 +! end associate +end + +! { dg-final { scan-tree-dump-not " \\.tmp" "optimized" } } -- 2.35.3