Skip to content

Commit 719a4d1

Browse files
committed
[windows] fix compatibility
+ Improve check.ml to normalize expected paths as soon as they are extracted from the report files. + Fix aggregate.ml to avoid explicit usage of '/' when manipulating paths + deadCommon.ml uses Filename.dir_sep instead of '/' when manipulating paths
1 parent b02278c commit 719a4d1

5 files changed

Lines changed: 99 additions & 90 deletions

File tree

check/src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
COMPFLAGS=-w +A-4-9-40-42 -bin-annot -keep-locs
1+
COMPFLAGS=-g -w +A-4-9-40-42 -bin-annot -keep-locs
22
OCAMLC=ocamlc $(COMPFLAGS)
33
OCAMLOPT=ocamlopt $(COMPFLAGS)
44

check/src/aggregate.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let update state line =
6464
let state =
6565
let end_of_fp = "Should not be detected" ^ PP.style_reset in
6666
let end_of_fn = "Not detected" ^ PP.style_reset in
67-
if String.starts_with ~prefix:"./examples" line then
67+
if String.starts_with ~prefix:(Filename.concat "." "examples") line then
6868
let unique_success_lines =
6969
add_unique_line state.State.unique_success_lines
7070
in

check/src/check.ml

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,54 @@ module StringSet = Set.Make(String)
3636
module SectionMap = Map.Make(Section)
3737

3838
module Reports = struct
39+
40+
type report_info = {
41+
filepath: string;
42+
line_nb : int;
43+
value : string;
44+
}
45+
46+
let line_of_report_info ri =
47+
Printf.sprintf "%s:%d:%s" ri.filepath ri.line_nb ri.value
48+
49+
(* Format of report lines is : "file_path:line_number: value"
50+
with value possibly containing ':'. In case the line comes from
51+
the direct report of dca (is_res_line), the filepath will be relocated
52+
to correspond to filepaths coming from expected reports *)
53+
let report_info_of_line ~is_res_line line =
54+
let report_line_format = "filepath:line_nb:value" in
55+
match String.split_on_char ':' line with
56+
| [] | _::[] | _::_::[] ->
57+
let err =
58+
Printf.sprintf
59+
"Unrecognized report line format. Expected : '%s'"
60+
report_line_format
61+
in
62+
PP.error ~err ~ctx:line ();
63+
None
64+
| filepath::line_number::value ->
65+
try
66+
let line_nb = int_of_string line_number in
67+
let filepath = (* relocate to match expected paths *)
68+
if is_res_line then Path.relocate filepath
69+
else filepath
70+
in
71+
let filepath = Path.normalize filepath in
72+
let value = String.concat ":" value in
73+
Some {filepath; line_nb; value}
74+
with Failure _int_of_string ->
75+
let err =
76+
Printf.sprintf
77+
"Is not an int. Expected report line format is : '%s'"
78+
report_line_format
79+
in
80+
PP.error ~err ~ctx:line_number ();
81+
None
82+
3983
type t = {
4084
current_filepath : string option; (* file containg current expected reports *)
41-
remaining_content : string list; (* expected reports in filename not
42-
observed yet *)
85+
remaining_content : report_info list; (* expected reports in filename not
86+
observed yet *)
4387
root : string; (* directory containing the expected reports files*)
4488
files_map : StringSet.t SectionMap.t (* remaining files containing expected
4589
reports. Once a file is consumed it
@@ -130,28 +174,30 @@ module State = struct
130174
let scores = Scores.incr_fn state.scores in
131175
{state with scores}
132176

133-
let report_fn exp_line state =
134-
PP.error ~err:"Not detected" ~ctx:exp_line ();
177+
let report_fn ri state =
178+
let ctx = Reports.line_of_report_info ri in
179+
PP.error ~err:"Not detected" ~ctx ();
135180
incr_fn state
136181

137182
let incr_fp state =
138183
let scores = Scores.incr_fp state.scores in
139184
{state with scores}
140185

141-
let report_fp res_line state =
142-
PP.error ~err:"Should not be detected" ~ctx:res_line ();
186+
let report_fp ri state =
187+
let ctx = Reports.line_of_report_info ri in
188+
PP.error ~err:"Should not be detected" ~ctx ();
143189
incr_fp state
144190

145191
let incr_success state =
146192
let scores = Scores.incr_success state.scores in
147193
{state with scores}
148194

149-
let report_success res_line state =
150-
print_endline res_line;
195+
let report_success ri state =
196+
let line = Reports.line_of_report_info ri in
197+
print_endline line;
151198
incr_success state
152199

153200
let update_remaining_content state remaining_content =
154-
let remaining_content = List.filter (( <> ) "") remaining_content in
155201
let expected_reports = {state.expected_reports with remaining_content} in
156202
{state with expected_reports}
157203

@@ -206,6 +252,8 @@ module State = struct
206252
let current_filepath = Some exp_filepath in
207253
let state =
208254
In_channel.with_open_text exp_filepath In_channel.input_lines
255+
|> List.filter (( <> ) "")
256+
|> List.filter_map (Reports.report_info_of_line ~is_res_line:false)
209257
|> update_remaining_content state
210258
in
211259
let expected_reports =
@@ -269,82 +317,39 @@ module State = struct
269317

270318
end
271319

272-
(* Format of report lines is : "file_path:line_number: report_info"
273-
with report_info possibly containing ':'. In case the line comes from
274-
the direct report of dca (is_res_line), the filepath will be relocated
275-
to correspond to filepaths coming from expected reports *)
276-
let infos_of_report_line ~is_res_line line =
277-
let report_line_format = "filepath:line_nb:report_info" in
278-
match String.split_on_char ':' line with
279-
| [] | _::[] | _::_::[] ->
280-
let err =
281-
Printf.sprintf
282-
"Unrecognized report line format. Expected : '%s'"
283-
report_line_format
284-
in
285-
PP.error ~err ~ctx:line ();
286-
None
287-
| filepath::line_number::report_info ->
288-
try
289-
let line_nb = int_of_string line_number in
290-
let filepath = (* relocate to match expected paths *)
291-
if is_res_line then Path.relocate filepath
292-
else filepath
293-
in
294-
let filepath = Path.normalize filepath in
295-
let report_info = String.concat ":" report_info in
296-
let line = (* recontruct the line with updated fields *)
297-
if is_res_line then
298-
String.concat ":" [filepath; line_number; report_info]
299-
else line
300-
in
301-
Some (filepath, line_nb, report_info, line)
302-
with Failure _int_of_string ->
303-
let err =
304-
Printf.sprintf
305-
"Is not an int. Expected report line format is : '%s'"
306-
report_line_format
307-
in
308-
PP.error ~err ~ctx:line_number ();
309-
None
310320

311-
let rec process_report_line state (filepath, line_number, report_info, res_line) =
312-
let state = State.maybe_change_file filepath state in
321+
let rec process_report_line state (got : Reports.report_info) =
322+
let state = State.maybe_change_file got.filepath state in
313323
match state.expected_reports.remaining_content with
314-
| [] -> State.report_fp res_line state
315-
| exp_line::remaining_content when exp_line = res_line ->
324+
| [] -> State.report_fp got state
325+
| expected::remaining_content when expected = got ->
316326
State.update_remaining_content state remaining_content
317-
|> State.report_success res_line
318-
| exp_line::remaining_content ->
319-
match infos_of_report_line ~is_res_line:false exp_line with
320-
| None ->
321-
(* exp_line reported in infos_of_report_line as misformatted *)
322-
state
323-
| Some (exp_filepath, exp_line_number, _, exp_line) ->
324-
let compare =
325-
let paths_compare = String.compare exp_filepath filepath in
326-
if paths_compare = 0 then exp_line_number - line_number
327-
else paths_compare
327+
|> State.report_success expected
328+
| expected::remaining_content ->
329+
let compare =
330+
let paths_compare = String.compare expected.filepath got.filepath in
331+
if paths_compare = 0 then expected.line_nb - got.line_nb
332+
else paths_compare
333+
in
334+
if compare > 0 then State.report_fp got state
335+
else if compare < 0 then
336+
let state =
337+
State.update_remaining_content state remaining_content
338+
|> State.report_fn expected
328339
in
329-
if compare > 0 then State.report_fp res_line state
330-
else if compare < 0 then
331-
let state =
332-
State.update_remaining_content state remaining_content
333-
|> State.report_fn exp_line
334-
in
335-
process_report_line state (filepath, line_number, report_info, res_line)
336-
else
337-
(* The location is fine but report_info does not match.
338-
The reports are not organized according to the report_info but
339-
only the locations (including the column which is not reported.
340-
Check if the current line exists in the remaining_content.
341-
If so, then it is a successful report which can be removed from
342-
the remaining content. Otherwise, it is a fp. *)
343-
if List.mem res_line remaining_content then
344-
List.filter (( <> ) res_line) remaining_content
345-
|> State.update_remaining_content state
346-
|> State.report_success res_line
347-
else State.report_fp res_line state
340+
process_report_line state got
341+
else
342+
(* The location is fine but report_info does not match.
343+
The reports are not organized according to the report_info but
344+
only the locations (including the column which is not reported.
345+
Check if the current line exists in the remaining_content.
346+
If so, then it is a successful report which can be removed from
347+
the remaining content. Otherwise, it is a fp. *)
348+
if List.mem got remaining_content then
349+
List.filter (( <> ) got) remaining_content
350+
|> State.update_remaining_content state
351+
|> State.report_success got
352+
else State.report_fp got state
348353

349354
let process state res_line =
350355
let is_report_line, state =
@@ -359,12 +364,12 @@ let process state res_line =
359364
| Some _ as sec ->
360365
false, State.change_section sec state
361366
| None -> (* res_line is a report line *)
362-
match infos_of_report_line ~is_res_line:true res_line with
367+
match Reports.report_info_of_line ~is_res_line:true res_line with
363368
| None ->
364369
(* res_line reported in infos_of_report_line as misformatted *)
365370
false, state
366-
| Some infos ->
367-
true, process_report_line state infos
371+
| Some got ->
372+
true, process_report_line state got
368373
in
369374
if not is_report_line then print_endline res_line;
370375
state

src/deadCommon.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,21 @@ let hashtbl_replace_list hashtbl key l =
104104

105105
let hashtbl_merge_unique_list tbl1 key1 tbl2 key2 =
106106
List.iter (fun elt -> hashtbl_add_unique_to_list tbl1 key1 elt) (hashtbl_find_list tbl2 key2)
107-
let is_sub_path ?(sep = '/') sub_path path =
107+
108+
let is_sub_path ~sep sub_path path =
108109
let len_sub = String.length sub_path in
109110
let len_path = String.length path in
110111
let diff_len = len_path - len_sub in
112+
let len_sep = String.length sep in
111113
let compatible_length =
112114
(* sub_path is smaller than path and would start right after a separator*)
113-
diff_len = 0 || diff_len > 0 && path.[diff_len - 1] = sep
115+
diff_len >= len_sep && String.sub path (diff_len - len_sep) len_sep = sep
116+
|| diff_len = 0
114117
in
115118
compatible_length && String.sub path diff_len len_sub = sub_path
116119

117-
let find_path fn ?(sep = '/') l =
120+
let find_path fn l =
121+
let sep = Filename.dir_sep in
118122
List.find (is_sub_path ~sep fn) l
119123

120124
let find_abspath fn =

src/deadObj.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let get_loc path =
7575
let path =
7676
let exported_path =
7777
Hashtbl.to_seq_values incl
78-
|> Seq.find (fun (_, exported_path) -> is_sub_path ~sep:'.' path exported_path)
78+
|> Seq.find (fun (_, exported_path) -> is_sub_path ~sep:"." path exported_path)
7979
in
8080
match exported_path with
8181
| Some (_, exported_path) -> exported_path

0 commit comments

Comments
 (0)