diff mbox

[Ada] Aliasing checks on subprograms may cause side effects

Message ID 20170123132524.GA21464@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Jan. 23, 2017, 1:25 p.m. UTC
Compiling with aliasing checks on subprograms parameters (-gnateA),
if the actual of a subprogram parameter is a function call the
compiler may generate a runtime check that invokes the function
more than once.

After this patch the following test compiles and executes well.

with Ada.Text_IO; use Ada.Text_IO;
procedure Do_Test is
   Call_Counter : Natural := 0;

   function Get_Value return String is
   begin
      Call_Counter := Call_Counter + 1;

      if Call_Counter = 1 then
         return "OK";
      else
         return "Failure";
      end if;
   end Get_Value;

   procedure Do_Test (S1 : String; S2 : in out String) is
   begin
      Put_Line (S1);
   end Do_Test;
 
   Dummy : String := ":";
begin
   Do_Test (Get_Value, Dummy);
end;

Command: gnatmake -gnateA do_test.adb; ./do_test
 Output: OK

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

2017-01-23  Javier Miranda  <miranda@adacore.com>

	* checks.adb (Apply_Parameter_Aliasing_Checks):
	Remove side effects of the actuals before generating the overlap
	check.
diff mbox

Patch

Index: checks.adb
===================================================================
--- checks.adb	(revision 244792)
+++ checks.adb	(working copy)
@@ -2360,6 +2360,9 @@ 
                  and then not Is_Elementary_Type (Etype (Orig_Act_2))
                  and then May_Cause_Aliasing (Formal_1, Formal_2)
                then
+                  Remove_Side_Effects (Actual_1);
+                  Remove_Side_Effects (Actual_2);
+
                   Overlap_Check
                     (Actual_1 => Actual_1,
                      Actual_2 => Actual_2,