public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] ada: Fix bug in Sem_Util.Enclosing_Declaration
@ 2024-01-09 13:15 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2024-01-09 13:15 UTC (permalink / raw)
  To: gcc-patches; +Cc: Steve Baird

From: Steve Baird <baird@adacore.com>

Fix Sem_Util.Enclosing_Declaration to not return an N_Subprogram_Specification
node. Remove code in various places that was formerly needed to cope with this
misbehavior.

gcc/ada/

	* sem_util.adb (Enclosing_Declaration): Instead of returning a
	subprogram specification node, return its parent (which is
	presumably a subprogram declaration).
	* contracts.adb (Insert_Stable_Property_Check): Remove code
	formerly needed to compensate for incorrect behavior of
	Sem_Util.Enclosing_Declaration.
	* exp_attr.adb (In_Available_Context): Remove code formerly needed
	to compensate for incorrect behavior of
	Sem_Util.Enclosing_Declaration.
	* sem_ch8.adb (Is_Actual_Subp_Of_Inst): Remove code formerly
	needed to compensate for incorrect behavior of
	Sem_Util.Enclosing_Declaration.

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

---
 gcc/ada/contracts.adb | 12 ++----------
 gcc/ada/exp_attr.adb  | 11 +----------
 gcc/ada/sem_ch8.adb   | 10 ----------
 gcc/ada/sem_util.adb  |  5 +++++
 4 files changed, 8 insertions(+), 30 deletions(-)

diff --git a/gcc/ada/contracts.adb b/gcc/ada/contracts.adb
index fa0d59a246a..21bbbf1219e 100644
--- a/gcc/ada/contracts.adb
+++ b/gcc/ada/contracts.adb
@@ -2529,17 +2529,9 @@ package body Contracts is
                 Pragma_Argument_Associations => Args,
                 Class_Present                => Class_Present);
 
-            Subp_Decl : Node_Id := Subp_Id;
+            Subp_Decl : constant Node_Id := Enclosing_Declaration (Subp_Id);
+            pragma Assert (Is_Declaration (Subp_Decl));
          begin
-            --  Enclosing_Declaration may return, for example,
-            --  a N_Procedure_Specification node. Cope with this.
-            loop
-               Subp_Decl := Enclosing_Declaration (Subp_Decl);
-               exit when Is_Declaration (Subp_Decl);
-               Subp_Decl := Parent (Subp_Decl);
-               pragma Assert (Present (Subp_Decl));
-            end loop;
-
             Insert_After_And_Analyze (Subp_Decl, Prag);
          end Insert_Stable_Property_Check;
 
diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb
index 66fd6848a1d..a781f93c4ef 100644
--- a/gcc/ada/exp_attr.adb
+++ b/gcc/ada/exp_attr.adb
@@ -8653,17 +8653,8 @@ package body Exp_Attr is
       --------------------------
 
       function In_Available_Context (Ent : Entity_Id) return Boolean is
-         Decl : Node_Id := Enclosing_Declaration (Ent);
+         Decl : constant Node_Id := Enclosing_Declaration (Ent);
       begin
-         --  Enclosing_Declaration does not always return a declaration;
-         --  cope with this irregularity.
-         if Decl in N_Subprogram_Specification_Id
-           and then Nkind (Parent (Decl)) in
-                      N_Subprogram_Body | N_Subprogram_Declaration
-         then
-            Decl := Parent (Decl);
-         end if;
-
          if Has_Declarations (Parent (Decl)) then
             return In_Subtree (Attr_Ref, Root => Parent (Decl));
          elsif Is_List_Member (Decl) then
diff --git a/gcc/ada/sem_ch8.adb b/gcc/ada/sem_ch8.adb
index 5408be3e1a5..fc2fec5f224 100644
--- a/gcc/ada/sem_ch8.adb
+++ b/gcc/ada/sem_ch8.adb
@@ -6562,16 +6562,6 @@ package body Sem_Ch8 is
 
                      Decl := Enclosing_Declaration (E);
 
-                     --  Enclosing_Declaration does not always return a
-                     --  declaration; cope with this irregularity.
-                     if Decl in N_Subprogram_Specification_Id
-                       and then Nkind (Parent (Decl)) in
-                         N_Subprogram_Body | N_Subprogram_Declaration
-                           | N_Subprogram_Renaming_Declaration
-                     then
-                        Decl := Parent (Decl);
-                     end if;
-
                      --  Look for the suprogram renaming declaration built
                      --  for a generic actual subprogram. Unclear why
                      --  Original_Node call is needed, but sometimes it is.
diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 9cff9e1be16..01be86cb41f 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -7386,6 +7386,11 @@ package body Sem_Util is
          Decl := Parent (Decl);
       end loop;
 
+      --  cope with oddness in definition of N_Declaration
+      if Nkind (Decl) in N_Subprogram_Specification then
+         Decl := Parent (Decl);
+      end if;
+
       return Decl;
    end Enclosing_Declaration;
 
-- 
2.43.0


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

only message in thread, other threads:[~2024-01-09 13:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-09 13:15 [COMMITTED] ada: Fix bug in Sem_Util.Enclosing_Declaration 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).