diff options
author | Piotr Trojanek <trojanek@adacore.com> | 2023-02-11 21:09:11 +0100 |
---|---|---|
committer | Marc Poulhiès <poulhies@adacore.com> | 2023-05-23 09:50:58 +0200 |
commit | 0333af4d174c86cb7b908ac702318f1de7bc6dec (patch) | |
tree | c409a95462faaab3b0806680a24d221929731d7f | |
parent | 0b369aed43e622b0cf67879e84e4b4adf401b350 (diff) | |
download | gcc-0333af4d174c86cb7b908ac702318f1de7bc6dec.zip gcc-0333af4d174c86cb7b908ac702318f1de7bc6dec.tar.gz gcc-0333af4d174c86cb7b908ac702318f1de7bc6dec.tar.bz2 |
ada: Transfer fix for pretty-printed parentheses from GNATprove to GNAT
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.
-rw-r--r-- | gcc/ada/pprint.adb | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/gcc/ada/pprint.adb b/gcc/ada/pprint.adb index 8add10c..bcc7a25 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; |