diff --git a/infer/src/base/DB.ml b/infer/src/base/DB.ml index 813bcb0e9..01886a19e 100644 --- a/infer/src/base/DB.ml +++ b/infer/src/base/DB.ml @@ -131,6 +131,30 @@ let source_file_is_cpp_model file = string_is_prefix Config.relative_cpp_models_dir path | _ -> false +let source_file_exists_cache = Hashtbl.create 256 + +let source_file_path_exists abs_path = + try Hashtbl.find source_file_exists_cache abs_path + with Not_found -> + let result = Sys.file_exists abs_path in + Hashtbl.add source_file_exists_cache abs_path result; + result + + +let source_file_of_header header_file = + let abs_path = source_file_to_abs_path header_file in + let source_file_exts = ["c"; "cc"; "cpp"; "cxx"; "m"; "mm"] in + let header_file_exts = ["h"; "hh"; "hpp"; "hxx"] in + let file_no_ext, ext_opt = Core.Std.Filename.split_extension abs_path in + let file_opt = match ext_opt with + | Some ext when IList.mem string_equal ext header_file_exts -> ( + let possible_files = IList.map (fun ext -> file_no_ext ^ "." ^ ext) source_file_exts in + try Some (IList.find source_file_path_exists possible_files) + with Not_found -> None + ) + | _ -> None in + Option.map source_file_from_abs_path file_opt + (** {2 Source Dirs} *) (** source directory: the directory inside the results dir corresponding to a source file *) diff --git a/infer/src/base/DB.mli b/infer/src/base/DB.mli index 6553d0023..6801dae44 100644 --- a/infer/src/base/DB.mli +++ b/infer/src/base/DB.mli @@ -120,6 +120,10 @@ val source_file_is_infer_model : source_file -> bool (** Returns true if the file is a C++ model *) val source_file_is_cpp_model : source_file -> bool +(** Return approximate source file corresponding to the parameter if it's header file and + file exists. returns None otherwise *) +val source_file_of_header : source_file -> source_file option + (** {2 Source Dirs} *) (** source directory: the directory inside the results dir corresponding to a source file *) diff --git a/infer/src/integration/CaptureCompilationDatabase.ml b/infer/src/integration/CaptureCompilationDatabase.ml index 15911f8c6..858719288 100644 --- a/infer/src/integration/CaptureCompilationDatabase.ml +++ b/infer/src/integration/CaptureCompilationDatabase.ml @@ -17,18 +17,7 @@ let capture_text = else "translating" let replace_header_file_with_source_file file_path = - let file_path = DB.source_file_to_abs_path file_path in - let possible_file_replacements file_path = - IList.map (fun suffix -> (Filename.chop_extension file_path) ^ suffix ) [".m"; ".mm"] in - let file = - if Filename.check_suffix file_path ".h" || Filename.check_suffix file_path ".hh" then - try - IList.find Sys.file_exists (possible_file_replacements file_path) - with Not_found -> - Logging.out "Couldn't find any replacement source file for file %s " file_path; - file_path - else file_path in - DB.source_file_from_abs_path file + Option.default file_path (DB.source_file_of_header file_path) (** Read the files to compile from the changed files index. *) let should_capture_file_from_index () =