diff mbox

[Ada] Suppress Import-In-Pure-Unit warning if Pure_Function given

Message ID 20150130152545.GA29632@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Jan. 30, 2015, 3:25 p.m. UTC
The warning for use of Import in a Pure unit is refined so that it is
omitted if an explicit Pure_Function aspect is given, as shown by
the following test, compiled with -gnatl

     1. package PureImportF is
     2. pragma Pure (PureImportF);
     3.    function F (A : integer) return integer;
     4.    pragma Import (C, F);
           |
        >>> warning: pragma Import in Pure unit
        >>> warning: calls to "F" may be omitted (RM 10.2.1(18/3))

     5.    function F2 (A : integer) return integer;
     6.    pragma Pure_Function (F2);
     7.    pragma Import (C, F2);
     8. end PureImportF;

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

2015-01-30  Robert Dewar  <dewar@adacore.com>

	* freeze.adb (Freeze_Profile): Add test for suspicious import
	in pure unit.
	* sem_prag.adb (Process_Import_Or_Interface): Test for suspicious
	use in Pure unit is now moved to Freeze (to properly catch
	Pure_Function exemption).
diff mbox

Patch

Index: freeze.adb
===================================================================
--- freeze.adb	(revision 220284)
+++ freeze.adb	(working copy)
@@ -3081,6 +3081,44 @@ 
             end if;
          end if;
 
+         --  Check suspicious use of Import in pure unit
+
+         if Is_Imported (E) and then Is_Pure (Cunit_Entity (Current_Sem_Unit))
+
+           --  Ignore internally generated entity. This happens in some cases
+           --  of subprograms in specs, where we generate an implied body.
+
+           and then Comes_From_Source (Import_Pragma (E))
+
+           --  Assume run-time knows what it is doing
+
+           and then not GNAT_Mode
+
+           --  Assume explicit Pure_Function means import is pure
+
+           and then not Has_Pragma_Pure_Function (E)
+
+           --  Don't need warning in relaxed semantics mode
+
+           and then not Relaxed_RM_Semantics
+
+           --  Assume convention Intrinsic is OK, since this is specialized.
+           --  This deals with the DEC unit current_exception.ads
+
+           and then Convention (E) /= Convention_Intrinsic
+
+            --  Assume that ASM interface knows what it is doing. This deals
+            --  with unsigned.ads in the AAMP back end.
+
+           and then Convention (E) /= Convention_Assembler
+         then
+            Error_Msg_N
+              ("pragma Import in Pure unit??", Import_Pragma (E));
+            Error_Msg_NE
+              ("\calls to & may be omitted (RM 10.2.1(18/3))??",
+               Import_Pragma (E), E);
+         end if;
+
          return True;
       end Freeze_Profile;