Skip to content

Commit 3b66ba0

Browse files
authored
Merge pull request #1880 from goblint/channel-with
Use standard `In_channel.with_open_*` and `Out_channel.with_open_*` where possible
2 parents 49b112c + 335469c commit 3b66ba0

File tree

9 files changed

+19
-38
lines changed

9 files changed

+19
-38
lines changed

src/analyses/basePriv.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,12 +2031,11 @@ struct
20312031
v
20322032

20332033
let dump () =
2034-
let f = open_out_bin (get_string "exp.priv-prec-dump") in
2034+
Out_channel.with_open_bin (get_string "exp.priv-prec-dump") @@ fun f ->
20352035
(* LVH.iter (fun (l, x) v ->
20362036
Logs.debug "%a %a = %a" CilType.Location.pretty l CilType.Varinfo.pretty x VD.pretty v
20372037
) lvh; *)
2038-
Marshal.output f ({name = get_string "ana.base.privatization"; results = lvh}: result);
2039-
close_out_noerr f
2038+
Stdlib.Marshal.to_channel f ({name = get_string "ana.base.privatization"; results = lvh}: result) []
20402039

20412040
let finalize () =
20422041
if !is_dumping then

src/common/framework/cfgTools.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,11 @@ let fprint_hash_dot cfg =
600600
let extraNodeStyles node = []
601601
end
602602
in
603-
let out = open_out "cfg.dot" in
603+
Out_channel.with_open_text "cfg.dot" @@ fun out ->
604604
let iter_edges f = H.iter (fun n es -> List.iter (f n) es) cfg in
605605
let ppf = Format.formatter_of_out_channel out in
606606
fprint_dot (module CfgPrinters (NoExtraNodeStyles)) iter_edges ppf;
607-
Format.pp_print_flush ppf ();
608-
close_out out
607+
Format.pp_print_flush ppf ()
609608

610609

611610
let getCFG (file: file) : cfg * cfg * _ =
@@ -664,11 +663,10 @@ let dead_code_cfg ~path (module FileCfg: MyCFG.FileCfg) live =
664663
let dot_file_name = fd.svar.vname^".dot" in
665664
let file_dir = GobSys.mkdir_or_exists_absolute Fpath.(base_dir / c_file_name) in
666665
let fname = Fpath.(file_dir / dot_file_name) in
667-
let out = open_out (Fpath.to_string fname) in
666+
Out_channel.with_open_text (Fpath.to_string fname) @@ fun out ->
668667
let ppf = Format.formatter_of_out_channel out in
669668
fprint_fundec_html_dot (module FileCfg.Cfg) live fd ppf;
670669
Format.pp_print_flush ppf ();
671-
close_out out
672670
| _ -> ()
673671
)
674672

src/config/gobConfig.ml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,7 @@ struct
415415
in
416416
match file with
417417
| Some fn ->
418-
let v =
419-
let ic = Stdlib.open_in (Fpath.to_string fn) in
420-
Fun.protect ~finally:(fun () ->
421-
Stdlib.close_in ic
422-
) (fun () ->
423-
Yojson.Safe.from_channel ic
424-
)
425-
in
418+
let v = In_channel.with_open_text (Fpath.to_string fn) Yojson.Safe.from_channel in
426419
merge v;
427420
if Goblint_tracing.tracing then Goblint_tracing.trace "conf" "Merging with '%a', resulting\n%a." GobFpath.pretty fn GobYojson.pretty !json_conf
428421
| None -> raise (Sys_error (Printf.sprintf "%s: No such file or directory" (Fpath.to_string fn)))

src/framework/control.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,9 @@ struct
582582
end
583583
in
584584
(* Yojson.Safe.to_file meta Meta.json; *)
585-
Yojson.Safe.pretty_to_channel (Stdlib.open_out (Fpath.to_string meta)) Meta.json; (* the above is compact, this is pretty-printed *)
585+
Out_channel.with_open_text (Fpath.to_string meta) (fun oc ->
586+
Yojson.Safe.pretty_to_channel oc Meta.json (* the above is compact, this is pretty-printed *)
587+
);
586588
if gobview then (
587589
Logs.Format.debug "Saving the analysis table to %a, the CIL state to %a, the warning table to %a, and the runtime stats to %a" Fpath.pp analyses Fpath.pp cil Fpath.pp warnings Fpath.pp stats;
588590
Serialize.marshal MCPRegistry.registered_name analyses;
@@ -789,13 +791,10 @@ struct
789791
end
790792
in
791793
let module ArgDot = ArgTools.Dot (Arg) (NoLabelNodeStyle) in
792-
let oc = Batteries.open_out arg_dot_path in
793-
Fun.protect (fun () ->
794-
let ppf = Format.formatter_of_out_channel oc in
794+
Out_channel.with_open_text arg_dot_path (fun oc ->
795+
let ppf = Stdlib.Format.formatter_of_out_channel oc in
795796
ArgDot.dot ppf;
796797
Format.pp_print_flush ppf ()
797-
) ~finally:(fun () ->
798-
Batteries.close_out oc
799798
)
800799
);
801800
ArgTools.current_arg := Some (module Arg);

src/incremental/serialize.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
(** Serialization/deserialization of incremental analysis data. *)
22

3-
open Batteries
4-
53
(* TODO: GoblintDir *)
64
let incremental_data_file_name = "analysis.data"
75
let results_dir = "results"
@@ -22,13 +20,12 @@ let gob_results_dir op =
2220
let server () = GobConfig.get_bool "server.enabled"
2321

2422
let marshal obj fileName =
25-
let chan = open_out_bin (Fpath.to_string fileName) in
26-
Marshal.output chan obj;
27-
close_out chan
23+
Out_channel.with_open_bin (Fpath.to_string fileName) @@ fun chan ->
24+
Marshal.to_channel chan obj []
2825

2926
let unmarshal fileName =
3027
Logs.debug "Unmarshalling %s... If type of content changed, this will result in a segmentation fault!" (Fpath.to_string fileName);
31-
Marshal.input (open_in_bin (Fpath.to_string fileName))
28+
In_channel.with_open_bin (Fpath.to_string fileName) Marshal.from_channel
3229

3330
let results_exist () =
3431
(* If Goblint did not crash irregularly, the existence of the result directory indicates that there are results *)

src/solver/sLR.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ module PrintInfluence =
481481
struct
482482
module S1 = Sol (S) (HM)
483483
let solve x y =
484-
let ch = Legacy.open_out "test.dot" in
484+
Out_channel.with_open_text "test.dot" @@ fun ch ->
485485
let r = S1.solve x y in
486486
let f k _ =
487487
let q = if HM.mem S1.wpoint k then " shape=box style=rounded" else "" in
@@ -498,7 +498,6 @@ module PrintInfluence =
498498
ignore (Pretty.fprintf ch "digraph G {\nedge [arrowhead=vee];\n");
499499
HM.iter f r;
500500
ignore (Pretty.fprintf ch "}\n");
501-
Legacy.close_out_noerr ch;
502501
r
503502
end
504503

src/transform/transform.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ let run_transformations ?(file_output = true) file names ask =
3737

3838
if file_output && List.exists (fun (_, (module T : S)) -> T.requires_file_output) active_transformations then
3939
let filename = GobConfig.get_string "trans.output" in
40-
let oc = Stdlib.open_out filename in
40+
Out_channel.with_open_text filename @@ fun oc ->
4141
GobRef.wrap GoblintCil.lineDirectiveStyle None @@ fun () ->
42-
dumpFile defaultCilPrinter oc filename file;
43-
Stdlib.close_out oc
42+
dumpFile defaultCilPrinter oc filename file
4443

4544
let run file name = run_transformations ~file_output:false file [name]
4645

src/util/precCompare.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ struct
9595
open Util
9696

9797
let load filename =
98-
let f = open_in_bin filename in
99-
let dump: dump = Marshal.from_channel f in
98+
let dump: dump = In_channel.with_open_bin filename Stdlib.Marshal.from_channel in
10099
let dump: result = {name = dump.name; results = unmarshal dump.marshalled } in
101-
close_in_noerr f;
102100
dump
103101

104102
module CompareDump = MakeHashtbl (Key) (Dom) (RH)

tests/regression/cfg/util/cfgDot.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ let main () =
8787

8888
GoblintCil.iterGlobals ast (function
8989
| GFun (fd, _) ->
90-
let out = open_out (fd.svar.vname ^ ".dot") in
90+
Out_channel.with_open_text (fd.svar.vname ^ ".dot") @@ fun out ->
9191
let iter_edges = CfgTools.iter_fd_edges (module Cfg) fd in
9292
let ppf = Format.formatter_of_out_channel out in
9393
CfgTools.fprint_dot (module CfgTools.CfgPrinters (LocationExtraNodeStyles)) iter_edges ppf;
9494
Format.pp_print_flush ppf ();
95-
close_out out
9695
| _ -> ()
9796
)
9897

0 commit comments

Comments
 (0)