Skip to content

Commit 4da69b7

Browse files
committed
Fix pretty not resetting all global state between calls
breakAllMode persisting caused weird line breaks with max_int width in Goblint.
1 parent 4ef5a08 commit 4da69b7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/ocamlutil/pretty.ml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,36 +566,54 @@ let emitDoc
566566
let fprint (chn: out_channel) ~(width: int) doc =
567567
let doc = if !flattenBeforePrint then flatten Nil doc else doc in
568568
(* Save some parameters, to allow for nested calls of these routines. *)
569+
let old_maxCol = !maxCol in
569570
maxCol := width;
570571
let old_breaks = !breaks in
571572
breaks := [];
572573
let old_alignDepth = !alignDepth in
573574
alignDepth := 0;
574575
let old_activeMarkups = !activeMarkups in
575576
activeMarkups := [];
577+
let old_aligns = !aligns in
578+
aligns := [{ gainBreak = 0; isTaken = ref false; deltaFromPrev = ref 0; deltaToNext = ref 0; }];
579+
let old_topAlignAbsCol = !topAlignAbsCol in
580+
topAlignAbsCol := 0;
581+
let old_breakAllMode = !breakAllMode in
582+
breakAllMode := false;
576583
ignore (scan 0 doc);
577584
breaks := List.rev !breaks;
578585
ignore (emitDoc
579586
(fun s nrcopies ->
580587
for _ = 1 to nrcopies do
581588
output_string chn s
582589
done) doc);
590+
breakAllMode := old_breakAllMode;
591+
topAlignAbsCol := old_topAlignAbsCol;
592+
aligns := old_aligns;
583593
activeMarkups := old_activeMarkups;
584594
alignDepth := old_alignDepth;
585-
breaks := old_breaks (* We must do this especially if we don't do emit
595+
breaks := old_breaks; (* We must do this especially if we don't do emit
586596
(which consumes breaks) because otherwise we waste
587597
memory *)
598+
maxCol := old_maxCol
588599

589600
(* Print the document to a string *)
590601
let sprint ~(width : int) doc : string =
591602
let doc = if !flattenBeforePrint then flatten Nil doc else doc in
603+
let old_maxCol = !maxCol in
592604
maxCol := width;
593605
let old_breaks = !breaks in
594606
breaks := [];
595607
let old_activeMarkups = !activeMarkups in
596608
activeMarkups := [];
597609
let old_alignDepth = !alignDepth in
598610
alignDepth := 0;
611+
let old_aligns = !aligns in
612+
aligns := [{ gainBreak = 0; isTaken = ref false; deltaFromPrev = ref 0; deltaToNext = ref 0; }];
613+
let old_topAlignAbsCol = !topAlignAbsCol in
614+
topAlignAbsCol := 0;
615+
let old_breakAllMode = !breakAllMode in
616+
breakAllMode := false;
599617
ignore (scan 0 doc);
600618
breaks := List.rev !breaks;
601619
let buf = Buffer.create 1024 in
@@ -604,9 +622,13 @@ let sprint ~(width : int) doc : string =
604622
else begin Buffer.add_string buf str; add_n_strings str (num - 1) end
605623
in
606624
emitDoc add_n_strings doc;
625+
breakAllMode := old_breakAllMode;
626+
topAlignAbsCol := old_topAlignAbsCol;
627+
aligns := old_aligns;
607628
breaks := old_breaks;
608629
activeMarkups := old_activeMarkups;
609630
alignDepth := old_alignDepth;
631+
maxCol := old_maxCol;
610632
Buffer.contents buf
611633

612634

0 commit comments

Comments
 (0)