From patchwork Wed Jun 16 08:43:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1492824 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G4f9Y17MCz9sXL for ; Wed, 16 Jun 2021 18:53:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5BB2839960D3 for ; Wed, 16 Jun 2021 08:53:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTPS id 06F77393D025 for ; Wed, 16 Jun 2021 08:44:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 06F77393D025 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 26C685615B; Wed, 16 Jun 2021 04:43:56 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com 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 hySiktuATN1b; Wed, 16 Jun 2021 04:43:56 -0400 (EDT) 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 0616B56157; Wed, 16 Jun 2021 04:43:56 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 053E8180; Wed, 16 Jun 2021 04:43:56 -0400 (EDT) Date: Wed, 16 Jun 2021 04:43:56 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix aliasing check for actual parameters passed by reference Message-ID: <20210616084355.GA95936@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_ASCII_DIVIDERS, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Piotr Trojanek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The aliasing check applies when some of the formals has their passing mechanism unspecified; RM 6.2 (12/3). Previously it only applied when the first formal had its passing mechanism unspecified and the second had its passing mechanism either unspecified or by-reference. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * checks.adb (Apply_Scalar_Range_Check): Fix handling of check depending on the parameter passing mechanism. Grammar adjustment ("has" => "have"). (Parameter_Passing_Mechanism_Specified): Add a hyphen in a comment. diff --git a/gcc/ada/checks.adb b/gcc/ada/checks.adb --- a/gcc/ada/checks.adb +++ b/gcc/ada/checks.adb @@ -2306,6 +2306,11 @@ package body Checks is is Loc : constant Source_Ptr := Sloc (Call); + function Parameter_Passing_Mechanism_Specified + (Typ : Entity_Id) + return Boolean; + -- Returns True if parameter-passing mechanism is specified for type Typ + function May_Cause_Aliasing (Formal_1 : Entity_Id; Formal_2 : Entity_Id) return Boolean; @@ -2332,6 +2337,19 @@ package body Checks is -- Check contains all and-ed simple tests generated so far or remains -- unchanged in the case of detailed exception messaged. + ------------------------------------------- + -- Parameter_Passing_Mechanism_Specified -- + ------------------------------------------- + + function Parameter_Passing_Mechanism_Specified + (Typ : Entity_Id) + return Boolean + is + begin + return Is_Elementary_Type (Typ) + or else Is_By_Reference_Type (Typ); + end Parameter_Passing_Mechanism_Specified; + ------------------------ -- May_Cause_Aliasing -- ------------------------ @@ -2493,10 +2511,7 @@ package body Checks is -- Elementary types are always passed by value, therefore actuals of -- such types cannot lead to aliasing. An aggregate is an object in -- Ada 2012, but an actual that is an aggregate cannot overlap with - -- another actual. A type that is By_Reference (such as an array of - -- controlled types) is not subject to the check because any update - -- will be done in place and a subsequent read will always see the - -- correct value, see RM 6.2 (12/3). + -- another actual. if Nkind (Orig_Act_1) = N_Aggregate or else (Nkind (Orig_Act_1) = N_Qualified_Expression @@ -2504,10 +2519,7 @@ package body Checks is then null; - elsif Is_Object_Reference (Orig_Act_1) - and then not Is_Elementary_Type (Etype (Orig_Act_1)) - and then not Is_By_Reference_Type (Etype (Orig_Act_1)) - then + elsif Is_Object_Reference (Orig_Act_1) then Actual_2 := Next_Actual (Actual_1); Formal_2 := Next_Formal (Formal_1); while Present (Actual_2) and then Present (Formal_2) loop @@ -2518,18 +2530,28 @@ package body Checks is -- the mode of the two formals may lead to aliasing. if Is_Object_Reference (Orig_Act_2) - and then not Is_Elementary_Type (Etype (Orig_Act_2)) and then May_Cause_Aliasing (Formal_1, Formal_2) then - Remove_Side_Effects (Actual_1); - Remove_Side_Effects (Actual_2); - - Overlap_Check - (Actual_1 => Actual_1, - Actual_2 => Actual_2, - Formal_1 => Formal_1, - Formal_2 => Formal_2, - Check => Check); + + -- The aliasing check only applies when some of the formals + -- have their passing mechanism unspecified; RM 6.2 (12/3). + + if Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_1)) + and then + Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_2)) + then + null; + else + Remove_Side_Effects (Actual_1); + Remove_Side_Effects (Actual_2); + + Overlap_Check + (Actual_1 => Actual_1, + Actual_2 => Actual_2, + Formal_1 => Formal_1, + Formal_2 => Formal_2, + Check => Check); + end if; end if; Next_Actual (Actual_2);