public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <pmderodat@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-306] [Ada] Detect infinite loops with operators in exit conditions
Date: Wed, 11 May 2022 08:55:59 +0000 (GMT)	[thread overview]
Message-ID: <20220511085559.B97C23884512@sourceware.org> (raw)

https://gcc.gnu.org/g:dc9ecb41c8572c53bdcd86e73526e2a27bd3dff9

commit r13-306-gdc9ecb41c8572c53bdcd86e73526e2a27bd3dff9
Author: Piotr Trojanek <trojanek@adacore.com>
Date:   Wed Feb 2 17:19:59 2022 +0100

    [Ada] Detect infinite loops with operators in exit conditions
    
    To warn about infinite loops we detect variables referenced in loop exit
    conditions. We handle references within boolean operators, i.e.
    comparison and negation, which are likely to appear at the top level of
    the condition (e.g. "X > 0"). However, we can easily handle all
    operators, because they are likely to appear inside the condition (e.g.
    "abs (X) > 0.0").
    
    Cleanup related to a new restriction No_Uninitialized_Local_Scalars.
    
    gcc/ada/
    
            * sem_warn.adb (Find_Var): Detect all operators; replace
            "condition" to "expression" in comments, because when this
            routine is called recursively it no longer examines the
            condition.
            (Is_Suspicious_Function_Name): Reduce scope of a local variable
            to avoid shadowing with a parameter of a nested
            Substring_Present function.

Diff:
---
 gcc/ada/sem_warn.adb | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/gcc/ada/sem_warn.adb b/gcc/ada/sem_warn.adb
index f86c2c71736..4d3452242bf 100644
--- a/gcc/ada/sem_warn.adb
+++ b/gcc/ada/sem_warn.adb
@@ -284,15 +284,15 @@ package body Sem_Warn is
 
       procedure Find_Var (N : Node_Id) is
       begin
-         --  Condition is a direct variable reference
+         --  Expression is a direct variable reference
 
          if Is_Entity_Name (N) then
             Ref := N;
             Var := Entity (Ref);
 
-         --  Case of condition is a comparison with compile time known value
+         --  If expression is an operator, check its operands
 
-         elsif Nkind (N) in N_Op_Compare then
+         elsif Nkind (N) in N_Binary_Op then
             if Compile_Time_Known_Value (Right_Opnd (N)) then
                Find_Var (Left_Opnd (N));
 
@@ -305,9 +305,9 @@ package body Sem_Warn is
                return;
             end if;
 
-         --  If condition is a negation, check its operand
+         --  If expression is a unary operator, check its operand
 
-         elsif Nkind (N) = N_Op_Not then
+         elsif Nkind (N) in N_Unary_Op then
             Find_Var (Right_Opnd (N));
 
          --  Case of condition is function call
@@ -445,8 +445,6 @@ package body Sem_Warn is
       ---------------------------------
 
       function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is
-         S : Entity_Id;
-
          function Substring_Present (S : String) return Boolean;
          --  Returns True if name buffer has given string delimited by non-
          --  alphabetic characters or by end of string. S is lower case.
@@ -473,6 +471,10 @@ package body Sem_Warn is
             return False;
          end Substring_Present;
 
+         --  Local variables
+
+         S : Entity_Id;
+
       --  Start of processing for Is_Suspicious_Function_Name
 
       begin


                 reply	other threads:[~2022-05-11  8:55 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=20220511085559.B97C23884512@sourceware.org \
    --to=pmderodat@gcc.gnu.org \
    --cc=gcc-cvs@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).