This patch fixes a bug where if pragma Assertion_Policy(Ignore) is in
effect, if the only reference to a given declaration is in an Invariant,
spurious warnings about unused entities are given. For example, if a
compilation unit says "with X;", and the only reference to X is in an
invariant, the compiler would warn that the with clause is useless.
The effect of this patch is that we generate invariant-checking
procedures in Assertion_Policy(Ignore) mode, but they are empty, and we
don't call them.
Tested on x86_64-pc-linux-gnu, committed on trunk
2020-06-10 Bob Duff <d...@adacore.com>
gcc/ada/
* sem_prag.adb (Invariant): Remove the pragma removing code. It
doesn't work to remove the pragma, because various flags are set
during Build_Invariant_Procedure_Declaration and
Build_Invariant_Procedure_Body that need to be set to avoid the
spurious warnings.
* exp_util.adb (Make_Invariant_Call): Avoid calling the
invariant-checking procedure if the body is empty. This is an
optimization.
--- gcc/ada/exp_util.adb
+++ gcc/ada/exp_util.adb
@@ -2298,9 +2298,8 @@ package body Exp_Util is
-- Generate:
-- <Comp_Typ>Invariant (_object (<Indices>));
- -- Note that the invariant procedure may have a null body if
- -- assertions are disabled or Assertion_Policy Ignore is in
- -- effect.
+ -- The invariant procedure has a null body if assertions are
+ -- disabled or Assertion_Policy Ignore is in effect.
if not Has_Null_Body (Proc_Id) then
Append_New_To (Comp_Checks,
@@ -9360,19 +9359,22 @@ package body Exp_Util is
function Make_Invariant_Call (Expr : Node_Id) return Node_Id is
Loc : constant Source_Ptr := Sloc (Expr);
Typ : constant Entity_Id := Base_Type (Etype (Expr));
-
- Proc_Id : Entity_Id;
-
- begin
pragma Assert (Has_Invariants (Typ));
-
- Proc_Id := Invariant_Procedure (Typ);
+ Proc_Id : constant Entity_Id := Invariant_Procedure (Typ);
pragma Assert (Present (Proc_Id));
+ begin
+ -- The invariant procedure has a null body if assertions are disabled or
+ -- Assertion_Policy Ignore is in effect. In that case, generate a null
+ -- statement instead of a call to the invariant procedure.
- return
- Make_Procedure_Call_Statement (Loc,
- Name => New_Occurrence_Of (Proc_Id, Loc),
- Parameter_Associations => New_List (Relocate_Node (Expr)));
+ if Has_Null_Body (Proc_Id) then
+ return Make_Null_Statement (Loc);
+ else
+ return
+ Make_Procedure_Call_Statement (Loc,
+ Name => New_Occurrence_Of (Proc_Id, Loc),
+ Parameter_Associations => New_List (Relocate_Node (Expr)));
+ end if;
end Make_Invariant_Call;
------------------------
--- gcc/ada/sem_prag.adb
+++ gcc/ada/sem_prag.adb
@@ -18607,20 +18607,6 @@ package body Sem_Prag is
return;
end if;
- -- If invariants should be ignored, delete the pragma and then
- -- return. We do this here, after checking for errors, and before
- -- generating anything that has a run-time effect.
-
- if Present (Check_Policy_List)
- and then
- (Policy_In_Effect (Name_Invariant) = Name_Ignore
- and then
- Policy_In_Effect (Name_Type_Invariant) = Name_Ignore)
- then
- Rewrite (N, Make_Null_Statement (Loc));
- return;
- end if;
-
-- A pragma that applies to a Ghost entity becomes Ghost for the
-- purposes of legality checks and removal of ignored Ghost code.