diff mbox series

[Ada] Prevent crash on inspection point for unfrozen entity

Message ID 20210708135010.GA2465658@adacore.com
State New
Headers show
Series [Ada] Prevent crash on inspection point for unfrozen entity | expand

Commit Message

Pierre-Marie de Rodat July 8, 2021, 1:50 p.m. UTC
Before this patch, the following program would make GNAT crash:

procedure P is
    Unused_Var : Integer with Shared => False;
    pragma Inspection_Point;
begin
    null;
end tmp;

This was because the Shared aspect resulted in a freeze node being
inserted after the Inspection_Point pragma. This made Gigi delay the
translation of the declaration of Unused_Var to the freeze node.
This delaying resulted in a reference to an undeclared entity when
trying to translate Inspection_Point from gnat to gnu.

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

gcc/ada/

	* exp_prag.adb (Expand_Pragma_Inspection_Point): After expansion
	of the Inspection_Point pragma, check if referenced entities
	that have a freeze node are already frozen. If they aren't, emit
	a warning and turn the pragma into a no-op.
diff mbox series

Patch

diff --git a/gcc/ada/exp_prag.adb b/gcc/ada/exp_prag.adb
--- a/gcc/ada/exp_prag.adb
+++ b/gcc/ada/exp_prag.adb
@@ -2361,6 +2361,7 @@  package body Exp_Prag is
       S     : Entity_Id;
       E     : Entity_Id;
 
+      Remove_Inspection_Point : Boolean := False;
    begin
       if No (Pragma_Argument_Associations (N)) then
          A := New_List;
@@ -2400,6 +2401,36 @@  package body Exp_Prag is
          Expand (Expression (Assoc));
          Next (Assoc);
       end loop;
+
+      --  If any of the references have a freeze node, it must appear before
+      --  pragma Inspection_Point, otherwise the entity won't be available when
+      --  Gigi processes Inspection_Point.
+      --  When this requirement isn't met, turn the pragma into a no-op.
+
+      Assoc := First (Pragma_Argument_Associations (N));
+      while Present (Assoc) loop
+
+         if Present (Freeze_Node (Entity (Expression (Assoc)))) and then
+           not Is_Frozen (Entity (Expression (Assoc)))
+         then
+            Error_Msg_NE ("?inspection point references unfrozen object &",
+              Assoc,
+              Entity (Expression (Assoc)));
+            Remove_Inspection_Point := True;
+         end if;
+
+         Next (Assoc);
+      end loop;
+
+      if Remove_Inspection_Point then
+         Error_Msg_N ("\pragma will be ignored", N);
+
+         --  We can't just remove the pragma from the tree as it might be
+         --  iterated over by the caller. Turn it into a null statement
+         --  instead.
+
+         Rewrite (N, Make_Null_Statement (Sloc (N)));
+      end if;
    end Expand_Pragma_Inspection_Point;
 
    --------------------------------------