From 3ffe57d4b11ca8daab277d94c86db2b95feafa9d Mon Sep 17 00:00:00 2001 From: Justin Squirek Date: Wed, 23 Dec 2020 13:06:22 -0500 Subject: [Ada] Expansion in _postconditions confusing CodePeer gcc/ada/ * contracts.adb (Build_Postconditions_Procedure): Remove internally generated if statement used to control finalization actions. * exp_ch6.adb (Add_Return, Expand_Non_Function_Return, Expand_Simple_Function_Return): Add if statement around _postconditions to control finalization. * exp_ch7.adb (Build_Finalizer): Likewise. * sem_prag.adb (Find_Related_Declaration_Or_Body): Add case to handle Context itself being a handled sequence of statements. --- gcc/ada/exp_ch6.adb | 62 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'gcc/ada/exp_ch6.adb') diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index 6b14656..cc6c177 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -6246,7 +6246,8 @@ package body Exp_Ch6 is -- has contract assertions that need to be verified on exit. -- Also, mark the successful return to signal that postconditions - -- need to be evaluated when finalization occurs. + -- need to be evaluated when finalization occurs by setting + -- Return_Success_For_Postcond to be True. if Ekind (Spec_Id) = E_Procedure and then Present (Postconditions_Proc (Spec_Id)) @@ -6254,19 +6255,30 @@ package body Exp_Ch6 is -- Generate: -- -- Return_Success_For_Postcond := True; - -- _postconditions; + -- if Postcond_Enabled then + -- _postconditions; + -- end if; Insert_Action (Stmt, Make_Assignment_Statement (Loc, Name => New_Occurrence_Of - (Get_Return_Success_For_Postcond (Spec_Id), Loc), + (Get_Return_Success_For_Postcond (Spec_Id), Loc), Expression => New_Occurrence_Of (Standard_True, Loc))); + -- Wrap the call to _postconditions within a test of the + -- Postcond_Enabled flag to delay postcondition evaluation + -- until after finalization when required. + Insert_Action (Stmt, - Make_Procedure_Call_Statement (Loc, - Name => - New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc))); + Make_If_Statement (Loc, + Condition => + New_Occurrence_Of (Get_Postcond_Enabled (Spec_Id), Loc), + Then_Statements => New_List ( + Make_Procedure_Call_Statement (Loc, + Name => + New_Occurrence_Of + (Postconditions_Proc (Spec_Id), Loc))))); end if; -- Ada 2020 (AI12-0279): append the call to 'Yield unless this is @@ -6699,7 +6711,9 @@ package body Exp_Ch6 is -- Generate: -- -- Return_Success_For_Postcond := True; - -- _postconditions; + -- if Postcond_Enabled then + -- _postconditions; + -- end if; Insert_Action (N, Make_Assignment_Statement (Loc, @@ -6708,9 +6722,19 @@ package body Exp_Ch6 is (Get_Return_Success_For_Postcond (Scope_Id), Loc), Expression => New_Occurrence_Of (Standard_True, Loc))); + -- Wrap the call to _postconditions within a test of the + -- Postcond_Enabled flag to delay postcondition evaluation until + -- after finalization when required. + Insert_Action (N, - Make_Procedure_Call_Statement (Loc, - Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc))); + Make_If_Statement (Loc, + Condition => + New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc), + Then_Statements => New_List ( + Make_Procedure_Call_Statement (Loc, + Name => + New_Occurrence_Of + (Postconditions_Proc (Scope_Id), Loc))))); end if; -- Ada 2020 (AI12-0279) @@ -7621,6 +7645,9 @@ package body Exp_Ch6 is -- Generate: -- -- Return_Success_For_Postcond := True; + -- if Postcond_Enabled then + -- _Postconditions ([exp]); + -- end if; Insert_Action (Exp, Make_Assignment_Statement (Loc, @@ -7629,13 +7656,20 @@ package body Exp_Ch6 is (Get_Return_Success_For_Postcond (Scope_Id), Loc), Expression => New_Occurrence_Of (Standard_True, Loc))); - -- Generate call to _Postconditions + -- Wrap the call to _postconditions within a test of the + -- Postcond_Enabled flag to delay postcondition evaluation until + -- after finalization when required. Insert_Action (Exp, - Make_Procedure_Call_Statement (Loc, - Name => - New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc), - Parameter_Associations => New_List (New_Copy_Tree (Exp)))); + Make_If_Statement (Loc, + Condition => + New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc), + Then_Statements => New_List ( + Make_Procedure_Call_Statement (Loc, + Name => + New_Occurrence_Of + (Postconditions_Proc (Scope_Id), Loc), + Parameter_Associations => New_List (New_Copy_Tree (Exp)))))); end if; -- Ada 2005 (AI-251): If this return statement corresponds with an -- cgit v1.1