diff --git a/infer/src/base/SourceFile.ml b/infer/src/base/SourceFile.ml index 6615dcf99..b05c6fdf3 100644 --- a/infer/src/base/SourceFile.ml +++ b/infer/src/base/SourceFile.ml @@ -33,13 +33,6 @@ module Map = Caml.Map.Make (OrderedSourceFile) module Set = Caml.Set.Make (OrderedSourceFile) -let rel_path_from_abs_path root fname = - let relative_complemented_fname = Utils.filename_to_relative root fname in - if String.is_prefix ~prefix:root fname && - Filename.is_relative relative_complemented_fname then - Some relative_complemented_fname - else None (* The project root is not a prefix of the file name *) - let from_abs_path fname = if Filename.is_relative fname then (failwithf @@ -49,10 +42,10 @@ let from_abs_path fname = let fname_real = try Utils.realpath fname with Unix.Unix_error _ -> fname in let project_root_real = Utils.realpath Config.project_root in let models_dir_real = Config.models_src_dir in - match rel_path_from_abs_path project_root_real fname_real with + match Utils.filename_to_relative ~root:project_root_real fname_real with | Some path -> RelativeProjectRoot path | None -> ( - match rel_path_from_abs_path models_dir_real fname_real with + match Utils.filename_to_relative ~root:models_dir_real fname_real with | Some path -> RelativeInferModel path | None -> Absolute fname (* fname is absolute already *) ) diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index 2e1e62d09..9d626692e 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -120,13 +120,13 @@ let filename_to_absolute ~root fname = (** Convert an absolute filename to one relative to the given directory. *) -let filename_to_relative root fname = +let filename_to_relative ~root fname = let rec relativize_if_under origin target = match origin, target with | x :: xs, y :: ys when x = y -> relativize_if_under xs ys - | [], [] -> "." - | [], ys -> Filename.of_parts ys - | _ -> fname + | [], [] -> Some "." + | [], ys -> Some (Filename.of_parts ys) + | _ -> None in relativize_if_under (Filename.parts root) (Filename.parts fname) diff --git a/infer/src/base/Utils.mli b/infer/src/base/Utils.mli index 90c93d002..884cf7730 100644 --- a/infer/src/base/Utils.mli +++ b/infer/src/base/Utils.mli @@ -32,8 +32,9 @@ val read_file : string -> string list option (** Convert a filename to an absolute one if it is relative, and normalize "." and ".." *) val filename_to_absolute : root:string -> string -> string -(** Convert an absolute filename to one relative to a root directory *) -val filename_to_relative : string -> string -> string +(** Convert an absolute filename to one relative to a root directory. Returns [None] if filename is + not under root. *) +val filename_to_relative : root:string -> string -> string option (** type for files used for printing *) type outfile =