From f2728f5592a42b13dcc2547db54e57a326ea2c76 Mon Sep 17 00:00:00 2001 From: Martino Luca Date: Fri, 9 Jun 2017 05:31:54 -0700 Subject: [PATCH] Suppress warnings on SourceFile creation, if desired Summary: When dealing with differential's comparisons, SourceFiles in reports may not exist anymore because of changes between revisions. Disable user visible warnings if desired, through the labeled argument `~warn_on_error` Reviewed By: jvillard Differential Revision: D5217097 fbshipit-source-id: 7a1542b --- infer/src/base/SourceFile.ml | 14 +++++++------- infer/src/base/SourceFile.mli | 19 ++++++++++++------- infer/src/base/Utils.ml | 7 ++++--- infer/src/base/Utils.mli | 8 +++++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/infer/src/base/SourceFile.ml b/infer/src/base/SourceFile.ml index 298c845e1..951da068b 100644 --- a/infer/src/base/SourceFile.ml +++ b/infer/src/base/SourceFile.ml @@ -34,14 +34,14 @@ module Map = Caml.Map.Make (OrderedSourceFile) module Set = Caml.Set.Make (OrderedSourceFile) -let from_abs_path fname = +let from_abs_path ?(warn_on_error=true) fname = if Filename.is_relative fname then (failwithf "ERROR: Path %s is relative, when absolute path was expected .@." fname); (* try to get realpath of source file. Use original if it fails *) - let fname_real = try Utils.realpath fname with Unix.Unix_error _ -> fname in - let project_root_real = Utils.realpath Config.project_root in + let fname_real = try Utils.realpath ~warn_on_error fname with Unix.Unix_error _ -> fname in + let project_root_real = Utils.realpath ~warn_on_error Config.project_root in let models_dir_real = Config.models_src_dir in match Utils.filename_to_relative ~root:project_root_real fname_real with | Some path -> RelativeProjectRoot path @@ -113,7 +113,7 @@ let path_exists abs_path = String.Table.set exists_cache ~key:abs_path ~data:result; result -let of_header header_file = +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 @@ -124,14 +124,14 @@ let of_header header_file = List.find ~f:path_exists possible_files ) | _ -> None in - Option.map ~f:from_abs_path file_opt + Option.map ~f:(from_abs_path ~warn_on_error) file_opt -let create path = +let create ?(warn_on_error=true) path = if Filename.is_relative path then (* sources in changed-files-index may be specified relative to project root *) RelativeProjectRoot path else - from_abs_path path + from_abs_path ~warn_on_error path let changed_files_set = Option.bind Config.changed_files_index Utils.read_file |> diff --git a/infer/src/base/SourceFile.mli b/infer/src/base/SourceFile.mli index 58db4c4ad..950e8f172 100644 --- a/infer/src/base/SourceFile.mli +++ b/infer/src/base/SourceFile.mli @@ -38,12 +38,15 @@ val invalid : string -> t (** equality of source files *) val equal : t -> t -> bool -(** create source file from absolute path *) -val from_abs_path : string -> t +(** create source file from absolute path. + 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 from_abs_path : ?warn_on_error:bool -> string -> t -(* Create a SourceFile from a given path. If relative, it assumes it is w.r.t. - project root *) -val create : 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 (** Returns true if the file is a C++ model *) val is_cpp_model : t -> bool @@ -57,8 +60,10 @@ val is_under_project_root : t -> bool val line_count : t -> int (** Return approximate source file corresponding to the parameter if it's header file and - file exists. returns None otherwise *) -val of_header : t -> t option + 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).*) +val of_header : ?warn_on_error:bool -> t -> t option (** pretty print t *) val pp : Format.formatter -> t -> unit diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index aa1f90eea..b818bd3dc 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -255,7 +255,7 @@ let create_dir dir = let realpath_cache = Hashtbl.create 1023 -let realpath path = +let realpath ?(warn_on_error=true) path = match Hashtbl.find realpath_cache path with | exception Not_found -> ( match Filename.realpath path with @@ -263,8 +263,9 @@ let realpath path = Hashtbl.add realpath_cache path (Ok realpath); realpath | exception Unix.Unix_error (code, f, arg) -> - F.eprintf - "WARNING: Failed to resolve file %s with \"%s\" @\n@." arg (Unix.error_message code); + 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)) diff --git a/infer/src/base/Utils.mli b/infer/src/base/Utils.mli index 51949e7f1..0a06e0995 100644 --- a/infer/src/base/Utils.mli +++ b/infer/src/base/Utils.mli @@ -71,9 +71,11 @@ val shell_escape_command : string list -> string (** create a directory if it does not exist already *) val create_dir : string -> unit -(** [realpath path] returns path with all symbolic links resolved. It caches results of previous - calls to avoid expensive system calls *) -val realpath : string -> string +(** [realpath warn_on_error path] returns path with all symbolic links resolved. + It caches results of previous calls to avoid expensive system calls. + 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 realpath : ?warn_on_error:bool -> string -> string (** wraps a function expecting 2 arguments in another that temporarily redirects stderr to /dev/null for the duration of the function call *)