Source file executable.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
module Name = Executable__name
module Public_name = Executable__public_name
let field_name = "executable"
module Field_name = struct
module T0 = struct
type t =
[ `name
| `public_name
| `instrumentation
| `lint
| `preprocess
]
[@@deriving compare, hash, sexp_of]
end
include T0
include Comparable.Make (T0)
end
type t =
{ mutable name : Name.t option
; mutable public_name : Public_name.t option
; flags : Flags.t
; libraries : Libraries.t
; mutable instrumentation : Instrumentation.t option
; mutable lint : Lint.t option
; mutable preprocess : Preprocess.t option
; marked_for_removal : Hash_set.M(Field_name).t
}
[@@deriving sexp_of]
let indicative_field_ordering =
[ "name"
; "public_name"
; "package"
; "inline_tests"
; "flags"
; "libraries"
; "instrumentation"
; "lint"
; "preprocess"
]
;;
let normalize t = Libraries.dedup_and_sort t.libraries
let create
?name
?public_name
?(flags = [])
?(libraries = [])
?instrumentation
?lint
?preprocess
()
=
let name = Option.map name ~f:(fun name -> Name.create ~name) in
let public_name =
Option.map public_name ~f:(fun public_name -> Public_name.create ~public_name)
in
let flags = Flags.create ~flags in
let libraries = Libraries.create ~libraries in
let t =
{ name
; public_name
; flags
; libraries
; instrumentation
; lint
; preprocess
; marked_for_removal = Hash_set.create (module Field_name)
}
in
normalize t;
t
;;
let read ~sexps_rewriter ~field =
let fields = Dunolinter.Sexp_handler.get_args ~field_name ~sexps_rewriter ~field in
let name = ref None in
let public_name = ref None in
let flags = ref None in
let libraries = ref None in
let instrumentation = ref None in
let lint = ref None in
let preprocess = ref None in
List.iter fields ~f:(fun field ->
match (field : Sexp.t) with
| List (Atom "name" :: _) -> name := Some (Name.read ~sexps_rewriter ~field)
| List (Atom "public_name" :: _) ->
public_name := Some (Public_name.read ~sexps_rewriter ~field)
| List (Atom "flags" :: _) -> flags := Some (Flags.read ~sexps_rewriter ~field)
| List (Atom "libraries" :: _) ->
libraries := Some (Libraries.read ~sexps_rewriter ~field)
| List (Atom "instrumentation" :: _) ->
instrumentation := Some (Instrumentation.read ~sexps_rewriter ~field)
| List (Atom "lint" :: _) -> lint := Some (Lint.read ~sexps_rewriter ~field)
| List (Atom "preprocess" :: _) ->
preprocess := Some (Preprocess.read ~sexps_rewriter ~field)
| List _ | Atom _ -> ());
let libraries =
match !libraries with
| Some libraries -> libraries
| None -> Libraries.create ~libraries:[]
in
let flags =
match !flags with
| Some flags -> flags
| None -> Flags.create ~flags:[]
in
{ name = !name
; public_name = !public_name
; flags
; libraries
; instrumentation = !instrumentation
; lint = !lint
; preprocess = !preprocess
; marked_for_removal = Hash_set.create (module Field_name)
}
;;
let write_fields
({ name
; public_name
; flags
; libraries
; instrumentation
; lint
; preprocess
; marked_for_removal = _
} as t)
=
normalize t;
List.filter_opt
[ Option.map name ~f:Name.write
; Option.map public_name ~f:Public_name.write
; (if Flags.is_empty flags then None else Some (Flags.write flags))
; (if Libraries.is_empty libraries then None else Some (Libraries.write libraries))
; Option.map instrumentation ~f:Instrumentation.write
; Option.map lint ~f:Lint.write
; Option.map preprocess ~f:Preprocess.write
]
;;
let write t = Sexp.List (Atom field_name :: write_fields t)
let rewrite t ~sexps_rewriter ~field ~load_existing_libraries =
let fields = Dunolinter.Sexp_handler.get_args ~field_name ~sexps_rewriter ~field in
let () =
if load_existing_libraries
then (
let existing_entries =
Dunolinter.Sexp_handler.find (module Libraries) ~sexps_rewriter ~fields
|> Option.value_map ~default:[] ~f:Libraries.entries
in
Libraries.add_entries t.libraries ~entries:existing_entries)
in
normalize t;
let new_fields = write_fields t in
Dunolinter.Sexp_handler.insert_new_fields
~sexps_rewriter
~indicative_field_ordering
~fields
~new_fields;
let file_rewriter = Sexps_rewriter.file_rewriter sexps_rewriter in
let maybe_remove state field_name field =
if Option.is_none state && Hash_set.mem t.marked_for_removal field_name
then (
let range = Sexps_rewriter.range sexps_rewriter field in
File_rewriter.remove file_rewriter ~range)
in
List.iter fields ~f:(fun field ->
match (field : Sexp.t) with
| List (Atom "name" :: _) ->
Option.iter t.name ~f:(fun t -> Name.rewrite t ~sexps_rewriter ~field);
maybe_remove t.name `name field
| List (Atom "public_name" :: _) ->
Option.iter t.public_name ~f:(fun t -> Public_name.rewrite t ~sexps_rewriter ~field);
maybe_remove t.public_name `public_name field
| List (Atom "flags" :: _) -> Flags.rewrite t.flags ~sexps_rewriter ~field
| List (Atom "libraries" :: _) -> Libraries.rewrite t.libraries ~sexps_rewriter ~field
| List (Atom "instrumentation" :: _) ->
Option.iter t.instrumentation ~f:(fun t ->
Instrumentation.rewrite t ~sexps_rewriter ~field);
maybe_remove t.instrumentation `instrumentation field
| List (Atom "lint" :: _) ->
Option.iter t.lint ~f:(fun t -> Lint.rewrite t ~sexps_rewriter ~field);
maybe_remove t.lint `lint field
| List (Atom "preprocess" :: _) ->
Option.iter t.preprocess ~f:(fun t -> Preprocess.rewrite t ~sexps_rewriter ~field);
maybe_remove t.preprocess `preprocess field
| _ -> ())
;;
type predicate = Dune.Executable.Predicate.t
let eval t ~predicate =
match (predicate : predicate) with
| `name condition ->
(match t.name with
| None -> Dunolint.Trilang.Undefined
| Some name ->
Dunolint.Trilang.eval condition ~f:(fun predicate -> Name.eval name ~predicate))
| `public_name condition ->
(match t.public_name with
| None -> Dunolint.Trilang.Undefined
| Some public_name ->
Dunolint.Trilang.eval condition ~f:(fun predicate ->
Public_name.eval public_name ~predicate))
| `instrumentation condition ->
(match t.instrumentation with
| None -> Dunolint.Trilang.Undefined
| Some instrumentation ->
Dunolint.Trilang.eval condition ~f:(fun predicate ->
Instrumentation.eval instrumentation ~predicate))
| `lint condition ->
(match t.lint with
| None -> Dunolint.Trilang.Undefined
| Some lint ->
Dunolint.Trilang.eval condition ~f:(fun predicate -> Lint.eval lint ~predicate))
| `preprocess condition ->
(match t.preprocess with
| None -> Dunolint.Trilang.Undefined
| Some preprocess ->
Dunolint.Trilang.eval condition ~f:(fun predicate ->
Preprocess.eval preprocess ~predicate))
| `has_field field ->
(match field with
| `name -> Option.is_some t.name
| `public_name -> Option.is_some t.public_name
| `lint -> Option.is_some t.lint
| `instrumentation -> Option.is_some t.instrumentation
| `preprocess -> Option.is_some t.preprocess)
|> Dunolint.Trilang.const
;;
let enforce =
Dunolinter.Linter.enforce
(module Dune.Executable.Predicate)
~eval
~enforce:(fun t predicate ->
match predicate with
| Not condition ->
(match condition with
| `has_field has_field ->
Hash_set.add t.marked_for_removal has_field;
(match has_field with
| `name -> t.name <- None
| `public_name -> t.public_name <- None
| `instrumentation -> t.instrumentation <- None
| `lint -> t.lint <- None
| `preprocess -> t.preprocess <- None);
Ok
| condition ->
let () =
match[@coverage off] condition with
| `has_field _ -> assert false
| `instrumentation _ | `public_name _ | `preprocess _ | `name _ | `lint _ ->
()
in
Eval)
| T (`has_field `name) ->
(match t.name with
| Some _ -> Ok
| None -> Fail)
| T (`name condition) ->
(match t.name with
| Some name ->
Name.enforce name ~condition;
Ok
| None ->
(match
List.find_map
(Dunolinter.Linter.at_positive_enforcing_position condition)
~f:(function
| `equals name -> Some name
| `is_prefix _ | `is_suffix _ -> None)
with
| None -> Eval
| Some name ->
let name = Name.create ~name in
t.name <- Some name;
Name.enforce name ~condition;
Ok))
| T (`has_field `public_name) ->
(match t.public_name with
| Some _ -> Ok
| None -> Fail)
| T (`public_name condition) ->
(match t.public_name with
| Some public_name ->
Public_name.enforce public_name ~condition;
Ok
| None ->
(match
List.find_map
(Dunolinter.Linter.at_positive_enforcing_position condition)
~f:(function
| `equals public_name -> Some public_name
| `is_prefix _ | `is_suffix _ -> None)
with
| None -> Eval
| Some public_name ->
let public_name = Public_name.create ~public_name in
t.public_name <- Some public_name;
Public_name.enforce public_name ~condition;
Ok))
| T (`has_field `instrumentation) ->
(match t.instrumentation with
| Some _ -> Ok
| None ->
t.instrumentation <- Some (Instrumentation.initialize ~condition:Blang.true_);
Ok)
| T (`instrumentation condition) ->
let instrumentation =
match t.instrumentation with
| Some instrumentation -> instrumentation
| None ->
let instrumentation = Instrumentation.initialize ~condition in
t.instrumentation <- Some instrumentation;
instrumentation
in
Instrumentation.enforce instrumentation ~condition;
Ok
| T (`has_field `lint) ->
(match t.lint with
| Some _ -> Ok
| None ->
t.lint <- Some (Lint.create ());
Ok)
| T (`lint condition) ->
let lint =
match t.lint with
| Some lint -> lint
| None ->
let lint = Lint.create () in
t.lint <- Some lint;
lint
in
Lint.enforce lint ~condition;
Ok
| T (`has_field `preprocess) ->
(match t.preprocess with
| Some _ -> Ok
| None ->
t.preprocess <- Some (Preprocess.create ());
Ok)
| T (`preprocess condition) ->
let preprocess =
match t.preprocess with
| Some preprocess -> preprocess
| None ->
let preprocess = Preprocess.create () in
t.preprocess <- Some preprocess;
preprocess
in
Preprocess.enforce preprocess ~condition;
Ok)
;;
module Top = struct
type nonrec t = t
let eval = eval
let enforce = enforce
end
module Linter = struct
type t = Top.t
type predicate = Dune.Predicate.t
let eval (t : t) ~predicate =
match[@coverage off] (predicate : Dune.Predicate.t) with
| `stanza stanza ->
Blang.eval stanza (fun stanza -> Dune.Stanza.Predicate.equal stanza `executable)
|> Dunolint.Trilang.const
| `include_subdirs _ | `library _ -> Dunolint.Trilang.Undefined
| `executable condition ->
Dunolint.Trilang.eval condition ~f:(fun predicate -> Top.eval t ~predicate)
| (`instrumentation _ | `lint _ | `preprocess _ | `has_field _) as predicate ->
Top.eval t ~predicate
;;
let enforce =
Dunolinter.Linter.enforce
(module Dune.Predicate)
~eval
~enforce:(fun t predicate ->
match[@coverage off] predicate with
| Not _ -> Eval
| T (`include_subdirs _ | `library _ | `stanza _) -> Unapplicable
| T (`executable condition) ->
Top.enforce t ~condition;
Ok
| T ((`instrumentation _ | `lint _ | `preprocess _ | `has_field _) as predicate)
->
Top.enforce t ~condition:(Blang.base predicate);
Ok)
;;
end
module Private = struct
let rewrite = rewrite
end
let rewrite t ~sexps_rewriter ~field =
rewrite t ~sexps_rewriter ~field ~load_existing_libraries:false
;;