From 43758e9fd793ada1e6749decb446e1c043de1a4f Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 29 Nov 2017 06:04:07 -0800 Subject: [PATCH] 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 --- infer/src/base/SourceFile.ml | 31 +++++++++++-------------------- infer/src/base/SourceFile.mli | 16 ++++------------ infer/src/base/Utils.ml | 18 +++++++++--------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/infer/src/base/SourceFile.ml b/infer/src/base/SourceFile.ml index aff24c6c9..91df425ca 100644 --- a/infer/src/base/SourceFile.ml +++ b/infer/src/base/SourceFile.ml @@ -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 diff --git a/infer/src/base/SourceFile.mli b/infer/src/base/SourceFile.mli index 402d85fd6..626baa94b 100644 --- a/infer/src/base/SourceFile.mli +++ b/infer/src/base/SourceFile.mli @@ -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 *) diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index c760f0609..5eddbd5b9 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -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 *)