From bc615438752eee47b3ed69006055de7c65320250 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Thu, 6 Jun 2019 12:52:30 -0700 Subject: [PATCH] [buckjava2] refactor Reviewed By: jberdine Differential Revision: D15516135 fbshipit-source-id: e8067cf66 --- infer/src/IR/SourceFiles.ml | 2 +- infer/src/IR/Tenv.ml | 12 +++++++----- infer/src/IR/Tenv.mli | 10 ++++++++-- infer/src/base/MergeResults.ml | 10 +++++++--- infer/src/base/MergeResults.mli | 2 ++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/infer/src/IR/SourceFiles.ml b/infer/src/IR/SourceFiles.ml index 8890fbb73..689e9e8fa 100644 --- a/infer/src/IR/SourceFiles.ml +++ b/infer/src/IR/SourceFiles.ml @@ -56,7 +56,7 @@ let add source_file cfg tenv integer_type_widths = if not (Caml.Hashtbl.mem existing_proc_names proc_name) then proc_name :: proc_names else proc_names ) in - (Tenv.merge ~dst:old_tenv ~src:tenv, proc_names) + (Tenv.merge_per_file ~dst:old_tenv ~src:tenv, proc_names) | None -> (tenv, new_proc_names) in diff --git a/infer/src/IR/Tenv.ml b/infer/src/IR/Tenv.ml index 44638b105..0d5ac6717 100644 --- a/infer/src/IR/Tenv.ml +++ b/infer/src/IR/Tenv.ml @@ -100,13 +100,14 @@ module SQLite : SqliteUtils.Data with type t = per_file = struct FileLocal (Marshal.from_string b 0) end -let merge ~src ~dst = +let merge ~src ~dst = TypenameHash.iter (fun pname cfg -> TypenameHash.replace dst pname cfg) src + +let merge_per_file ~src ~dst = match (src, dst) with | Global, Global -> Global | FileLocal src_tenv, FileLocal dst_tenv -> - TypenameHash.iter (fun pname cfg -> TypenameHash.replace dst_tenv pname cfg) src_tenv ; - FileLocal dst_tenv + merge ~src:src_tenv ~dst:dst_tenv ; FileLocal dst_tenv | Global, FileLocal _ | FileLocal _, Global -> L.die InternalError "Cannot merge Global tenv with FileLocal tenv" @@ -125,9 +126,10 @@ let global_tenv : t option ref = ref None let global_tenv_path = Config.(results_dir ^/ global_tenv_filename) |> DB.filename_from_string +let read path = Serialization.read_from_file tenv_serializer path + let load_global () : t option = - if is_none !global_tenv then - global_tenv := Serialization.read_from_file tenv_serializer global_tenv_path ; + if is_none !global_tenv then global_tenv := read global_tenv_path ; !global_tenv diff --git a/infer/src/IR/Tenv.mli b/infer/src/IR/Tenv.mli index c8689a8f0..deb3b8ed4 100644 --- a/infer/src/IR/Tenv.mli +++ b/infer/src/IR/Tenv.mli @@ -20,6 +20,9 @@ val load : SourceFile.t -> t option val store_debug_file_for_source : SourceFile.t -> t -> unit +val read : DB.filename -> t option +(** read and return a type environment from the given file *) + val load_global : unit -> t option (** load the global type environment (Java) *) @@ -54,8 +57,11 @@ val pp_per_file : Format.formatter -> per_file -> unit [@@warning "-32"] (** print per file type environment *) -val merge : src:per_file -> dst:per_file -> per_file +val merge : src:t -> dst:t -> unit +(** Merge [src] into [dst] possibly overwriting pre existing procs in [dst]. *) + +val merge_per_file : src:per_file -> dst:per_file -> per_file (** Best-effort merge of [src] into [dst]. If a procedure is both in [dst] and [src], the one in - [src] will get overwritten. *) + [dst] will get overwritten. *) module SQLite : SqliteUtils.Data with type t = per_file diff --git a/infer/src/base/MergeResults.ml b/infer/src/base/MergeResults.ml index 17977d0c7..4057d59ef 100644 --- a/infer/src/base/MergeResults.ml +++ b/infer/src/base/MergeResults.ml @@ -42,7 +42,8 @@ let merge_source_files_table ~db_file = ~log:(Printf.sprintf "copying source_files of database '%s'" db_file) -let merge ~db_file = +let merge infer_out_src = + let db_file = infer_out_src ^/ ResultsDatabase.database_filename in let main_db = ResultsDatabase.get_database () in Sqlite3.exec main_db (Printf.sprintf "ATTACH '%s' AS attached" db_file) |> SqliteUtils.check_result_code main_db ~log:(Printf.sprintf "attaching database '%s'" db_file) ; @@ -53,7 +54,7 @@ let merge ~db_file = () -let merge_buck_flavors_results infer_deps_file = +let iter_infer_deps infer_deps_file ~f = let one_line line = match String.split ~on:'\t' line with | [_; _; target_results_dir] -> @@ -62,7 +63,7 @@ let merge_buck_flavors_results infer_deps_file = Filename.dirname (Config.project_root ^/ "buck-out") ^/ target_results_dir else target_results_dir in - merge ~db_file:(infer_out_src ^/ ResultsDatabase.database_filename) + f infer_out_src | _ -> assert false in @@ -71,3 +72,6 @@ let merge_buck_flavors_results infer_deps_file = List.iter ~f:one_line lines | Error error -> L.internal_error "Couldn't read deps file '%s': %s" infer_deps_file error + + +let merge_buck_flavors_results infer_deps_file = iter_infer_deps infer_deps_file ~f:merge diff --git a/infer/src/base/MergeResults.mli b/infer/src/base/MergeResults.mli index eca4eaa7a..97c260cd5 100644 --- a/infer/src/base/MergeResults.mli +++ b/infer/src/base/MergeResults.mli @@ -10,3 +10,5 @@ open! IStd val merge_buck_flavors_results : string -> unit (** Merge the results from sub-invocations of infer inside buck-out/. Takes as argument the infer_deps file. *) + +val iter_infer_deps : string -> f:(string -> unit) -> unit