From patchwork Mon May 22 08:49:33 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: 97185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1301336vqo; Mon, 22 May 2023 01:56:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ttoWsqKMRKZveFU1iTaFIdVyCvMiBp1U5iox0IEOLK3RFl9xFYf3/cE9QCH8Pa647XfnD X-Received: by 2002:aa7:d045:0:b0:510:86cc:f687 with SMTP id n5-20020aa7d045000000b0051086ccf687mr7924560edo.20.1684745772838; Mon, 22 May 2023 01:56:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684745772; cv=none; d=google.com; s=arc-20160816; b=aBEIjnbCt57tMX3dJIKYw9aIyKUOiZem5+zvpZDoPFL3vn05otgvNq5A3lQ7MCdnzS ZJT7rc/cb82ce8+Kv+c6/378Ij7CUhQYsMvWUzzmiQRglRNSVewoJtku40QTKVVxE+Gn /bwlbMBDXxuI7kVQvsewmeaAvbt/mSwBgRH2HQcfofZ1QpWK/NpgfDGk3gPrs4ToVO+r oaJ09Yu4SMsEl/y3EnykHPd/+0qPIXApianOFFN9ll6SpSTA4NFxvi8J4S1rdK2m8MYk M8cZgNaqhRofsQR6553HysDR+Op7iJ89QU4pthN3M9TXrISdi8cWMTuAQoCYy0aUeKHR iCag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=kS1pQrSzZ/mjzdNpejvtn/UUQsaU/4hu/4eDmOrV1X8=; b=CBk/W9oyqwN83gv5TwxvjFGhrmT5h0icIgFcwOVdnfbS/EDRqba1LpRwU2bh9RAObk iBDhwRkEs4vHFZkehGbAa+mDSmvA18/ot+Bp0MwrSCtHP8+qLJsGsg8YRjmSAHs2em2N kMje+wHprKQDSwhUbYIEM/h+7TctiWfTGZJJ+A8Mqy86goorRnyRWtJGxXCfMq+7FIYQ e5wpuZsSQHAdEk21jGU9MLEKrnUdG7qqikG2jKZ3789jxaFPBncM6L1lyMff1O7EyOUW R27Z3gP2+og1p98qNjMki6yL1YHpVPCg6FiDcdyLNjJMePPny9vJUSPqj9Ob+BsI2/Cy p7bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=bezJ35fR; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i21-20020aa7c715000000b0051085449b4fsi2705928edq.450.2023.05.22.01.56.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:56:12 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=bezJ35fR; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BE71D382E6A5 for ; Mon, 22 May 2023 08:52:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE71D382E6A5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684745555; bh=kS1pQrSzZ/mjzdNpejvtn/UUQsaU/4hu/4eDmOrV1X8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=bezJ35fR++OKHRT2l9jmN99/TAGiZaDRJGBqnOPgPhoneCr8rYuBR3vRhjsvwHqvU IRatPG5mUDbUlFYR1GwpaMYVT2OhMDwB0rfm7Q986vqgxRd+g0IvCVRuxcdv60+O5P hVMrA7wggX4f1dita4eUcCRKKg4EyjLkQbv+SgBs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 1862B3857728 for ; Mon, 22 May 2023 08:49:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1862B3857728 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-30a95ec7744so222168f8f.3 for ; Mon, 22 May 2023 01:49:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684745376; x=1687337376; 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=kS1pQrSzZ/mjzdNpejvtn/UUQsaU/4hu/4eDmOrV1X8=; b=Z27sbZUwtDt6juaavEtceWzy5054Ux0rCvW/yWeTr0M/CgPdmJ5+fKPhiP68NWOkaW 4Dh2wYtT68Z5Jm4/89W/waqwcl74yihaGeYicLM+jZBWlTHweVDw8Af+bFYmK/c7qWrb KYrz0loTZbyPyz5ON3HY7rhqwbtUqZp7RtLunZMqgO7LtI8HaDnBGJyoxHambLXM4hd3 WX7OgIMu2as6YOAZIKQehwI4eXpmX/tPgJsDROxs0AWc8PjDDRj8kkYLVoMgf9D9v/ZX kefo1VsohSzq9+GkmN681uwbAoREnvCE1ic+4X+zBJ4aewqFypKqBetPK8Hbcr8azowA V8NQ== X-Gm-Message-State: AC+VfDyU1ke8kYC22RQXj9hXxlLeYH04J26E4+ivubzbQAj/E1tYSKoG Mn3DU28VU81rPH2NJzTx2oczNCKKbOcCgCG3zNr4cA== X-Received: by 2002:a05:6000:1a50:b0:307:97dd:1de2 with SMTP id t16-20020a0560001a5000b0030797dd1de2mr8379117wry.25.1684745375854; Mon, 22 May 2023 01:49:35 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id f18-20020adfdb52000000b002f7780eee10sm7033467wrj.59.2023.05.22.01.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:49:35 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [COMMITTED] ada: Fix traversal for the rightmost node of a pretty-printed expression Date: Mon, 22 May 2023 10:49:33 +0200 Message-Id: <20230522084933.1725506-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766583983566739858?= X-GMAIL-MSGID: =?utf-8?q?1766583983566739858?= From: Piotr Trojanek When getting the rightmost node of a pretty-printed expression we incorrectly traversed some composite nodes, which caused the expression image to be chopped. gcc/ada/ * pprint.adb (Expression_Image): Reduce scope of local variables; inline local uncommented constant From_Source; concatenate string with a single character, as it is likely to execute faster; add missing cases to traversal for the rightmost node and assertion to demonstrate that the ??? comment is no longer relevant. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/pprint.adb | 147 +++++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 46 deletions(-) diff --git a/gcc/ada/pprint.adb b/gcc/ada/pprint.adb index 2a86bd58cd8..8fdb5d6916e 100644 --- a/gcc/ada/pprint.adb +++ b/gcc/ada/pprint.adb @@ -53,13 +53,6 @@ package body Pprint is (Expr : Node_Id; Default : String) return String is - From_Source : constant Boolean := - Comes_From_Source (Expr) - and then not Opt.Debug_Generated_Code; - Append_Paren : Natural := 0; - Left : Node_Id := Original_Node (Expr); - Right : Node_Id := Original_Node (Expr); - function Expr_Name (Expr : Node_Id; Take_Prefix : Boolean := True; @@ -302,7 +295,7 @@ package body Pprint is return Str; end; else - return "'" & Get_Name_String (Attribute_Name (Expr)); + return ''' & Get_Name_String (Attribute_Name (Expr)); end if; when N_Explicit_Dereference => @@ -639,10 +632,20 @@ package body Pprint is end case; end Expr_Name; + -- Local variables + + Append_Paren : Natural := 0; + Left : Node_Id := Original_Node (Expr); + Right : Node_Id := Original_Node (Expr); + + Left_Sloc, Right_Sloc : Source_Ptr; + -- Start of processing for Expression_Image begin - if not From_Source then + if not Comes_From_Source (Expr) + or else Opt.Debug_Generated_Code + then declare S : constant String := Expr_Name (Expr); begin @@ -661,8 +664,6 @@ package body Pprint is end if; -- Compute left (start) and right (end) slocs for the expression - -- Consider using Sinput.Sloc_Range instead, except that it does not - -- work properly currently??? loop case Nkind (Left) is @@ -706,13 +707,24 @@ package body Pprint is loop case Nkind (Right) is - when N_And_Then - | N_Membership_Test + when N_Membership_Test | N_Op - | N_Or_Else + | N_Short_Circuit => Right := Original_Node (Right_Opnd (Right)); + when N_Attribute_Reference => + declare + Exprs : constant List_Id := Expressions (Right); + begin + if Present (Exprs) then + Right := Original_Node (Last (Expressions (Right))); + Append_Paren := Append_Paren + 1; + else + exit; + end if; + end; + when N_Expanded_Name | N_Selected_Component => @@ -755,40 +767,37 @@ package body Pprint is Append_Paren := Append_Paren + 1; when N_Function_Call => - if Present (Parameter_Associations (Right)) then - declare - Rover : Node_Id; - Found : Boolean; - - begin - -- Avoid source position confusion associated with - -- parameters for which Comes_From_Source is False. - - Rover := First (Parameter_Associations (Right)); - Found := False; - while Present (Rover) loop - if Comes_From_Source (Original_Node (Rover)) then - Right := Original_Node (Rover); - Found := True; - end if; + declare + Has_Source_Param : Boolean := False; + -- True iff function call has a parameter coming from source - Next (Rover); - end loop; + Param : Node_Id; - if Found then - Append_Paren := Append_Paren + 1; + begin + -- Avoid source position confusion associated with + -- parameters for which Comes_From_Source is False. + + Param := First (Parameter_Associations (Right)); + while Present (Param) loop + if Comes_From_Source (Original_Node (Param)) then + if Nkind (Param) = N_Parameter_Association then + Right := + Original_Node (Explicit_Actual_Parameter (Param)); + else + Right := Original_Node (Param); + end if; + Has_Source_Param := True; end if; - -- Quit loop if no Comes_From_Source parameters - - exit when not Found; - end; - - -- Quit loop if no parameters + Next (Param); + end loop; - else - exit; - end if; + if Has_Source_Param then + Append_Paren := Append_Paren + 1; + else + Right := Original_Node (Name (Right)); + end if; + end; when N_Quantified_Expression => Right := Original_Node (Condition (Right)); @@ -823,6 +832,45 @@ package body Pprint is Append_Paren := Append_Paren + 1; end; + when N_Slice => + declare + Rng : constant Node_Id := Discrete_Range (Right); + begin + if Nkind (Rng) = N_Subtype_Indication then + Right := + Original_Node (Range_Expression (Constraint (Rng))); + else + Right := Original_Node (High_Bound (Rng)); + end if; + end; + + when N_Raise_Expression => + declare + Exp : constant Node_Id := Expression (Right); + begin + if Present (Exp) then + Right := Original_Node (Exp); + else + Right := Original_Node (Name (Right)); + end if; + end; + + when N_If_Expression => + declare + Cond_Expr : constant Node_Id := First (Expressions (Right)); + Then_Expr : constant Node_Id := Next (Cond_Expr); + Else_Expr : constant Node_Id := Next (Then_Expr); + begin + if Present (Else_Expr) then + Right := Original_Node (Else_Expr); + else + Right := Original_Node (Then_Expr); + end if; + end; + + when N_Allocator => + Right := Original_Node (Expression (Right)); + -- For all other items, quit the loop when others => @@ -830,10 +878,17 @@ package body Pprint is end case; end loop; + -- We could just use Sinput.Sloc_Range, but we still need Append_Paren. + -- Make sure that we indeed got the left and right-most nodes. + + Sinput.Sloc_Range (Expr, Left_Sloc, Right_Sloc); + + pragma Assert (Left_Sloc = Sloc (Left)); + pragma Assert (Right_Sloc = Sloc (Right)); + declare - Scn : Source_Ptr := Original_Location (Sloc (Left)); - End_Sloc : constant Source_Ptr := - Original_Location (Sloc (Right)); + Scn : Source_Ptr := Left_Sloc; + End_Sloc : constant Source_Ptr := Right_Sloc; Src : constant Source_Buffer_Ptr := Source_Text (Get_Source_File_Index (Scn));