From 7f992bbaa9d3fd34dd520dea274aed811bd9dfd9 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Fri, 6 Mar 2020 09:13:39 -0800 Subject: [PATCH] [java] move the class map of models into JModels Summary: The class map of biabduction models does not need to be part of the program type. This diff centralises all that in JModels module. Reviewed By: jvillard Differential Revision: D20283623 fbshipit-source-id: 5f95a7477 --- infer/src/base/Utils.ml | 10 ++------- infer/src/base/Utils.mli | 6 ++---- infer/src/java/jClasspath.ml | 40 ++++++++++------------------------- infer/src/java/jClasspath.mli | 2 -- infer/src/java/jModels.ml | 30 ++++++++++++++++++++------ infer/src/java/jModels.mli | 4 +++- infer/src/java/jTransType.ml | 6 +++--- 7 files changed, 44 insertions(+), 54 deletions(-) diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index 15bc57895..c12ed5425 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -525,14 +525,8 @@ let set_best_cpu_for worker_id = Setcore.setcore ((chosen_core * threads_per_core) + chosen_thread_in_core) -let zip_fold_filenames ~init ~f ~chop_extension ~zip_filename = +let zip_fold_filenames ~init ~f ~zip_filename = let file_in = Zip.open_in zip_filename in - let collect acc (entry : Zip.entry) = - match Filename.split_extension entry.filename with - | basename, Some extension when String.equal extension chop_extension -> - f acc basename - | _ -> - acc - in + let collect acc (entry : Zip.entry) = f acc entry.filename in let result = List.fold ~f:collect ~init (Zip.entries file_in) in Zip.close_in file_in ; result diff --git a/infer/src/base/Utils.mli b/infer/src/base/Utils.mli index c8afc9d36..e98f5736f 100644 --- a/infer/src/base/Utils.mli +++ b/infer/src/base/Utils.mli @@ -179,7 +179,5 @@ val numcores : int val set_best_cpu_for : int -> unit (** Pins processes to CPUs aiming to saturate physical cores evenly *) -val zip_fold_filenames : - init:'a -> f:('a -> string -> 'a) -> chop_extension:string -> zip_filename:string -> 'a -(** fold over each filename in the given [zip_filename] which has as extension [chop_extension]. [f] - receives the filename with the extension removed. *) +val zip_fold_filenames : init:'a -> f:('a -> string -> 'a) -> zip_filename:string -> 'a +(** fold over each filename in the given [zip_filename]. *) diff --git a/infer/src/java/jClasspath.ml b/infer/src/java/jClasspath.ml index 1409f3c65..6374250b1 100644 --- a/infer/src/java/jClasspath.ml +++ b/infer/src/java/jClasspath.ml @@ -140,20 +140,21 @@ let load_from_verbose_output javac_verbose_out = loop [] String.Set.empty String.Map.empty JBasics.ClassSet.empty -let fold_classnames ~init ~f jar_filename = - let f acc class_filename = - let classname = - JBasics.make_cn (String.map ~f:(function '/' -> '.' | c -> c) class_filename) - in - f acc classname +let collect_classnames init jar_filename = + let f acc filename_with_extension = + match Filename.split_extension filename_with_extension with + | class_filename, Some extension when String.equal extension "class" -> + let classname = + JBasics.make_cn (String.map ~f:(function '/' -> '.' | c -> c) class_filename) + in + JBasics.ClassSet.add classname acc + | _ -> + acc in - Utils.zip_fold_filenames ~init ~f ~chop_extension:"class" ~zip_filename:jar_filename + Utils.zip_fold_filenames ~init ~f ~zip_filename:jar_filename let search_classes path = - let collect_classnames start_classmap jar_filename = - fold_classnames ~f:(fun map cn -> JBasics.ClassSet.add cn map) ~init:start_classmap jar_filename - in let add_class roots classes class_filename = let cn, root_dir = Javalib.extract_class_name_from_file class_filename in (add_root_path root_dir roots, JBasics.ClassSet.add cn classes) @@ -198,7 +199,6 @@ type classmap = JCode.jcode Javalib.interface_or_class JBasics.ClassMap.t type program = { classpath_channel: Javalib.class_path - ; models: classmap ; mutable classmap: classmap ; callees: callee_status Procname.Hash.t } @@ -208,8 +208,6 @@ let mem_classmap cn program = JBasics.ClassMap.mem cn program.classmap let get_classpath_channel program = program.classpath_channel -let get_models program = program.models - let add_class cn jclass program = (* [prefix] must be a fresh class name *) let prefix = JBasics.cn_name cn ^ Config.java_lambda_marker_infix in @@ -253,26 +251,10 @@ let lookup_node cn program = None ) -let collect_classes start_classmap jar_filename = - let classpath = Javalib.class_path jar_filename in - let collect classmap cn = - try JBasics.ClassMap.add cn (javalib_get_class classpath cn) classmap - with JBasics.Class_structure_error _ -> classmap - in - let classmap = fold_classnames ~f:collect ~init:start_classmap jar_filename in - Javalib.close_class_path classpath ; - classmap - - let load_program ~classpath classes = L.(debug Capture Medium) "loading program ... %!" ; - let models = - JModels.get_models_jar_filename () - |> Option.fold ~init:JBasics.ClassMap.empty ~f:collect_classes - in let program = { classpath_channel= Javalib.class_path classpath - ; models ; classmap= JBasics.ClassMap.empty ; callees= Procname.Hash.create 128 } in diff --git a/infer/src/java/jClasspath.mli b/infer/src/java/jClasspath.mli index ef3a28531..90b811fb4 100644 --- a/infer/src/java/jClasspath.mli +++ b/infer/src/java/jClasspath.mli @@ -28,8 +28,6 @@ val get_classmap : program -> classmap val mem_classmap : JBasics.class_name -> program -> bool -val get_models : program -> classmap - val cleanup : program -> unit val load_program : classpath:string -> JBasics.ClassSet.t -> program diff --git a/infer/src/java/jModels.ml b/infer/src/java/jModels.ml index ebf78a65b..028426154 100644 --- a/infer/src/java/jModels.ml +++ b/infer/src/java/jModels.ml @@ -7,25 +7,39 @@ *) open! IStd +open Javalib_pack module L = Logging let models_jar = ref None -let get_models_jar_filename () = !models_jar - module StringHash = Caml.Hashtbl.Make (String) let models_specs_filenames = StringHash.create 1 +let models_classmap = ref JBasics.ClassMap.empty + let specs_file_extension = String.chop_prefix_exn ~prefix:"." Config.specs_files_suffix let collect_specs_filenames jar_filename = - let f () filename = - let proc_filename = Filename.basename filename in - StringHash.replace models_specs_filenames proc_filename () + (* version of Javalib.get_class that does not spam stderr *) + let javalib_get_class = Utils.suppress_stderr2 Javalib.get_class in + let classpath = Javalib.class_path jar_filename in + let f classmap filename_with_extension = + match Filename.split_extension filename_with_extension with + | filename, Some extension when String.equal extension specs_file_extension -> + let proc_filename = Filename.basename filename in + StringHash.replace models_specs_filenames proc_filename () ; + classmap + | filename, Some extension when String.equal extension "class" -> ( + let cn = JBasics.make_cn (String.map ~f:(function '/' -> '.' | c -> c) filename) in + try JBasics.ClassMap.add cn (javalib_get_class classpath cn) classmap + with JBasics.Class_structure_error _ -> classmap ) + | _ -> + classmap in - Utils.zip_fold_filenames ~init:() ~f ~chop_extension:specs_file_extension - ~zip_filename:jar_filename + models_classmap := + Utils.zip_fold_filenames ~init:JBasics.ClassMap.empty ~f ~zip_filename:jar_filename ; + Javalib.close_class_path classpath let set_models ~jar_filename = @@ -43,3 +57,5 @@ let set_models ~jar_filename = let is_model procname = StringHash.mem models_specs_filenames (Procname.to_filename procname) + +let get_classmap () = !models_classmap diff --git a/infer/src/java/jModels.mli b/infer/src/java/jModels.mli index c6e8126cb..49034873d 100644 --- a/infer/src/java/jModels.mli +++ b/infer/src/java/jModels.mli @@ -7,6 +7,7 @@ *) open! IStd +open Javalib_pack val set_models : jar_filename:string -> unit (** Sets the procnames in the given jar file as models *) @@ -14,4 +15,5 @@ val set_models : jar_filename:string -> unit val is_model : Procname.t -> bool (** Check if there is a model for the given procname *) -val get_models_jar_filename : unit -> string option +val get_classmap : unit -> JCode.jcode Javalib.interface_or_class JBasics.ClassMap.t +(** get map of model classes *) diff --git a/infer/src/java/jTransType.ml b/infer/src/java/jTransType.ml index 35a34aafe..f66bd8a29 100644 --- a/infer/src/java/jTransType.ml +++ b/infer/src/java/jTransType.ml @@ -260,7 +260,7 @@ let collect_models_class_fields classpath_field_map cn cf fields = else (static, (field_name, field_type, annotation) :: nonstatic) -let add_model_fields program classpath_fields cn = +let add_model_fields classpath_fields cn = let statics, nonstatics = classpath_fields in let classpath_field_map = let collect_fields map = @@ -269,7 +269,7 @@ let add_model_fields program classpath_fields cn = collect_fields (collect_fields Fieldname.Map.empty statics) nonstatics in try - match JBasics.ClassMap.find cn (JClasspath.get_models program) with + match JBasics.ClassMap.find cn (JModels.get_classmap ()) with | Javalib.JClass _ as jclass -> Javalib.cf_fold (collect_models_class_fields classpath_field_map cn) jclass classpath_fields | _ -> @@ -362,7 +362,7 @@ and get_class_struct_typ = | Javalib.JClass jclass -> let statics, nonstatics = let classpath_static, classpath_nonstatic = get_all_fields program tenv cn in - add_model_fields program (classpath_static, classpath_nonstatic) cn + add_model_fields (classpath_static, classpath_nonstatic) cn in let item_annotation = JAnnotation.translate_item jclass.Javalib.c_annotations in let interface_list = create_super_list jclass.Javalib.c_interfaces in