Loop (in)variants should appear next to each other, which is checked by GNAT
frontend. As statements inserted during expansion may break this contiguity,
GNAT recognizes specially such statements which originate in loop pragmas. In
some cases, this special treatment was not properly put in place, which lead to
spurious errors being issued.

Tested on x86_64-pc-linux-gnu, committed on trunk

2018-05-31  Yannick Moy  <m...@adacore.com>

gcc/ada/

        * sem_prag.adb (Analyze_Pragma.Check_Loop_Pragma_Placement): Inverse
        order of treatment between nodes recognized as loop pragmas (or
        generated from one) and block statements.
--- gcc/ada/sem_prag.adb
+++ gcc/ada/sem_prag.adb
@@ -5931,23 +5931,9 @@ package body Sem_Prag is
                Stmt := First (L);
                while Present (Stmt) loop
 
-                  --  Pragmas Loop_Invariant and Loop_Variant may only appear
-                  --  inside a loop or a block housed inside a loop. Inspect
-                  --  the declarations and statements of the block as they may
-                  --  contain the first grouping.
-
-                  if Nkind (Stmt) = N_Block_Statement then
-                     HSS := Handled_Statement_Sequence (Stmt);
-
-                     Check_Grouping (Declarations (Stmt));
-
-                     if Present (HSS) then
-                        Check_Grouping (Statements (HSS));
-                     end if;
-
                   --  First pragma of the first topmost grouping has been found
 
-                  elsif Is_Loop_Pragma (Stmt) then
+                  if Is_Loop_Pragma (Stmt) then
 
                      --  The group and the current pragma are not in the same
                      --  declarative or statement list.
@@ -6004,6 +5990,24 @@ package body Sem_Prag is
 
                         raise Program_Error;
                      end if;
+
+                  --  Pragmas Loop_Invariant and Loop_Variant may only appear
+                  --  inside a loop or a block housed inside a loop. Inspect
+                  --  the declarations and statements of the block as they may
+                  --  contain the first grouping. This case follows the one for
+                  --  loop pragmas, as block statements which originate in a
+                  --  loop pragma (and so Is_Loop_Pragma will return True on
+                  --  that block statement) should be treated in the previous
+                  --  case.
+
+                  elsif Nkind (Stmt) = N_Block_Statement then
+                     HSS := Handled_Statement_Sequence (Stmt);
+
+                     Check_Grouping (Declarations (Stmt));
+
+                     if Present (HSS) then
+                        Check_Grouping (Statements (HSS));
+                     end if;
                   end if;
 
                   Next (Stmt);

Reply via email to