From patchwork Tue Nov 28 09:39:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 170650 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3792634vqx; Tue, 28 Nov 2023 01:40:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGbnE15g1i+jR9yeZV8bggTLfftH2gv37ZTxZYDwAqMJYQCsd1GLVzsScX193zmk8kDkUj1 X-Received: by 2002:a05:622a:647:b0:423:9598:1878 with SMTP id a7-20020a05622a064700b0042395981878mr16833124qtb.25.1701164412967; Tue, 28 Nov 2023 01:40:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701164412; cv=pass; d=google.com; s=arc-20160816; b=SgIX5Oms9+4sX/zmkItxiL+CMfsAlVqxshgMJq37Gs2C1YkJ5Jbqsfnco6ZhMrpbY3 nl00LDbbtYZJQWCFESBwP2hNTB36hyWh1S7eYh5EDeAEN0DQUTy/Cdog8tjDXTcL5nZn 8eMA3AkJ1zGQRWeTr1DlTicGBkehH3qn2O60NcEsZn5vHTo+zR+BN6CbIXTN4PPmR8gZ KcHeQRV0nQDox0/7QTlg5VZPNE/RnXULlS907FYvKBJK+ED+3yjhIEzjaPp9YwvUw6cV 2hbm5US8XA/2SNxzoAEk4XjfITL/KtM7ALnn89yBFRiTm4KiTJI9uDJJLC3KeM3HL46g d4lg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=66Rh2LWDDUvbS9bQGYAh/JwbUxlZ1YkElJDARu1voGY=; fh=hOhsC1930eHatDPYd2EALQ9Ry/g++vvN4vxlQkRuX+0=; b=DmtyaEXDLzvQDdC6Dy9h/BuI2xW7folzkpLb19MFt49NJvHqtKybKJ++Unwo2oMnb1 MDrdjsgwwks8Inx6kkPwGvA6lpsZFvfiwJlvzUaOpIeldFFy91erq9rdq3I8HeoCn/sZ O4xEHNdGuTA9x9d2gN59IesBFnUISnySkDFnqcTOQeFzfjvi0PwoF90hMAQKyWxqvbVP w2VXj4WahBInSM2YhJ4NwMpbZzwoKWPM78zDWuBYogyihVk0/YjezLzHEaHax3iqRx/E BG7HJ2Z0Lg/jn4S4iTd7aluIGiDGd9FX97atCjl7OVrIHSMBgbltK5DcXEN4zf/1zeWL L/Mw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b="k3FsNr/o"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id cc27-20020a05622a411b00b004239a98c2ffsi8875374qtb.604.2023.11.28.01.40.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 01:40:12 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b="k3FsNr/o"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3117B3948A52 for ; Tue, 28 Nov 2023 09:39:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id 329A1394743F for ; Tue, 28 Nov 2023 09:39:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 329A1394743F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 329A1394743F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701164372; cv=none; b=T4/vH4jU4VqxlgOt1MH6HEyP1z68o8SFR+c2n+hr316MuV8q1JUXOgQj1OpBhuniWbCdhpT1EQLYsxasfSm4Do2qB4v0ZANZK9Tz7YwHptcOo9s5WlAaDUNJKeziJ90peCHNMCPM/AH31QGaGry74Ctk1uu82QBudqmAbNLUPDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701164372; c=relaxed/simple; bh=a9BQhnKjGIDrbFXwHu5sdoiB5h+Wun1hKXixMVjJpe0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DB+I5K/pVFZIlRXaae56oQW0Mo3OtqqZxs0TKH6gBjHbNQYPNCLiAebna1DyDpzPy1s6tBGjFN/Ypnoc8R+gqIe3HrCnCNMe7bDyPevSA32htJ0zyy7KutngdbP75SOUTKRGYUYubw8pO15mGdBS3jKjzpq4F3krxG+n70emTYc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3b52360cdf0so2540144b6e.2 for ; Tue, 28 Nov 2023 01:39:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1701164370; x=1701769170; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=66Rh2LWDDUvbS9bQGYAh/JwbUxlZ1YkElJDARu1voGY=; b=k3FsNr/oYqZyXXXkd8zSV4TvsHjvsg9+/+UqFixP6PVClTrxtZwG05UnwshDzIo8jd iE9gceCGdY3F7yEqYcowbc7tyZKKC30di5kCfWt60nfWR7jvClDTwEkaMhgQ/O7xFKqx Yxc8jdbQTPSsCvbkYlfK6mAwQYyI/beXLMJCdQw4NG7cv9piI8cjG9t3V+mZ6qEF4Fpt 3aJ7XP4RdmHNh14adOwqb0SDwC+ohNtNlOQ8tmol0p16t+VNFbhVMyvovI9xYfw3Q0m2 Kzah2NxPE3Z2JaqLW9M5l6ns5euqUPgiSceykMBU6p+Pms9vTTwpaCGx8eMm+NwVLCFw wnfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701164370; x=1701769170; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=66Rh2LWDDUvbS9bQGYAh/JwbUxlZ1YkElJDARu1voGY=; b=VRv9dkglRNYOX1cxo631Huht0FODUNJizCjY+N/oYOvQblTZT8SR//C3G54qxNgWaD GkORpmGq7drFkreRo1re9JxPgfgjk2puZ7GmFdJ3VIHuCDFtexocSHIjVTmgWGru/Fzq UEc0VdiMWiKkmMtaSjwN1UY4FBA8uhXyEWjKe23bw31/EUW+UFp2eFIrEXMduNwhjH8H AQuQvmWP20d4t93PIVesU4iH4L+ig2x8aBatbleZqdDkdZ/s6vMgqW5QQgH0lLHZsMXx FUIs6u+NkrfPagDlU8g7yXMy5Wvm6XKmYRIktaOAtW4FRIXlxmjW3pkUzgfm/2NPKP6Q 2jDg== X-Gm-Message-State: AOJu0Yxm5GRcM3hqmc6kJ40mHQwNCylXfBRUCWWWDn4HxVX64lNxsqAP bPN8uZ1jnhgmnULKtOe0iK0aGCtHWmI9ebAm1VGxOA== X-Received: by 2002:a05:6808:e83:b0:3b2:e9ad:c01f with SMTP id k3-20020a0568080e8300b003b2e9adc01fmr15471079oil.44.1701164370500; Tue, 28 Nov 2023 01:39:30 -0800 (PST) Received: from localhost.localdomain ([2001:861:3382:1a90:a24b:a65f:9576:c701]) by smtp.gmail.com with ESMTPSA id j24-20020aa78d18000000b006b58af8aae3sm8457066pfe.77.2023.11.28.01.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 01:39:30 -0800 (PST) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix premature finalization for nested return within extended one Date: Tue, 28 Nov 2023 10:39:21 +0100 Message-ID: <20231128093921.2970982-1-poulhies@adacore.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783800175905706629 X-GMAIL-MSGID: 1783800175905706629 From: Eric Botcazou The return object is incorrectly finalized when the nested return is taken, because the special flag attached to the return object is not updated. gcc/ada/ * exp_ch6.adb (Build_Flag_For_Function): New function made up of the code building the special flag for return object present... (Expand_N_Extended_Return_Statement): ...in there. Replace the code with a call to Build_Flag_For_Function. Add assertion for the flag. (Expand_Non_Function_Return): For a nested return, if the return object needs finalization actions, update the special flag. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch6.adb | 148 +++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 50 deletions(-) diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index d4802402670..a2b5cdcfa8e 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -194,6 +194,10 @@ package body Exp_Ch6 is -- the activation Chain. Note: Master_Actual can be Empty, but only if -- there are no tasks. + function Build_Flag_For_Function (Func_Id : Entity_Id) return Entity_Id; + -- Generate code to declare a boolean flag initialized to False in the + -- function Func_Id and return the entity for the flag. + function Caller_Known_Size (Func_Call : Node_Id; Result_Subt : Entity_Id) return Boolean; @@ -909,6 +913,53 @@ package body Exp_Ch6 is end if; end BIP_Suffix_Kind; + ----------------------------- + -- Build_Flag_For_Function -- + ----------------------------- + + function Build_Flag_For_Function (Func_Id : Entity_Id) return Entity_Id is + Flag_Decl : Node_Id; + Flag_Id : Entity_Id; + Func_Bod : Node_Id; + Loc : Source_Ptr; + + begin + -- Recover the function body + + Func_Bod := Unit_Declaration_Node (Func_Id); + + if Nkind (Func_Bod) = N_Subprogram_Declaration then + Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod))); + end if; + + if Nkind (Func_Bod) = N_Function_Specification then + Func_Bod := Parent (Func_Bod); -- one more level for child units + end if; + + pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body); + + Loc := Sloc (Func_Bod); + + -- Create a flag to track the function state + + Flag_Id := Make_Temporary (Loc, 'F'); + + -- Insert the flag at the beginning of the function declarations, + -- generate: + -- Fnn : Boolean := False; + + Flag_Decl := + Make_Object_Declaration (Loc, + Defining_Identifier => Flag_Id, + Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), + Expression => New_Occurrence_Of (Standard_False, Loc)); + + Prepend_To (Declarations (Func_Bod), Flag_Decl); + Analyze (Flag_Decl); + + return Flag_Id; + end Build_Flag_For_Function; + --------------------------- -- Build_In_Place_Formal -- --------------------------- @@ -5615,49 +5666,14 @@ package body Exp_Ch6 is -- perform the appropriate cleanup should it fail to return. The state -- of the function itself is tracked through a flag which is coupled -- with the scope finalizer. There is one flag per each return object - -- in case of multiple returns. - - if Needs_Finalization (Etype (Ret_Obj_Id)) then - declare - Flag_Decl : Node_Id; - Flag_Id : Entity_Id; - Func_Bod : Node_Id; - - begin - -- Recover the function body - - Func_Bod := Unit_Declaration_Node (Func_Id); - - if Nkind (Func_Bod) = N_Subprogram_Declaration then - Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod))); - end if; - - if Nkind (Func_Bod) = N_Function_Specification then - Func_Bod := Parent (Func_Bod); -- one more level for child units - end if; - - pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body); - - -- Create a flag to track the function state - - Flag_Id := Make_Temporary (Loc, 'F'); - Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id); + -- in case of multiple extended returns. Note that the flag has already + -- been created if the extended return contains a nested return. - -- Insert the flag at the beginning of the function declarations, - -- generate: - -- Fnn : Boolean := False; - - Flag_Decl := - Make_Object_Declaration (Loc, - Defining_Identifier => Flag_Id, - Object_Definition => - New_Occurrence_Of (Standard_Boolean, Loc), - Expression => - New_Occurrence_Of (Standard_False, Loc)); - - Prepend_To (Declarations (Func_Bod), Flag_Decl); - Analyze (Flag_Decl); - end; + if Needs_Finalization (Etype (Ret_Obj_Id)) + and then No (Status_Flag_Or_Transient_Decl (Ret_Obj_Id)) + then + Set_Status_Flag_Or_Transient_Decl + (Ret_Obj_Id, Build_Flag_For_Function (Func_Id)); end if; -- Build a simple_return_statement that returns the return object when @@ -5722,6 +5738,8 @@ package body Exp_Ch6 is Status_Flag_Or_Transient_Decl (Ret_Obj_Id); begin + pragma Assert (Present (Flag_Id)); + -- Generate: -- Fnn := True; @@ -6387,14 +6405,44 @@ package body Exp_Ch6 is -- return of the previously declared return object. elsif Kind = E_Return_Statement then - Rewrite (N, - Make_Simple_Return_Statement (Loc, - Expression => - New_Occurrence_Of (First_Entity (Scope_Id), Loc))); - Set_Comes_From_Extended_Return_Statement (N); - Set_Return_Statement_Entity (N, Scope_Id); - Expand_Simple_Function_Return (N); - return; + declare + Ret_Obj_Id : constant Entity_Id := First_Entity (Scope_Id); + + Flag_Id : Entity_Id; + + begin + -- Apply the same processing as Expand_N_Extended_Return_Statement + -- if the returned object needs finalization actions. Note that we + -- are invoked before Expand_N_Extended_Return_Statement but there + -- may be multiple nested returns within the extended one. + + if Needs_Finalization (Etype (Ret_Obj_Id)) then + if Present (Status_Flag_Or_Transient_Decl (Ret_Obj_Id)) then + Flag_Id := Status_Flag_Or_Transient_Decl (Ret_Obj_Id); + else + Flag_Id := + Build_Flag_For_Function (Return_Applies_To (Scope_Id)); + Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id); + end if; + + -- Generate: + -- Fnn := True; + + Insert_Action (N, + Make_Assignment_Statement (Loc, + Name => + New_Occurrence_Of (Flag_Id, Loc), + Expression => New_Occurrence_Of (Standard_True, Loc))); + end if; + + Rewrite (N, + Make_Simple_Return_Statement (Loc, + Expression => New_Occurrence_Of (Ret_Obj_Id, Loc))); + Set_Comes_From_Extended_Return_Statement (N); + Set_Return_Statement_Entity (N, Scope_Id); + Expand_Simple_Function_Return (N); + return; + end; end if; pragma Assert (Is_Entry (Scope_Id));