From patchwork Fri Jan 6 10:34:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 711812 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tw1BN6p3Qz9sxS for ; Fri, 6 Jan 2017 21:34:20 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="UlonrBxX"; dkim-atps=neutral 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=gueJHHzG54eG3jWK5haWkg5+i3zLC7BC+f88OCwR98kZnrsIBV rVMvCHSRh4DwRQMRaP+x7SwpeNRjicINdUraKYnkmXVP/tr+dliMQ5oZXn/BFGC6 8tkjNwhzT5e1hAyF0Cgqc0vmIDwMaK40ivhasBe4HGUUM90efTdioDE90= 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=jolLYHDr5CBR78OPLAMrZvj5fnM=; b=UlonrBxXkjNmI0456cDC 8eIClcDCBUCFKvAKr/0sLzFmlwVYreT4RiXeehpQZOu6a/nBCnHRd7fXnOwE4iiP FPhNx8J0FX/y/C9RTRo+XkfMrGqbZbBeKwjS7Y11xvSFUA5rJZjmXBqTvACqIekM 7cfOfnhs/lS8jrbmdR9AcpA= Received: (qmail 100875 invoked by alias); 6 Jan 2017 10:34:11 -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 100852 invoked by uid 89); 6 Jan 2017 10:34:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=1.6 required=5.0 tests=BAYES_50, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=no version=3.3.2 spammy=Controlled, 244124, Nkind, mx X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 06 Jan 2017 10:34:06 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 34CA01171C9; Fri, 6 Jan 2017 05:34:05 -0500 (EST) 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 a8NWGPGI8ntD; Fri, 6 Jan 2017 05:34:05 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 2453E1171C6; Fri, 6 Jan 2017 05:34:05 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4192) id 22F8C4BA; Fri, 6 Jan 2017 05:34:05 -0500 (EST) Date: Fri, 6 Jan 2017 05:34:05 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Hristian Kirtchev Subject: [Ada] Missing finalization on function result Message-ID: <20170106103405.GA128727@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This patch updates the funalization mechanism to correctly recognize a redefined unary operator which returns an interface class-wide type. Such objects require finalization actions. ------------ -- Source -- ------------ -- types.ads with Ada.Finalization; use Ada.Finalization; package Types is type One is interface; type Int_Access is access Integer; type Managed is new Controlled with record X : Int_Access; end record; overriding procedure Adjust (M : in out Managed); overriding procedure Finalize (M : in out Managed); function Build (I : Integer) return Managed; type Two is new One with record M : Managed := Build (1); end record; end Types; -- types.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Unchecked_Deallocation; package body Types is procedure Free is new Ada.Unchecked_Deallocation (Integer, Int_Access); overriding procedure Adjust (M : in out Managed) is Old_Val : Integer; New_Val : Integer; Val_Ptr : Int_Access renames M.X; begin if Val_Ptr = null then Put_Line ("adj: null"); else Old_Val := Val_Ptr.all; New_Val := Old_Val + 1; Put_Line ("adj:" & Old_Val'Img & " ->" & New_Val'Img); Val_Ptr := new Integer'(New_Val); end if; end Adjust; function Build (I : Integer) return Managed is begin return Managed'(Controlled with X => new Integer'(I)); end Build; overriding procedure Finalize (M : in out Managed) is Val_Ptr : Int_Access renames M.X; begin if Val_Ptr = null then Put_Line ("fin: null"); else Put_Line ("fin:" & Val_Ptr.all'Img); Free (Val_Ptr); end if; end Finalize; end Types; -- leak.adb with Ada.Text_IO; use Ada.Text_IO; with Types; use Types; procedure Leak is function Pass (X : Two'Class) return One'Class is (X); function "not" (X : Two'Class) return One'Class is (X); Obj_1 : Two; begin Obj_1.M := Build (1); Put_Line ("start"); for I in 1 .. 3 loop Put_Line ("spart Pass"); declare Obj_2 : One'Class := Pass (Obj_1); begin null; end; Put_Line ("end Pass"); Put_Line ("start not"); declare Obj_3 : One'Class := not Obj_1; begin null; end; Put_Line ("end not"); end loop; Put_Line ("end"); end Leak; ---------------------------- -- Compilation and output -- ---------------------------- $ gnatmake -q leak.adb -largs -lgmem $ ./leak $ gnatmem ./leak > leaks.txt $ grep -c "Number of non freed allocations" leaks.txt dj: 1 -> 2 fin: 1 adj: 2 -> 3 fin: 2 adj: 1 -> 2 fin: 1 fin: 3 adj: 2 -> 3 fin: 2 start spart Pass adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end Pass start not adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end not spart Pass adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end Pass start not adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end not spart Pass adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end Pass start not adj: 3 -> 4 adj: 4 -> 5 fin: 4 fin: 5 end not end fin: 3 0 Tested on x86_64-pc-linux-gnu, committed on trunk 2017-01-06 Hristian Kirtchev * exp_util.adb (Is_Controlled_Function_Call): Reimplemented. Consider any node which has an entity as the function call may appear in various ways. Index: exp_util.adb =================================================================== --- exp_util.adb (revision 244124) +++ exp_util.adb (working copy) @@ -4912,35 +4912,28 @@ -- Obj.Func (Formal => Actual) N_Function_Call, whose Name is an -- N_Selected_Component - case Nkind (Expr) is - when N_Function_Call => + loop + if Nkind (Expr) = N_Function_Call then Expr := Name (Expr); - -- Check for "Obj.Func (Formal => Actual)" case - - if Nkind (Expr) = N_Selected_Component then - Expr := Selector_Name (Expr); - end if; - -- "Obj.Func (Actual)" case - when N_Indexed_Component => + elsif Nkind (Expr) = N_Indexed_Component then Expr := Prefix (Expr); - if Nkind (Expr) = N_Selected_Component then - Expr := Selector_Name (Expr); - end if; + -- "Obj.Func" or "Obj.Func (Formal => Actual) case - -- "Obj.Func" case - - when N_Selected_Component => + elsif Nkind (Expr) = N_Selected_Component then Expr := Selector_Name (Expr); - when others => null; - end case; + else + exit; + end if; + end loop; return - Nkind_In (Expr, N_Expanded_Name, N_Identifier) + Nkind (Expr) in N_Has_Entity + and then Present (Entity (Expr)) and then Ekind (Entity (Expr)) = E_Function and then Needs_Finalization (Etype (Entity (Expr))); end Is_Controlled_Function_Call;