SourceFile: random stuff

Summary:
I was
wanderingaround
- killed dead `UNSAFE`, dead comment
- fixed comment placement
- simplifications

Reviewed By: jberdine

Differential Revision: D6408188

fbshipit-source-id: ac8caa6
master
Mehdi Bouaziz 7 years ago committed by Facebook Github Bot
parent 5a06a187f2
commit 43758e9fd7

@ -29,10 +29,9 @@ type t =
let equal = [%compare.equal : t] let equal = [%compare.equal : t]
module OrderedSourceFile = struct module OrderedSourceFile = struct
(* Don't use nonrec due to https://github.com/janestreet/ppx_compare/issues/2 *) type nonrec t = t
type t_ = t [@@deriving compare]
type t = t_ [@@deriving compare] let compare = compare
end end
module Map = Caml.Map.Make (OrderedSourceFile) module Map = Caml.Map.Make (OrderedSourceFile)
@ -53,11 +52,10 @@ let from_abs_path ?(warn_on_error= true) fname =
| Some path -> | Some path ->
RelativeInferModel path RelativeInferModel path
| None -> | None ->
(* fname_real is absolute already *)
Absolute fname_real Absolute fname_real
(* fname_real is absolute already *)
let to_string fname = let to_string fname =
match fname with match fname with
| Invalid origin -> | Invalid origin ->
@ -70,7 +68,6 @@ let to_string fname =
let pp fmt fname = Format.fprintf fmt "%s" (to_string fname) let pp fmt fname = Format.fprintf fmt "%s" (to_string fname)
(* Checking if the path exists may be needed only in some cases, hence the flag check_exists *)
let to_abs_path fname = let to_abs_path fname =
match fname with match fname with
| Invalid origin -> | Invalid origin ->
@ -137,16 +134,14 @@ let of_header ?(warn_on_error= true) header_file =
let abs_path = to_abs_path header_file in let abs_path = to_abs_path header_file in
let source_exts = ["c"; "cc"; "cpp"; "cxx"; "m"; "mm"] in let source_exts = ["c"; "cc"; "cpp"; "cxx"; "m"; "mm"] in
let header_exts = ["h"; "hh"; "hpp"; "hxx"] in let header_exts = ["h"; "hh"; "hpp"; "hxx"] in
let file_no_ext, ext_opt = Filename.split_extension abs_path in match Filename.split_extension abs_path with
let file_opt = | file_no_ext, Some ext when List.mem ~equal:String.equal header_exts ext ->
match ext_opt with List.find_map source_exts ~f:(fun ext ->
| Some ext when List.mem ~equal:String.equal header_exts ext -> let possible_file = file_no_ext ^ "." ^ ext in
let possible_files = List.map ~f:(fun ext -> file_no_ext ^ "." ^ ext) source_exts in if path_exists possible_file then Some (from_abs_path ~warn_on_error possible_file)
List.find ~f:path_exists possible_files else None )
| _ -> | _ ->
None None
in
Option.map ~f:(from_abs_path ~warn_on_error) file_opt
let create ?(warn_on_error= true) path = let create ?(warn_on_error= true) path =
@ -167,7 +162,3 @@ let changed_sources_from_changed_files changed_files =
| None -> | None ->
changed_files' ) changed_files' )
module UNSAFE = struct
let from_string str = if Filename.is_relative str then RelativeProjectRoot str else Absolute str
end

@ -17,13 +17,6 @@ module Map : Caml.Map.S with type key = t
(** Set of source files *) (** Set of source files *)
module Set : Caml.Set.S with type elt = t module Set : Caml.Set.S with type elt = t
module UNSAFE : sig
val from_string : string -> t
(** Create a SourceFile from any path. This is unchecked and should not be
used when the existence of source files is a requirement. Furthermore,
absolute paths won't be made relative to project root.*)
end
val is_invalid : t -> bool val is_invalid : t -> bool
(** Is the source file the invalid source file? *) (** Is the source file the invalid source file? *)
@ -43,12 +36,11 @@ val from_abs_path : ?warn_on_error:bool -> string -> t
WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for
the given path (e.g. if it does not exist). *) the given path (e.g. if it does not exist). *)
(* Create a SourceFile from a given path. If relative, it assumes it is w.r.t. project root. val create : ?warn_on_error:bool -> string -> t
(** Create a SourceFile from a given path. If relative, it assumes it is w.r.t. project root.
WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for
the given path (e.g. if it does not exist). *) the given path (e.g. if it does not exist). *)
val create : ?warn_on_error:bool -> string -> t
val is_cpp_model : t -> bool val is_cpp_model : t -> bool
(** Returns true if the file is a C++ model *) (** Returns true if the file is a C++ model *)

@ -283,17 +283,17 @@ let realpath ?(warn_on_error= true) path =
| realpath -> | realpath ->
Hashtbl.add realpath_cache path (Ok realpath) ; Hashtbl.add realpath_cache path (Ok realpath) ;
realpath realpath
| exception Unix.Unix_error (code, f, arg) -> | exception (Unix.Unix_error (code, _, arg) as exn) ->
reraise_after exn ~f:(fun () ->
if warn_on_error then if warn_on_error then
F.eprintf "WARNING: Failed to resolve file %s with \"%s\" @\n@." arg F.eprintf "WARNING: Failed to resolve file %s with \"%s\" @\n@." arg
(Unix.Error.message code) ; (Unix.Error.message code) ;
(* cache failures as well *) (* cache failures as well *)
Hashtbl.add realpath_cache path (Error (code, f, arg)) ; Hashtbl.add realpath_cache path (Error exn) ) )
raise (Unix.Unix_error (code, f, arg)) )
| Ok path -> | Ok path ->
path path
| Error (code, f, arg) -> | Error exn ->
raise (Unix.Unix_error (code, f, arg)) raise exn
(* never closed *) (* never closed *)

Loading…
Cancel
Save