]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Do not expand code inside ignored ghost bodies
authorpmderodat <pmderodat@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 11 Dec 2018 11:12:26 +0000 (11:12 +0000)
committerpmderodat <pmderodat@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 11 Dec 2018 11:12:26 +0000 (11:12 +0000)
While ignored ghost code is not compiled into the executable, it may
lead to compilation errors when it makes use of language features
requiring runtime support that is not available in the available runtime
library.  These errors are spurious, as the executable will never call
in these runtime units.

This patch deactivates the expansion of code inside ignored ghost bodies
of subprograms and packages, so that this code is still checked for
possible semantic errors, but it does not force the presence of useless
runtime units.

There is no impact on the executable produced.

2018-12-11  Yannick Moy  <moy@adacore.com>

gcc/ada/

* sem_ch6.adb (Analyze_Subprogram_Body_Helper): Deactivate
expansion in ignored ghost subprogram body.
* sem_ch7.adb (Analyze_Package_Body_Helper): Deactivate
expansion in ignored ghost package body.

gcc/testsuite/

* gnat.dg/ghost4.adb: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@267015 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ada/ChangeLog
gcc/ada/sem_ch6.adb
gcc/ada/sem_ch7.adb
gcc/testsuite/ChangeLog
gcc/testsuite/gnat.dg/ghost4.adb [new file with mode: 0644]

index b16262327d83a247f0afdcb285810bbec9c088e8..fd56c3aae97dd6fbb0edc6117d038d71ab41584e 100644 (file)
@@ -1,3 +1,10 @@
+2018-12-11  Yannick Moy  <moy@adacore.com>
+
+       * sem_ch6.adb (Analyze_Subprogram_Body_Helper): Deactivate
+       expansion in ignored ghost subprogram body.
+       * sem_ch7.adb (Analyze_Package_Body_Helper): Deactivate
+       expansion in ignored ghost package body.
+
 2018-12-11  Ed Schonberg  <schonberg@adacore.com>
 
        * exp_unst.adb (Register_Subprogram): A subprogram whose address
index 32c45614d42aa0b72cdbbc2bddfb689a6a29d90c..c291fb635c382241766d7596f72085f015ed1416 100644 (file)
@@ -3370,6 +3370,7 @@ package body Sem_Ch6 is
 
       Saved_GM   : constant Ghost_Mode_Type := Ghost_Mode;
       Saved_IGR  : constant Node_Id         := Ignored_Ghost_Region;
+      Saved_EA   : constant Boolean         := Expander_Active;
       Saved_ISMP : constant Boolean         :=
                      Ignore_SPARK_Mode_Pragmas_In_Instance;
       --  Save the Ghost and SPARK mode-related data to restore on exit
@@ -3610,6 +3611,18 @@ package body Sem_Ch6 is
          end if;
       end if;
 
+      --  Deactivate expansion inside the body of ignored Ghost entities,
+      --  as this code will ultimately be ignored. This avoids requiring the
+      --  presence of run-time units which are not needed. Only do this for
+      --  user entities, as internally generated entitities might still need
+      --  to be expanded (e.g. those generated for types).
+
+      if Present (Ignored_Ghost_Region)
+        and then Comes_From_Source (Body_Id)
+      then
+         Expander_Active := False;
+      end if;
+
       --  Previously we scanned the body to look for nested subprograms, and
       --  rejected an inline directive if nested subprograms were present,
       --  because the back-end would generate conflicting symbols for the
@@ -4588,6 +4601,10 @@ package body Sem_Ch6 is
       end if;
 
    <<Leave>>
+      if Present (Ignored_Ghost_Region) then
+         Expander_Active := Saved_EA;
+      end if;
+
       Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
       Restore_Ghost_Region (Saved_GM, Saved_IGR);
    end Analyze_Subprogram_Body_Helper;
index 50045170fde9edc6135d85bc956a09c50a0b029e..01c2260885f5cbae766f4c117ceaf653f42d485e 100644 (file)
@@ -669,6 +669,7 @@ package body Sem_Ch7 is
 
       Saved_GM   : constant Ghost_Mode_Type := Ghost_Mode;
       Saved_IGR  : constant Node_Id         := Ignored_Ghost_Region;
+      Saved_EA   : constant Boolean         := Expander_Active;
       Saved_ISMP : constant Boolean         :=
                      Ignore_SPARK_Mode_Pragmas_In_Instance;
       --  Save the Ghost and SPARK mode-related data to restore on exit
@@ -780,6 +781,18 @@ package body Sem_Ch7 is
 
       Mark_And_Set_Ghost_Body (N, Spec_Id);
 
+      --  Deactivate expansion inside the body of ignored Ghost entities,
+      --  as this code will ultimately be ignored. This avoids requiring the
+      --  presence of run-time units which are not needed. Only do this for
+      --  user entities, as internally generated entitities might still need
+      --  to be expanded (e.g. those generated for types).
+
+      if Present (Ignored_Ghost_Region)
+        and then Comes_From_Source (Body_Id)
+      then
+         Expander_Active := False;
+      end if;
+
       --  If the body completes the initial declaration of a compilation unit
       --  which is subject to pragma Elaboration_Checks, set the model of the
       --  pragma because it applies to all parts of the unit.
@@ -1075,6 +1088,10 @@ package body Sem_Ch7 is
          end if;
       end if;
 
+      if Present (Ignored_Ghost_Region) then
+         Expander_Active := Saved_EA;
+      end if;
+
       Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
       Restore_Ghost_Region (Saved_GM, Saved_IGR);
    end Analyze_Package_Body_Helper;
index 61f1e31f9a10554a3540867a2bbeb86678c91f69..b718fed5f17e3656841a316e36be1ab0fc8d7027 100644 (file)
@@ -1,3 +1,7 @@
+2018-12-11  Yannick Moy  <moy@adacore.com>
+
+       * gnat.dg/ghost4.adb: New testcase.
+
 2018-12-11  Ed Schonberg  <schonberg@adacore.com>
 
        * gnat.dg/iter4.adb: New testcase.
diff --git a/gcc/testsuite/gnat.dg/ghost4.adb b/gcc/testsuite/gnat.dg/ghost4.adb
new file mode 100644 (file)
index 0000000..884c1e6
--- /dev/null
@@ -0,0 +1,15 @@
+pragma Restrictions (No_Secondary_Stack);
+
+procedure Ghost4 is
+
+   procedure Dummy with Ghost is
+      function Slice (S : String) return String is
+         (S (S'First .. S'First + 3));
+
+      X : String := Slice ("hello");
+   begin
+         null;
+   end Dummy;
+begin
+   Dummy;
+end Ghost4;