From patchwork Tue May 23 08:07:52 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: 97843 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1974058vqo; Tue, 23 May 2023 01:12:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ42MPTKblQZTHv/lZ6YcZ1QUIS+Lguf7fZZCAZ+FbgMasph8gRPwVW/caq+xFGPGXk0Ya7X X-Received: by 2002:a17:906:7188:b0:967:e015:f542 with SMTP id h8-20020a170906718800b00967e015f542mr10626724ejk.44.1684829542053; Tue, 23 May 2023 01:12:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684829542; cv=none; d=google.com; s=arc-20160816; b=HlSL3UHsvQb3GIMk4n31tjVCer+xDuH+J0rGsvvzGJXlGOa6FqECEZ7eQDo/uB2uwk XXOlyXWoaFZp2wQaXRPx6C8S/NUxkD2dturFgy8sO9uGeXB5tidZVbh+JtVMs1SKlXEd nfyvWj4I/vnHJ/9KqsLcAYGkN8ruGKO0Fp3LwBBk09bXQrddWmaQfFsU/JZnA31xDHY2 84xDGE5T4y0b7fzR8Dl16GkpZ14jr9j4TZSBflQLpjucdXnp0KgvJiljBVbrnXYWuu1Y fonZ0ba9WgVtYlckotaZvMsqJGwuusF+IzCzyylddLHid/V90iiLXdugXZPvqa8b3vpi sNZg== 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=eQKKomG2hWJMOtFvi9i0TjN95RE3yklC/644wfo2314=; b=vgf9au2epicIUk9fdNTDMJpaiEwnvYhzWTbQuygXLfm3al7lONtHDQO20CN1cu5NF8 zcAcoNxlgmpya+s6XIo83Wsi1oEQFChKtgb8e1jss/T0LVnYLZ+Ofni0y+65A2rn8JUg X9ADpCzRhYIsec6wAy1XvhLGtqiZukPOgrJqQu1/SQ+B8Q/f0eZSixjBG2i0nPJgIVgJ 25EoUn4LBxVqq0JMS66H1Jkrx9nfCvI9vBLAmghWUquXRs8lVPzL2viRGtzkJm9fGC/S TyweJlsN5ICePuuAc6DOtja4u5rn3HPPazGvApZLnkopR7xlfpS/jU4SluGHy2LUSK/x zL7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vjmRu2T3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z12-20020a170906240c00b0096f5dc867f4si3555955eja.203.2023.05.23.01.12.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 01:12:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vjmRu2T3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 769F33882107 for ; Tue, 23 May 2023 08:09:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 769F33882107 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684829394; bh=eQKKomG2hWJMOtFvi9i0TjN95RE3yklC/644wfo2314=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vjmRu2T3YKsdti//K7ftMtvGYGNwSErALbpXrabzvJGrBkcmzAifc3IFa1BgdVXxC ZYWcmiYin6Ntnzc8wRpxPpdwWcmCyChjvvsKG4JNFgYBc04HIfWrWYoeAjbyCEBUZm VtE2ZtUdqNtiTNNrHhQ+MLzwJ1Gul0f48LcQ3w88= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id 25BB43856263 for ; Tue, 23 May 2023 08:07:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 25BB43856263 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3f607dc98cdso10782845e9.1 for ; Tue, 23 May 2023 01:07:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684829273; x=1687421273; 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=eQKKomG2hWJMOtFvi9i0TjN95RE3yklC/644wfo2314=; b=EMe7qPFvyDVjGLW2ObBh1R+HuIeXckYGjY/sUndUlRaIUorLLtAmbhm7j9GZ3MNrz0 VepwU5A+/qZFTXNc7d6tVduNj7Dw++bhFu8J9Zd6/U3sb5NsfIsEOFLJ+ofBX+niUSGA i/Jids7gFfbJx4TS6Vn/H8LqL71SO4zXsUqW5S4+iJFiaJ8w42c1lGwmMB0l/h/n6Chw AR3hVcYacXlFwADH/NcI7NH3F4UzJLltRnAR117r93SO7PKmsGveN2WhIUPr8f4rhMdn Pew4VTXGQddEt4bF828ouhXDlz4WgJWnvwW0w//WetcsMbJGJmtQEz/3NbEzUgxg+f7E K4ZA== X-Gm-Message-State: AC+VfDzOSqIjTzJDIwe17aqByOXPYDi4DKiXpUyUrOwWqHQsWuyfltAX 2XiEkrReyrkBmiUOG4tPCw0qTH8iqVWHUysmJJxhGA== X-Received: by 2002:a05:600c:21c3:b0:3f6:7af:8fe7 with SMTP id x3-20020a05600c21c300b003f607af8fe7mr3087830wmj.10.1684829273625; Tue, 23 May 2023 01:07:53 -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 k25-20020a05600c1c9900b003f1738d0d13sm1872694wms.1.2023.05.23.01.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 01:07:53 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [COMMITTED] ada: Transfer fix for pretty-printed parentheses from GNATprove to GNAT Date: Tue, 23 May 2023 10:07:52 +0200 Message-Id: <20230523080752.1873021-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 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.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?1766671822104924445?= X-GMAIL-MSGID: =?utf-8?q?1766671822104924445?= From: Piotr Trojanek Expressions with parentheses are notoriously problematic to pretty-print. In GNATprove we had a post-processing fix for them, but it is better to have this fix in the GNAT frontend repository and apply it for CodePeer as well. gcc/ada/ * pprint.adb (Expression_Image): Move Count_Parentheses and Fix_Parentheses routines from GNATprove and apply them before returning the slice of a source code buffer. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/pprint.adb | 70 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/gcc/ada/pprint.adb b/gcc/ada/pprint.adb index 8add10c8f22..bcc7a257630 100644 --- a/gcc/ada/pprint.adb +++ b/gcc/ada/pprint.adb @@ -64,6 +64,19 @@ package body Pprint is -- Expand_Type is True and Expr is a type, try to expand Expr (an -- internally generated type) into a user understandable name. + function Count_Parentheses (S : String; C : Character) return Natural + with Pre => C in '(' | ')'; + -- Returns the number of times parenthesis character C should be added + -- to string S for getting a correctly parenthesized result. For C = '(' + -- this means prepending the character, for C = ')' this means appending + -- the character. + + function Fix_Parentheses (S : String) return String; + -- Counts the number of required opening and closing parentheses in S to + -- respectively prepend and append for getting correct parentheses. Then + -- returns S with opening parentheses prepended and closing parentheses + -- appended so that the result is correctly parenthesized. + Max_List_Depth : constant := 3; -- Limit number of nested lists to print @@ -650,6 +663,61 @@ package body Pprint is end case; end Expr_Name; + ----------------------- + -- Count_Parentheses -- + ----------------------- + + function Count_Parentheses (S : String; C : Character) return Natural is + + procedure Next_Char (Count : in out Natural; C, D, Ch : Character); + -- Process next character Ch and update the number Count of C + -- characters to add for correct parenthesizing, where D is the + -- opposite parenthesis. + + --------------- + -- Next_Char -- + --------------- + + procedure Next_Char (Count : in out Natural; C, D, Ch : Character) is + begin + if Ch = D then + Count := Count + 1; + elsif Ch = C and then Count > 0 then + Count := Count - 1; + end if; + end Next_Char; + + -- Local variables + + Count : Natural := 0; + + -- Start of processing for Count_Parentheses + + begin + if C = '(' then + for Ch of reverse S loop + Next_Char (Count, C, ')', Ch); + end loop; + else + for Ch of S loop + Next_Char (Count, C, '(', Ch); + end loop; + end if; + + return Count; + end Count_Parentheses; + + --------------------- + -- Fix_Parentheses -- + --------------------- + + function Fix_Parentheses (S : String) return String is + Count_Open : constant Natural := Count_Parentheses (S, '('); + Count_Close : constant Natural := Count_Parentheses (S, ')'); + begin + return (1 .. Count_Open => '(') & S & (1 .. Count_Close => ')'); + end Fix_Parentheses; + -- Local variables Left, Right : Source_Ptr; @@ -775,7 +843,7 @@ package body Pprint is Scn := Scn + 1; end loop; - return Buffer (1 .. Index); + return Fix_Parentheses (Buffer (1 .. Index)); end; end Expression_Image;