Skip to content

Commit 31b2e1c

Browse files
committed
Use pretty instead of show in C-2PO domains (issue #1795)
1 parent 1e89a9b commit 31b2e1c

File tree

5 files changed

+108
-133
lines changed

5 files changed

+108
-133
lines changed

src/analyses/c2poAnalysis.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct
100100
| lval_size, (Some rterm, Some roffset) ->
101101
let dummy_var = MayBeEqual.dummy_var lval_t in
102102

103-
if M.tracing then M.trace "c2po-assign" "assigning: var: %s; expr: %s + %s. \nTo_cil: lval: %a; expr: %a\n" (T.show lterm) (T.show rterm) (Z.to_string roffset) d_exp (T.to_cil lterm) d_exp (T.to_cil rterm);
103+
if M.tracing then M.trace "c2po-assign" "assigning: var: %a; expr: %a + %a. \nTo_cil: lval: %a; expr: %a\n" T.pretty lterm T.pretty rterm GobZ.pretty roffset d_exp (T.to_cil lterm) d_exp (T.to_cil rterm);
104104

105105
let equal_dummy_rterm = [Equal (dummy_var, rterm, roffset)] in
106106
let equal_dummy_lterm = [Equal (lterm, dummy_var, Z.zero)] in
@@ -158,7 +158,7 @@ struct
158158
with Unsat ->
159159
`Bot
160160
in
161-
if M.tracing then M.trace "c2po" "branch:\n Actual equality: %a; pos: %b; valid_prop_list: %s; is_bot: %b\n" d_exp e pos (show_conj valid_props) (D.is_bot res);
161+
if M.tracing then M.trace "c2po" "branch:\n Actual equality: %a; pos: %b; valid_prop_list: %a; is_bot: %b" d_exp e pos pretty_conj valid_props (D.is_bot res);
162162
if D.is_bot res then raise Deadcode;
163163
res
164164

src/cdomains/c2poDomain.ml

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ module C2PODomain = struct
1111

1212
type t = CongruenceClosure.t[@@deriving ord, hash]
1313

14-
let show x = show_conj (get_conjunction x)
14+
let pretty () x = pretty_conj () (get_conjunction x)
1515
let name () = "c2po"
1616

1717
type domain = t
18-
include Printable.SimpleShow (struct type t = domain let show = show end)
18+
include Printable.SimplePretty (struct type t = domain let pretty = pretty end)
1919

2020
let equal_standard x y =
2121
let cc1 = x.data in
@@ -36,7 +36,7 @@ module C2PODomain = struct
3636
&& equal_diseqs cc1 cc2
3737
&& equal_bldis cc1 cc2
3838
in
39-
if M.tracing then M.trace "c2po-equal" "equal eq classes. %b\nx=\n%s\ny=\n%s" res (show_all x) (show_all y);
39+
if M.tracing then M.trace "c2po-equal" "equal eq classes. %b\nx=\n%a\ny=\n%a" res pretty_all x pretty_all y;
4040
res
4141

4242
let equal_normal_form x y =
@@ -48,10 +48,10 @@ module C2PODomain = struct
4848
else
4949
let nf1 = get_normal_form x in
5050
let nf2 = get_normal_form y in
51-
if M.tracing then M.trace "c2po-min-repr" "Normal form of x = %s; Normal form of y = %s" (show_conj nf1) (show_conj nf2);
51+
if M.tracing then M.trace "c2po-min-repr" "Normal form of x = %a; Normal form of y = %a" pretty_conj nf1 pretty_conj nf2;
5252
T.props_equal nf1 nf2
5353
in
54-
if M.tracing then M.trace "c2po-equal" "equal min repr. %b\nx=\n%s\ny=\n%s" res (show_all x) (show_all y);
54+
if M.tracing then M.trace "c2po-equal" "equal min repr. %b\nx=\n%a\ny=\n%a" res pretty_all x pretty_all y;
5555
res
5656

5757
let equal a b =
@@ -85,8 +85,7 @@ module C2PODomain = struct
8585
if exactly_equal cc1 cc2 then
8686
cc1
8787
else begin
88-
if M.tracing then M.tracel "c2po-join" "JOIN AUTOMATON. FIRST ELEMENT: %s\nSECOND ELEMENT: %s\n"
89-
(show_all x) (show_all y);
88+
if M.tracing then M.tracel "c2po-join" "JOIN AUTOMATON. FIRST ELEMENT: %a\nSECOND ELEMENT: %a" pretty_all x pretty_all y;
9089
let a = cc1 in
9190
let b = cc2 in
9291
let cc, _ = join_cc_function a b in
@@ -98,8 +97,7 @@ module C2PODomain = struct
9897
end
9998
in
10099
let res = CongruenceClosure.data_to_t res in
101-
if M.tracing then M.tracel "c2po-join" "JOIN. JOIN: %s\n"
102-
(show_all res);
100+
if M.tracing then M.tracel "c2po-join" "JOIN. JOIN: %a" pretty_all res;
103101
res
104102

105103
let join (a: t) (b :t) =
@@ -189,19 +187,19 @@ module C2PODomain = struct
189187
in
190188
let x_diff = List.filter (not_in y_conj) x_conj in
191189
let y_diff = List.filter (not_in x_conj) y_conj in
192-
Pretty.dprintf ("Additional propositions of first element:\n%s\nAdditional propositions of second element:\n%s\n") (show_conj x_diff) (show_conj y_diff)
190+
Pretty.dprintf ("Additional propositions of first element:\n%a\nAdditional propositions of second element:\n%a\n") pretty_conj x_diff pretty_conj y_diff
193191

194192
end
195193

196194

197195
module D = struct
198196
include Lattice.LiftBot (C2PODomain)
199197

200-
let show_all = function
198+
let pretty_all () = function
201199
| `Bot ->
202-
show `Bot
200+
pretty () `Bot
203201
| `Lifted x ->
204-
show_all x
202+
pretty_all () x
205203

206204
let meet a b =
207205
try
@@ -218,12 +216,12 @@ module D = struct
218216
let printXml f x = match x with
219217
| `Lifted x ->
220218
BatPrintf.fprintf f "<value>\n<map>\n<key>\nnormal form\n</key>\n<value>\n%s</value>\n<key>\nuf\n</key>\n<value>\n%s</value>\n<key>\nsubterm set\n</key>\n<value>\n%s</value>\n<key>\nmap\n</key>\n<value>\n%s</value>\n<key>\nmin. repr\n</key>\n<value>\n%s</value>\n<key>\ndiseq\n</key>\n<value>\n%s</value>\n</map>\n</value>\n"
221-
(XmlUtil.escape (Format.asprintf "%s" (show (`Lifted x))))
222-
(XmlUtil.escape (Format.asprintf "%s" (TUF.show_uf x.data.uf)))
223-
(XmlUtil.escape (Format.asprintf "%s" (SSet.show_set x.data.set)))
224-
(XmlUtil.escape (Format.asprintf "%s" (LMap.show_map x.data.map)))
225-
(XmlUtil.escape (Format.asprintf "%s" (show_normal_form x.normal_form)))
226-
(XmlUtil.escape (Format.asprintf "%s" (Disequalities.show_neq x.data.diseq)))
219+
(XmlUtil.escape (GobPretty.sprint pretty (`Lifted x)))
220+
(XmlUtil.escape (GobPretty.sprint TUF.pretty_uf x.data.uf))
221+
(XmlUtil.escape (GobPretty.sprint SSet.pretty_set x.data.set))
222+
(XmlUtil.escape (GobPretty.sprint LMap.pretty_map x.data.map))
223+
(XmlUtil.escape (GobPretty.sprint pretty_normal_form x.normal_form))
224+
(XmlUtil.escape (GobPretty.sprint Disequalities.pretty_neq x.data.diseq))
227225
| `Bot ->
228226
BatPrintf.fprintf f "<value>\nbottom\n</value>\n"
229227

@@ -271,7 +269,7 @@ module D = struct
271269
(** Remove terms from the data structure.
272270
It removes all terms that may be changed after an assignment to "term".*)
273271
let remove_may_equal_terms ask size term cc =
274-
if M.tracing then M.trace "c2po" "remove_may_equal_terms: %s\n" (T.show term);
272+
if M.tracing then M.trace "c2po" "remove_may_equal_terms: %a" T.pretty term;
275273
let _, cc = insert cc term in
276274
let may_equal_term =
277275
MayBeEqual.may_be_equal ask cc size term

src/cdomains/congruenceClosure.ml

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,20 @@ module Disequalities = struct
352352
*)
353353

354354
(** Produces a string for the number used as offset; helper function for show* functions below.*)
355-
let show_number r =
355+
let pretty_number () r =
356356
if Z.equal r Z.zero then
357-
""
357+
Pretty.nil
358358
else if Z.leq r Z.zero then
359-
Z.to_string r
359+
GobZ.pretty () r
360360
else
361-
" + " ^ Z.to_string r
361+
Pretty.dprintf " + %a" GobZ.pretty r
362362

363-
let show_neq neq =
363+
let pretty_neq () neq =
364364
let clist = bindings neq in
365-
let do_neq =
366-
(fun s (v,r,v') ->
367-
s ^ "\t" ^ T.show v ^ show_number r ^ " != " ^ T.show v' ^ "\n")
365+
let do_neq () (v,r,v') =
366+
Pretty.dprintf "\t%a%a != %a\n" T.pretty v pretty_number r T.pretty v'
368367
in
369-
List.fold_left do_neq "" clist
368+
Pretty.docList ~sep:Pretty.nil (do_neq ()) () clist
370369

371370
let get_disequalities x =
372371
let to_disequality (t1, z, t2) =
@@ -404,11 +403,11 @@ module SSet = struct
404403
let find_opt = TSet.find_opt
405404
let union = TSet.union
406405

407-
let show_set set =
408-
let show_element v s =
409-
s ^ "\t" ^ T.show v ^ ";\n"
406+
let pretty_set () set =
407+
let pretty_element () v =
408+
Pretty.dprintf "\t%a;\n" T.pretty v
410409
in
411-
TSet.fold show_element set "" ^ "\n"
410+
Pretty.docList ~sep:Pretty.nil (pretty_element ()) () (TSet.elements set)
412411

413412
(** Adds all subterms of t to the SSet and the LookupMap*)
414413
let rec subterms_of_term (set,map) t =
@@ -495,12 +494,11 @@ module MRMap = struct
495494
let mem = TMap.mem
496495
let empty = TMap.empty
497496

498-
let show_min_rep min_representatives =
499-
let show_one_rep s (state, (rep, z)) =
500-
s ^ "\tState: " ^ T.show state ^
501-
"\n\tMin: (" ^ T.show rep ^ ", " ^ Z.to_string z ^ ")--\n\n"
497+
let pretty_min_rep () min_representatives =
498+
let pretty_one_rep () (state, (rep, z)) =
499+
Pretty.dprintf "\tState: %a\n\tMin: (%a, %a)--\n\n" T.pretty state T.pretty rep GobZ.pretty z
502500
in
503-
List.fold_left show_one_rep "" (bindings min_representatives)
501+
Pretty.docList ~sep:Pretty.nil (pretty_one_rep ()) () (bindings min_representatives)
504502

505503
let rec update_min_repr (uf, set, map) min_representatives = function
506504
| [] -> min_representatives
@@ -607,14 +605,12 @@ let get_transitions (uf, map) =
607605
in
608606
List.concat_map do_bindings (LMap.bindings map)
609607

610-
let show_conj list =
608+
let pretty_conj () list =
611609
match list with
612-
| [] -> "top"
610+
| [] -> Pretty.text "top"
613611
| list ->
614-
let show_prop s d =
615-
s ^ "\t" ^ T.show_prop d ^ ";\n"
616-
in
617-
List.fold_left show_prop "" list
612+
let pretty_prop () d = Pretty.dprintf "\t%a;\n" T.pretty_prop d in
613+
Pretty.docList ~sep:Pretty.nil (pretty_prop ()) () list
618614

619615
type data = {
620616
uf: TUF.t;
@@ -675,7 +671,7 @@ let get_normal_conjunction cc get_normal_repr =
675671
| Equal (t1,t2,z) -> failwith "No equality expected."
676672
| BlNequal (t1,t2) -> failwith "No block disequality expected."
677673
in
678-
if M.tracing then M.trace "c2po-diseq" "DISEQUALITIES: %s;\nUnion find: %s\nMap: %s\n" (show_conj disequalities) (TUF.show_uf cc.uf) (LMap.show_map cc.map);
674+
if M.tracing then M.trace "c2po-diseq" "DISEQUALITIES: %a;\nUnion find: %a\nMap: %a\n" pretty_conj disequalities TUF.pretty_uf cc.uf LMap.pretty_map cc.map;
679675
let disequalities = List.map normalize_disequality disequalities in
680676
(* block disequalities *)
681677
let normalize_bldis t = match t with
@@ -753,11 +749,11 @@ let get_normal_form cc =
753749
LazyNormalFormEval.force cc.normal_form
754750

755751
(** Converts the normal form to string, but only if it was already computed. *)
756-
let show_normal_form normal_form =
752+
let pretty_normal_form () normal_form =
757753
if LazyNormalFormEval.is_val normal_form then
758-
show_conj (LazyNormalFormEval.force normal_form)
754+
pretty_conj () (LazyNormalFormEval.force normal_form)
759755
else
760-
"not computed"
756+
Pretty.text "not computed"
761757

762758

763759
let get_conjunction_from_data data =
@@ -778,21 +774,15 @@ let data_to_t (cc : data) : t =
778774
{data = cc;
779775
normal_form = LazyNormalFormEval.make cc}
780776

781-
let show_all (x: t) =
782-
"Normal form:\n" ^
783-
show_conj((get_conjunction x)) ^
784-
"Union Find partition:\n" ^
785-
TUF.show_uf x.data.uf
786-
^ "\nSubterm set:\n"
787-
^ SSet.show_set x.data.set
788-
^ "\nLookup map/transitions:\n"
789-
^ LMap.show_map x.data.map
790-
^ "\nNeq:\n"
791-
^ Disequalities.show_neq x.data.diseq
792-
^ "\nBlock diseqs:\n"
793-
^ show_conj (BlDis.to_conj x.data.bldis)
794-
^ "\nMin repr:\n"
795-
^ show_normal_form x.normal_form
777+
let pretty_all () (x: t) =
778+
Pretty.dprintf "Normal form:\n%a\nUnion Find partition:\n%a\nSubterm set:\n%a\nLookup map/transitions:\n%a\nNeq:\n%a\nBlock diseqs:\n%a\nMin repr:\n%a"
779+
pretty_conj (get_conjunction x)
780+
TUF.pretty_uf x.data.uf
781+
SSet.pretty_set x.data.set
782+
LMap.pretty_map x.data.map
783+
Disequalities.pretty_neq x.data.diseq
784+
pretty_conj (BlDis.to_conj x.data.bldis)
785+
pretty_normal_form x.normal_form
796786

797787
(** Splits the conjunction into three groups: the first one contains all equality propositions,
798788
the second one contains all inequality propositions
@@ -832,7 +822,7 @@ let congruence_neq cc neg' =
832822
(* take explicit dis-equalities into account *)
833823
let uf, neq_list = Disequalities.init_list_neq uf neg in
834824
let neq = Disequalities.propagate_neq (uf, cmap, arg, neq) cc.bldis neq_list in
835-
if M.tracing then M.trace "c2po-neq" "congruence_neq: %s\nUnion find: %s\n" (Disequalities.show_neq neq) (TUF.show_uf uf);
825+
if M.tracing then M.trace "c2po-neq" "congruence_neq: %a\nUnion find: %a\n" Disequalities.pretty_neq neq TUF.pretty_uf uf;
836826
{cc with uf; diseq = neq}
837827

838828
(**
@@ -856,7 +846,7 @@ let rec closure (uf, map, new_repr) = function
856846
let v2, r2, uf = TUF.find uf t2 in
857847
let sizet1, sizet2 = T.get_size t1, T.get_size t2 in
858848
if not (Z.equal sizet1 sizet2) then (
859-
if M.tracing then M.trace "c2po" "ignoring equality because the sizes are not the same: %s = %s + %s" (T.show t1) (Z.to_string r) (T.show t2);
849+
if M.tracing then M.trace "c2po" "ignoring equality because the sizes are not the same: %a = %a + %a" T.pretty t1 GobZ.pretty r T.pretty t2;
860850
closure (uf, map, new_repr) rest
861851
)
862852
else if T.equal v1 v2 then
@@ -1046,7 +1036,7 @@ let meet_pos_conjs cc pos_conjs =
10461036
let cc = insert_set cc subterms in
10471037
closure cc pos_conjs
10481038
in
1049-
if M.tracing then M.trace "c2po-meet" "meet_pos_conjs result: %s\n" (show_conj (get_conjunction_from_data res));
1039+
if M.tracing then M.trace "c2po-meet" "meet_pos_conjs result: %a\n" pretty_conj (get_conjunction_from_data res); (* TODO: avoid eager computation in argument *)
10501040
res
10511041

10521042
(** Adds propositions to the data structure.
@@ -1342,7 +1332,7 @@ let join_neq diseq1 diseq2 cc1 cc2 cc cmap1 cmap2 =
13421332
let subterms, _ = SSet.subterms_of_conj diseq in
13431333
let cc = insert_set cc subterms in
13441334
let res = congruence_neq cc diseq in
1345-
(if M.tracing then M.trace "c2po-neq" "join_neq: %s\n\n" (Disequalities.show_neq res.diseq));
1335+
(if M.tracing then M.trace "c2po-neq" "join_neq: %a" Disequalities.pretty_neq res.diseq);
13461336
res
13471337

13481338
(** Joins the block disequalities bldiseq1 and bldiseq2, given a congruence closure data structure.
@@ -1369,7 +1359,7 @@ let join_bldis bldiseq1 bldiseq2 cc1 cc2 cc cmap1 cmap2 =
13691359
let cc = insert_set cc subterms in
13701360
let diseqs_ref_terms = List.filter both_root bldiseq in
13711361
let bldis = List.fold_left BlDis.add_block_diseq BlDis.empty diseqs_ref_terms in
1372-
(if M.tracing then M.trace "c2po-neq" "join_bldis: %s\n\n" (show_conj (BlDis.to_conj bldis)));
1362+
(if M.tracing then M.trace "c2po-neq" "join_bldis: %a" pretty_conj (BlDis.to_conj bldis));
13731363
{cc with bldis}
13741364

13751365
(** Check for equality of two congruence closures,
@@ -1516,8 +1506,8 @@ module MayBeEqual = struct
15161506
res
15171507
in
15181508

1519-
if M.tracing then M.trace "c2po-query" "may-point-to %a -> equal terms: %s"
1520-
d_exp exp (List.fold_left (fun s (t,z) -> s ^ "(" ^ T.show t ^","^ Z.to_string Z.(z + offset) ^")") "" equal_terms);
1509+
if M.tracing then M.trace "c2po-query" "may-point-to %a -> equal terms: %a"
1510+
d_exp exp (Pretty.docList ~sep:Pretty.nil (fun (t,z) -> Pretty.dprintf "(%a,%a)" T.pretty t GobZ.pretty Z.(z + offset))) equal_terms;
15211511

15221512
List.fold_left intersect_query_result (AD.top ()) equal_terms
15231513

@@ -1542,8 +1532,8 @@ module MayBeEqual = struct
15421532
with IntDomain.ArithmeticOnIntegerBot _ ->
15431533
AD.bot ()
15441534
in
1545-
M.tracel "c2po-maypointto2" "QUERY MayPointTo. \nres: %a;\nt2: %s; exp2: %a; res: %a; \nmeet: %a; result: %s\n"
1546-
AD.pretty mpt1 (T.show t2) d_plainexp exp2 AD.pretty mpt2 AD.pretty meet (string_of_bool res);
1535+
M.tracel "c2po-maypointto2" "QUERY MayPointTo. \nres: %a;\nt2: %a; exp2: %a; res: %a; \nmeet: %a; result: %B"
1536+
AD.pretty mpt1 T.pretty t2 d_plainexp exp2 AD.pretty mpt2 AD.pretty meet res;
15471537
end;
15481538
res
15491539

@@ -1555,8 +1545,8 @@ module MayBeEqual = struct
15551545
let exp1 = T.to_cil t1 in
15561546
let mpt1 = may_point_to_all_equal_terms ask exp1 cc t1 Z.zero in
15571547
let res = may_point_to_address ask mpt1 t2 off cc in
1558-
if M.tracing && res then M.tracel "c2po-maypointto2" "QUERY MayPointTo. \nres: %a;\nt1: %s; exp1: %a;\n"
1559-
AD.pretty mpt1 (T.show t1) d_plainexp exp1;
1548+
if M.tracing && res then M.tracel "c2po-maypointto2" "QUERY MayPointTo. \nres: %a;\nt1: %a; exp1: %a;"
1549+
AD.pretty mpt1 T.pretty t1 d_plainexp exp1;
15601550
res
15611551

15621552
(** Returns true if `t1` and `t2` may possibly be equal or may possibly overlap. *)
@@ -1597,7 +1587,7 @@ module MayBeEqual = struct
15971587
The parameter s is the size in bits of the variable t1 we are assigning to. *)
15981588
let may_be_equal ask cc size t1 t2 =
15991589
let res = may_be_equal ask cc size t1 t2 in
1600-
if M.tracing then M.tracel "c2po-maypointto" "May be equal: %s %s: %b\n" (T.show t1) (T.show t2) res;
1590+
if M.tracing then M.tracel "c2po-maypointto" "May be equal: %a %a: %b" T.pretty t1 T.pretty t2 res;
16011591
res
16021592

16031593
(**Returns true if `t2` or any subterm of `t2` may possibly point to one of the addresses in `addresses`.*)

src/cdomains/duplicateVars.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ module VarType = struct
7171
| DuplicVar v ->
7272
duplic_var_prefix ^ v.vname ^ duplic_var_postfix
7373

74+
let pretty () v = Pretty.text (show v)
75+
7476
let get_type v = match v with
7577
| AssignAux t
7678
| ReturnAux t -> t

0 commit comments

Comments
 (0)