From patchwork Thu Apr 25 10:11:13 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 239452 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id AF47D2C0108 for ; Thu, 25 Apr 2013 20:11:23 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=uS36qFXxQHateVaC0dqAPsQXI14KRiabaHieQuYG5nH0Vf05Sm vNip9+34d62cqoG9WcIZsP/8VK1bUCNDPEsj9gjqh53ShHazcGNVDeTqY38QbMQB gVEdwW7FT1Vdjg6rJzPwf2YdkOou3cmpuA9ofi1KldJZUpQKsooseeVuA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=OO+s4oIpD89tNs+acPEsK8JA5IQ=; b=y7F2uLNn5kNjQwPghB5H H//nOEArjS6g6ygr3hreIeI/LRzleiI9dhQiM7N7CtSdljIDfnEHVHEu/ccXnty2 eXov2w6xAfmrlVMJcEyNsYrubvMMl5B366SnKLNTRGlyrV9O0Hz4PgSAGxY7E1ea Ws4HDoZFq8NbRpBCDQOwtes= Received: (qmail 5354 invoked by alias); 25 Apr 2013 10:11:16 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 5343 invoked by uid 89); 25 Apr 2013 10:11:16 -0000 X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO, TW_TM autolearn=ham version=3.3.1 Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 25 Apr 2013 10:11:15 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 095D02E890; Thu, 25 Apr 2013 06:11:14 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 5mfpTgnEDI7g; Thu, 25 Apr 2013 06:11:13 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id DE0022E7DC; Thu, 25 Apr 2013 06:11:13 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id DD01D3FF09; Thu, 25 Apr 2013 06:11:13 -0400 (EDT) Date: Thu, 25 Apr 2013 06:11:13 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Hristian Kirtchev Subject: [Ada] Locally enabled invariants are ignored Message-ID: <20130425101113.GA32723@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Found: No This patch modifies the logic that generates procedure _Postconditions to take into account empty invariant procedures and disabled predicates. ------------ -- Source -- ------------ -- main.adb pragma Assertion_Policy (Invariant => Check); procedure Main is X : Integer := 1; type R is new Integer with Predicate => X > 0; package Pack is type T is tagged private; procedure P (Arg1 : in out T; Arg2 : in out R) with Post => X > 0, Post'Class => X > 0; private type T is tagged null record with Invariant => X > 0; end Pack; package body Pack is procedure P (Arg1 : in out T; Arg2 : in out R) is begin null; end P; end Pack; use Pack; Y : T; Z : R := 2; begin X := 0; P (Y, Z); end Main; ---------------------------- -- Compilation and output -- ---------------------------- $ gnatmake -q -gnat12 main.adb $ ./main raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : failed invariant from main.adb:12 Tested on x86_64-pc-linux-gnu, committed on trunk 2013-04-25 Hristian Kirtchev * sem_ch6.adb (Check_Access_Invariants): Test whether an invariant procedure is empty before generating a call to it. (Has_Enabled_Predicate): New routine. (Has_Null_Body): New routine. (Process_PPCs): Test whether an invariant procedure is empty before generating a call to it. Test whether predicates are enabled for a particular type before generating a predicate call. * sem_util.ads, sem_util.adb (Find_Pragma): New routine. Index: sem_util.adb =================================================================== --- sem_util.adb (revision 198237) +++ sem_util.adb (working copy) @@ -4882,6 +4882,26 @@ end if; end Find_Parameter_Type; + ----------------- + -- Find_Pragma -- + ----------------- + + function Find_Pragma (Id : Entity_Id; Name : Name_Id) return Node_Id is + Item : Node_Id; + + begin + Item := First_Rep_Item (Id); + while Present (Item) loop + if Nkind (Item) = N_Pragma and then Pragma_Name (Item) = Name then + return Item; + end if; + + Item := Next_Rep_Item (Item); + end loop; + + return Empty; + end Find_Pragma; + ----------------------------- -- Find_Static_Alternative -- ----------------------------- Index: sem_util.ads =================================================================== --- sem_util.ads (revision 198237) +++ sem_util.ads (working copy) @@ -494,6 +494,11 @@ -- Return the type of formal parameter Param as determined by its -- specification. + function Find_Pragma (Id : Entity_Id; Name : Name_Id) return Node_Id; + -- Given entity Id and pragma name Name, attempt to find the corresponding + -- pragma in Id's chain of representation items. The function returns Empty + -- if no such pragma has been found. + function Find_Static_Alternative (N : Node_Id) return Node_Id; -- N is a case statement whose expression is a compile-time value. -- Determine the alternative chosen, so that the code of non-selected Index: sem_ch6.adb =================================================================== --- sem_ch6.adb (revision 198275) +++ sem_ch6.adb (working copy) @@ -11241,6 +11241,14 @@ -- references to parameters of the inherited subprogram to point to the -- corresponding parameters of the current subprogram. + function Has_Checked_Predicate (Typ : Entity_Id) return Boolean; + -- Determine whether type Typ has or inherits at least one predicate + -- aspect or pragma, for which the applicable policy is Checked. + + function Has_Null_Body (Proc_Id : Entity_Id) return Boolean; + -- Determine whether the body of procedure Proc_Id contains a sole null + -- statement, possibly followed by an optional return. + procedure Insert_After_Last_Declaration (Nod : Node_Id); -- Insert node Nod after the last declaration of the context @@ -11294,6 +11302,7 @@ if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) + and then not Has_Null_Body (Invariant_Procedure (Typ)) and then Is_Public_Subprogram_For (Typ) then Obj := @@ -11886,6 +11895,91 @@ return CP; end Grab_PPC; + --------------------------- + -- Has_Checked_Predicate -- + --------------------------- + + function Has_Checked_Predicate (Typ : Entity_Id) return Boolean is + Anc : Entity_Id; + Pred : Node_Id; + + begin + -- Climb the ancestor type chain staring from the input. This is done + -- because the input type may lack aspect/pragma predicate and simply + -- inherit those from its ancestor. + + Anc := Typ; + while Present (Anc) loop + Pred := Find_Pragma (Anc, Name_Predicate); + + if Present (Pred) and then not Is_Ignored (Pred) then + return True; + end if; + + Anc := Nearest_Ancestor (Anc); + end loop; + + return False; + end Has_Checked_Predicate; + + ------------------- + -- Has_Null_Body -- + ------------------- + + function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is + Body_Id : Entity_Id; + Decl : Node_Id; + Spec : Node_Id; + Stmt1 : Node_Id; + Stmt2 : Node_Id; + + begin + Spec := Parent (Proc_Id); + Decl := Parent (Spec); + + -- Retrieve the entity of the invariant procedure body + + if Nkind (Spec) = N_Procedure_Specification + and then Nkind (Decl) = N_Subprogram_Declaration + then + Body_Id := Corresponding_Body (Decl); + + -- The body acts as a spec + + else + Body_Id := Proc_Id; + end if; + + -- The body will be generated later + + if No (Body_Id) then + return False; + end if; + + Spec := Parent (Body_Id); + Decl := Parent (Spec); + + pragma Assert + (Nkind (Spec) = N_Procedure_Specification + and then Nkind (Decl) = N_Subprogram_Body); + + Stmt1 := First (Statements (Handled_Statement_Sequence (Decl))); + + -- Look for a null statement followed by an optional return statement + + if Nkind (Stmt1) = N_Null_Statement then + Stmt2 := Next (Stmt1); + + if Present (Stmt2) then + return Nkind (Stmt2) = N_Simple_Return_Statement; + else + return True; + end if; + end if; + + return False; + end Has_Null_Body; + ----------------------------------- -- Insert_After_Last_Declaration -- ----------------------------------- @@ -12262,11 +12356,7 @@ -- Add an invariant call to check the result of a function - if Ekind (Designator) /= E_Procedure - and then Expander_Active - -- Check of Assertions_Enabled is certainly wrong ??? - and then Assertions_Enabled - then + if Ekind (Designator) /= E_Procedure and then Expander_Active then Func_Typ := Etype (Designator); Result := Make_Defining_Identifier (Loc, Name_uResult); @@ -12285,6 +12375,7 @@ if Has_Invariants (Func_Typ) and then Present (Invariant_Procedure (Func_Typ)) + and then not Has_Null_Body (Invariant_Procedure (Func_Typ)) and then Is_Public_Subprogram_For (Func_Typ) then Append_Enabled_Item @@ -12305,8 +12396,7 @@ -- this is done for functions as well, since in Ada 2012 they can have -- IN OUT args. - if Expander_Active and then Assertions_Enabled then - -- Check of Assertions_Enabled is certainly wrong ??? + if Expander_Active then Formal := First_Formal (Designator); while Present (Formal) loop if Ekind (Formal) /= E_In_Parameter @@ -12316,6 +12406,7 @@ if Has_Invariants (Formal_Typ) and then Present (Invariant_Procedure (Formal_Typ)) + and then not Has_Null_Body (Invariant_Procedure (Formal_Typ)) and then Is_Public_Subprogram_For (Formal_Typ) then Append_Enabled_Item @@ -12325,7 +12416,10 @@ Check_Access_Invariants (Formal); - if Present (Predicate_Function (Formal_Typ)) then + if Has_Predicates (Formal_Typ) + and then Present (Predicate_Function (Formal_Typ)) + and then Has_Checked_Predicate (Formal_Typ) + then Append_Enabled_Item (Make_Predicate_Check (Formal_Typ, New_Occurrence_Of (Formal, Loc)),