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]
module OrderedSourceFile = struct
(* Don't use nonrec due to https://github.com/janestreet/ppx_compare/issues/2 *)
type t_ = t [@@deriving compare]
type nonrec t = t
type t = t_ [@@deriving compare]
let compare = compare
end
module Map = Caml.Map.Make (OrderedSourceFile)
@ -53,11 +52,10 @@ let from_abs_path ?(warn_on_error= true) fname =
| Some path ->
RelativeInferModel path
| None ->
(* fname_real is absolute already *)
Absolute fname_real
(* fname_real is absolute already *)
let to_string fname =
match fname with
| Invalid origin ->
@ -70,7 +68,6 @@ let 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 =
match fname with
| 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 source_exts = ["c"; "cc"; "cpp"; "cxx"; "m"; "mm"] in
let header_exts = ["h"; "hh"; "hpp"; "hxx"] in
let file_no_ext, ext_opt = Filename.split_extension abs_path in
let file_opt =
match ext_opt with
| Some ext when List.mem ~equal:String.equal header_exts ext ->
let possible_files = List.map ~f:(fun ext -> file_no_ext ^ "." ^ ext) source_exts in
List.find ~f:path_exists possible_files
| _ ->
None
in
Option.map ~f:(from_abs_path ~warn_on_error) file_opt
match Filename.split_extension abs_path with
| file_no_ext, Some ext when List.mem ~equal:String.equal header_exts ext ->
List.find_map source_exts ~f:(fun ext ->
let possible_file = file_no_ext ^ "." ^ ext in
if path_exists possible_file then Some (from_abs_path ~warn_on_error possible_file)
else None )
| _ ->
None
let create ?(warn_on_error= true) path =
@ -167,7 +162,3 @@ let changed_sources_from_changed_files changed_files =
| None ->
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 *)
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
(** 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
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
the given path (e.g. if it does not exist). *)
val create : ?warn_on_error:bool -> string -> t
val is_cpp_model : t -> bool
(** Returns true if the file is a C++ model *)
@ -63,8 +55,8 @@ val line_count : t -> int
val of_header : ?warn_on_error:bool -> t -> t option
(** Return approximate source file corresponding to the parameter if it's header file and
file exists. returns None otherwise.
WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for
the given SourceFile (e.g. if it does not exist).*)
WARNING: If warn_on_error is false, no warning will be shown whenever an error occurs for
the given SourceFile (e.g. if it does not exist). *)
val pp : Format.formatter -> t -> unit
(** pretty print t *)

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

Loading…
Cancel
Save