1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
let opt_files = ref ([] : string list)
let opt_taken = ref ([] : string list)
let opt_taken_list = ref ([] : string list)
let opt_all = ref "all_branches"
let opt_tab_width = ref 4
let opt_index = ref None
let opt_index_default = ref None
let opt_prefix = ref ""
let opt_cumulative_table = ref None
type color = {
hue: int;
saturation: int;
}
let opt_bad_color = ref { hue = 0; saturation = 85 }
let opt_good_color = ref { hue = 120; saturation = 85 }
let opt_darken = ref 5
let clamp_degree n = max 0 (min n 360)
let clamp_percent n = max 0 (min n 100)
let use_alt_colors () =
opt_good_color := { !opt_good_color with hue = 220 };
opt_bad_color := { !opt_good_color with hue = 50 }
let options =
Arg.align [
( "-t",
Arg.String (fun str -> opt_taken := str::!opt_taken),
"<file> coverage information for branches taken while executing the model (default: sail_coverage)");
( "--taken",
Arg.String (fun str -> opt_taken := str::!opt_taken),
" long form of -t");
( "--taken-list",
Arg.String (fun str -> opt_taken_list := str::!opt_taken_list),
"<file> file containing a list of filenames of branches taken");
( "-a",
Arg.String (fun str -> opt_all := str),
"<file> information about all possible branches (default: all_branches)");
( "--all",
Arg.String (fun str -> opt_all := str),
" long form of -a");
( "--tab-width",
Arg.Int (fun n -> opt_tab_width := n),
"<integer> set the tab width for html output (default: 4)");
( "--index",
Arg.String (fun str -> opt_index := Some str),
"<name> create a <name>.html page as an index");
( "--index-default",
Arg.String (fun str -> opt_index_default := Some str),
"<file> The .sail file that will be displayed by the generated index page by default");
( "--prefix",
Arg.String (fun str -> opt_prefix := str),
"<string> Prepend a prefix to all generated html coverage files");
( "--covered-hue",
Arg.Int (fun n -> opt_good_color := { !opt_good_color with hue = clamp_degree n }),
"<int> set the hue (between 0 and 360) of the color used for code that is covered (default: 120 (green))");
( "--uncovered-hue",
Arg.Int (fun n -> opt_bad_color := { !opt_bad_color with hue = clamp_degree n }),
"<int> set the hue (between 0 and 360) of the color used for code that is not covered (default: 0 (red))");
( "--covered-saturation",
Arg.Int (fun n -> opt_good_color := { !opt_good_color with saturation = clamp_percent n }),
"<int> set the saturation (between 0 and 100) of the color used for code that is covered (default: 85)");
( "--uncovered-saturation",
Arg.Int (fun n -> opt_bad_color := { !opt_bad_color with saturation = clamp_percent n }),
"<int> set the hue (between 0 and 100) of the color used for code that is not covered (default: 85)");
( "--nesting-darkness",
Arg.Int (fun n -> opt_darken := n),
"<int> factor which controls how much darker nested spans of the same color become (default: 5)");
( "--alt-colors",
Arg.Unit use_alt_colors,
" swap default colors from red/green into alternate yellow/blue theme");
( "--alt-colours",
Arg.Unit use_alt_colors,
"");
( "--cumulative-table",
Arg.String (fun str -> opt_cumulative_table := Some str),
"<file> write a table of cumulative coverage to file")
]
type span = {
l1 : int;
c1 : int;
l2 : int;
c2 : int;
}
let string_of_span file span =
Printf.sprintf "\"%s\" %d:%d - %d:%d" file span.l1 span.c1 span.l2 span.c2
module Span = struct
type t = span
let compare s1 s2 = compare s1 s2
end
module SpanSet = Set.Make(Span)
module SpanMap = Map.Make(Span)
module StringMap = Map.Make(String)
let add_span spans _ file l1 c1 l2 c2 =
StringMap.update (Filename.basename file)
(fun x ->
let file_spans = Option.value x ~default:SpanSet.empty in
Some (SpanSet.add { l1 = l1; c1 = c1; l2 = l2; c2 = c2 } file_spans))
spans
let read_more_coverage filename spans =
let spans = ref spans in
let chan = open_in filename in
try
let rec loop () =
let line = input_line chan in
spans := Scanf.sscanf line "%c %S, %d, %d, %d, %d" (add_span !spans);
loop ()
in
loop ()
with End_of_file ->
close_in chan;
!spans
let read_coverage filename = read_more_coverage filename StringMap.empty
(** We color the source either red (bad) or green (good) if it's
covered vs uncovered. If we have nested uncovered branches, they
will be increasingly bad, whereas nested covered branches will be
increasingly good. *)
type source_char = {
mutable badness : int;
mutable goodness : int;
mutable bad_zero_width: bool;
char : char;
}
let zero_width span = span.l1 = span.l2 && span.c1 = span.c2
let mark_bad_region source span =
if zero_width span then (
source.(span.l2 - 1).(span.c2 - 1).bad_zero_width <- true
) else (
source.(span.l1 - 1).(span.c1).badness <- source.(span.l1 - 1).(span.c1).badness + 1;
source.(span.l2 - 1).(span.c2 - 1).badness <- source.(span.l2 - 1).(span.c2 - 1).badness - 1
)
let mark_good_region source span =
if not (zero_width span) then (
source.(span.l1 - 1).(span.c1).goodness <- source.(span.l1 - 1).(span.c1).goodness + 1;
source.(span.l2 - 1).(span.c2 - 1).goodness <- source.(span.l2 - 1).(span.c2 - 1).goodness - 1
)
let process_line l = Array.init (String.length l) (fun n -> { badness = 0; goodness = 0; char = l.[n]; bad_zero_width = false })
let read_source filename =
let lines = ref [] in
let chan = open_in filename in
try
let rec loop () =
lines := process_line (input_line chan) :: !lines;
loop ()
in
loop ()
with End_of_file ->
close_in chan;
Array.of_list (List.rev !lines)
let output_html_char chan c =
if c == ' ' then
output_string chan " "
else if c == '<' then
output_string chan "<"
else if c == '>' then
output_string chan ">"
else if c == '"' then
output_string chan """
else if c == '\t' then
output_string chan (String.concat "" (List.init !opt_tab_width (fun _ -> " ")))
else
output_char chan c
let file_info file all taken =
let not_taken = SpanSet.diff all taken in
let percent =
if SpanSet.cardinal all != 0 then
let p = 100. *. (Float.of_int (SpanSet.cardinal taken) /. Float.of_int (SpanSet.cardinal all)) in
Printf.sprintf "%.0f%%" p
else
"-"
in
taken,
not_taken,
Printf.sprintf "%s (%d/%d) %s" (Filename.basename file) (SpanSet.cardinal taken) (SpanSet.cardinal all) percent
let index_css = "
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
table {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border-collapse: collapse;
overflow: hidden;
}
.left {
width: 20%;
}
.left .scroll {
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
.right {
width: 80%;
}
td {
padding: 0;
margin: 0;
}
tr {
padding: 0;
margin: 0;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
}
iframe {
height: 100%;
width: 100%;
display: block;
margin: 0;
padding: 0;
}
"
let html_file_for file =
!opt_prefix ^ Filename.remove_extension (Filename.basename file) ^ ".html"
let read_taken_files () =
match !opt_cumulative_table with
| Some table_name ->
let table_chan = open_out table_name in
List.iter (fun src_file -> Printf.fprintf table_chan "%s, " (Filename.basename src_file)) !opt_files;
Printf.fprintf table_chan ",, Total\n";
let read_more filename spans =
let new_spans = read_more_coverage filename spans in
let counts = List.map (fun src_name ->
let taken = Option.value ~default:SpanSet.empty (StringMap.find_opt (Filename.basename src_name) new_spans) in
SpanSet.cardinal taken
) !opt_files
in
List.iter (fun i -> Printf.fprintf table_chan "%d, " i) counts;
Printf.fprintf table_chan ",, %d\n" (List.fold_left (+) 0 counts);
new_spans
in
let spans = List.fold_right read_more !opt_taken StringMap.empty in
close_out table_chan;
spans
| None ->
List.fold_right read_more_coverage !opt_taken StringMap.empty
let get_file_spans filename all taken =
let file = Filename.basename filename in
let all = match StringMap.find_opt file all with
| None -> Printf.eprintf "Warning: file %s not found\n" file; SpanSet.empty
| Some s -> s
in
let taken = Option.value ~default:SpanSet.empty (StringMap.find_opt file taken) in
all, taken
let main () =
let all = read_coverage !opt_all in
let taken = read_taken_files () in
List.iter (fun file ->
let all, taken = get_file_spans file all taken in
let taken, not_taken, desc = file_info file all taken in
print_endline desc;
let source = read_source file in
SpanSet.iter (mark_good_region source) taken;
SpanSet.iter (mark_bad_region source) not_taken;
let output_file = html_file_for file in
let chan = open_out output_file in
let current_goodness = ref 0 in
let current_badness = ref 0 in
let clamp_lightness l = max 30 (min 80 l) in
let html_color color darkness =
Printf.sprintf "hsl(%d, %d%%, %d%%)"
color.hue color.saturation (clamp_lightness ((80 + !opt_darken) - darkness * !opt_darken))
in
let good_color () = html_color !opt_good_color !current_goodness in
let bad_color () = html_color !opt_bad_color !current_badness in
output_string chan "<!DOCTYPE html>\n";
output_string chan "<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n";
output_string chan (Printf.sprintf "<title>%s</title>" (Filename.remove_extension (Filename.basename file)));
output_string chan "</head>\n";
output_string chan "<body>\n";
output_string chan (Printf.sprintf "<h1>%s</h1>\n" desc);
output_string chan "<code style=\"display: block\">\n";
Array.iter (fun line ->
Array.iter (fun loc ->
if loc.goodness < 0 && loc.badness < 0 then (
output_html_char chan loc.char;
current_goodness := !current_goodness + loc.goodness;
current_badness := !current_badness + loc.badness;
for _ = 1 to abs loc.goodness + abs loc.badness do
output_string chan (Printf.sprintf "</span>")
done
) else if loc.goodness < 0 then (
assert (loc.badness >= 0);
output_html_char chan loc.char;
current_goodness := !current_goodness + loc.goodness;
for _ = 1 to abs loc.goodness do
output_string chan (Printf.sprintf "</span>")
done
) else if loc.badness < 0 then (
assert (loc.goodness >= 0);
output_html_char chan loc.char;
current_badness := !current_badness + loc.badness;
for _ = 1 to abs loc.badness do
output_string chan (Printf.sprintf "</span>")
done
) else if loc.badness > 0 then (
assert (loc.goodness <= 0);
current_badness := !current_badness + loc.badness;
for _ = 1 to loc.badness do
output_string chan (Printf.sprintf "<span style=\"background-color: %s\">" (bad_color ()));
done;
output_html_char chan loc.char
) else if loc.goodness > 0 then (
assert (!current_badness == 0);
current_goodness := !current_goodness + loc.goodness;
for _ = 1 to loc.goodness do
output_string chan (Printf.sprintf "<span style=\"background-color: %s\">" (good_color ()));
done;
output_html_char chan loc.char
) else (
output_html_char chan loc.char
);
if loc.bad_zero_width then (
output_string chan (Printf.sprintf "<span style=\"background-color: %s\">" (bad_color ()));
output_string chan "«Invisible branch not taken here»";
output_string chan "</span>"
);
assert (!current_goodness >= 0);
assert (!current_badness >= 0)
) line;
output_string chan "<br>\n"
) source;
output_string chan "</code>\n";
output_string chan "</body>\n";
output_string chan "</html>";
close_out chan
) !opt_files;
begin match !opt_index with
| Some name ->
let chan = open_out (name ^ ".html") in
output_string chan "<!DOCTYPE html>\n";
output_string chan "<html lang=\"en\">\n";
Printf.ksprintf (output_string chan)
"<head>\n<meta charset=\"utf-8\">\n<title>Coverage Report</title>\n<style>%s</style>\n</head>\n"
index_css;
output_string chan "<body>\n";
output_string chan "<table><tr><td class=\"left\"><div class=\"scroll\">";
List.iter (fun file ->
let all, taken = get_file_spans file all taken in
let _, _, desc = file_info file all taken in
Printf.ksprintf (output_string chan) "<a href=\"%s\" target=\"source\">%s</a><br>\n"
(html_file_for file) desc
) !opt_files;
output_string chan "</div></td>";
output_string chan "<td class=\"right\">";
begin match !opt_index_default with
| Some default_file ->
Printf.ksprintf (output_string chan) "<iframe src=\"%s\" name=\"source\"></iframe>"
(html_file_for default_file);
| None ->
output_string chan "<iframe name=\"source\"></iframe>"
end;
output_string chan "</td></tr></table>\n";
output_string chan "</body>\n";
output_string chan "</html>";
close_out chan
| None -> ()
end
let usage_msg = "usage: sailcov -t <file> -a <file> <.sail files>\n"
let read_taken_lists () =
List.iter (fun filename ->
let chan = open_in filename in
try
let rec loop () =
opt_taken := (input_line chan)::!opt_taken;
loop ()
in loop ()
with End_of_file ->
()) !opt_taken_list
let _ =
Arg.parse options
(fun s -> opt_files := !opt_files @ [s])
usage_msg;
read_taken_lists ();
begin opt_taken := match !opt_taken with
| [] -> ["sail_coverage"]
| l -> List.rev l
end;
try main () with
| Sys_error msg -> prerr_endline msg; exit 1
|