[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
master
Jeremy Dubreil 7 years ago committed by Facebook Github Bot
parent 4b296003fa
commit cc5708bb4e

@ -1802,6 +1802,13 @@ and reactive_capture =
"Compile source files only when required by analyzer (clang only)" "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 = and report =
CLOpt.mk_bool ~long:"report" ~default:true CLOpt.mk_bool ~long:"report" ~default:true
~in_help:InferCommand.[(Analyze, manual_generic); (Run, manual_generic)] ~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 reactive_capture = !reactive_capture
and relative_path_backtack = !relative_path_backtack
and report = !report and report = !report
and report_current = !report_current and report_current = !report_current

@ -189,6 +189,8 @@ val property_attributes : string
val racerd_issues_dir_name : string val racerd_issues_dir_name : string
val relative_path_backtack : int
val report : bool val report : bool
val report_condition_always_true_in_clang : bool val report_condition_always_true_in_clang : bool

@ -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 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 project_root_real = Utils.realpath ~warn_on_error Config.project_root in
let models_dir_real = Config.models_src_dir 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 -> | Some path ->
RelativeProjectRoot path RelativeProjectRoot path
| None when Config.buck_cache_mode && Filename.check_suffix fname_real "java" -> | None when Config.buck_cache_mode && Filename.check_suffix fname_real "java" ->

@ -97,19 +97,21 @@ let filename_to_absolute ~root fname =
(** Convert an absolute filename to one relative to the given directory. *) (** Convert an absolute filename to one relative to the given directory. *)
let filename_to_relative ~root fname = let filename_to_relative ?(backtrack= 0) ~root fname =
let rec relativize_if_under origin target = let rec relativize_if_under prefix backtrack origin target =
match (origin, target) with match (origin, target) with
| x :: xs, y :: ys when String.equal x y -> | 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 "." Some "."
| [], ys -> | [], ys ->
Some (Filename.of_parts ys) Some (Filename.of_parts (prefix @ ys))
| _ -> | _ ->
None None
in 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 = let directory_fold f init path =

@ -25,9 +25,10 @@ val read_file : string -> (string list, string) Result.t
val filename_to_absolute : root:string -> string -> string val filename_to_absolute : root:string -> string -> string
(** Convert a filename to an absolute one if it is relative, and normalize "." and ".." *) (** 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 (** 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 for files used for printing *)
type outfile = type outfile =

Loading…
Cancel
Save