Debian Codegraph @

lib/aux_file.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* The file format is a header
12 * ("COQAUX%d %s %s\n" version hex_hash path)
13 * followed by an arbitrary number of entries
14 * ("%d %d %s %S\n" loc_begin loc_end key value) *)
15
16 open Filename
17
18 let version = 1
19
20 let oc = ref None
21
22 let aux_file_name_for vfile =
23 let fn = Printf.sprintf ".%s.aux" (chop_extension(basename vfile)) in
24 Filename.concat (dirname vfile) fn
25
26 let mk_absolute vfile =
27 let vfile = CUnix.remove_path_dot vfile in
28 if Filename.is_relative vfile then CUnix.correct_path vfile (Sys.getcwd ())
29 else vfile
30
31 let start_aux_file ~aux_file:output_file ~v_file =
32 let vfile = mk_absolute v_file in
33 try
34 oc := Some (open_out output_file);
35 Printf.fprintf (Option.get !oc) "COQAUX%d %s %s\n"
36 version (Digest.to_hex (Digest.file vfile)) vfile
37 with Sys_error _ -> ()
38
39 let stop_aux_file () =
40 Option.iter close_out !oc;
41 oc := None
42
43 let recording () = not (Option.is_empty !oc)
44
45 module H = Map.Make(struct type t = int * int let compare = compare end)
46 module M = Map.Make(String)
47 type data = string M.t
48 type aux_file = data H.t
49
50 let contents x = x
51
52 let empty_aux_file = H.empty
53
54 let get ?loc aux key = M.find key (H.find (Option.cata Loc.unloc (0,0) loc) aux)
55
56 let record_in_aux_at ?loc key v =
57 Option.iter (fun oc ->
58 match loc with
59 | Some loc -> let i, j = Loc.unloc loc in
60 Printf.fprintf oc "%d %d %s %S\n" i j key v
61 | None -> Printf.fprintf oc "0 0 %s %S\n" key v
62 ) !oc
63
64 let current_loc : Loc.t option ref = ref None
65
66 let record_in_aux_set_at ?loc () = current_loc := loc
67
68 let record_in_aux key v = record_in_aux_at ?loc:!current_loc key v
69
70 let set h loc k v =
71 let m = try H.find loc h with Not_found -> M.empty in
72 H.add loc (M.add k v m) h
73
74 let load_aux_file_for vfile =
75 let vfile = mk_absolute vfile in
76 let ret3 x y z = x, y, z in
77 let ret4 x y z t = x, y, z, t in
78 let h = ref empty_aux_file in
79 let add loc k v = h := set !h loc k v in
80 let aux_fname = aux_file_name_for vfile in
81 try
82 let ch = open_in aux_fname in
83 Util.try_finally (fun () ->
84 let ib = Scanf.Scanning.from_channel ch in
85 let ver, hash, fname =
86 Scanf.bscanf ib "COQAUX%d %s %s\n" ret3 in
87 if ver <> version then raise (Failure "aux file version mismatch");
88 if fname <> vfile then
89 raise (Failure "aux file name mismatch");
90 let only_dummyloc = Digest.to_hex (Digest.file vfile) <> hash in
91 while true do
92 let i, j, k, v = Scanf.bscanf ib "%d %d %s %S\n" ret4 in
93 if not only_dummyloc || (i = 0 && j = 0) then add (i,j) k v;
94 done;
95 raise End_of_file)
96 ()
97 (fun () -> close_in ch)
98 ()
99 with
100 | End_of_file -> !h
101 | Sys_error s | Scanf.Scan_failure s
102 | Failure s | Invalid_argument s ->
103 CDebug.pp_misc Pp.(fun () -> str"Loading file "++str aux_fname++str": "++str s);
104 empty_aux_file
105
106 let set ?loc h k v = set h (Option.cata Loc.unloc (0,0) loc) k v