Source file instrument.ml
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
module Ast = Ast_405
module Location = Ast.Location
module Parsetree = Ast.Parsetree
module Pat = Ast.Ast_helper.Pat
module Exp = Ast.Ast_helper.Exp
module Str = Ast.Ast_helper.Str
module Cf = Ast.Ast_helper.Cf
module Ast_convenience = Ast_convenience_405
module Ast_mapper_class = Ast_mapper_class_405
module Generated_code :
sig
type points
val init : unit -> points
val instrument_expr :
points -> ?override_loc:Location.t -> Parsetree.expression ->
Parsetree.expression
val instrument_case :
points -> Parsetree.case -> Parsetree.case
val instrument_class_field_kind :
points -> Parsetree.class_field_kind -> Parsetree.class_field_kind
val runtime_initialization :
points -> string -> Parsetree.structure_item list
end =
struct
type points = Bisect.Common.point_definition list ref
let init () = ref []
let instrument_expr points ?override_loc e =
let rec outline () =
let point_loc = choose_location_of_point ~override_loc e in
if expression_should_not_be_instrumented ~point_loc then
e
else
let point_index = get_index_of_point_at_location ~point_loc in
[%expr ___bisect_visit___ [%e point_index]; [%e e]]
[@metaloc point_loc]
and choose_location_of_point ~override_loc e =
match override_loc with
| Some override_loc -> override_loc
| _ -> Parsetree.(e.pexp_loc)
and expression_should_not_be_instrumented ~point_loc:loc =
if Location.(loc.loc_ghost) then
true
else
let file, line =
let start = Location.(loc.loc_start) in
Lexing.(start.pos_fname, start.pos_lnum)
in
Comments.get file
|> Comments.line_is_ignored line
and get_index_of_point_at_location ~point_loc:loc =
let point_offset = Location.(Lexing.(loc.loc_start.pos_cnum)) in
let point =
try
List.find
(fun point -> Bisect.Common.(point.offset) = point_offset)
!points
with Not_found ->
let new_index = List.length !points in
let new_point =
Bisect.Common.{offset = point_offset; identifier = new_index} in
points := new_point::!points;
new_point
in
Ast_convenience.int point.identifier
in
outline ()
let instrument_case points case =
let module Helper_types =
struct
type location_trace = Location.t list
type rotated_case = location_trace * Parsetree.pattern
end
in
let open Helper_types in
let rec outline () =
if is_assert_false_or_refutation case then
case
else
let entire_pattern = Parsetree.(case.pc_lhs) in
let loc = Parsetree.(entire_pattern.ppat_loc) in
let non_exception_pattern, reassemble_exception_pattern_if_present =
go_into_exception_pattern_if_present ~entire_pattern in
let rotated_cases : rotated_case list =
rotate_or_patterns_to_top loc ~non_exception_pattern in
match rotated_cases with
| [] ->
empty_case_list case
| [(location_trace, _)] ->
no_or_patterns case location_trace
| _::_::_ ->
let new_case_pattern_with_alias =
add_bisect_matched_value_alias loc ~non_exception_pattern
|> reassemble_exception_pattern_if_present
in
let new_case_expr_with_nested_match =
generate_nested_match loc rotated_cases in
Exp.case
new_case_pattern_with_alias
?guard:(instrument_when_clause case)
new_case_expr_with_nested_match
and is_assert_false_or_refutation case =
match case.pc_rhs with
| [%expr assert false] -> true
| {pexp_desc = Pexp_unreachable; _} -> true
| _ -> false
and go_into_exception_pattern_if_present ~entire_pattern
: Parsetree.pattern * (Parsetree.pattern -> Parsetree.pattern) =
match entire_pattern with
| [%pat? exception [%p? nested_pattern]] ->
(nested_pattern,
(fun p ->
Parsetree.{entire_pattern with ppat_desc = Ppat_exception p}))
| _ ->
(entire_pattern,
(fun p -> p))
and empty_case_list case =
Parsetree.{case with
pc_rhs = instrument_expr points case.pc_rhs;
pc_guard = instrument_when_clause case}
and no_or_patterns case location_trace =
Parsetree.{case with
pc_rhs = instrumentation_for_location_trace case.pc_rhs location_trace;
pc_guard = instrument_when_clause case}
and instrument_when_clause case =
match Parsetree.(case.pc_guard) with
| None -> None
| Some guard -> Some (instrument_expr points guard)
and instrumentation_for_location_trace e location_trace =
location_trace
|> List.sort_uniq (fun l l' ->
l.Location.loc_start.Lexing.pos_cnum -
l'.Location.loc_start.Lexing.pos_cnum)
|> List.fold_left (fun e l ->
instrument_expr points ~override_loc:l e) e
and add_bisect_matched_value_alias loc ~non_exception_pattern =
[%pat? [%p non_exception_pattern] as ___bisect_matched_value___]
[@metaloc loc]
and generate_nested_match loc rotated_cases =
(rotated_cases
|> List.map (fun (location_trace, rotated_pattern) ->
Exp.case
rotated_pattern
(instrumentation_for_location_trace [%expr ()] location_trace))
|> fun nested_match_cases ->
nested_match_cases @ [Exp.case [%pat? _] [%expr ()]]
|> Exp.match_ ~loc ([%expr ___bisect_matched_value___])
|> fun nested_match ->
Exp.attr
nested_match
(Location.mkloc "ocaml.warning" loc,
PStr [[%stri "-4-8-9-11-26-27-28"]])
|> fun nested_match_with_attribute ->
[%expr [%e nested_match_with_attribute]; [%e case.pc_rhs]])
[@metaloc loc]
and rotate_or_patterns_to_top loc ~non_exception_pattern
: rotated_case list =
let rec recurse ~enclosing_loc p : rotated_case list =
let loc = Parsetree.(p.ppat_loc) in
let attrs = Parsetree.(p.ppat_attributes) in
match p.ppat_desc with
| Ppat_any | Ppat_var _ | Ppat_constant _ | Ppat_interval _
| Ppat_construct (_, None) | Ppat_variant (_, None) | Ppat_type _
| Ppat_unpack _ | Ppat_extension _ ->
[([enclosing_loc], p)]
| Ppat_alias (p', x) ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.alias ~loc ~attrs p'' x))
| Ppat_construct (c, Some p') ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.construct ~loc ~attrs c (Some p'')))
| Ppat_variant (c, Some p') ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.variant ~loc ~attrs c (Some p'')))
| Ppat_constraint (p', t) ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.constraint_ ~loc ~attrs p'' t))
| Ppat_lazy p' ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.lazy_ ~loc ~attrs p''))
| Ppat_open (c, p') ->
recurse ~enclosing_loc p'
|> List.map (fun (location_trace, p'') ->
(location_trace, Pat.open_ ~loc ~attrs c p''))
| Ppat_tuple ps ->
ps
|> List.map (recurse ~enclosing_loc)
|> all_combinations
|> List.map (fun (location_trace, ps') ->
(location_trace, Pat.tuple ~loc ~attrs ps'))
| Ppat_record (entries, closed) ->
let labels, ps = List.split entries in
ps
|> List.map (recurse ~enclosing_loc)
|> all_combinations
|> List.map (fun (location_trace, ps') ->
(location_trace,
Pat.record ~loc ~attrs (List.combine labels ps') closed))
| Ppat_array ps ->
ps
|> List.map (recurse ~enclosing_loc)
|> all_combinations
|> List.map (fun (location_trace, ps') ->
location_trace, Pat.array ~loc ~attrs ps')
| Ppat_or (p_1, p_2) ->
let ps_1 = recurse ~enclosing_loc:p_1.ppat_loc p_1 in
let ps_2 = recurse ~enclosing_loc:p_2.ppat_loc p_2 in
ps_1 @ ps_2
| Ppat_exception _ -> []
and all_combinations
: rotated_case list list ->
(location_trace * Parsetree.pattern list) list =
function
| [] -> []
| cases::more ->
let multiply product cases =
product |> List.map (fun (location_trace_1, ps) ->
cases |> List.map (fun (location_trace_2, p) ->
location_trace_1 @ location_trace_2, ps @ [p]))
|> List.flatten
in
let initial =
cases
|> List.map (fun (location_trace, p) -> location_trace, [p])
in
List.fold_left multiply initial more
in
recurse ~enclosing_loc:loc non_exception_pattern
in
outline ()
let instrument_class_field_kind points = function
| Parsetree.Cfk_virtual _ as cf ->
cf
| Parsetree.Cfk_concrete (o,e) ->
Cf.concrete o (instrument_expr points e)
let runtime_initialization points file =
let loc = Location.in_file file in
let mangled_module_name =
let buffer = Buffer.create ((String.length file) * 2) in
file |> String.iter (function
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' as c ->
Buffer.add_char buffer c
| _ ->
Buffer.add_string buffer "___");
"Bisect_visit___" ^ (Buffer.contents buffer)
in
let point_count = Ast_convenience.int ~loc (List.length !points) in
let points_data =
Ast_convenience.str ~loc (Bisect.Common.write_points !points) in
let file = Ast_convenience.str ~loc file in
let generated_module =
let bisect_visit_function =
[%stri
let ___bisect_visit___ =
let point_definitions = [%e points_data] in
let `Staged cb =
Bisect.Runtime.register_file
[%e file] ~point_count:[%e point_count] ~point_definitions
in
cb
]
[@metaloc loc]
in
let open Ast.Ast_helper in
Str.module_ ~loc @@
Mb.mk ~loc
(Location.mkloc mangled_module_name loc)
(Mod.structure ~loc [bisect_visit_function])
in
let module_open =
let open Ast.Ast_helper in
Str.open_ ~loc @@
Opn.mk ~loc
(Ast_convenience.lid ~loc mangled_module_name)
in
[generated_module; module_open]
end
class instrumenter =
let points = Generated_code.init () in
let instrument_expr = Generated_code.instrument_expr points in
let instrument_case = Generated_code.instrument_case points in
let instrument_class_field_kind =
Generated_code.instrument_class_field_kind points in
object (self)
inherit Ast_mapper_class.mapper as super
method! class_expr ce =
let loc = ce.pcl_loc in
let ce = super#class_expr ce in
match ce.pcl_desc with
| Pcl_apply (ce, args) ->
let args =
List.map
(fun (label, e) ->
(label, (instrument_expr e)))
args
in
Ast.Ast_helper.Cl.apply ~loc ~attrs:ce.pcl_attributes ce args
| _ ->
ce
method! class_field cf =
let loc = cf.pcf_loc in
let attrs = cf.pcf_attributes in
let cf = super#class_field cf in
match cf.pcf_desc with
| Pcf_val (name, mutable_, cf) ->
Cf.val_ ~loc ~attrs name mutable_ (instrument_class_field_kind cf)
| Pcf_method (name, private_, cf) ->
Cf.method_ ~loc ~attrs name private_ (instrument_class_field_kind cf)
| Pcf_initializer e ->
Cf.initializer_ ~loc ~attrs (instrument_expr e)
| _ ->
cf
method! expr e =
let loc = e.pexp_loc in
let attrs = e.pexp_attributes in
let e' = super#expr e in
match e'.pexp_desc with
| Pexp_let (rec_flag, bindings, e) ->
let bindings =
List.map (fun binding ->
Parsetree.{binding with pvb_expr =
instrument_expr binding.pvb_expr})
bindings
in
Exp.let_ ~loc ~attrs rec_flag bindings (instrument_expr e)
| Pexp_poly (e, type_) ->
Exp.poly ~loc ~attrs (instrument_expr e) type_
| Pexp_fun (label, default_value, p, e) ->
let default_value =
match default_value with
| None -> None
| Some default_value -> Some (instrument_expr default_value)
in
Exp.fun_ ~loc ~attrs label default_value p (instrument_expr e)
| Pexp_apply (e_function, [label_1, e1; label_2, e2]) ->
begin match e_function with
| [%expr (&&)]
| [%expr (&)]
| [%expr (||)]
| [%expr (or)] ->
Exp.apply ~loc ~attrs e_function
[label_1, (instrument_expr e1); label_2, (instrument_expr e2)]
| [%expr (|>)] ->
Exp.apply ~loc ~attrs e_function
[label_1, e1; label_2, (instrument_expr e2)]
| _ ->
e'
end
| Pexp_match (e, cases) ->
List.map instrument_case cases
|> Exp.match_ ~loc ~attrs e
| Pexp_function cases ->
List.map instrument_case cases
|> Exp.function_ ~loc ~attrs
| Pexp_try (e, cases) ->
List.map instrument_case cases
|> Exp.try_ ~loc ~attrs e
| Pexp_ifthenelse (condition, then_, else_) ->
Exp.ifthenelse ~loc ~attrs condition (instrument_expr then_)
(match else_ with
| Some e -> Some (instrument_expr e)
| None -> None)
| Pexp_sequence (e1, e2) ->
Exp.sequence ~loc ~attrs e1 (instrument_expr e2)
| Pexp_while (condition, body) ->
Exp.while_ ~loc ~attrs condition (instrument_expr body)
| Pexp_for (variable, initial, bound, direction, body) ->
Exp.for_
~loc ~attrs variable initial bound direction (instrument_expr body)
| _ ->
e'
method! structure_item si =
let loc = si.pstr_loc in
match si.pstr_desc with
| Pstr_value (rec_flag, bindings) ->
let bindings =
bindings
|> List.map begin fun binding ->
let maybe_name =
let open Parsetree in
match binding.pvb_pat.ppat_desc with
| Ppat_var ident
| Ppat_constraint ({ppat_desc = Ppat_var ident; _}, _) ->
Some ident
| _ ->
None
in
let do_not_instrument =
match maybe_name with
| Some name ->
Exclusions.contains_value
Location.(Lexing.(name.loc.loc_start.pos_fname))
name.txt
| None ->
false
in
if do_not_instrument then
binding
else
{binding with pvb_expr =
instrument_expr (self#expr binding.pvb_expr)}
end
in
Str.value ~loc rec_flag bindings
| Pstr_eval (e, a) ->
Str.eval ~loc ~attrs:a (instrument_expr (self#expr e))
| _ ->
super#structure_item si
method! extension e =
e
method! attribute a =
a
val mutable saw_top_level_structure_or_signature = false
method! signature ast =
if not saw_top_level_structure_or_signature then
saw_top_level_structure_or_signature <- true;
super#signature ast
method! structure ast =
if saw_top_level_structure_or_signature then
super#structure ast
else begin
saw_top_level_structure_or_signature <- true;
let always_ignore_paths = ["//toplevel//"; "(stdin)"] in
let always_ignore_basenames = [".ocamlinit"; "topfind"] in
let always_ignore path =
List.mem path always_ignore_paths ||
List.mem (Filename.basename path) always_ignore_basenames
in
if always_ignore !Location.input_name then
ast
else
if Exclusions.contains_file !Location.input_name then
ast
else begin
let instrumented_ast = super#structure ast in
let runtime_initialization =
Generated_code.runtime_initialization
points !Location.input_name
in
runtime_initialization @ instrumented_ast
end
end
end