From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3f607dc98cdso10782845e9.1 for ; Tue, 23 May 2023 01:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1684829273; x=1687421273; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=eQKKomG2hWJMOtFvi9i0TjN95RE3yklC/644wfo2314=; b=MsFahx5UUPJ8ch7cfeDSCZ/ce8THWFBUzgtkaNMFnvltObBHb5S/JEa03NHvabAokL faqVt8BMasF9YCo1DIAoXo/SEwZFTHQycCooA1U6y/RJMl1m9FJnLQSKwH14tl2oBCuu 0kKwmWtrV4NJBlw7ZnYV4G3DwWLZtOrYi1M3NEuPxyGNMYBh53c01YZamX8KfG2AyHLH TDDo3+H6LmtLCoR71vXPhPJ+TY59OLhoCkWBF6oKTY5vvUnjET4AqgZao2d4Rkv0liLk VbOgSeScz3hoCV+bnWL0Ne+2AOH9tn/QgL70vA26Y40KB8qAphtVSbJxipviqr85jZPZ psOg== 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-Google-Smtp-Source: ACHHUZ5Z3eBR9TtUGaFMlpVmYI0qIbXh9kMFGM0fyW1UO4k22fDvvAHGxgaNWWsHHY5JJ63T/uEQXA== 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) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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; -- 2.40.0