[Ada] Remove unnecessary guards for non-empty lists

Message ID 20220106171312.GA2921754@adacore.com
State Committed
Headers
Series [Ada] Remove unnecessary guards for non-empty lists |

Commit Message

Pierre-Marie de Rodat Jan. 6, 2022, 5:13 p.m. UTC
  All node lists can be safely iterated with First/Present/Next. There is
no need for explicit guard against empty lists. Code cleanup.

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

gcc/ada/

	* errout.adb (Remove_Warning_Messages): Remove unnecessary guard.
	* exp_util.adb (Kill_Dead_Code): Likewise.
	* par_sco.adb (Traverse_Declarations_Or_Statements): Likewise.
	* sem_ch3.adb (Build_Derived_Record_Type): Likewise.
	* sem_ch4.adb (Traverse_Interfaces): Likewise.
	* sem_eval.adb (Traverse_Interfaces): Likewise.
	* sem_util.adb (Collect_Interfaces): Likewise.
	(Has_Non_Null_Statements, Side_Effect_Free_Statements):
	Likewise; turn into WHILE loops, for consistency.
  

Patch

diff --git a/gcc/ada/errout.adb b/gcc/ada/errout.adb
--- a/gcc/ada/errout.adb
+++ b/gcc/ada/errout.adb
@@ -3378,13 +3378,11 @@  package body Errout is
    procedure Remove_Warning_Messages (L : List_Id) is
       Stat : Node_Id;
    begin
-      if Is_Non_Empty_List (L) then
-         Stat := First (L);
-         while Present (Stat) loop
-            Remove_Warning_Messages (Stat);
-            Next (Stat);
-         end loop;
-      end if;
+      Stat := First (L);
+      while Present (Stat) loop
+         Remove_Warning_Messages (Stat);
+         Next (Stat);
+      end loop;
    end Remove_Warning_Messages;
 
    --------------------


diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -9495,14 +9495,12 @@  package body Exp_Util is
    begin
       W := Warn;
 
-      if Is_Non_Empty_List (L) then
-         N := First (L);
-         while Present (N) loop
-            Kill_Dead_Code (N, W);
-            W := False;
-            Next (N);
-         end loop;
-      end if;
+      N := First (L);
+      while Present (N) loop
+         Kill_Dead_Code (N, W);
+         W := False;
+         Next (N);
+      end loop;
    end Kill_Dead_Code;
 
    -----------------------------


diff --git a/gcc/ada/par_sco.adb b/gcc/ada/par_sco.adb
--- a/gcc/ada/par_sco.adb
+++ b/gcc/ada/par_sco.adb
@@ -2417,21 +2417,18 @@  package body Par_SCO is
 
       --  Loop through statements or declarations
 
-      if Is_Non_Empty_List (L) then
-         N := First (L);
-         while Present (N) loop
+      N := First (L);
+      while Present (N) loop
 
-            --  Note: For separate bodies, we see the tree after Par.Labl has
-            --  introduced implicit labels, so we need to ignore those nodes.
+         --  Note: For separate bodies, we see the tree after Par.Labl has
+         --  introduced implicit labels, so we need to ignore those nodes.
 
-            if Nkind (N) /= N_Implicit_Label_Declaration then
-               Traverse_One (N);
-            end if;
-
-            Next (N);
-         end loop;
+         if Nkind (N) /= N_Implicit_Label_Declaration then
+            Traverse_One (N);
+         end if;
 
-      end if;
+         Next (N);
+      end loop;
 
       --  End sequence of statements and flush deferred decisions
 


diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -9351,13 +9351,11 @@  package body Sem_Ch3 is
          declare
             Iface : Node_Id;
          begin
-            if Is_Non_Empty_List (Interface_List (Type_Def)) then
-               Iface := First (Interface_List (Type_Def));
-               while Present (Iface) loop
-                  Freeze_Before (N, Etype (Iface));
-                  Next (Iface);
-               end loop;
-            end if;
+            Iface := First (Interface_List (Type_Def));
+            while Present (Iface) loop
+               Freeze_Before (N, Etype (Iface));
+               Next (Iface);
+            end loop;
          end;
       end if;
 


diff --git a/gcc/ada/sem_ch4.adb b/gcc/ada/sem_ch4.adb
--- a/gcc/ada/sem_ch4.adb
+++ b/gcc/ada/sem_ch4.adb
@@ -9597,31 +9597,29 @@  package body Sem_Ch4 is
          begin
             Error := False;
 
-            if Is_Non_Empty_List (Intface_List) then
-               Intface := First (Intface_List);
-               while Present (Intface) loop
+            Intface := First (Intface_List);
+            while Present (Intface) loop
 
-                  --  Look for acceptable class-wide homonyms associated with
-                  --  the interface.
+               --  Look for acceptable class-wide homonyms associated with the
+               --  interface.
 
-                  Traverse_Homonyms (Etype (Intface), Error);
+               Traverse_Homonyms (Etype (Intface), Error);
 
-                  if Error then
-                     return;
-                  end if;
+               if Error then
+                  return;
+               end if;
 
-                  --  Continue the search by looking at each of the interface's
-                  --  associated interface ancestors.
+               --  Continue the search by looking at each of the interface's
+               --  associated interface ancestors.
 
-                  Traverse_Interfaces (Etype (Intface), Error);
+               Traverse_Interfaces (Etype (Intface), Error);
 
-                  if Error then
-                     return;
-                  end if;
+               if Error then
+                  return;
+               end if;
 
-                  Next (Intface);
-               end loop;
-            end if;
+               Next (Intface);
+            end loop;
          end Traverse_Interfaces;
 
       --  Start of processing for Try_Class_Wide_Operation


diff --git a/gcc/ada/sem_eval.adb b/gcc/ada/sem_eval.adb
--- a/gcc/ada/sem_eval.adb
+++ b/gcc/ada/sem_eval.adb
@@ -7479,13 +7479,11 @@  package body Sem_Eval is
       procedure Why_Not_Static_List (L : List_Id) is
          N : Node_Id;
       begin
-         if Is_Non_Empty_List (L) then
-            N := First (L);
-            while Present (N) loop
-               Why_Not_Static (N);
-               Next (N);
-            end loop;
-         end if;
+         N := First (L);
+         while Present (N) loop
+            Why_Not_Static (N);
+            Next (N);
+         end loop;
       end Why_Not_Static_List;
 
    --  Start of processing for Why_Not_Static


diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -6046,31 +6046,29 @@  package body Sem_Util is
 
          --  Traverse the graph of ancestor interfaces
 
-         if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
-            Id := First (Abstract_Interface_List (Full_T));
-            while Present (Id) loop
-               Iface := Etype (Id);
-
-               --  Protect against wrong uses. For example:
-               --    type I is interface;
-               --    type O is tagged null record;
-               --    type Wrong is new I and O with null record; -- ERROR
-
-               if Is_Interface (Iface) then
-                  if Exclude_Parents
-                    and then Etype (T) /= T
-                    and then Interface_Present_In_Ancestor (Etype (T), Iface)
-                  then
-                     null;
-                  else
-                     Collect (Iface);
-                     Append_Unique_Elmt (Iface, Ifaces_List);
-                  end if;
+         Id := First (Abstract_Interface_List (Full_T));
+         while Present (Id) loop
+            Iface := Etype (Id);
+
+            --  Protect against wrong uses. For example:
+            --    type I is interface;
+            --    type O is tagged null record;
+            --    type Wrong is new I and O with null record; -- ERROR
+
+            if Is_Interface (Iface) then
+               if Exclude_Parents
+                 and then Etype (T) /= T
+                 and then Interface_Present_In_Ancestor (Etype (T), Iface)
+               then
+                  null;
+               else
+                  Collect (Iface);
+                  Append_Unique_Elmt (Iface, Ifaces_List);
                end if;
+            end if;
 
-               Next (Id);
-            end loop;
-         end if;
+            Next (Id);
+         end loop;
       end Collect;
 
    --  Start of processing for Collect_Interfaces
@@ -13024,18 +13022,15 @@  package body Sem_Util is
       Node : Node_Id;
 
    begin
-      if Is_Non_Empty_List (L) then
-         Node := First (L);
+      Node := First (L);
 
-         loop
-            if Nkind (Node) not in N_Null_Statement | N_Call_Marker then
-               return True;
-            end if;
+      while Present (Node) loop
+         if Nkind (Node) not in N_Null_Statement | N_Call_Marker then
+            return True;
+         end if;
 
-            Next (Node);
-            exit when Node = Empty;
-         end loop;
-      end if;
+         Next (Node);
+      end loop;
 
       return False;
    end Has_Non_Null_Statements;
@@ -13094,28 +13089,26 @@  package body Sem_Util is
       Node : Node_Id;
 
    begin
-      if Is_Non_Empty_List (L) then
-         Node := First (L);
+      Node := First (L);
 
-         loop
-            case Nkind (Node) is
-               when N_Null_Statement | N_Call_Marker | N_Raise_xxx_Error =>
-                  null;
-               when N_Object_Declaration =>
-                  if Present (Expression (Node))
-                    and then not Side_Effect_Free (Expression (Node))
-                  then
-                     return False;
-                  end if;
+      while Present (Node) loop
+         case Nkind (Node) is
+            when N_Null_Statement | N_Call_Marker | N_Raise_xxx_Error =>
+               null;
 
-               when others =>
+            when N_Object_Declaration =>
+               if Present (Expression (Node))
+                 and then not Side_Effect_Free (Expression (Node))
+               then
                   return False;
-            end case;
+               end if;
 
-            Next (Node);
-            exit when Node = Empty;
-         end loop;
-      end if;
+            when others =>
+               return False;
+         end case;
+
+         Next (Node);
+      end loop;
 
       return True;
    end Side_Effect_Free_Statements;