public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] ada: Cleanup expansion of locally handled exception handlers
@ 2023-06-13  7:37 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2023-06-13  7:37 UTC (permalink / raw)
  To: gcc-patches; +Cc: Piotr Trojanek

From: Piotr Trojanek <trojanek@adacore.com>

Code cleanup related to handling exceptions in GNATprove; semantics is
unaffected.

gcc/ada/

	* exp_ch11.ads (Find_Local_Handler): Fix typo in comment.
	* exp_ch11.adb (Find_Local_Handler): Remove redundant check for the
	Exception_Handler list being present; use membership test to eliminate
	local object LCN; fold nested IF statements. Remove useless ELSIF
	condition.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch11.adb | 124 +++++++++++++++++++------------------------
 gcc/ada/exp_ch11.ads |   2 +-
 2 files changed, 55 insertions(+), 71 deletions(-)

diff --git a/gcc/ada/exp_ch11.adb b/gcc/ada/exp_ch11.adb
index 753412eab16..da02eb9bfb2 100644
--- a/gcc/ada/exp_ch11.adb
+++ b/gcc/ada/exp_ch11.adb
@@ -1803,95 +1803,79 @@ package body Exp_Ch11 is
          --  Test for handled sequence of statements with at least one
          --  exception handler which might be the one we are looking for.
 
-         elsif Nkind (P) = N_Handled_Sequence_Of_Statements
-           and then Present (Exception_Handlers (P))
-         then
-            --  Before we proceed we need to check if the node N is covered
-            --  by the statement part of P rather than one of its exception
-            --  handlers (an exception handler obviously does not cover its
-            --  own statements).
-
-            --  This test is more delicate than might be thought. It is not
-            --  just a matter of checking the Statements (P), because the node
-            --  might be waiting to be wrapped in a transient scope, in which
-            --  case it will end up in the block statements, even though it
-            --  is not there now.
-
-            if Is_List_Member (N) then
-               declare
-                  LCN : constant List_Id := List_Containing (N);
+         --  We need to check if the node N is covered by the statement part of
+         --  P rather than one of its exception handlers (an exception handler
+         --  obviously does not cover its own statements).
 
-               begin
-                  if LCN = Statements (P)
-                       or else
-                     LCN = SSE.Actions_To_Be_Wrapped (Before)
-                       or else
-                     LCN = SSE.Actions_To_Be_Wrapped (After)
-                       or else
-                     LCN = SSE.Actions_To_Be_Wrapped (Cleanup)
-                  then
-                     --  Loop through exception handlers
+         --  This test is more delicate than might be thought. It is not just
+         --  a matter of checking the Statements (P), because the node might be
+         --  waiting to be wrapped in a transient scope, in which case it will
+         --  end up in the block statements, even though it is not there now.
 
-                     H := First (Exception_Handlers (P));
-                     while Present (H) loop
+         elsif Nkind (P) = N_Handled_Sequence_Of_Statements
+           and then Is_List_Member (N)
+           and then List_Containing (N) in Statements (P)
+                                         | SSE.Actions_To_Be_Wrapped (Before)
+                                         | SSE.Actions_To_Be_Wrapped (After)
+                                         | SSE.Actions_To_Be_Wrapped (Cleanup)
+         then
+            --  Loop through exception handlers
 
-                        --  Guard against other constructs appearing in the
-                        --  list of exception handlers.
+            H := First (Exception_Handlers (P));
+            while Present (H) loop
 
-                        if Nkind (H) = N_Exception_Handler then
+               --  Guard against other constructs appearing in the list of
+               --  exception handlers.
 
-                           --  Loop through choices in one handler
+               if Nkind (H) = N_Exception_Handler then
 
-                           C := First (Exception_Choices (H));
-                           while Present (C) loop
+                  --  Loop through choices in one handler
 
-                              --  Deal with others case
+                  C := First (Exception_Choices (H));
+                  while Present (C) loop
 
-                              if Nkind (C) = N_Others_Choice then
+                     --  Deal with others case
 
-                                 --  Matching others handler, but we need
-                                 --  to ensure there is no choice parameter.
-                                 --  If there is, then we don't have a local
-                                 --  handler after all (since we do not allow
-                                 --  choice parameters for local handlers).
+                     if Nkind (C) = N_Others_Choice then
 
-                                 if No (Choice_Parameter (H)) then
-                                    return H;
-                                 else
-                                    return Empty;
-                                 end if;
+                        --  Matching others handler, but we need to ensure
+                        --  there is no choice parameter. If there is, then we
+                        --  don't have a local handler after all (since we do
+                        --  not allow choice parameters for local handlers).
 
-                                 --  If not others must be entity name
+                        if No (Choice_Parameter (H)) then
+                           return H;
+                        else
+                           return Empty;
+                        end if;
 
-                              elsif Nkind (C) /= N_Others_Choice then
-                                 pragma Assert (Is_Entity_Name (C));
-                                 pragma Assert (Present (Entity (C)));
+                     --  If not others must be entity name
 
-                                 --  Get exception being handled, dealing with
-                                 --  renaming.
+                     else
+                        pragma Assert (Is_Entity_Name (C));
+                        pragma Assert (Present (Entity (C)));
 
-                                 EHandle := Get_Renamed_Entity (Entity (C));
+                        --  Get exception being handled, dealing with renaming
 
-                                 --  If match, then check choice parameter
+                        EHandle := Get_Renamed_Entity (Entity (C));
 
-                                 if ERaise = EHandle then
-                                    if No (Choice_Parameter (H)) then
-                                       return H;
-                                    else
-                                       return Empty;
-                                    end if;
-                                 end if;
-                              end if;
+                        --  If match, then check choice parameter
 
-                              Next (C);
-                           end loop;
+                        if ERaise = EHandle then
+                           if No (Choice_Parameter (H)) then
+                              return H;
+                           else
+                              return Empty;
+                           end if;
                         end if;
+                     end if;
 
-                        Next (H);
-                     end loop;
-                  end if;
-               end;
-            end if;
+                     Next (C);
+                  end loop;
+               end if;
+
+               Next (H);
+            end loop;
          end if;
 
          N := P;
diff --git a/gcc/ada/exp_ch11.ads b/gcc/ada/exp_ch11.ads
index 483c7591038..8d5b998eeb3 100644
--- a/gcc/ada/exp_ch11.ads
+++ b/gcc/ada/exp_ch11.ads
@@ -59,7 +59,7 @@ package Exp_Ch11 is
      (Ename : Entity_Id;
       Nod   : Node_Id) return Node_Id;
    --  This function searches for a local exception handler that will handle
-   --  the exception named by Ename. If such a local hander exists, then the
+   --  the exception named by Ename. If such a local handler exists, then the
    --  corresponding N_Exception_Handler is returned. If no such handler is
    --  found then Empty is returned. In order to match and return True, the
    --  handler may not have a choice parameter specification. Nod is the raise
-- 
2.40.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-13  7:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-13  7:37 [COMMITTED] ada: Cleanup expansion of locally handled exception handlers Marc Poulhiès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).