From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id DDD2F388A02B for ; Wed, 11 May 2022 08:54:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DDD2F388A02B Received: by mail-wr1-x42d.google.com with SMTP id i5so1915268wrc.13 for ; Wed, 11 May 2022 01:54:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=A3Zsow0pkvHilfhH6BoSWYiXTGf51Ty7NNEnFqRINi8=; b=hVDwTxjW72zdwpSkvT9X+n9Ll+b0CY9rFpflcyAiXrr4+NAUDpO0LvOSrxk5iVCVZA knZ1q2AxJn9Iz+il56OXETgdIpDzINYzqoP0jlQa8haxacyyJFDYzZv5Exu161u01oHG yeN6UwfWk9AWZSabumDsqWQn4WxTB8HwGJckzu56kFmxaciA6w0vRtwcWrU+SH/1k4NS TVOBQyA9O0tearTCeifU4xVADqqXhZCZIVsUm7W3r/83028TdZYwhBtsC0R8XJkfMcFz b1vwDWSw6dHVoV5Doo/cw9rA2n98i4T4hkw1HD/OhjTrwEP94kuf56pS0VK+WirEbHAD +HvQ== X-Gm-Message-State: AOAM531kxN9ksRKVGwQyqbr/ehhmrs7wGwd3mQD5z1ffkEF0aejIjvj3 YmzTSxqWKvquBTrAOhSbG9IoCSyfV4V/yg== X-Google-Smtp-Source: ABdhPJyYsGDGjXoeBwX2JuUaDtSYdncleOGQhvxCxF/OUqqRKHS+q3wWPfV/c59SwaQFVOCB4BdgnQ== X-Received: by 2002:a05:6000:18ac:b0:20c:ba84:1260 with SMTP id b12-20020a05600018ac00b0020cba841260mr16235174wri.379.1652259280442; Wed, 11 May 2022 01:54:40 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id b11-20020adff24b000000b0020adc114136sm1410286wrp.0.2022.05.11.01.54.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 01:54:39 -0700 (PDT) Date: Wed, 11 May 2022 08:54:39 +0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [Ada] Detect infinite loops with operators in exit conditions Message-ID: <20220511085439.GA2167330@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="/04w6evG8XlLl3ft" Content-Disposition: inline X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2022 08:54:42 -0000 --/04w6evG8XlLl3ft Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. Tested on x86_64-pc-linux-gnu, committed on trunk 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. --/04w6evG8XlLl3ft Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/sem_warn.adb b/gcc/ada/sem_warn.adb --- 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 --/04w6evG8XlLl3ft--