[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
master
Nikos Gorogiannis 5 years ago committed by Facebook Github Bot
parent f888160142
commit 7f992bbaa9

@ -525,14 +525,8 @@ let set_best_cpu_for worker_id =
Setcore.setcore ((chosen_core * threads_per_core) + chosen_thread_in_core) 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 file_in = Zip.open_in zip_filename in
let collect acc (entry : Zip.entry) = let collect acc (entry : Zip.entry) = f acc entry.filename in
match Filename.split_extension entry.filename with
| basename, Some extension when String.equal extension chop_extension ->
f acc basename
| _ ->
acc
in
let result = List.fold ~f:collect ~init (Zip.entries file_in) in let result = List.fold ~f:collect ~init (Zip.entries file_in) in
Zip.close_in file_in ; result Zip.close_in file_in ; result

@ -179,7 +179,5 @@ val numcores : int
val set_best_cpu_for : int -> unit val set_best_cpu_for : int -> unit
(** Pins processes to CPUs aiming to saturate physical cores evenly *) (** Pins processes to CPUs aiming to saturate physical cores evenly *)
val zip_fold_filenames : val zip_fold_filenames : init:'a -> f:('a -> string -> 'a) -> zip_filename:string -> 'a
init:'a -> f:('a -> string -> 'a) -> chop_extension:string -> zip_filename:string -> 'a (** fold over each filename in the given [zip_filename]. *)
(** fold over each filename in the given [zip_filename] which has as extension [chop_extension]. [f]
receives the filename with the extension removed. *)

@ -140,20 +140,21 @@ let load_from_verbose_output javac_verbose_out =
loop [] String.Set.empty String.Map.empty JBasics.ClassSet.empty loop [] String.Set.empty String.Map.empty JBasics.ClassSet.empty
let fold_classnames ~init ~f jar_filename = let collect_classnames init jar_filename =
let f acc class_filename = let f acc filename_with_extension =
let classname = match Filename.split_extension filename_with_extension with
JBasics.make_cn (String.map ~f:(function '/' -> '.' | c -> c) class_filename) | class_filename, Some extension when String.equal extension "class" ->
in let classname =
f acc classname JBasics.make_cn (String.map ~f:(function '/' -> '.' | c -> c) class_filename)
in
JBasics.ClassSet.add classname acc
| _ ->
acc
in 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 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 add_class roots classes class_filename =
let cn, root_dir = Javalib.extract_class_name_from_file class_filename in let cn, root_dir = Javalib.extract_class_name_from_file class_filename in
(add_root_path root_dir roots, JBasics.ClassSet.add cn classes) (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 = type program =
{ classpath_channel: Javalib.class_path { classpath_channel: Javalib.class_path
; models: classmap
; mutable classmap: classmap ; mutable classmap: classmap
; callees: callee_status Procname.Hash.t } ; 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_classpath_channel program = program.classpath_channel
let get_models program = program.models
let add_class cn jclass program = let add_class cn jclass program =
(* [prefix] must be a fresh class name *) (* [prefix] must be a fresh class name *)
let prefix = JBasics.cn_name cn ^ Config.java_lambda_marker_infix in let prefix = JBasics.cn_name cn ^ Config.java_lambda_marker_infix in
@ -253,26 +251,10 @@ let lookup_node cn program =
None ) 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 = let load_program ~classpath classes =
L.(debug Capture Medium) "loading program ... %!" ; 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 = let program =
{ classpath_channel= Javalib.class_path classpath { classpath_channel= Javalib.class_path classpath
; models
; classmap= JBasics.ClassMap.empty ; classmap= JBasics.ClassMap.empty
; callees= Procname.Hash.create 128 } ; callees= Procname.Hash.create 128 }
in in

@ -28,8 +28,6 @@ val get_classmap : program -> classmap
val mem_classmap : JBasics.class_name -> program -> bool val mem_classmap : JBasics.class_name -> program -> bool
val get_models : program -> classmap
val cleanup : program -> unit val cleanup : program -> unit
val load_program : classpath:string -> JBasics.ClassSet.t -> program val load_program : classpath:string -> JBasics.ClassSet.t -> program

@ -7,25 +7,39 @@
*) *)
open! IStd open! IStd
open Javalib_pack
module L = Logging module L = Logging
let models_jar = ref None let models_jar = ref None
let get_models_jar_filename () = !models_jar
module StringHash = Caml.Hashtbl.Make (String) module StringHash = Caml.Hashtbl.Make (String)
let models_specs_filenames = StringHash.create 1 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 specs_file_extension = String.chop_prefix_exn ~prefix:"." Config.specs_files_suffix
let collect_specs_filenames jar_filename = let collect_specs_filenames jar_filename =
let f () filename = (* version of Javalib.get_class that does not spam stderr *)
let proc_filename = Filename.basename filename in let javalib_get_class = Utils.suppress_stderr2 Javalib.get_class in
StringHash.replace models_specs_filenames proc_filename () 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 in
Utils.zip_fold_filenames ~init:() ~f ~chop_extension:specs_file_extension models_classmap :=
~zip_filename:jar_filename Utils.zip_fold_filenames ~init:JBasics.ClassMap.empty ~f ~zip_filename:jar_filename ;
Javalib.close_class_path classpath
let set_models ~jar_filename = 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 is_model procname = StringHash.mem models_specs_filenames (Procname.to_filename procname)
let get_classmap () = !models_classmap

@ -7,6 +7,7 @@
*) *)
open! IStd open! IStd
open Javalib_pack
val set_models : jar_filename:string -> unit val set_models : jar_filename:string -> unit
(** Sets the procnames in the given jar file as models *) (** 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 val is_model : Procname.t -> bool
(** Check if there is a model for the given procname *) (** 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 *)

@ -260,7 +260,7 @@ let collect_models_class_fields classpath_field_map cn cf fields =
else (static, (field_name, field_type, annotation) :: nonstatic) 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 statics, nonstatics = classpath_fields in
let classpath_field_map = let classpath_field_map =
let collect_fields 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 collect_fields (collect_fields Fieldname.Map.empty statics) nonstatics
in in
try 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.JClass _ as jclass ->
Javalib.cf_fold (collect_models_class_fields classpath_field_map cn) jclass classpath_fields 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 -> | Javalib.JClass jclass ->
let statics, nonstatics = let statics, nonstatics =
let classpath_static, classpath_nonstatic = get_all_fields program tenv cn in 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 in
let item_annotation = JAnnotation.translate_item jclass.Javalib.c_annotations in let item_annotation = JAnnotation.translate_item jclass.Javalib.c_annotations in
let interface_list = create_super_list jclass.Javalib.c_interfaces in let interface_list = create_super_list jclass.Javalib.c_interfaces in

Loading…
Cancel
Save