[Ada] Improve warnings about infinite loops

The compiler now has fewer false alarms when warning about infinite
loops. For example, a loop of the form "for X of A ...", where A is an
array, cannot be infinite.  The compiler no longer warns in this case.

2019-07-03  Bob Duff  <duff@adacore.com>

gcc/ada/

	* sem_warn.adb (Check_Infinite_Loop_Warning): Avoid the warning
	if an Iterator_Specification is present.

gcc/testsuite/

	* gnat.dg/warn20.adb, gnat.dg/warn20_pkg.adb,
	gnat.dg/warn20_pkg.ads: New testcase.

From-SVN: r272978
This commit is contained in:
Bob Duff 2019-07-03 08:15:28 +00:00 committed by Pierre-Marie de Rodat
parent 07fb741a36
commit e08a896b96
6 changed files with 48 additions and 2 deletions

View File

@ -1,3 +1,8 @@
2019-07-03 Bob Duff <duff@adacore.com>
* sem_warn.adb (Check_Infinite_Loop_Warning): Avoid the warning
if an Iterator_Specification is present.
2019-07-03 Bob Duff <duff@adacore.com>
* doc/gnat_ugn/gnat_utility_programs.rst: Document default

View File

@ -632,9 +632,16 @@ package body Sem_Warn is
Expression := Condition (Iter);
-- For iteration, do not process, since loop will always terminate
-- For Loop_Parameter_Specification, do not process, since loop
-- will always terminate. For Iterator_Specification, also do not
-- process. Either it will always terminate (e.g. "for X of
-- Some_Array ..."), or we can't tell if it's going to terminate
-- without looking at the iterator, so any warning here would be
-- noise.
elsif Present (Loop_Parameter_Specification (Iter)) then
elsif Present (Loop_Parameter_Specification (Iter))
or else Present (Iterator_Specification (Iter))
then
return;
end if;
end if;

View File

@ -1,3 +1,8 @@
2019-07-03 Bob Duff <duff@adacore.com>
* gnat.dg/warn20.adb, gnat.dg/warn20_pkg.adb,
gnat.dg/warn20_pkg.ads: New testcase.
2019-07-03 Ed Schonberg <schonberg@adacore.com>
* gnat.dg/predicate6.adb, gnat.dg/predicate6.ads: New testcase.

View File

@ -0,0 +1,11 @@
-- { dg-do compile }
-- { dg-options "-gnatwa" }
with Warn20_Pkg;
procedure Warn20 is
package P is new Warn20_Pkg (Integer, 0);
pragma Unreferenced (P);
begin
null;
end Warn20;

View File

@ -0,0 +1,10 @@
package body Warn20_Pkg is
L : array (1 .. 10) of T := (1 .. 10 => None);
procedure Foo is
begin
for A of L loop
exit when A = None;
Dispatch (A);
end loop;
end;
end;

View File

@ -0,0 +1,8 @@
generic
type T is private;
None : T;
package Warn20_Pkg is
generic
with procedure Dispatch (X : T) is null;
procedure Foo;
end;