This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Warning on unused loop variable of a quantified expression


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  <kirtchev@adacore.com>

	* 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.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]