public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-227] [Ada] Fix incorrect range computation
@ 2022-05-10  8:21 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2022-05-10  8:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5046228671e991170c19c1c0d9f70a51026e2cab

commit r13-227-g5046228671e991170c19c1c0d9f70a51026e2cab
Author: Marc Poulhiès <poulhies@adacore.com>
Date:   Fri Dec 17 14:34:15 2021 +0100

    [Ada] Fix incorrect range computation
    
    When the type range [Lo, Hi] and the computed expression range [Lor,
    Hir] are disjoint, the range-constraining logic breaks and returns an
    incorrect range. For example, when Lo<Hi<Lor<Hir, it currently returns
    [Lor, Hi]. Instead, return the computed range.
    
    The current constraining logic would require returning the base type's
    bounds. However, this would miss an opportunity to warn about out of
    range values for some cases (e.g. when type's upper bound is equal to
    base type upper bound).
    
    The alternative of always returning the computed values, even when
    ranges are intersecting, has unwanted effects (mainly useless
    constraint checks are inserted) in the Enable_Overflow_Check and
    Apply_Scalar_Range_Check as these bounds have a special interpretation.
    
    gcc/ada/
    
            * checks.adb (Determine_Range): Fix range refining.

Diff:
---
 gcc/ada/checks.adb | 53 ++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 40 insertions(+), 13 deletions(-)

diff --git a/gcc/ada/checks.adb b/gcc/ada/checks.adb
index e1a1b0cfec1..9950c18347c 100644
--- a/gcc/ada/checks.adb
+++ b/gcc/ada/checks.adb
@@ -5469,22 +5469,49 @@ package body Checks is
          --  we do NOT do this for the case of a modular type where the
          --  possible upper bound on the value is above the base type high
          --  bound, because that means the result could wrap.
+         --  Same applies for the lower bound if it is negative.
 
-         if Lor > Lo
-           and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound)
-         then
-            Lo := Lor;
-         end if;
+         if Is_Modular_Integer_Type (Typ) then
+            if Lor > Lo and then Hir <= Hbound then
+               Lo := Lor;
+            end if;
 
-         --  Similarly, if the refined value of the high bound is less than the
-         --  value so far, then reset it to the more restrictive value. Again,
-         --  we do not do this if the refined low bound is negative for a
-         --  modular type, since this would wrap.
+            if Hir < Hi and then Lor >= Uint_0 then
+               Hi := Hir;
+            end if;
 
-         if Hir < Hi
-           and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0)
-         then
-            Hi := Hir;
+         else
+            if Lor > Hi or else Hir < Lo then
+
+               --  If the ranges are disjoint, return the computed range.
+
+               --  The current range-constraining logic would require returning
+               --  the base type's bounds. However, this would miss an
+               --  opportunity to warn about out-of-range values for some cases
+               --  (e.g. when type's upper bound is equal to base type upper
+               --  bound).
+
+               --  The alternative of always returning the computed values,
+               --  even when ranges are intersecting, has unwanted effects
+               --  (mainly useless constraint checks are inserted) in the
+               --  Enable_Overflow_Check and Apply_Scalar_Range_Check as these
+               --  bounds have a special interpretation.
+
+               Lo := Lor;
+               Hi := Hir;
+            else
+
+               --  If the ranges Lor .. Hir and Lo .. Hi intersect, try to
+               --  refine the returned range.
+
+               if Lor > Lo then
+                  Lo := Lor;
+               end if;
+
+               if Hir < Hi then
+                  Hi := Hir;
+               end if;
+            end if;
          end if;
       end if;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-10  8:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-10  8:21 [gcc r13-227] [Ada] Fix incorrect range computation Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).