From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1285) id AB8E4385483B; Wed, 5 Oct 2022 11:49:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AB8E4385483B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664970568; bh=bCWglhXvwfEBz6T+0/p+En4mp0ho9zDs9VNDvKGncFM=; h=From:To:Subject:Date:From; b=ZQgkXJalr4RtHx6oKqblTVuK5edGtFULSZe5y7GB+8QToGba1MS82yXT0bPCrO96R I3JJO+smtfRRUh347AFzrxkM/bic6f4WOosyGiyepooFs3C8ujiIv3d46OM8zCO1g1 ZGdzEUld5VybGqKBLrKCjDNKnrU1z/ZblyqUpZtc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Eric Botcazou To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3084] Fix bogus -Wstringop-overflow warning in Ada X-Act-Checkin: gcc X-Git-Author: Eric Botcazou X-Git-Refname: refs/heads/master X-Git-Oldrev: bcc27369910b818047bca466fbcef6a3034dfc7f X-Git-Newrev: 853ce8eea4ff97850a987167e603387b3d0f1401 Message-Id: <20221005114928.AB8E4385483B@sourceware.org> Date: Wed, 5 Oct 2022 11:49:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:853ce8eea4ff97850a987167e603387b3d0f1401 commit r13-3084-g853ce8eea4ff97850a987167e603387b3d0f1401 Author: Eric Botcazou Date: Wed Oct 5 12:21:03 2022 +0200 Fix bogus -Wstringop-overflow warning in Ada It comes from a discrepancy between get_offset_range, which uses a signed type, and handle_array_ref, which uses an unsigned one, to do computations. gcc/ PR tree-optimization/106698 * pointer-query.cc (handle_array_ref): Fix handling of low bound. gcc/testsuite/ * gnat.dg/lto26.adb: New test. * gnat.dg/lto26_pkg1.ads, gnat.dg/lto26_pkg1.adb: New helper. * gnat.dg/lto26_pkg2.ads, gnat.dg/lto26_pkg2.adb: Likewise. Diff: --- gcc/pointer-query.cc | 19 ++++++++++++------- gcc/testsuite/gnat.dg/lto26.adb | 13 +++++++++++++ gcc/testsuite/gnat.dg/lto26_pkg1.adb | 11 +++++++++++ gcc/testsuite/gnat.dg/lto26_pkg1.ads | 11 +++++++++++ gcc/testsuite/gnat.dg/lto26_pkg2.adb | 15 +++++++++++++++ gcc/testsuite/gnat.dg/lto26_pkg2.ads | 9 +++++++++ 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc index ae561731216..0f0100233c1 100644 --- a/gcc/pointer-query.cc +++ b/gcc/pointer-query.cc @@ -1796,14 +1796,19 @@ handle_array_ref (tree aref, gimple *stmt, bool addr, int ostype, orng[0] = -orng[1] - 1; } - /* Convert the array index range determined above to a byte - offset. */ + /* Convert the array index range determined above to a byte offset. */ tree lowbnd = array_ref_low_bound (aref); - if (!integer_zerop (lowbnd) && tree_fits_uhwi_p (lowbnd)) - { - /* Adjust the index by the low bound of the array domain - (normally zero but 1 in Fortran). */ - unsigned HOST_WIDE_INT lb = tree_to_uhwi (lowbnd); + if (TREE_CODE (lowbnd) == INTEGER_CST && !integer_zerop (lowbnd)) + { + /* Adjust the index by the low bound of the array domain (0 in C/C++, + 1 in Fortran and anything in Ada) by applying the same processing + as in get_offset_range. */ + const wide_int wlb = wi::to_wide (lowbnd); + signop sgn = SIGNED; + if (TYPE_UNSIGNED (TREE_TYPE (lowbnd)) + && wlb.get_precision () < TYPE_PRECISION (sizetype)) + sgn = UNSIGNED; + const offset_int lb = offset_int::from (wlb, sgn); orng[0] -= lb; orng[1] -= lb; } diff --git a/gcc/testsuite/gnat.dg/lto26.adb b/gcc/testsuite/gnat.dg/lto26.adb new file mode 100644 index 00000000000..a27348b2e7f --- /dev/null +++ b/gcc/testsuite/gnat.dg/lto26.adb @@ -0,0 +1,13 @@ +-- { dg-do run } +-- { dg-options "-O2 -flto" { target lto } } + +with Ada.Streams; use Ada.Streams; +with Lto26_Pkg1; use Lto26_Pkg1; + +procedure Lto26 is + R : Rec; +begin + for I in 1 .. 10 loop + Set (R, (7, 0, 84, Stream_Element (I), 0, 0, 0), 1); + end loop; +end; diff --git a/gcc/testsuite/gnat.dg/lto26_pkg1.adb b/gcc/testsuite/gnat.dg/lto26_pkg1.adb new file mode 100644 index 00000000000..c68b134ffaa --- /dev/null +++ b/gcc/testsuite/gnat.dg/lto26_pkg1.adb @@ -0,0 +1,11 @@ +with Lto26_Pkg2; use Lto26_Pkg2; + +package body Lto26_Pkg1 is + + procedure Set (R : Rec; A : Stream_Element_Array; C :Unsigned_8) is + procedure My_Build is new Build; + begin + My_Build (A, C); + end; + +end Lto26_Pkg1; diff --git a/gcc/testsuite/gnat.dg/lto26_pkg1.ads b/gcc/testsuite/gnat.dg/lto26_pkg1.ads new file mode 100644 index 00000000000..527974750b0 --- /dev/null +++ b/gcc/testsuite/gnat.dg/lto26_pkg1.ads @@ -0,0 +1,11 @@ +with Ada.Finalization; +with Ada.Streams; use Ada.Streams; +with Interfaces; use Interfaces; + +package Lto26_Pkg1 is + + type Rec is new Ada.Finalization.Limited_Controlled with null record; + + procedure Set (R : Rec; A : Stream_Element_Array; C :Unsigned_8); + +end Lto26_Pkg1; diff --git a/gcc/testsuite/gnat.dg/lto26_pkg2.adb b/gcc/testsuite/gnat.dg/lto26_pkg2.adb new file mode 100644 index 00000000000..9f897334d25 --- /dev/null +++ b/gcc/testsuite/gnat.dg/lto26_pkg2.adb @@ -0,0 +1,15 @@ +package body Lto26_Pkg2 is + + procedure Build (A : Stream_Element_Array; C : Unsigned_8) is + Start : Stream_Element_Offset := A'First; + Next : Stream_Element_Offset; + Length : Unsigned_8; + begin + for I in 1 .. C loop + Length := Unsigned_8 (A (A'First)); + Next := Start + Stream_Element_Offset (Length); + Start := Next; + end loop; + end; + +end Lto26_Pkg2; diff --git a/gcc/testsuite/gnat.dg/lto26_pkg2.ads b/gcc/testsuite/gnat.dg/lto26_pkg2.ads new file mode 100644 index 00000000000..68741bb8d8b --- /dev/null +++ b/gcc/testsuite/gnat.dg/lto26_pkg2.ads @@ -0,0 +1,9 @@ +with Ada.Streams; use Ada.Streams; +with Interfaces; use Interfaces; + +package Lto26_Pkg2 is + + generic + procedure Build (A : Stream_Element_Array; C : Unsigned_8); + +end Lto26_Pkg2;