123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020(**************************************************************************)(* *)(* OCaml *)(* *)(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)(* *)(* Copyright 1996 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)openLexingtypet=Warnings.loc={loc_start:position;loc_end:position;loc_ghost:bool}letin_file=Warnings.ghost_loc_in_fileletnone=in_file"_none_"letis_nonel=(l=none)letcurrlexbuf={loc_start=lexbuf.lex_start_p;loc_end=lexbuf.lex_curr_p;loc_ghost=false}letinitlexbuffname=lexbuf.lex_curr_p<-{pos_fname=fname;pos_lnum=1;pos_bol=0;pos_cnum=0;}letsymbol_rloc()={loc_start=Parsing.symbol_start_pos();loc_end=Parsing.symbol_end_pos();loc_ghost=false;}letsymbol_gloc()={loc_start=Parsing.symbol_start_pos();loc_end=Parsing.symbol_end_pos();loc_ghost=true;}letrhs_locn={loc_start=Parsing.rhs_start_posn;loc_end=Parsing.rhs_end_posn;loc_ghost=false;}letrhs_intervalmn={loc_start=Parsing.rhs_start_posm;loc_end=Parsing.rhs_end_posn;loc_ghost=false;}(* return file, line, char from the given position *)letget_pos_infopos=(pos.pos_fname,pos.pos_lnum,pos.pos_cnum-pos.pos_bol)type'aloc={txt:'a;loc:t;}letmkloctxtloc={txt;loc}letmknoloctxt=mkloctxtnone(******************************************************************************)(* Input info *)letinput_name=ref"_none_"letinput_lexbuf=ref(None:lexbufoption)letinput_phrase_buffer=ref(None:Buffer.toption)(******************************************************************************)(* Terminal info *)(*
let status = ref Terminfo.Uninitialised
let setup_terminal () =
if !status = Terminfo.Uninitialised then
status := Terminfo.setup stdout
*)(* The number of lines already printed after input.
This is used by [highlight_terminfo] to identify the current position of the
input in the terminal. This would not be possible without this information,
since printing several warnings/errors adds text between the user input and
the bottom of the terminal.
We also use for {!is_first_report}, see below.
*)letnum_loc_lines=ref0(* We use [num_loc_lines] to determine if the report about to be
printed is the first or a follow-up report of the current
"batch" -- contiguous reports without user input in between, for
example for the current toplevel phrase. We use this to print
a blank line between messages of the same batch.
*)letis_first_message()=!num_loc_lines=0(* This is used by the toplevel to reset [num_loc_lines] before each phrase *)letreset()=num_loc_lines:=0(* This is used by the toplevel *)letecho_eof()=print_newline();incrnum_loc_lines(* This is used by the toplevel and the report printers below. *)letseparate_new_messageppf=ifnot(is_first_message())thenbeginFormat.pp_print_newlineppf();incrnum_loc_linesend(* Code printing errors and warnings must be wrapped using this function, in
order to update [num_loc_lines].
[print_updating_num_loc_lines ppf f arg] is equivalent to calling [f ppf
arg], and additionally updates [num_loc_lines]. *)letprint_updating_num_loc_linesppffarg=letopenFormatinletout_functions=pp_get_formatter_out_functionsppf()inletout_stringstrstartlen=letreccountic=ifi=start+lenthencelseifString.getstri='\n'thencount(succi)(succc)elsecount(succi)cinnum_loc_lines:=!num_loc_lines+countstart0;out_functions.out_stringstrstartleninpp_set_formatter_out_functionsppf{out_functionswithout_string};fppfarg;pp_print_flushppf();pp_set_formatter_out_functionsppfout_functions(******************************************************************************)(* Printing locations, e.g. 'File "foo.ml", line 3, characters 10-12' *)letrewrite_absolute_pathpath=(*
match Misc.get_build_path_prefix_map () with
| None -> path
| Some map -> Build_path_prefix_map.rewrite map path
*)path(*
let rewrite_find_first_existing path =
match Misc.get_build_path_prefix_map () with
| None ->
if Sys.file_exists path then Some path
else None
| Some prefix_map ->
match Build_path_prefix_map.rewrite_all prefix_map path with
| [] ->
if Sys.file_exists path then Some path
else None
| matches ->
Some (List.find Sys.file_exists matches)
let rewrite_find_all_existing_dirs path =
let ok path = Sys.file_exists path && Sys.is_directory path in
match Misc.get_build_path_prefix_map () with
| None ->
if ok path then [path]
else []
| Some prefix_map ->
match Build_path_prefix_map.rewrite_all prefix_map path with
| [] ->
if ok path then [path]
else []
| matches ->
match (List.filter ok matches) with
| [] -> raise Not_found
| results -> results *)letabsolute_paths=(* This function could go into Filename *)letopenFilenameinlets=if(is_relatives)then(concat(Sys.getcwd())s)elsesinlets=rewrite_absolute_pathsin(* Now simplify . and .. components *)letrecauxs=letbase=basenamesinletdir=dirnamesinifdir=sthendirelseifbase=current_dir_namethenauxdirelseifbase=parent_dir_namethendirname(auxdir)elseconcat(auxdir)baseinauxsletshow_filenamefile=(* if !Clflags.absname then absolute_path file else *)fileletprint_filenameppffile=Format.pp_print_stringppf(show_filenamefile)(* Best-effort printing of the text describing a location, of the form
'File "foo.ml", line 3, characters 10-12'.
Some of the information (filename, line number or characters numbers) in the
location might be invalid; in which case we do not print it.
*)letprint_locppfloc=letfile_valid=function|"_none_"->(* This is a dummy placeholder, but we print it anyway to please editors
that parse locations in error messages (e.g. Emacs). *)true|""|"//toplevel//"->false|_->trueinletline_validline=line>0inletchars_valid~startchar~endchar=startchar<>-1&&endchar<>-1inletfile=(* According to the comment in location.mli, if [pos_fname] is "", we must
use [!input_name]. *)ifloc.loc_start.pos_fname=""then!input_nameelseloc.loc_start.pos_fnameinletline=loc.loc_start.pos_lnuminletstartchar=loc.loc_start.pos_cnum-loc.loc_start.pos_bolinletendchar=loc.loc_end.pos_cnum-loc.loc_start.pos_bolinletfirst=reftrueinletcapitalizes=if!firstthen(first:=false;String.capitalize_asciis)elsesinletcomma()=if!firstthen()elseFormat.fprintfppf", "inFormat.fprintfppf"@{<loc>";iffile_validfilethenFormat.fprintfppf"%s \"%a\""(capitalize"file")print_filenamefile;(* Print "line 1" in the case of a dummy line number. This is to please the
existing setup of editors that parse locations in error messages (e.g.
Emacs). *)comma();Format.fprintfppf"%s %i"(capitalize"line")(ifline_validlinethenlineelse1);ifchars_valid~startchar~endcharthen(comma();Format.fprintfppf"%s %i-%i"(capitalize"characters")startcharendchar);Format.fprintfppf"@}"(* Print a comma-separated list of locations *)letprint_locsppflocs=Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf",@ ")print_locppflocs(******************************************************************************)(* An interval set structure; additionally, it stores user-provided information
at interval boundaries.
The implementation provided here is naive and assumes the number of intervals
to be small, but the interface would allow for a more efficient
implementation if needed.
Note: the structure only stores maximal intervals (that therefore do not
overlap).
*)(*
module ISet : sig
type 'a bound = 'a * int
type 'a t
(* bounds are included *)
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
(* non overlapping intervals *)
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') ->
(* Make `S come before `E so that consecutive intervals get merged
together in the fold below *)
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_) =
try Some (
List.find_map ~f:(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
) with Not_found -> None
let is_start iset ~pos =
try Some (
List.find_map ~f:(fun ((a, x), _) ->
if pos = x then Some a else None
) iset
) with Not_found -> None
let is_end iset ~pos =
try Some (
List.find_map ~f:(fun (_, (b, y)) ->
if pos = y then Some b else None
) iset
) with Not_found -> None
let extrema iset =
if iset = [] then None
else Some (fst (List.hd iset), snd (List.hd (List.rev iset)))
end
*)(* Highlight the location by printing it again.
There are two different styles for highlighting errors in "dumb" mode,
depending if the error fits on a single line or spans across several lines.
For single-line errors,
foo the_error bar
gets displayed as follows, where X is the line number:
X | foo the_error bar
^^^^^^^^^
For multi-line errors,
foo the_
error bar
gets displayed as:
X1 | ....the_
X2 | error....
An ellipsis hides the middle lines of the multi-line error if it has more
than [max_lines] lines.
If [locs] is empty then this function is a no-op.
*)(*
type input_line = {
text : string;
start_pos : int;
}
*)(* Takes a list of lines with possibly missing line numbers.
If the line numbers that are present are consistent with the number of lines
between them, then infer the intermediate line numbers.
This is not always the case, typically if lexer line directives are
involved... *)(*
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
*)(* [get_lines] must return the lines to highlight, given starting and ending
positions.
See [lines_around_from_current_input] below for an instantiation of
[get_lines] that reads from the current input.
*)(*
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
Format.fprintf ppf "@[<v>";
begin match lines with
| [] | [("", _, _)] -> ()
| [(line, line_nb, line_start_cnum)] ->
(* Single-line error *)
Format.fprintf ppf "%s | %s@," line_nb line;
Format.fprintf ppf "%*s " (String.length line_nb) "";
(* Iterate up to [rightmost], which can be larger than the length of
the line because we may point to a location after the end of the
last token on the line, for instance:
{[
token
^
Did you forget ...
]} *)
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
Format.fprintf ppf "@{<%s>" highlight_tag;
if ISet.mem iset ~pos then Format.pp_print_char ppf '^'
else if i < String.length line then begin
(* For alignment purposes, align using a tab for each tab in the
source code *)
if line.[i] = '\t' then Format.pp_print_char ppf '\t'
else Format.pp_print_char ppf ' '
end;
if ISet.is_end iset ~pos <> None then
Format.fprintf ppf "@}"
done;
Format.fprintf ppf "@}@,"
| _ ->
(* Multi-line error *)
Misc.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
end;
Format.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 begin
let text = Buffer.contents b in
Buffer.clear b;
lines := { text; start_pos = !bol } :: !lines;
bol := !cur
end
in
let rec loop () =
if !bol >= end_pos.pos_cnum then ()
else begin
match read_char () with
| None ->
(* end of input *)
add_line ()
| Some c ->
incr cur;
match c with
| '\r' -> loop ()
| '\n' -> add_line (); loop ()
| _ -> Buffer.add_char b c; loop ()
end
in
loop ();
List.rev !lines
*)(*
(* Try to get lines from a lexbuf *)
let lines_around_from_lexbuf
~(start_pos: position) ~(end_pos: position)
(lb: lexbuf):
input_line list
=
(* Converts a global position to one that is relative to the lexing buffer *)
let rel n = n - lb.lex_abs_pos in
if rel start_pos.pos_bol < 0 then begin
(* Do nothing if the buffer does not contain the input (because it has been
refilled while lexing it) *)
[]
end else begin
let pos = ref 0 in (* relative position *)
let seek n = pos := rel n in
let read_char () =
if !pos >= lb.lex_buffer_len then (* end of buffer *) 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
end
*)(*
(* Attempt to get lines from the phrase buffer *)
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 begin
let c = Buffer.nth pb !pos in
incr pos; Some c
end
in
lines_around ~start_pos ~end_pos ~seek ~read_char
*)(*
(* Get lines from a file *)
let lines_around_from_file
~(start_pos: position) ~(end_pos: position)
(filename: string):
input_line list
=
try
let cin = open_in_bin filename in
let read_char () =
try Some (input_char cin) with End_of_file -> None
in
let lines =
lines_around ~start_pos ~end_pos ~seek:(seek_in cin) ~read_char
in
close_in cin;
lines
with Sys_error _ -> []
*)(*
(* A [get_lines] function for [highlight_quote] that reads from the current
input.
It first tries to read from [!input_lexbuf], then if that fails (because the
lexbuf no longer contains the input we want), it reads from [!input_name]
directly *)
let lines_around_from_current_input ~start_pos ~end_pos =
(* Be a bit defensive, and do not try to open one of the possible
[!input_name] values that we know do not denote valid filenames. *)
let file_valid = function
| "//toplevel//" | "_none_" | "" -> false
| _ -> true
in
let from_file () =
if file_valid !input_name then
lines_around_from_file !input_name ~start_pos ~end_pos
else
[]
in
match !input_lexbuf with
| Some lb ->
begin match lines_around_from_lexbuf lb ~start_pos ~end_pos with
| [] -> (* The input is likely not in the lexbuf anymore *)
from_file ()
| lines ->
lines
end
| None ->
from_file ()
*)(******************************************************************************)(* Reporting errors and warnings *)typemsg=(Format.formatter->unit)locletmsg?(loc=none)fmt=Format.kdprintf(funtxt->{loc;txt})fmttypereport_kind=|Report_error|Report_warningofstring|Report_warning_as_errorofstring|Report_alertofstring|Report_alert_as_errorofstringtypeerror_source=Lexer|Parser|Typer|Warning|Unknown|Env|Configtypereport={kind:report_kind;main:msg;sub:msglist;source:error_source;}letloc_of_report{main;_}=main.locletprint_msgfmtmsg=msg.txtfmtletprint_mainfmt{main;_}=print_msgfmtmainletprint_sub_msg=print_msgtypereport_printer={(* The entry point *)pp:report_printer->Format.formatter->report->unit;pp_report_kind:report_printer->report->Format.formatter->report_kind->unit;pp_main_loc:report_printer->report->Format.formatter->t->unit;pp_main_txt:report_printer->report->Format.formatter->(Format.formatter->unit)->unit;pp_submsgs:report_printer->report->Format.formatter->msglist->unit;pp_submsg:report_printer->report->Format.formatter->msg->unit;pp_submsg_loc:report_printer->report->Format.formatter->t->unit;pp_submsg_txt:report_printer->report->Format.formatter->(Format.formatter->unit)->unit;}(*
let is_dummy_loc loc =
(* Fixme: this should be just [loc.loc_ghost] and the function should be
inlined below. However, currently, the compiler emits in some places ghost
locations with valid ranges that should still be printed. These locations
should be made non-ghost -- in the meantime we just check if the ranges are
valid. *)
loc.loc_start.pos_cnum = -1 || loc.loc_end.pos_cnum = -1
*)(* It only makes sense to highlight (i.e. quote or underline the corresponding
source code) locations that originate from the current input.
As of now, this should only happen in the following cases:
- if dummy locs or ghost locs leak out of the compiler or a buggy ppx;
- more generally, if some code uses the compiler-libs API and feeds it
locations that do not match the current values of [!Location.input_name],
[!Location.input_lexbuf];
- when calling the compiler on a .ml file that contains lexer line directives
indicating an other file. This should happen relatively rarely in practice --
in particular this is not what happens when using -pp or -ppx or a ppx
driver.
*)(*
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 error_style () =
let open Misc.Error_style in
match !Clflags.error_style with
| Some Contextual | None -> Contextual
| Some Short -> Short
*)letbatch_mode_printer:report_printer=letpp_loc_self_report_ppf_loc=(*
let tag = match report.kind with
| Report_warning_as_error _
| Report_alert_as_error _
| Report_error -> "error"
| Report_warning _
| Report_alert _ -> "warning"
in
let highlight ppf loc =
match error_style () with
| Misc.Error_style.Contextual ->
if is_quotable_loc loc then
highlight_quote ppf
~get_lines:lines_around_from_current_input
tag [loc]
| Misc.Error_style.Short ->
()
in
Format.fprintf ppf "@[<v>%a:@ %a@]" print_loc loc highlight loc
*)()inletpp_txtppftxt=Format.fprintfppf"@[%t@]"txtinletppselfppfreport=separate_new_messageppf;(* Make sure we keep [num_loc_lines] updated.
The tabulation box is here to give submessage the option
to be aligned with the main message box
*)print_updating_num_loc_linesppf(funppf()->Format.fprintfppf"@[<v>%a%a%a: %a%a%a%a@]@."Format.pp_open_tbox()(self.pp_main_locselfreport)report.main.loc(self.pp_report_kindselfreport)report.kindFormat.pp_set_tab()(self.pp_main_txtselfreport)report.main.txt(self.pp_submsgsselfreport)report.subFormat.pp_close_tbox())()inletpp_report_kind_self_ppf=function|Report_error->Format.fprintfppf"@{<error>Error@}"|Report_warningw->Format.fprintfppf"@{<warning>Warning@} %s"w|Report_warning_as_errorw->Format.fprintfppf"@{<error>Error@} (warning %s)"w|Report_alertw->Format.fprintfppf"@{<warning>Alert@} %s"w|Report_alert_as_errorw->Format.fprintfppf"@{<error>Error@} (alert %s)"winletpp_main_locselfreportppfloc=pp_locselfreportppflocinletpp_main_txt_self_ppftxt=pp_txtppftxtinletpp_submsgsselfreportppfmsgs=List.iter(funmsg->Format.fprintfppf"@,%a"(self.pp_submsgselfreport)msg)msgsinletpp_submsgselfreportppf{loc;txt}=Format.fprintfppf"@[%a %a@]"(self.pp_submsg_locselfreport)loc(self.pp_submsg_txtselfreport)txtinletpp_submsg_locselfreportppfloc=ifnotloc.loc_ghostthenpp_locselfreportppflocinletpp_submsg_txt_self_ppfloc=pp_txtppflocin{pp;pp_report_kind;pp_main_loc;pp_main_txt;pp_submsgs;pp_submsg;pp_submsg_loc;pp_submsg_txt}(*
let terminfo_toplevel_printer (lb: lexbuf): report_printer =
let pp self ppf err =
setup_colors ();
(* Highlight all toplevel locations of the report, instead of displaying
the main location. Do it now instead of in [pp_main_loc], to avoid
messing with Format boxes. *)
let sub_locs = List.map (fun { loc; _ } -> loc) err.sub in
let all_locs = err.main.loc :: sub_locs in
let locs_highlighted = List.filter is_quotable_loc all_locs in
highlight_terminfo lb ppf locs_highlighted;
batch_mode_printer.pp self ppf err
in
let pp_main_loc _ _ _ _ = () in
let pp_submsg_loc _ _ ppf loc =
if not loc.loc_ghost then
Format.fprintf ppf "%a:@ " print_loc loc in
{ batch_mode_printer with pp; pp_main_loc; pp_submsg_loc }
let best_toplevel_printer () =
setup_terminal ();
match !status, !input_lexbuf with
| Terminfo.Good_term, Some lb ->
terminfo_toplevel_printer lb
| _, _ ->
batch_mode_printer
*)(* Creates a printer for the current input *)letdefault_report_printer():report_printer=batch_mode_printerletreport_printer=refdefault_report_printerletprint_reportppfreport=letprinter=!report_printer()inprinter.ppprinterppfreport(******************************************************************************)(* Reporting errors *)typeerror=reportletreport_errorppferr=print_reportppferrletmkerrorlocsubtxtsource={kind=Report_error;main={loc;txt};sub;source}leterrorf?(loc=none)?(sub=[])?(source=Typer)=Format.kdprintf(funmsg->mkerrorlocsubmsgsource)leterror?(loc=none)?(sub=[])?(source=Typer)msg_str=mkerrorlocsub(funppf->Format.pp_print_stringppfmsg_str)sourceleterror_of_printer?(loc=none)?(sub=[])?(source=Typer)ppx=mkerrorlocsub(funppf->ppppfx)sourceleterror_of_printer_file?sourceprintx=error_of_printer?source~loc:(in_file!input_name)printx(******************************************************************************)(* Reporting warnings: generating a report from a warning number using the
information in [Warnings] + convenience functions. *)letdefault_warning_alert_reporter?(source=Typer)reportmk(loc:t)w:reportoption=matchreportwwith|`Inactive->None|`Active{Warnings.id;message;is_error;sub_locs}->letmsg_of_strstr=funppf->Format.pp_print_stringppfstrinletkind=mkis_erroridinletmain={loc;txt=msg_of_strmessage}inletsub=List.map(fun(loc,sub_message)->{loc;txt=msg_of_strsub_message})sub_locsinSome{kind;main;sub;source}letdefault_warning_reporter=default_warning_alert_reporterWarnings.report(funis_errorid->ifis_errorthenReport_warning_as_erroridelseReport_warningid)letwarning_reporter=refdefault_warning_reporterletreport_warninglocw=!warning_reporterlocwletformatter_for_warnings=refFormat.err_formatterletprint_warninglocppfw=matchreport_warninglocwwith|None->()|Somereport->print_reportppfreportletprerr_warning_ref=ref(funlocw->print_warningloc!formatter_for_warningsw)letprerr_warninglocw=!prerr_warning_reflocwletdefault_alert_reporter=default_warning_alert_reporterWarnings.report_alert(funis_errorid->ifis_errorthenReport_alert_as_erroridelseReport_alertid)letalert_reporter=refdefault_alert_reporterletreport_alertlocw=!alert_reporterlocwletprint_alertlocppfw=matchreport_alertlocwwith|None->()|Somereport->print_reportppfreportletprerr_alert_ref=ref(funlocw->print_alertloc!formatter_for_warningsw)letprerr_alertlocw=!prerr_alert_reflocwletalert?(def=none)?(use=none)~kindlocmessage=prerr_alertloc{Warnings.kind;message;def;use}letdeprecated?def?uselocmessage=alert?def?use~kind:"deprecated"locmessageletauto_include_alertlib=letmessage=Printf.sprintf"\
OCaml's lib directory layout changed in 5.0. The %s subdirectory has been \
automatically added to the search path, but you should add -I +%s to the \
command-line to silence this alert (e.g. by adding %s to the list of \
libraries in your dune file, or adding use_%s to your _tags file for \
ocamlbuild, or using -package %s for ocamlfind)."libliblibliblibinletalert={Warnings.kind="ocaml_deprecated_auto_include";use=none;def=none;message=Format.asprintf"@[@\n%a@]"Format.pp_print_textmessage}inprerr_alertnonealertletdeprecated_script_alertprogram=letmessage=Printf.sprintf"\
Running %s where the first argument is an implicit basename with no \
extension (e.g. %s script-file) is deprecated. Either rename the script \
(%s script-file.ml) or qualify the basename (%s ./script-file)"programprogramprogramprograminletalert={Warnings.kind="ocaml_deprecated_cli";use=none;def=none;message=Format.asprintf"@[@\n%a@]"Format.pp_print_textmessage}inprerr_alertnonealert(******************************************************************************)(* Reporting errors on exceptions *)leterror_of_exn:(exn->erroroption)listref=ref[]letregister_error_of_exnf=error_of_exn:=f::!error_of_exnexceptionAlready_displayed_error=Warnings.Errorsleterror_of_exnexn=matchexnwith|Already_displayed_error->Some`Already_displayed|_->letrecloop=function|[]->None|f::rest->matchfexnwith|Someerror->Some(`Okerror)|None->looprestinloop!error_of_exnlet()=register_error_of_exn(function|Sys_errormsg->Some(errorf~loc:(in_file!input_name)"I/O error: %s"msg)|_->None)externalreraise:exn->'a="%reraise"letreport_exceptionppfexn=letrecloopnexn=matcherror_of_exnexnwith|None->reraiseexn|Some`Already_displayed->()|Some(`Okerr)->report_errorppferr|exceptionexnwhenn>0->loop(n-1)exninloop5exnexceptionErroroferrorlet()=register_error_of_exn(function|Errore->Somee|_->None)letraise_errorf?(loc=none)?(sub=[])?(source=Typer)=Format.kdprintf(funtxt->raise(Error(mkerrorlocsubtxtsource)))