Source file coverage_prim.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
open! Core0

module State = struct
  module Named = struct
    module T = struct
      type t =
        { name : string
        ; value : int
        }
      [@@deriving bin_io, equal, sexp_of, compare, hash]
    end

    include Comparator.Make (T)
    include T
  end

  type t =
    { name : string option
    ; value : int
    }
  [@@deriving sexp_of, equal]

  let of_named { Named.name; value } = { name = Some name; value }
end

module Case = struct
  module Positional = struct
    module T = struct
      type t =
        | Specified of int
        | Default
      [@@deriving equal, sexp_of, compare, hash]
    end

    include Comparator.Make (T)
    include T
  end

  module Positional_with_state = struct
    type t =
      | Specified of
          { position : int
          ; state : State.Named.t option
          }
      | Default of { states : State.Named.t list }
    [@@deriving bin_io, sexp_of]

    let to_string = function
      | Specified { state = Some state; _ } -> state.name
      | Specified { position; _ } -> Int.to_string position
      | Default { states } ->
        let names = List.map states ~f:(fun { name; _ } -> name) in
        (match names with
         | [] -> "default"
         | [ name ] -> name
         | names -> "(" ^ String.concat names ~sep:" | " ^ ")")
    ;;

    let to_positional = function
      | Default _ -> Positional.Default
      | Specified { position; _ } -> Specified position
    ;;

    let of_positional = function
      | Positional.Default -> Default { states = [] }
      | Specified position -> Specified { position; state = None }
    ;;
  end

  type t =
    | Positional of int
    | State of State.Named.t
    | Default
  [@@deriving bin_io, sexp_of, equal]

  let to_string = function
    | Positional position -> Int.to_string position
    | State state -> state.name
    | Default -> "default"
  ;;
end

module Cases = struct
  type t = Case.Positional_with_state.t list [@@deriving bin_io, sexp_of]

  let create ~non_default_cases ~default_states =
    Case.Positional_with_state.Default { states = default_states }
    :: List.mapi non_default_cases ~f:(fun position a ->
      match a with
      | `Positional -> Case.Positional_with_state.Specified { position; state = None }
      | `State state -> Specified { position; state = Some state })
  ;;

  let create_from_count ~non_default_case_count =
    Case.Positional_with_state.Default { states = [] }
    :: List.init non_default_case_count ~f:(fun position ->
      Case.Positional_with_state.Specified { position; state = None })
  ;;

  let to_case_list t = t
  let length t = List.length t
end

module Toggle = struct
  module T = struct
    type t =
      { bit : int
      ; on : bool
      }
    [@@deriving bin_io, sexp_of, compare, hash, equal]
  end

  include Comparator.Make (T)
  include T

  let to_string { bit; on } =
    let toggle = if on then "on" else "off" in
    [%string {|bit-%{bit#Int} %{toggle}|}]
  ;;

  let all_bits_up_to_excl up_to =
    let open List.Let_syntax in
    let%bind bit = List.init up_to ~f:Fn.id in
    let%map on = Bool.all in
    { bit; on }
  ;;
end

module Transition = struct
  type 'a t =
    { from : 'a
    ; to_ : 'a
    }
  [@@deriving bin_io, sexp_of, compare, equal, hash]

  let to_string t ~f = [%string {|%{f t.from} -> %{f t.to_}|}]
  let map t ~f = { from = f t.from; to_ = f t.to_ }

  module Value = struct
    module T = struct
      type nonrec t = int t [@@deriving sexp_of, compare, equal, hash]
    end

    include Comparator.Make (T)
    include T
  end

  module State = struct
    type nonrec t = State.t t [@@deriving sexp_of, equal]

    let to_string (t : t) =
      to_string t ~f:(fun (state : State.t) ->
        match state.name with
        | Some name -> name
        | None -> [%string {|"!unknown_state with value %{state.value#Int}"|}])
    ;;
  end
end

module Waiver = struct
  type 'a kind =
    | Only_expect of 'a list
    | Exclude of 'a list
    | None
  [@@deriving bin_io, sexp_of]

  type 'a t =
    { kind : 'a kind
    ; warn_on_waived_but_observed : bool
    }
  [@@deriving bin_io, sexp_of, fields ~getters]

  let create ?(warn_on_waived_but_observed = true) kind =
    { kind; warn_on_waived_but_observed }
  ;;

  let none () = create None ~warn_on_waived_but_observed:false

  let only_expect ?warn_on_waived_but_observed ls =
    create ?warn_on_waived_but_observed (Only_expect ls)
  ;;

  let exclude ?warn_on_waived_but_observed ls =
    create ?warn_on_waived_but_observed (Exclude ls)
  ;;

  let to_string t ~f =
    match t.kind with
    | Only_expect only -> [%string {|only %{List.to_string only ~f}|}]
    | Exclude exclude -> [%string {|exclude %{List.to_string exclude ~f}|}]
    | None -> "none"
  ;;

  let is_none { kind; _ } =
    match kind with
    | None -> true
    | _ -> false
  ;;

  let map' t ~f =
    let kind =
      match t.kind with
      | None -> None
      | Exclude ls -> Exclude (f ls)
      | Only_expect ls -> Only_expect (f ls)
    in
    { t with kind }
  ;;

  let map t ~f = map' t ~f:(List.map ~f)
  let concat_map t ~f = map' t ~f:(List.concat_map ~f)
  let filter_map t ~f = map' t ~f:(List.filter_map ~f)

  let waived_list t ~all ~equal =
    match t.kind with
    | None -> []
    | Exclude waived -> waived
    | Only_expect keep -> List.filter all ~f:(fun a -> not (List.mem keep a ~equal))
  ;;

  let join_kind a b ~equal =
    match a, b with
    | None, b -> b
    | a, None -> a
    | Only_expect a, Only_expect b -> Only_expect (a @ b)
    | Exclude a, Exclude b -> Exclude (a @ b)
    | Only_expect only, Exclude exclude | Exclude exclude, Only_expect only ->
      Only_expect (List.filter only ~f:(fun x -> not (List.mem exclude x ~equal)))
  ;;

  let join a b ~equal =
    let kind = join_kind a.kind b.kind ~equal in
    let warn_on_waived_but_observed =
      a.warn_on_waived_but_observed || b.warn_on_waived_but_observed
    in
    { kind; warn_on_waived_but_observed }
  ;;

  let join_multi ts ~equal =
    List.fold ts ~init:(none ()) ~f:(fun acc t -> join acc t ~equal)
  ;;
end

module Always_metadata = struct
  module Variable = struct
    module State_reg = struct
      type t =
        { creation_pos : Source_code_position.t
        ; states_by_value : State.Named.t Int.Map.t
        ; transitions_by_value : Int.Hash_set.t Int.Table.t
        ; initial_value : int
        }
      [@@deriving bin_io, sexp_of]
    end

    type t =
      | User_created of Source_code_position.t
      | State_machine_state of State_reg.t
    [@@deriving bin_io, sexp_of]
  end

  module Target = struct
    type t =
      { variable : Variable.t
      ; names : Name_and_loc.t list
      }
    [@@deriving bin_io, sexp_of]
  end

  module If = struct
    module Kind = struct
      type t =
        | If
        | Elif
        | When
        | Unless
        | Proc
      [@@deriving bin_io, sexp_of, to_string ~capitalize:"snake_case"]
    end

    type t =
      { creation_pos : Source_code_position.t
      ; target : Target.t
      ; kind : Kind.t
      }
    [@@deriving bin_io, sexp_of]
  end

  module Switch_cases = struct
    type t =
      { creation_pos : Source_code_position.t
      ; target : Target.t
      ; cases : Cases.t
      }
    [@@deriving bin_io, sexp_of]
  end

  module Switch_mux = struct
    type t =
      { creation_pos : Source_code_position.t
      ; target : Target.t
      ; case : Case.t
      }
    [@@deriving bin_io, sexp_of]
  end
end