[Ada] Cleanup unnecessary declare block in Check_Unreachable_Code

Message ID 20220510082104.GA3029167@adacore.com
State Committed
Headers
Series [Ada] Cleanup unnecessary declare block in Check_Unreachable_Code |

Commit Message

Pierre-Marie de Rodat May 10, 2022, 8:21 a.m. UTC
  Cleanup related to static detection of references to uninitialized
variables. Semantics is unaffected.

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

gcc/ada/

	* sem_ch5.adb (Check_Unreachable_Code): Remove inner declare
	block; refill code and comments.
  

Patch

diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb
--- a/gcc/ada/sem_ch5.adb
+++ b/gcc/ada/sem_ch5.adb
@@ -4397,149 +4397,145 @@  package body Sem_Ch5 is
 
    procedure Check_Unreachable_Code (N : Node_Id) is
       Error_Node : Node_Id;
+      Nxt        : Node_Id;
       P          : Node_Id;
 
    begin
       if Is_List_Member (N) and then Comes_From_Source (N) then
-         declare
-            Nxt : Node_Id;
+         Nxt := Original_Node (Next (N));
 
-         begin
-            Nxt := Original_Node (Next (N));
+         --  Skip past pragmas
 
-            --  Skip past pragmas
+         while Nkind (Nxt) = N_Pragma loop
+            Nxt := Original_Node (Next (Nxt));
+         end loop;
 
-            while Nkind (Nxt) = N_Pragma loop
-               Nxt := Original_Node (Next (Nxt));
-            end loop;
+         --  If a label follows us, then we never have dead code, since someone
+         --  could branch to the label, so we just ignore it.
 
-            --  If a label follows us, then we never have dead code, since
-            --  someone could branch to the label, so we just ignore it.
+         if Nkind (Nxt) = N_Label then
+            return;
 
-            if Nkind (Nxt) = N_Label then
-               return;
+         --  Otherwise see if we have a real statement following us
 
-            --  Otherwise see if we have a real statement following us
+         elsif Present (Nxt)
+           and then Comes_From_Source (Nxt)
+           and then Is_Statement (Nxt)
+         then
+            --  Special very annoying exception. If we have a return that
+            --  follows a raise, then we allow it without a warning, since
+            --  the Ada RM annoyingly requires a useless return here.
 
-            elsif Present (Nxt)
-              and then Comes_From_Source (Nxt)
-              and then Is_Statement (Nxt)
+            if Nkind (Original_Node (N)) /= N_Raise_Statement
+              or else Nkind (Nxt) /= N_Simple_Return_Statement
             then
-               --  Special very annoying exception. If we have a return that
-               --  follows a raise, then we allow it without a warning, since
-               --  the Ada RM annoyingly requires a useless return here.
-
-               if Nkind (Original_Node (N)) /= N_Raise_Statement
-                 or else Nkind (Nxt) /= N_Simple_Return_Statement
-               then
-                  --  The rather strange shenanigans with the warning message
-                  --  here reflects the fact that Kill_Dead_Code is very good
-                  --  at removing warnings in deleted code, and this is one
-                  --  warning we would prefer NOT to have removed.
-
-                  Error_Node := Nxt;
+               --  The rather strange shenanigans with the warning message
+               --  here reflects the fact that Kill_Dead_Code is very good at
+               --  removing warnings in deleted code, and this is one warning
+               --  we would prefer NOT to have removed.
 
-                  --  If we have unreachable code, analyze and remove the
-                  --  unreachable code, since it is useless and we don't
-                  --  want to generate junk warnings.
+               Error_Node := Nxt;
 
-                  --  We skip this step if we are not in code generation mode
-                  --  or CodePeer mode.
+               --  If we have unreachable code, analyze and remove the
+               --  unreachable code, since it is useless and we don't want
+               --  to generate junk warnings.
 
-                  --  This is the one case where we remove dead code in the
-                  --  semantics as opposed to the expander, and we do not want
-                  --  to remove code if we are not in code generation mode,
-                  --  since this messes up the tree or loses useful information
-                  --  for CodePeer.
+               --  We skip this step if we are not in code generation mode
+               --  or CodePeer mode.
 
-                  --  Note that one might react by moving the whole circuit to
-                  --  exp_ch5, but then we lose the warning in -gnatc mode.
+               --  This is the one case where we remove dead code in the
+               --  semantics as opposed to the expander, and we do not want
+               --  to remove code if we are not in code generation mode, since
+               --  this messes up the tree or loses useful information for
+               --  CodePeer.
 
-                  if Operating_Mode = Generate_Code
-                    and then not CodePeer_Mode
-                  then
-                     loop
-                        Nxt := Next (N);
+               --  Note that one might react by moving the whole circuit to
+               --  exp_ch5, but then we lose the warning in -gnatc mode.
 
-                        --  Quit deleting when we have nothing more to delete
-                        --  or if we hit a label (since someone could transfer
-                        --  control to a label, so we should not delete it).
+               if Operating_Mode = Generate_Code
+                 and then not CodePeer_Mode
+               then
+                  loop
+                     Nxt := Next (N);
 
-                        exit when No (Nxt) or else Nkind (Nxt) = N_Label;
+                     --  Quit deleting when we have nothing more to delete
+                     --  or if we hit a label (since someone could transfer
+                     --  control to a label, so we should not delete it).
 
-                        --  Statement/declaration is to be deleted
+                     exit when No (Nxt) or else Nkind (Nxt) = N_Label;
 
-                        Analyze (Nxt);
-                        Remove (Nxt);
-                        Kill_Dead_Code (Nxt);
-                     end loop;
-                  end if;
+                     --  Statement/declaration is to be deleted
 
-                  Error_Msg
-                    ("??unreachable code!", Sloc (Error_Node), Error_Node);
+                     Analyze (Nxt);
+                     Remove (Nxt);
+                     Kill_Dead_Code (Nxt);
+                  end loop;
                end if;
 
-            --  If the unconditional transfer of control instruction is the
-            --  last statement of a sequence, then see if our parent is one of
-            --  the constructs for which we count unblocked exits, and if so,
-            --  adjust the count.
-
-            else
-               P := Parent (N);
+               Error_Msg
+                 ("??unreachable code!", Sloc (Error_Node), Error_Node);
+            end if;
 
-               --  Statements in THEN part or ELSE part of IF statement
+         --  If the unconditional transfer of control instruction is the
+         --  last statement of a sequence, then see if our parent is one of
+         --  the constructs for which we count unblocked exits, and if so,
+         --  adjust the count.
 
-               if Nkind (P) = N_If_Statement then
-                  null;
+         else
+            P := Parent (N);
 
-               --  Statements in ELSIF part of an IF statement
+            --  Statements in THEN part or ELSE part of IF statement
 
-               elsif Nkind (P) = N_Elsif_Part then
-                  P := Parent (P);
-                  pragma Assert (Nkind (P) = N_If_Statement);
+            if Nkind (P) = N_If_Statement then
+               null;
 
-               --  Statements in CASE statement alternative
+            --  Statements in ELSIF part of an IF statement
 
-               elsif Nkind (P) = N_Case_Statement_Alternative then
-                  P := Parent (P);
-                  pragma Assert (Nkind (P) = N_Case_Statement);
+            elsif Nkind (P) = N_Elsif_Part then
+               P := Parent (P);
+               pragma Assert (Nkind (P) = N_If_Statement);
 
-               --  Statements in body of block
+            --  Statements in CASE statement alternative
 
-               elsif Nkind (P) = N_Handled_Sequence_Of_Statements
-                 and then Nkind (Parent (P)) = N_Block_Statement
-               then
-                  --  The original loop is now placed inside a block statement
-                  --  due to the expansion of attribute 'Loop_Entry. Return as
-                  --  this is not a "real" block for the purposes of exit
-                  --  counting.
+            elsif Nkind (P) = N_Case_Statement_Alternative then
+               P := Parent (P);
+               pragma Assert (Nkind (P) = N_Case_Statement);
 
-                  if Nkind (N) = N_Loop_Statement
-                    and then Subject_To_Loop_Entry_Attributes (N)
-                  then
-                     return;
-                  end if;
+            --  Statements in body of block
 
-               --  Statements in exception handler in a block
+            elsif Nkind (P) = N_Handled_Sequence_Of_Statements
+              and then Nkind (Parent (P)) = N_Block_Statement
+            then
+               --  The original loop is now placed inside a block statement
+               --  due to the expansion of attribute 'Loop_Entry. Return as
+               --  this is not a "real" block for the purposes of exit
+               --  counting.
 
-               elsif Nkind (P) = N_Exception_Handler
-                 and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
-                 and then Nkind (Parent (Parent (P))) = N_Block_Statement
+               if Nkind (N) = N_Loop_Statement
+                 and then Subject_To_Loop_Entry_Attributes (N)
                then
-                  null;
-
-               --  None of these cases, so return
-
-               else
                   return;
                end if;
 
-               --  This was one of the cases we are looking for (i.e. the
-               --  parent construct was IF, CASE or block) so decrement count.
+            --  Statements in exception handler in a block
 
-               Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
+            elsif Nkind (P) = N_Exception_Handler
+              and then Nkind (Parent (P)) = N_Handled_Sequence_Of_Statements
+              and then Nkind (Parent (Parent (P))) = N_Block_Statement
+            then
+               null;
+
+            --  None of these cases, so return
+
+            else
+               return;
             end if;
-         end;
+
+            --  This was one of the cases we are looking for (i.e. the parent
+            --  construct was IF, CASE or block) so decrement count.
+
+            Unblocked_Exit_Count := Unblocked_Exit_Count - 1;
+         end if;
       end if;
    end Check_Unreachable_Code;