Source file utils.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
(** Various helper functions. *)
[@@@ocaml.text "/*"]
(** Copyright 2021-2025, Kakadu. *)
(** SPDX-License-Identifier: LGPL-3.0-or-later *)
[@@@ocaml.text "/*"]
open Format
let printfn fmt = kfprintf (fun ppf -> fprintf ppf "\n%!") std_formatter fmt
module ErrorFormat = struct
let pp ppf ~filename ~line ~col:_ msg x =
fprintf ppf "%s:%d:%d:%a\n%!" filename line 0 msg x
;;
end
type rdjsonl_code = string * string option
module RDJsonl : sig
val pp
: formatter
-> filename:string
-> line:int
-> ?code:rdjsonl_code
-> (formatter -> 'a -> unit)
-> 'a
-> unit
end = struct
let pp ppf ~filename ~line ?code msg x =
let location file ~line ~col =
`Assoc
[ "path", `String file
; "range", `Assoc [ "start", `Assoc [ "line", `Int line; "column", `Int col ] ]
]
in
let j =
`Assoc
([ "message", `String (asprintf "%a" msg x)
; "location", location filename ~line ~col:1
; "severity", `String "INFO"
]
@
match code with
| None -> []
| Some (desc, None) -> [ "code", `Assoc [ "value", `String desc ] ]
| Some (desc, Some url) ->
[ "code", `Assoc [ "value", `String desc; "url", `String url ] ])
in
fprintf ppf "%s\n%!" (Yojson.to_string j)
;;
end
let cut_build_dir s =
let prefix = "_build/default/" in
if String.starts_with ~prefix s
then Base.String.drop_prefix s (String.length prefix)
else s
;;
include (
struct
open Location
open Lexing
type input_line =
{ text : string
; start_pos : int
}
let infer_line_numbers (lines : (int option * input_line) list)
: (int option * input_line) list
=
let _, offset, consistent =
List.fold_left
(fun (i, offset, consistent) (lnum, _) ->
match lnum, offset with
| None, _ -> i + 1, offset, consistent
| Some n, None -> i + 1, Some (n - i), consistent
| Some n, Some m -> i + 1, offset, consistent && n = m + i)
(0, None, true)
lines
in
match offset, consistent with
| Some m, true -> List.mapi (fun i (_, line) -> Some (m + i), line) lines
| _, _ -> lines
;;
module ISet : sig
type 'a bound = 'a * int
type 'a t
val of_intervals : ('a bound * 'a bound) list -> 'a t
val mem : 'a t -> pos:int -> bool
val find_bound_in : 'a t -> range:int * int -> 'a bound option
val is_start : 'a t -> pos:int -> 'a option
val is_end : 'a t -> pos:int -> 'a option
val extrema : 'a t -> ('a bound * 'a bound) option
end = struct
type 'a bound = 'a * int
type 'a t = ('a bound * 'a bound) list
let of_intervals intervals =
let pos =
List.map
(fun ((a, x), (b, y)) -> if x > y then [] else [ (a, x), `S; (b, y), `E ])
intervals
|> List.flatten
|> List.sort (fun ((_, x), k) ((_, y), k') ->
let kn = function
| `S -> 0
| `E -> 1
in
compare (x, kn k) (y, kn k'))
in
let nesting, acc =
List.fold_left
(fun (nesting, acc) (a, kind) ->
match kind, nesting with
| `S, `Outside -> `Inside (a, 0), acc
| `S, `Inside (s, n) -> `Inside (s, n + 1), acc
| `E, `Outside -> assert false
| `E, `Inside (s, 0) -> `Outside, (s, a) :: acc
| `E, `Inside (s, n) -> `Inside (s, n - 1), acc)
(`Outside, [])
pos
in
assert (nesting = `Outside);
List.rev acc
;;
let mem iset ~pos = List.exists (fun ((_, s), (_, e)) -> s <= pos && pos <= e) iset
let find_bound_in iset ~range:(start, end_) =
List.find_map
(fun ((a, x), (b, y)) ->
if start <= x && x <= end_
then Some (a, x)
else if start <= y && y <= end_
then Some (b, y)
else None)
iset
;;
let is_start iset ~pos =
List.find_map (fun ((a, x), _) -> if pos = x then Some a else None) iset
;;
let is_end iset ~pos =
List.find_map (fun (_, (b, y)) -> if pos = y then Some b else None) iset
;;
let extrema iset =
if iset = [] then None else Some (fst (List.hd iset), snd (List.hd (List.rev iset)))
;;
end
let num_loc_lines = ref 0
let is_first_message () = !num_loc_lines = 0
let reset () = num_loc_lines := 0
let echo_eof () =
print_newline ();
incr num_loc_lines
;;
let print_updating_num_loc_lines ppf f arg =
let open Format in
let out_functions = pp_get_formatter_out_functions ppf () in
let out_string str start len =
let rec count i c =
if i = start + len
then c
else if String.get str i = '\n'
then count (succ i) (succ c)
else count (succ i) c
in
num_loc_lines := !num_loc_lines + count start 0;
out_functions.out_string str start len
in
pp_set_formatter_out_functions ppf { out_functions with out_string };
f ppf arg;
pp_print_flush ppf ();
pp_set_formatter_out_functions ppf out_functions
;;
module Fmt = Format
let pp_two_columns ?(sep = "|") ?max_lines ppf (lines : (string * string) list) =
let left_column_size =
List.fold_left (fun acc (s, _) -> Int.max acc (String.length s)) 0 lines
in
let lines_nb = List.length lines in
let ellipsed_first, ellipsed_last =
match max_lines with
| Some max_lines when lines_nb > max_lines ->
let printed_lines = max_lines - 1 in
let lines_before = (printed_lines / 2) + (printed_lines mod 2) in
let lines_after = printed_lines / 2 in
lines_before, lines_nb - lines_after - 1
| _ -> -1, -1
in
Format.fprintf ppf "@[<v>";
List.iteri
(fun k (line_l, line_r) ->
if k = ellipsed_first then Format.fprintf ppf "...@,";
if ellipsed_first <= k && k <= ellipsed_last
then ()
else Format.fprintf ppf "%*s %s %s@," left_column_size line_l sep line_r)
lines;
Format.fprintf ppf "@]"
;;
let highlight_quote
ppf
~(get_lines : start_pos:position -> end_pos:position -> input_line list)
?(max_lines = 10)
highlight_tag
locs
=
let iset =
ISet.of_intervals
@@ List.filter_map
(fun loc ->
let s, e = loc.loc_start, loc.loc_end in
if s.pos_cnum = -1 || e.pos_cnum = -1
then None
else Some ((s, s.pos_cnum), (e, e.pos_cnum - 1)))
locs
in
match ISet.extrema iset with
| None -> ()
| Some ((leftmost, _), (rightmost, _)) ->
let lines =
get_lines ~start_pos:leftmost ~end_pos:rightmost
|> List.map (fun ({ text; start_pos } as line) ->
let end_pos = start_pos + String.length text - 1 in
let line_nb =
match ISet.find_bound_in iset ~range:(start_pos, end_pos) with
| None -> None
| Some (p, _) -> Some p.pos_lnum
in
line_nb, line)
|> infer_line_numbers
|> List.map (fun (lnum, { text; start_pos }) ->
text, Option.fold ~some:Int.to_string ~none:"" lnum, start_pos)
in
Fmt.fprintf ppf "@[<v>";
(match lines with
| [] | [ ("", _, _) ] -> ()
| [ (line, line_nb, line_start_cnum) ] ->
Fmt.fprintf ppf "%s | %s@," line_nb line;
Fmt.fprintf ppf "%*s " (String.length line_nb) "";
for i = 0 to rightmost.pos_cnum - line_start_cnum - 1 do
let pos = line_start_cnum + i in
if ISet.is_start iset ~pos <> None then Fmt.fprintf ppf "@{<%s>" highlight_tag;
if ISet.mem iset ~pos
then Fmt.pp_print_char ppf '^'
else if i < String.length line
then
if line.[i] = '\t'
then Fmt.pp_print_char ppf '\t'
else Fmt.pp_print_char ppf ' ';
if ISet.is_end iset ~pos <> None then Fmt.fprintf ppf "@}"
done;
Fmt.fprintf ppf "@}@,"
| _ ->
pp_two_columns ~sep:"|" ~max_lines ppf
@@ List.map
(fun (line, line_nb, line_start_cnum) ->
let line =
String.mapi
(fun i car ->
if ISet.mem iset ~pos:(line_start_cnum + i) then car else '.')
line
in
line_nb, line)
lines);
Fmt.fprintf ppf "@]"
;;
let lines_around
~(start_pos : position)
~(end_pos : position)
~(seek : int -> unit)
~(read_char : unit -> char option)
: input_line list
=
seek start_pos.pos_bol;
let lines = ref [] in
let bol = ref start_pos.pos_bol in
let cur = ref start_pos.pos_bol in
let b = Buffer.create 80 in
let add_line () =
if !bol < !cur
then (
let text = Buffer.contents b in
Buffer.clear b;
lines := { text; start_pos = !bol } :: !lines;
bol := !cur)
in
let rec loop () =
if !bol >= end_pos.pos_cnum
then ()
else (
match read_char () with
| None ->
add_line ()
| Some c ->
incr cur;
(match c with
| '\r' -> loop ()
| '\n' ->
add_line ();
loop ()
| _ ->
Buffer.add_char b c;
loop ()))
in
loop ();
List.rev !lines
;;
let lines_around_from_lexbuf ~(start_pos : position) ~(end_pos : position) (lb : lexbuf)
: input_line list
=
let rel n = n - lb.lex_abs_pos in
if rel start_pos.pos_bol < 0
then
[]
else (
let pos = ref 0 in
let seek n = pos := rel n in
let read_char () =
if !pos >= lb.lex_buffer_len
then None
else (
let c = Bytes.get lb.lex_buffer !pos in
incr pos;
Some c)
in
lines_around ~start_pos ~end_pos ~seek ~read_char)
;;
let lines_around_from_phrasebuf
~(start_pos : position)
~(end_pos : position)
(pb : Buffer.t)
: input_line list
=
let pos = ref 0 in
let seek n = pos := n in
let read_char () =
if !pos >= Buffer.length pb
then None
else (
let c = Buffer.nth pb !pos in
incr pos;
Some c)
in
lines_around ~start_pos ~end_pos ~seek ~read_char
;;
let lines_around_from_current_input ~start_pos ~end_pos =
match !Location.input_lexbuf, !Location.input_phrase_buffer, !Location.input_name with
| _, Some pb, "//toplevel//" -> lines_around_from_phrasebuf pb ~start_pos ~end_pos
| Some lb, _, _ ->
let xs = lines_around_from_lexbuf lb ~start_pos ~end_pos in
xs
| None, _, _ -> []
;;
let is_dummy_loc loc =
loc.loc_start.pos_cnum = -1 || loc.loc_end.pos_cnum = -1
;;
let is_quotable_loc loc =
(not (is_dummy_loc loc))
&& loc.loc_start.pos_fname = !input_name
&& loc.loc_end.pos_fname = !input_name
;;
let report_printer ppf loc f x =
let highlight ppf loc =
if is_quotable_loc loc
then
highlight_quote ppf ~get_lines:lines_around_from_current_input "warning" [ loc ]
in
Format.fprintf ppf "@[<v>%a:@ %a@]" print_loc loc highlight loc;
Format.fprintf ppf "@[Alert zanuda-linter: @[%a@]@]@," f x;
Format.fprintf ppf "%!"
;;
end :
sig
val report_printer : formatter -> Location.t -> (formatter -> 'a -> unit) -> 'a -> unit
end)
module Report = struct
let txt ~loc ~filename ppf msg msg_arg =
Location.input_name := cut_build_dir filename;
Clflags.error_style := Some Misc.Error_style.Contextual;
let file_contents = In_channel.with_open_text filename In_channel.input_all in
Location.input_lexbuf := Some (Lexing.from_string file_contents);
let loc =
let open Location in
{ loc with
loc_start = { loc.loc_start with pos_fname = !input_name }
; loc_end = { loc.loc_end with pos_fname = !input_name }
}
in
report_printer ppf loc msg msg_arg
;;
let rdjsonl ~loc ~filename ~code ppf msg msg_arg =
let code = code, Some "https://kakadu.github.io/zanuda/" in
RDJsonl.pp ppf ~filename ~line:loc.Location.loc_start.pos_lnum ~code msg msg_arg
;;
end
let string_of_group : LINT.group -> string = function
| LINT.Correctness -> "correctness"
| Style -> "style"
| Perf -> "perf"
| Restriction -> "restriction"
| Deprecated -> "deprecated"
| Pedantic -> "pedantic"
| Complexity -> "complexity"
| Suspicious -> "suspicious"
| Nursery -> "nursery"
;;
let string_of_level : LINT.level -> string = function
| LINT.Allow -> "allow"
| Warn -> "warn"
| Deny -> "deny"
| Deprecated -> "deprecated"
;;
let string_of_impl = function
| LINT.Typed -> "typed"
| _ -> "untyped"
;;
let describe_as_clippy_json
?(group = LINT.Correctness)
?(level = LINT.Deny)
?(impl = LINT.Typed)
id
~docs
: Yojson.Safe.t
=
`Assoc
[ "id", `String id
; "group", `String (string_of_group group)
; "level", `String (string_of_level level)
; "impl", `String (string_of_impl impl)
; "docs", `String docs
; ( "applicability"
, `Assoc
[ "is_multi_part_suggestion", `Bool false
; "applicability", `String "Unresolved"
] )
]
;;
exception Ident_is_found
let no_ident_iterator ident =
let open Tast_iterator in
let open Typedtree in
{ default_iterator with
expr =
(fun self e ->
let rec ident_in_list = function
| [] -> false
| (_, (id, _)) :: _ when Ident.equal id ident -> true
| _ :: tl -> ident_in_list tl
in
Tast_pattern.(
let p1 =
map2 (texp_function_body __ __) ~f:(fun args rhs -> `Function (args, rhs))
in
let p2 = map1 (texp_ident __) ~f:(fun x -> `Ident x) in
parse
(p1 ||| p2)
e.exp_loc
e
~on_error:(fun _ -> default_iterator.expr self e))
(function
| `Function (args, _rhs) when ident_in_list args -> ()
| `Function (_, rhs) -> self.expr self rhs
| `Ident (Pident id) when Ident.same id ident -> raise_notrace Ident_is_found
| _ -> default_iterator.expr self e))
; case =
(fun (type a) self (c : a case) ->
match c.c_lhs.pat_desc with
| Tpat_value v ->
(match (v :> pattern) with
| p ->
Tast_pattern.(
parse
(tpat_id __)
Location.none
p
~on_error:(fun _ -> default_iterator.case self c)
(fun id ->
if Ident.equal ident id then () else default_iterator.case self c)))
| _ -> default_iterator.case self c)
}
;;
let no_ident ident f =
try
f (no_ident_iterator ident);
true
with
| Ident_is_found -> false
;;
let has_ident ident f = not (no_ident ident f)
[%%if ocaml_version < (5, 0, 0)]
type intf_or_impl =
| Intf
| Impl
let with_info _kind ~source_file f =
Compile_common.with_info
~native:false
~source_file
~tool_name:"asdf"
~output_prefix:"asdf"
~dump_ext:"asdf"
f
;;
[%%else]
type intf_or_impl = Unit_info.intf_or_impl
let with_info kind ~source_file =
Compile_common.with_info
~native:false
~tool_name:"asdf"
~dump_ext:"asdf"
(Unit_info.make ~source_file kind "")
;;
[%%endif]
[%%if ocaml_version < (5, 0, 0)]
let pp_path = Path.print
[%%else]
let pp_path = Format_doc.compat Path.print
[%%endif]
[%%if ocaml_version < (5, 0, 0)]
let source_of_info info = info.Compile_common.source_file
[%%else]
let source_of_info info = Unit_info.source_file info.Compile_common.target
[%%endif]