From cc5708bb4e6d6fb6883e2dd0a03eda2041df6fb4 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Tue, 22 May 2018 16:11:41 -0700 Subject: [PATCH] [infer] convert absolute paths to relative with respect to the common prefix betwen the path and the project root Reviewed By: mbouaziz Differential Revision: D8088106 fbshipit-source-id: 3711cf6 --- infer/src/base/Config.ml | 9 +++++++++ infer/src/base/Config.mli | 2 ++ infer/src/base/SourceFile.ml | 5 ++++- infer/src/base/Utils.ml | 12 +++++++----- infer/src/base/Utils.mli | 5 +++-- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 400a9c93e..1ac1c6c03 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -1802,6 +1802,13 @@ and reactive_capture = "Compile source files only when required by analyzer (clang only)" +and relative_path_backtack = + CLOpt.mk_int ~long:"backtrack-level" ~default:0 ~meta:"int" + "Maximum level of backtracking to convert an absolute path to path relative to the common \ + prefix between the project root and the path. For instance, with bactraking level 1, it \ + will convert /my/source/File.java with project root /my/root into ../source/File.java" + + and report = CLOpt.mk_bool ~long:"report" ~default:true ~in_help:InferCommand.[(Analyze, manual_generic); (Run, manual_generic)] @@ -2757,6 +2764,8 @@ and reactive_mode = !reactive || InferCommand.(equal Diff) command and reactive_capture = !reactive_capture +and relative_path_backtack = !relative_path_backtack + and report = !report and report_current = !report_current diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index a63c09038..65ff3b760 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -189,6 +189,8 @@ val property_attributes : string val racerd_issues_dir_name : string +val relative_path_backtack : int + val report : bool val report_condition_always_true_in_clang : bool diff --git a/infer/src/base/SourceFile.ml b/infer/src/base/SourceFile.ml index b39bc5cf9..3a365e44b 100644 --- a/infer/src/base/SourceFile.ml +++ b/infer/src/base/SourceFile.ml @@ -52,7 +52,10 @@ let from_abs_path ?(warn_on_error= true) fname = 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 + match + Utils.filename_to_relative ~backtrack:Config.relative_path_backtack ~root:project_root_real + fname_real + with | Some path -> RelativeProjectRoot path | None when Config.buck_cache_mode && Filename.check_suffix fname_real "java" -> diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index 92507404a..804ab7b2d 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -97,19 +97,21 @@ let filename_to_absolute ~root fname = (** Convert an absolute filename to one relative to the given directory. *) -let filename_to_relative ~root fname = - let rec relativize_if_under origin target = +let filename_to_relative ?(backtrack= 0) ~root fname = + let rec relativize_if_under prefix backtrack origin target = match (origin, target) with | x :: xs, y :: ys when String.equal x y -> - relativize_if_under xs ys + relativize_if_under prefix backtrack xs ys + | _ :: xs, y :: ys when backtrack > 0 -> + relativize_if_under (Filename.parent_dir_name :: y :: prefix) (backtrack - 1) xs ys | [], [] -> Some "." | [], ys -> - Some (Filename.of_parts ys) + Some (Filename.of_parts (prefix @ ys)) | _ -> None in - relativize_if_under (Filename.parts root) (Filename.parts fname) + relativize_if_under [] backtrack (Filename.parts root) (Filename.parts fname) let directory_fold f init path = diff --git a/infer/src/base/Utils.mli b/infer/src/base/Utils.mli index f66260dbc..b03d1a6a0 100644 --- a/infer/src/base/Utils.mli +++ b/infer/src/base/Utils.mli @@ -25,9 +25,10 @@ val read_file : string -> (string list, string) Result.t val filename_to_absolute : root:string -> string -> string (** Convert a filename to an absolute one if it is relative, and normalize "." and ".." *) -val filename_to_relative : root:string -> string -> string option +val filename_to_relative : ?backtrack:int -> root:string -> string -> string option (** Convert an absolute filename to one relative to a root directory. Returns [None] if filename is - not under root. *) + not under root. The backtrack level sets the maximum level of steps in the parent directories + to search for a common prefix *) (** type for files used for printing *) type outfile =