[Ada] Enabling assertions in predefined units

Arnaud Charlet charlet@adacore.com
Mon Jan 20 15:35:00 GMT 2014


Some configuration switches that usually do not apply to predefined units, such
as enabling assertion checking, can be applied to such a unit when they are the
main unit and the switch is provided in the compilation command. When such a
unit is a package body, the switch setting must apply as well to its spec.

The following must execute quietly:

    gcc -c -gnatg s-libm.ads
    gcc -c -gnatg -gnata s-libaux.adb
    gnatmake -q -gnatws missedpost
    missedpost

---
with System.Libm.Aux; use System.Libm.Aux;
with Ada.Assertions; use Ada.Assertions;
procedure Missedpost is

   X    : Float := 0.866;

begin
   P (X);

   if abs X >= 0.5 then
      raise Program_Error;
   end if;
exception
   when Assertion_Error => null;
end Missedpost;
---
package body System.Libm.Aux is

   procedure P (X : in out Float) is
   begin
      X := (0.5 + X) * 0.5;
   end P;
end System.Libm.Aux;
---
package System.Libm.Aux is
   pragma Assertion_Policy (Postcondition => Check);

   procedure P (X : in out Float) with Post => abs X <= 0.5;

end System.Libm.Aux;
---
package System.Libm is
end System.Libm;

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

2014-01-20  Ed Schonberg  <schonberg@adacore.com>

	* sem.adb (Semantics): When saving/restoring configuration
	switches, the spec of a pre- defined unit that is the main unit
	must be treated as a predefined unit as well.

-------------- next part --------------
Index: sem.adb
===================================================================
--- sem.adb	(revision 206804)
+++ sem.adb	(working copy)
@@ -1322,6 +1322,16 @@
       --  If the main unit is generic, every compiled unit, including its
       --  context, is compiled with expansion disabled.
 
+      --  configuration flags have special settings when compiling a predefined
+      --  file as a main unit. This applies to its spec as well.
+
+      Is_Main_Unit : constant Boolean :=
+         Current_Sem_Unit = Main_Unit
+           or else
+             (Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
+               and then Library_Unit (Cunit (Main_Unit)) =
+                  Cunit (Current_Sem_Unit));
+
       Ext_Main_Source_Unit : constant Boolean :=
                                In_Extended_Main_Source_Unit (Comp_Unit);
       --  Determine if unit is in extended main source unit
@@ -1421,7 +1431,7 @@
       Save_Opt_Config_Switches (Save_Config_Switches);
       Set_Opt_Config_Switches
         (Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit)),
-         Current_Sem_Unit = Main_Unit);
+          Is_Main_Unit);
 
       --  Save current non-partition-wide restrictions
 


More information about the Gcc-patches mailing list