]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Elaboration entity must not be ghost in ghost generic instances
authorPiotr Trojanek <trojanek@adacore.com>
Thu, 26 Jun 2025 12:57:14 +0000 (14:57 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 18 Jul 2025 08:29:49 +0000 (10:29 +0200)
For non-instance units GNAT builds elaboration entities before the ghost mode
is inherited from those units. However, for generic instances GNAT was building
elaboration entities with ghost mode inherited from those instances, which
effectively caused elaboration entities to become ghost objects.

This patch add ghost management to routine that builds elaboration entities,
which seems simpler and more robust than adjusting the ghost mode in all
callers of this routine.

gcc/ada/ChangeLog:

* sem_util.adb (Build_Elaboration_Entity): Set ghost mode to none
before creating the elaboration entity; restore the ghost mode
afterwards.

gcc/ada/sem_util.adb

index 74de26a933a556cb8fc88503a4a7b43eafb1930b..40bc6339fd9afd2d57661e7c2352906c65d595be 100644 (file)
@@ -35,6 +35,7 @@ with Exp_Ch11;       use Exp_Ch11;
 with Exp_Util;       use Exp_Util;
 with Fname;          use Fname;
 with Freeze;         use Freeze;
+with Ghost;          use Ghost;
 with Itypes;         use Itypes;
 with Lib;            use Lib;
 with Lib.Xref;       use Lib.Xref;
@@ -1923,6 +1924,10 @@ package body Sem_Util is
    -- Build_Elaboration_Entity --
    ------------------------------
 
+   --  WARNING: This routine manages Ghost regions. Return statements must be
+   --  replaced by gotos which jump to the end of the routine and restore the
+   --  Ghost mode.
+
    procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
       Loc      : constant Source_Ptr := Sloc (N);
       Decl     : Node_Id;
@@ -1956,6 +1961,12 @@ package body Sem_Util is
          end if;
       end Set_Package_Name;
 
+      --  Local variables
+
+      Saved_GM  : constant Ghost_Mode_Type := Ghost_Mode;
+      Saved_IGR : constant Node_Id         := Ignored_Ghost_Region;
+      --  Save the Ghost-related attributes to restore on exit
+
    --  Start of processing for Build_Elaboration_Entity
 
    begin
@@ -2003,6 +2014,11 @@ package body Sem_Util is
          return;
       end if;
 
+      --  Elaboration entity is never a ghost object, regardless of the context
+      --  in which this routine is called.
+
+      Install_Ghost_Region (None, N);
+
       --  Here we need the elaboration entity
 
       --  Construct name of elaboration entity as xxx_E, where xxx is the unit
@@ -2043,6 +2059,8 @@ package body Sem_Util is
 
       Set_Has_Qualified_Name       (Elab_Ent);
       Set_Has_Fully_Qualified_Name (Elab_Ent);
+
+      Restore_Ghost_Region (Saved_GM, Saved_IGR);
    end Build_Elaboration_Entity;
 
    --------------------------------