From patchwork Thu Apr 25 10:31:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 239472 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id B99E82C00CF for ; Thu, 25 Apr 2013 20:31:59 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=IHJ6fZBxNRrWOfZ0LqjxkXTc8Wt64YD6P9F6zbak7yAbpHT0wa 1/BN5+CHJ/JUY/+S4tsoSDBA75O8vEQ0nk2spuM5cyh/4fUCVWZoYk4TKFrT8C37 PajUC6scd9rfw6SpG34rN5zgBOGHQo8SAc/Y7ENg42swxz5UiPy2mSnuM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=qKhI3Kn5lq9jlSh6G2ZSFREdSOg=; b=hx8GII7vOFv6F4iUIo0e +Epufl00il8gT+fCs549c58VYMXoRA0G7nslstT5ffNRWEtR6JxUUtVHfEHkWm3U X/Uaia2FkfPO7PFNfFc2b6izCjJWNjqlu+6c8rQKR2naXpBLaAwm55oQ+GWrr/uD QnRXG7HWo8H3FKX02Xmd8ZI= Received: (qmail 30936 invoked by alias); 25 Apr 2013 10:31:49 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 30905 invoked by uid 89); 25 Apr 2013 10:31:49 -0000 X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO autolearn=ham version=3.3.1 Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 25 Apr 2013 10:31:48 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id B5B672ED66; Thu, 25 Apr 2013 06:31:46 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id f9tvmPPWFLCV; Thu, 25 Apr 2013 06:31:46 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 9BF6F2EA2E; Thu, 25 Apr 2013 06:31:46 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 9AF163FF09; Thu, 25 Apr 2013 06:31:46 -0400 (EDT) Date: Thu, 25 Apr 2013 06:31:46 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Hristian Kirtchev Subject: [Ada] Warning on unused loop variable of a quantified expression Message-ID: <20130425103146.GA18385@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Found: No This patch adds a check in the analysis of quantified expressions to detect an unused loop variable and issue a warning. ------------ -- Source -- ------------ -- warnings.adb with Ada.Containers.Doubly_Linked_Lists; procedure Warnings is package DLL is new Ada.Containers.Doubly_Linked_Lists (Natural, "="); type Test_Array is array (Natural range <>, Natural range <>) of Natural; function Factorial (Val : Natural) return Natural is begin if Val > 1 then return Val * Factorial (Val - 1); end if; return 1; end Factorial; TA : constant Test_Array (1 .. 3, 1 .. 4) := (others => (others => 0)); Flag : Boolean; L : DLL.List; begin Flag := (for all Element of TA => Element > 1); Flag := (for some Element of TA => Factorial (3) = 6); Flag := (for all Element of L => Element > 1); Flag := (for some Element of L => Factorial (3) = 6); Flag := (for all Element in 1 .. 4 => Factorial (Element) > 0); Flag := (for some Element in 1 .. 4 => Factorial (3) = 6); Flag := (for all Element in DLL.Iterate (L) => DLL.Element (Element) > 1); Flag := (for some Element in DLL.Iterate (L) => Factorial (3) = 6); end Warnings; ---------------------------- -- Compilation and output -- ---------------------------- $ gcc -c -gnat12 -gnatw.t warnings.adb warnings.adb:23:22: warning: unused variable "Element" warnings.adb:25:22: warning: unused variable "Element" warnings.adb:27:22: warning: unused variable "Element" warnings.adb:29:22: warning: unused variable "Element" Tested on x86_64-pc-linux-gnu, committed on trunk 2013-04-25 Hristian Kirtchev * sem_ch4.adb (Analyze_Quantified_Expression): Add local variable Loop_Id. Verify that the loop variable is used within the condition of the quantified expression. (Referenced): New routine. Index: sem_ch4.adb =================================================================== --- sem_ch4.adb (revision 198287) +++ sem_ch4.adb (working copy) @@ -3510,6 +3510,9 @@ -- Determine whether if expression If_Expr lacks an else part or if it -- has one, it evaluates to True. + function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean; + -- Determine whether entity Id is referenced within expression Expr + -------------------- -- Is_Empty_Range -- -------------------- @@ -3561,9 +3564,44 @@ and then Is_True (Expr_Value (Else_Expr))); end No_Else_Or_Trivial_True; + ---------------- + -- Referenced -- + ---------------- + + function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is + Seen : Boolean := False; + + function Is_Reference (N : Node_Id) return Traverse_Result; + -- Determine whether node N denotes a reference to Id. If this is the + -- case, set global flag Seen to True and stop the traversal. + + function Is_Reference (N : Node_Id) return Traverse_Result is + begin + if Is_Entity_Name (N) + and then Present (Entity (N)) + and then Entity (N) = Id + then + Seen := True; + return Abandon; + else + return OK; + end if; + end Is_Reference; + + procedure Inspect_Expression is new Traverse_Proc (Is_Reference); + + -- Start of processing for Referenced + + begin + Inspect_Expression (Expr); + + return Seen; + end Referenced; + -- Local variables Cond : constant Node_Id := Condition (N); + Loop_Id : Entity_Id; QE_Scop : Entity_Id; -- Start of processing for Analyze_Quantified_Expression @@ -3590,22 +3628,39 @@ if Present (Iterator_Specification (N)) then Preanalyze (Iterator_Specification (N)); + -- Do not proceed with the analysis when the range of iteration is + -- empty. The appropriate error is issued by Is_Empty_Range. + if Is_Entity_Name (Name (Iterator_Specification (N))) and then Is_Empty_Range (Etype (Name (Iterator_Specification (N)))) then return; end if; - else + else pragma Assert (Present (Loop_Parameter_Specification (N))); Preanalyze (Loop_Parameter_Specification (N)); end if; Preanalyze_And_Resolve (Cond, Standard_Boolean); End_Scope; - Set_Etype (N, Standard_Boolean); + -- Verify that the loop variable is used within the condition of the + -- quantified expression. + + if Present (Iterator_Specification (N)) then + Loop_Id := Defining_Identifier (Iterator_Specification (N)); + else + Loop_Id := Defining_Identifier (Loop_Parameter_Specification (N)); + end if; + + if Warn_On_Suspicious_Contract + and then not Referenced (Loop_Id, Cond) + then + Error_Msg_N ("?T?unused variable &", Loop_Id); + end if; + -- Diagnose a possible misuse of the "some" existential quantifier. When -- we have a quantified expression of the form --