|
| 1 | + |
| 2 | +(* This file is free software, part of dolmen. See file "LICENSE" for more information *) |
| 3 | + |
| 4 | +include Dolmen_loop.State |
| 5 | + |
| 6 | +(* Erros, Warnings & locations *) |
| 7 | +(* ************************************************************************* *) |
| 8 | + |
| 9 | +let loc_input ?file st (loc : Dolmen.Std.Loc.loc) = |
| 10 | + (* sanity check to avoid pp_loc trying to read and/or print |
| 11 | + too much when printing the source code snippet) *) |
| 12 | + if loc.max_line_length >= 150 || |
| 13 | + loc.stop_line - loc.start_line >= 100 then |
| 14 | + None |
| 15 | + else begin |
| 16 | + match get report_style st, (file : _ file option) with |
| 17 | + | _, None -> None |
| 18 | + | _, Some { source = `Stdin; _ } -> None |
| 19 | + | (Minimal | Regular), _ -> None |
| 20 | + | Contextual, Some { source = `File filename; dir; _ } -> |
| 21 | + let full_filename = Filename.concat dir filename in |
| 22 | + let input = Pp_loc.Input.file full_filename in |
| 23 | + Some input |
| 24 | + | Contextual, Some { source = `Raw (_, contents); _ } -> |
| 25 | + let input = Pp_loc.Input.string contents in |
| 26 | + Some input |
| 27 | + end |
| 28 | + |
| 29 | +let pp_loc ?file st fmt o = |
| 30 | + match o with |
| 31 | + | None -> () |
| 32 | + | Some loc -> |
| 33 | + if Dolmen.Std.Loc.is_dummy loc then () |
| 34 | + else begin |
| 35 | + match loc_input ?file st loc with |
| 36 | + | None -> |
| 37 | + Format.fprintf fmt "%a:@ " |
| 38 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `White)) Dolmen.Std.Loc.fmt) loc |
| 39 | + | Some input -> |
| 40 | + let loc_start, loc_end = Dolmen.Std.Loc.lexing_positions loc in |
| 41 | + let locs = Pp_loc.Position.of_lexing loc_start, Pp_loc.Position.of_lexing loc_end in |
| 42 | + Format.fprintf fmt "%a:@ %a" |
| 43 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `White)) Dolmen.Std.Loc.fmt) loc |
| 44 | + (Pp_loc.pp ~max_lines:5 ~input) [locs] |
| 45 | + end |
| 46 | + |
| 47 | +let flush st () = |
| 48 | + let aux _ = set cur_warn 0 st in |
| 49 | + let cur = get cur_warn st in |
| 50 | + let max = get max_warn st in |
| 51 | + if cur <= max then |
| 52 | + aux () |
| 53 | + else |
| 54 | + match get report_style st with |
| 55 | + | Minimal -> |
| 56 | + Format.kfprintf aux Format.err_formatter |
| 57 | + "W:%d@." (cur - max) |
| 58 | + | Regular | Contextual -> |
| 59 | + Format.kfprintf aux Format.err_formatter |
| 60 | + ("@[<v>%a @[<hov>%s@ %d@ %swarnings@]@]@.") |
| 61 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `Magenta)) string) "Warning" |
| 62 | + (if max = 0 then "Counted" else "Plus") |
| 63 | + (cur - max) (if max = 0 then "" else "additional ") |
| 64 | + |
| 65 | +let error ?file ?loc st error payload = |
| 66 | + let () = Tui.finalise_display () in |
| 67 | + let st = flush st () in |
| 68 | + let loc = Dolmen.Std.Misc.opt_map loc Dolmen.Std.Loc.full_loc in |
| 69 | + let aux _ = Dolmen_loop.Code.exit (Dolmen_loop.Report.Error.code error) in |
| 70 | + match get report_style st with |
| 71 | + | Minimal -> |
| 72 | + Format.kfprintf aux Format.err_formatter |
| 73 | + "E:%s@." (Dolmen_loop.Report.Error.mnemonic error) |
| 74 | + | Regular | Contextual -> |
| 75 | + Format.kfprintf aux Format.err_formatter |
| 76 | + ("@[<v>%a%a @[<hov>%a@]%a@]@.") |
| 77 | + (pp_loc ?file st) loc |
| 78 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `Red)) string) "Error" |
| 79 | + Dolmen_loop.Report.Error.print (error, payload) |
| 80 | + Dolmen_loop.Report.Error.print_hints (error, payload) |
| 81 | + |
| 82 | +let warn ?file ?loc st warn payload = |
| 83 | + let loc = Dolmen.Std.Misc.opt_map loc Dolmen.Std.Loc.full_loc in |
| 84 | + match Dolmen_loop.Report.Conf.status (get reports st) warn with |
| 85 | + | Disabled -> st |
| 86 | + | Enabled -> |
| 87 | + let aux _ = update cur_warn ((+) 1) st in |
| 88 | + if get cur_warn st >= get max_warn st then |
| 89 | + aux st |
| 90 | + else |
| 91 | + begin match get report_style st with |
| 92 | + | Minimal -> |
| 93 | + Tui.kfprintf aux Format.err_formatter |
| 94 | + "W:%s@." (Dolmen_loop.Report.Warning.mnemonic warn) |
| 95 | + | Regular | Contextual -> |
| 96 | + Tui.kfprintf aux Format.err_formatter |
| 97 | + ("@[<v>%a%a @[<hov>%a@]%a@]@.") |
| 98 | + (pp_loc ?file st) loc |
| 99 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `Magenta)) string) "Warning" |
| 100 | + Dolmen_loop.Report.Warning.print (warn, payload) |
| 101 | + Dolmen_loop.Report.Warning.print_hints (warn, payload) |
| 102 | + end |
| 103 | + | Fatal -> |
| 104 | + let () = Tui.finalise_display () in |
| 105 | + let st = flush st () in |
| 106 | + let aux _ = Dolmen_loop.Code.exit (Dolmen_loop.Report.Warning.code warn) in |
| 107 | + begin match get report_style st with |
| 108 | + | Minimal -> |
| 109 | + Format.kfprintf aux Format.err_formatter |
| 110 | + "F:%s@." (Dolmen_loop.Report.Warning.mnemonic warn) |
| 111 | + | Regular | Contextual -> |
| 112 | + Format.kfprintf aux Format.err_formatter |
| 113 | + ("@[<v>%a%a @[<hov>%a@]%a@]@.") |
| 114 | + (pp_loc ?file st) loc |
| 115 | + Fmt.(styled `Bold @@ styled (`Fg (`Hi `Red)) string) "Fatal Warning" |
| 116 | + Dolmen_loop.Report.Warning.print (warn, payload) |
| 117 | + Dolmen_loop.Report.Warning.print_hints (warn, payload) |
| 118 | + end |
| 119 | + |
| 120 | + |
0 commit comments