public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Steve Baird <baird@adacore.com>
Subject: [COMMITED] ada: Incorrect inferences drawn from if/elsif/while conditions with -gnatVo
Date: Thu,  6 Oct 2022 11:29:29 +0200	[thread overview]
Message-ID: <20221006092929.261152-1-poulhies@adacore.com> (raw)

From: Steve Baird <baird@adacore.com>

Within the first (respectively, second) statement list of this if statement
  declare
     X : constant Integer := ... ;
  begin
     if X > 0 then
       ...;
     else
       ...;
     end if;
  end;
we can safely assume that X is greater (respectively, not greater) than 0.

Fix a bug that incorrectly computed the region in which such assumptions
can be made to include the condition of the if-statement. This bug usually
had no effect because semantic analysis/simplification of the condition was
already complete before the code containing the bug was executed.
Unfortunately, this is not true in some cases involving -gnatVo validity
checking. In these cases, the bug could result in incorrect simplification
of the condition at compile time. This, in turn, could lead to incorrect
unconditional execution of one arm of the if-statement at run time. Similar
errors appear to be possible for the conditions of an elsif or a while loop;
the fix addresses these cases as well, although corresponding problems with
these constructs have not been demonstrated.

gcc/ada/

	* exp_util.adb
	(Get_Current_Value_Condition): Treat references occurring within
	the condition of an if statement, an elsif, or a while loop in the
	same way as references that occur before the start of that
	enclosing construct.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_util.adb | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index 61395adf195..f569d2ef583 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -6923,6 +6923,11 @@ package body Exp_Util is
             if Loc < Sloc (CV) then
                return;
 
+            --  In condition of IF statement
+
+            elsif In_Subtree (N => Var, Root => Condition (CV)) then
+               return;
+
             --  After end of IF statement
 
             elsif Loc >= Sloc (CV) + Text_Ptr (UI_To_Int (End_Span (CV))) then
@@ -7009,7 +7014,12 @@ package body Exp_Util is
             if Loc < Sloc (CV) then
                return;
 
-               --  After end of IF statement
+            --  In condition of ELSIF part
+
+            elsif In_Subtree (N => Var, Root => Condition (CV)) then
+               return;
+
+            --  After end of IF statement
 
             elsif Loc >= Sloc (Stm) +
               Text_Ptr (UI_To_Int (End_Span (Stm)))
@@ -7066,6 +7076,11 @@ package body Exp_Util is
                if Loc < Sloc (Loop_Stmt) then
                   return;
 
+               --  In condition of while loop
+
+               elsif In_Subtree (N => Var, Root => Condition (CV)) then
+                  return;
+
                --  After end of LOOP statement
 
                elsif Loc >= Sloc (End_Label (Loop_Stmt)) then
-- 
2.34.1


                 reply	other threads:[~2022-10-06  9:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221006092929.261152-1-poulhies@adacore.com \
    --to=poulhies@adacore.com \
    --cc=baird@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).