Entirely eliminate Dloc

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-08-23 11:57:31 +03:00
parent d9d7792cfb
commit ca6e7c04bd
16 changed files with 97 additions and 107 deletions

View File

@ -83,11 +83,11 @@ let static_deps t ~all_targets ~file_tree =
if Path.Set.is_empty result then begin if Path.Set.is_empty result then begin
match inspect_path file_tree dir with match inspect_path file_tree dir with
| None -> | None ->
Dloc.warn loc "Directory %s doesn't exist." Errors.warn loc "Directory %s doesn't exist."
(Path.to_string_maybe_quoted (Path.to_string_maybe_quoted
(Path.drop_optional_build_context dir)) (Path.drop_optional_build_context dir))
| Some Reg -> | Some Reg ->
Dloc.warn loc "%s is not a directory." Errors.warn loc "%s is not a directory."
(Path.to_string_maybe_quoted (Path.to_string_maybe_quoted
(Path.drop_optional_build_context dir)) (Path.drop_optional_build_context dir))
| Some Dir -> | Some Dir ->

View File

@ -565,7 +565,7 @@ let add_spec t fn spec ~copy_source =
| Some (File_spec.T { rule; _ }) -> | Some (File_spec.T { rule; _ }) ->
match copy_source, rule.mode with match copy_source, rule.mode with
| true, (Standard | Not_a_rule_stanza) -> | true, (Standard | Not_a_rule_stanza) ->
Dloc.warn (Internal_rule.loc rule ~dir:(Path.parent_exn fn) Errors.warn (Internal_rule.loc rule ~dir:(Path.parent_exn fn)
~file_tree:t.file_tree) ~file_tree:t.file_tree)
"File %s is both generated by a rule and present in the source tree.\n\ "File %s is both generated by a rule and present in the source tree.\n\
As a result, the rule is currently ignored, however this will become an error \ As a result, the rule is currently ignored, however this will become an error \

View File

@ -101,7 +101,7 @@ end = struct
if missing_intf_only <> [] then begin if missing_intf_only <> [] then begin
match Ordered_set_lang.loc buildable.modules_without_implementation with match Ordered_set_lang.loc buildable.modules_without_implementation with
| None -> | None ->
Dloc.warn buildable.loc Errors.warn buildable.loc
"Some modules don't have an implementation.\ "Some modules don't have an implementation.\
\nYou need to add the following field to this stanza:\ \nYou need to add the following field to this stanza:\
\n\ \n\
@ -122,7 +122,7 @@ end = struct
|> List.map ~f:(sprintf "- %s") |> List.map ~f:(sprintf "- %s")
|> String.concat ~sep:"\n" |> String.concat ~sep:"\n"
in in
Dloc.warn loc Errors.warn loc
"The following modules must be listed here as they don't \ "The following modules must be listed here as they don't \
have an implementation:\n\ have an implementation:\n\
%s\n\ %s\n\
@ -155,7 +155,7 @@ end = struct
) )
in in
Module.Name.Map.iteri fake_modules ~f:(fun m loc -> Module.Name.Map.iteri fake_modules ~f:(fun m loc ->
Dloc.warn loc "Module %a is excluded but it doesn't exist." Errors.warn loc "Module %a is excluded but it doesn't exist."
Module.Name.pp m Module.Name.pp m
); );
check_invalid_module_listing ~buildable:conf ~intf_only ~modules check_invalid_module_listing ~buildable:conf ~intf_only ~modules
@ -442,7 +442,7 @@ let build_modules_map (d : Super_context.Dir_with_jbuild.t) ~modules =
List.sort ~compare List.sort ~compare
(b.Buildable.loc :: List.map rest ~f:(fun b -> b.Buildable.loc)) (b.Buildable.loc :: List.map rest ~f:(fun b -> b.Buildable.loc))
in in
Dloc.warn (Loc.in_file b.loc.start.pos_fname) Errors.warn (Loc.in_file b.loc.start.pos_fname)
"Module %a is used in several stanzas:@\n\ "Module %a is used in several stanzas:@\n\
@[<v>%a@]@\n\ @[<v>%a@]@\n\
@[%a@]@\n\ @[%a@]@\n\

View File

@ -1,65 +0,0 @@
open! Stdune
open Import
include Loc
let in_file = Loc.in_file
let file_line path n =
Io.with_file_in ~binary:false path
~f:(fun ic ->
for _ = 1 to n - 1 do
ignore (input_line ic)
done;
input_line ic
)
let file_lines path ~start ~stop =
Io.with_file_in ~binary:true path
~f:(fun ic ->
let rec aux acc lnum =
if lnum > stop then
List.rev acc
else if lnum < start then
(ignore (input_line ic);
aux acc (lnum + 1))
else
let line = input_line ic in
aux ((string_of_int lnum, line) :: acc) (lnum + 1)
in
aux [] 1
)
let print ppf loc =
let { Loc.start; stop } = loc in
let start_c = start.pos_cnum - start.pos_bol in
let stop_c = stop.pos_cnum - start.pos_bol in
let num_lines = stop.pos_lnum - start.pos_lnum in
let pp_file_excerpt pp () =
let whole_file = start_c = 0 && stop_c = 0 in
if not whole_file then
let path = Path.of_string start.pos_fname in
if Path.exists path then
let line = file_line path start.pos_lnum in
if stop_c <= String.length line then
let len = stop_c - start_c in
Format.fprintf pp "%s\n%*s\n" line
stop_c
(String.make len '^')
else if num_lines <= 10 then
let lines = file_lines path ~start:start.pos_lnum ~stop:stop.pos_lnum in
let last_lnum = Option.map ~f:fst (List.last lines) in
let padding_width = Option.value_exn
(Option.map ~f:String.length last_lnum) in
List.iter ~f:(fun (lnum, l) ->
Format.fprintf pp "%*s: %s\n" padding_width lnum l)
lines
in
Format.fprintf ppf
"@{<loc>File \"%s\", line %d, characters %d-%d:@}@\n%a"
start.pos_fname start.pos_lnum start_c stop_c
pp_file_excerpt ()
let warn t fmt =
Errors.kerrf ~f:print_to_console
("%a@{<warning>Warning@}: " ^^ fmt ^^ "@.") print t

View File

@ -1,16 +0,0 @@
open! Stdune
type t = Loc.t =
{ start : Lexing.position
; stop : Lexing.position
}
val in_file : string -> t
val none : t
(** Prints "File ..., line ..., characters ...:\n" *)
val print : Format.formatter -> t -> unit
(** Prints a warning *)
val warn : t -> ('a, Format.formatter, unit) format -> 'a

View File

@ -80,7 +80,7 @@ end = struct
match res, wrapped with match res, wrapped with
| Ok s, _ -> s | Ok s, _ -> s
| Warn _, true -> Errors.fail loc "%s" wrapped_message | Warn _, true -> Errors.fail loc "%s" wrapped_message
| Warn s, false -> Dloc.warn loc "%s" wrapped_message; s | Warn s, false -> Errors.warn loc "%s" wrapped_message; s
| Invalid, _ -> Errors.fail loc "%s" invalid_message | Invalid, _ -> Errors.fail loc "%s" invalid_message
let valid_char = function let valid_char = function
@ -1289,7 +1289,7 @@ module Executables = struct
| Some (loc, _) -> | Some (loc, _) ->
let func = let func =
match file_kind with match file_kind with
| Jbuild -> Dloc.warn | Jbuild -> Errors.warn
| Dune -> Errors.fail | Dune -> Errors.fail
in in
func loc func loc

View File

@ -33,3 +33,66 @@ let fail_opt t fmt =
match t with match t with
| None -> die fmt | None -> die fmt
| Some t -> fail t fmt | Some t -> fail t fmt
let file_line path n =
Io.with_file_in ~binary:false path
~f:(fun ic ->
for _ = 1 to n - 1 do
ignore (input_line ic)
done;
input_line ic
)
let file_lines path ~start ~stop =
Io.with_file_in ~binary:true path
~f:(fun ic ->
let rec aux acc lnum =
if lnum > stop then
List.rev acc
else if lnum < start then
(ignore (input_line ic);
aux acc (lnum + 1))
else
let line = input_line ic in
aux ((string_of_int lnum, line) :: acc) (lnum + 1)
in
aux [] 1
)
let print ppf loc =
let { Loc.start; stop } = loc in
let start_c = start.pos_cnum - start.pos_bol in
let stop_c = stop.pos_cnum - start.pos_bol in
let num_lines = stop.pos_lnum - start.pos_lnum in
let pp_file_excerpt pp () =
let whole_file = start_c = 0 && stop_c = 0 in
if not whole_file then
let path = Path.of_string start.pos_fname in
if Path.exists path then
let line = file_line path start.pos_lnum in
if stop_c <= String.length line then
let len = stop_c - start_c in
Format.fprintf pp "%s\n%*s\n" line
stop_c
(String.make len '^')
else if num_lines <= 10 then
let lines = file_lines path ~start:start.pos_lnum ~stop:stop.pos_lnum in
let last_lnum = Option.map ~f:fst (List.last lines) in
let padding_width = Option.value_exn
(Option.map ~f:String.length last_lnum) in
List.iter ~f:(fun (lnum, l) ->
Format.fprintf pp "%*s: %s\n" padding_width lnum l)
lines
in
Format.fprintf ppf
"@{<loc>File \"%s\", line %d, characters %d-%d:@}@\n%a"
start.pos_fname start.pos_lnum start_c stop_c
pp_file_excerpt ()
(* This is ugly *)
let printer = ref (Printf.eprintf "%s%!")
let print_to_console s = !printer s
let warn t fmt =
kerrf ~f:print_to_console
("%a@{<warning>Warning@}: " ^^ fmt ^^ "@.") print t

View File

@ -27,3 +27,13 @@ val exnf : Loc.t -> ('a, Format.formatter, unit, exn) format4 -> 'a
val fail : Loc.t -> ('a, Format.formatter, unit, 'b ) format4 -> 'a val fail : Loc.t -> ('a, Format.formatter, unit, 'b ) format4 -> 'a
val fail_lex : Lexing.lexbuf -> ('a, Format.formatter, unit, 'b ) format4 -> 'a val fail_lex : Lexing.lexbuf -> ('a, Format.formatter, unit, 'b ) format4 -> 'a
val fail_opt : Loc.t option -> ('a, Format.formatter, unit, 'b ) format4 -> 'a val fail_opt : Loc.t option -> ('a, Format.formatter, unit, 'b ) format4 -> 'a
(** Prints "File ..., line ..., characters ...:\n" *)
val print : Format.formatter -> Loc.t -> unit
(** Prints a warning *)
val warn : Loc.t -> ('a, Format.formatter, unit) format -> 'a
val print_to_console : string -> unit
val printer : (string -> unit) ref

View File

@ -89,12 +89,12 @@ let load_jbuild_ignore path =
if Filename.dirname fn = Filename.current_dir_name then if Filename.dirname fn = Filename.current_dir_name then
true true
else begin else begin
Dloc.(warn (Loc.of_pos Errors.(warn (Loc.of_pos
( Path.to_string path ( Path.to_string path
, i + 1, 0 , i + 1, 0
, String.length fn , String.length fn
)) ))
"subdirectory expression %s ignored" fn); "subdirectory expression %s ignored" fn);
false false
end) end)
|> String.Set.of_list |> String.Set.of_list

View File

@ -89,6 +89,4 @@ module No_io = struct
module Io = struct end module Io = struct end
end end
(* This is ugly *) let print_to_console = Errors.print_to_console
let printer = ref (Printf.eprintf "%s%!")
let print_to_console s = !printer s

View File

@ -1138,7 +1138,7 @@ let report_lib_error ppf (e : Error.t) =
| No_solution_found_for_select { loc } -> | No_solution_found_for_select { loc } ->
Format.fprintf ppf Format.fprintf ppf
"%a@{<error>Error@}: No solution found for this select form.\n" "%a@{<error>Error@}: No solution found for this select form.\n"
Dloc.print loc Errors.print loc
| Dependency_cycle cycle -> | Dependency_cycle cycle ->
Format.fprintf ppf Format.fprintf ppf
"@{<error>Error@}: Dependency cycle detected between the \ "@{<error>Error@}: Dependency cycle detected between the \

View File

@ -16,7 +16,7 @@ let print ?(skip_trailing_cr=Sys.win32) path1 path2 =
in in
let loc = Loc.in_file file1 in let loc = Loc.in_file file1 in
let fallback () = let fallback () =
die "%aFiles %s and %s differ." Dloc.print loc die "%aFiles %s and %s differ." Errors.print loc
(Path.to_string_maybe_quoted path1) (Path.to_string_maybe_quoted path1)
(Path.to_string_maybe_quoted path2) (Path.to_string_maybe_quoted path2)
in in
@ -24,7 +24,7 @@ let print ?(skip_trailing_cr=Sys.win32) path1 path2 =
match Bin.which "diff" with match Bin.which "diff" with
| None -> fallback () | None -> fallback ()
| Some prog -> | Some prog ->
Format.eprintf "%a@?" Dloc.print loc; Format.eprintf "%a@?" Errors.print loc;
Process.run ~dir ~env:Env.initial Strict prog Process.run ~dir ~env:Env.initial Strict prog
(List.concat (List.concat
[ ["-u"] [ ["-u"]

View File

@ -121,7 +121,7 @@ let report exn =
else else
p.loc p.loc
in in
Option.iter loc ~f:(fun loc -> Dloc.print ppf loc); Option.iter loc ~f:(fun loc -> Errors.print ppf loc);
p.pp ppf; p.pp ppf;
Format.pp_print_flush ppf (); Format.pp_print_flush ppf ();
let s = Buffer.contents err_buf in let s = Buffer.contents err_buf in

View File

@ -196,7 +196,7 @@ let go ?(log=Log.no_log) ?(config=Config.default)
; waiting_for_available_job = Queue.create () ; waiting_for_available_job = Queue.create ()
} }
in in
printer := print t; Errors.printer := print t;
let fiber = let fiber =
Fiber.Var.set t_var t Fiber.Var.set t_var t
(Fiber.with_error_handler (fun () -> fiber) ~on_error:Report_error.report) (Fiber.with_error_handler (fun () -> fiber) ~on_error:Report_error.report)

View File

@ -88,7 +88,7 @@ module Of_sexp = struct
match Univ_map.find parsing_context (Syntax.key syntax) with match Univ_map.find parsing_context (Syntax.key syntax) with
| Some (0, _) -> | Some (0, _) ->
let last = Option.value_exn (List.last entries) in let last = Option.value_exn (List.last entries) in
Dloc.warn (Dsexp.Ast.loc last) Errors.warn (Dsexp.Ast.loc last)
"Field %S is present several times, previous occurrences are ignored." "Field %S is present several times, previous occurrences are ignored."
name name
| _ -> | _ ->

View File

@ -856,7 +856,7 @@ module Action = struct
assert false assert false
| Unnamed v :: _ -> [Path v] | Unnamed v :: _ -> [Path v]
| [] -> | [] ->
Dloc.warn loc "Variable '%s' used with no explicit \ Errors.warn loc "Variable '%s' used with no explicit \
dependencies@." key; dependencies@." key;
[Value.String ""] [Value.String ""]
end end
@ -874,7 +874,7 @@ module Action = struct
| [] -> () | [] -> ()
| x :: _ -> | x :: _ ->
let loc = String_with_vars.loc x in let loc = String_with_vars.loc x in
Dloc.warn loc Errors.warn loc
"Aliases must not have targets, this target will be ignored.\n\ "Aliases must not have targets, this target will be ignored.\n\
This will become an error in the future."; This will become an error in the future.";
end; end;