https://gcc.gnu.org/g:02c6ea423525f1953d1c97bcaf29a4569bcf0edb

commit r16-2336-g02c6ea423525f1953d1c97bcaf29a4569bcf0edb
Author: Piotr Trojanek <troja...@adacore.com>
Date:   Thu Jun 26 14:57:14 2025 +0200

    ada: Elaboration entity must not be ghost in ghost generic instances
    
    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.

Diff:
---
 gcc/ada/sem_util.adb | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 74de26a933a5..40bc6339fd9a 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -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;
 
    --------------------------------

Reply via email to