From c1d4e57561838a793a1e04f2db8a29ed41e8a733 Mon Sep 17 00:00:00 2001 From: David Pichardie Date: Wed, 18 Dec 2019 21:08:06 -0800 Subject: [PATCH] Cleaning in the Java Frontend Summary: From a set of classes [classes], JClasspath.load_program builds a program. This program contains (implicitely) the set of active classes and we should not keep using the set [classes] and the generated program as the same time, because they may not be synched anymore. (In particular, in a forthcoming diff load_program may add new classes during program construction) This patch forces this discipline. It adds a [in_classes] query for programs and makes sure we use it everywhere we should. Reviewed By: ngorogiannis Differential Revision: D18833335 fbshipit-source-id: a522f320c --- infer/src/java/jClasspath.ml | 2 ++ infer/src/java/jClasspath.mli | 2 ++ infer/src/java/jFrontend.ml | 13 +++++++------ infer/src/java/jFrontend.mli | 7 +++---- infer/src/java/jMain.ml | 19 +++++++++---------- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/infer/src/java/jClasspath.ml b/infer/src/java/jClasspath.ml index 7598583a9..3029ce4b0 100644 --- a/infer/src/java/jClasspath.ml +++ b/infer/src/java/jClasspath.ml @@ -249,6 +249,8 @@ type program = let get_classmap program = program.classmap +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 diff --git a/infer/src/java/jClasspath.mli b/infer/src/java/jClasspath.mli index fa9011139..5198a5505 100644 --- a/infer/src/java/jClasspath.mli +++ b/infer/src/java/jClasspath.mli @@ -33,6 +33,8 @@ type program val get_classmap : program -> classmap +val mem_classmap : JBasics.class_name -> program -> bool + val get_models : program -> classmap val cleanup : program -> unit diff --git a/infer/src/java/jFrontend.ml b/infer/src/java/jFrontend.ml index a3311a69a..84c3af68b 100644 --- a/infer/src/java/jFrontend.ml +++ b/infer/src/java/jFrontend.ml @@ -162,8 +162,9 @@ let create_icfg source_file linereader program tenv icfg cn node = Javalib.m_iter translate node -(* returns true for the set of classes that are selected to be translated *) -let should_capture classes package_opt source_basename node = +(* returns true for the set of classes that are selected to be translated in the given + progam *) +let should_capture program package_opt source_basename node = let classname = Javalib.get_name node in let match_package pkg cn = match JTransType.package_to_string (JBasics.cn_package cn) with @@ -172,7 +173,7 @@ let should_capture classes package_opt source_basename node = | Some found_pkg -> String.equal found_pkg pkg in - if JBasics.ClassSet.mem classname classes then + if JClasspath.mem_classmap classname program then match Javalib.get_sourcefile node with | None -> false @@ -186,9 +187,9 @@ let should_capture classes package_opt source_basename node = (* Computes the control - flow graph and call - graph of a given source file. - In the standard - mode, it translated all the classes that correspond to this + In the standard - mode, it translated all the classes of [program] that correspond to this source file. *) -let compute_source_icfg linereader classes program tenv source_basename package_opt source_file = +let compute_source_icfg linereader program tenv source_basename package_opt source_file = let icfg = {JContext.cfg= Cfg.create (); tenv} in let select test procedure cn node = if test node then try procedure cn node with Bir.Subroutine -> () @@ -196,7 +197,7 @@ let compute_source_icfg linereader classes program tenv source_basename package_ let () = JBasics.ClassMap.iter (select - (should_capture classes package_opt source_basename) + (should_capture program package_opt source_basename) (create_icfg source_file linereader program tenv icfg)) (JClasspath.get_classmap program) in diff --git a/infer/src/java/jFrontend.mli b/infer/src/java/jFrontend.mli index ff374c452..77636d3bb 100644 --- a/infer/src/java/jFrontend.mli +++ b/infer/src/java/jFrontend.mli @@ -20,16 +20,15 @@ val is_classname_cached : JBasics.class_name -> bool val compute_source_icfg : Printer.LineReader.t - -> JBasics.ClassSet.t -> JClasspath.program -> Tenv.t -> string -> string option -> SourceFile.t -> Cfg.t -(** [compute_cfg linereader classes program tenv source_basename source_file] create the control - flow graph for the file [source_file] by translating all the classes in [program] originating - from [source_file] *) +(** [compute_cfg linereader program tenv source_basename source_file] create the control flow graph + for the file [source_file] by translating all the classes in [program] originating from + [source_file] *) val compute_class_icfg : SourceFile.t diff --git a/infer/src/java/jMain.ml b/infer/src/java/jMain.ml index 641d9915b..c01eb1c86 100644 --- a/infer/src/java/jMain.ml +++ b/infer/src/java/jMain.ml @@ -27,12 +27,11 @@ let store_icfg source_file cfg = (* Given a source file, its code is translated, and the call-graph, control-flow-graph and type *) (* environment are obtained and saved. *) -let do_source_file linereader classes program tenv source_basename package_opt source_file = +let do_source_file linereader program tenv source_basename package_opt source_file = L.(debug Capture Medium) "@\nfilename: %a (%s)@." SourceFile.pp source_file source_basename ; init_global_state source_file ; let cfg = - JFrontend.compute_source_icfg linereader classes program tenv source_basename package_opt - source_file + JFrontend.compute_source_icfg linereader program tenv source_basename package_opt source_file in store_icfg source_file cfg @@ -81,11 +80,7 @@ let store_callee_attributes tenv program = (* The program is loaded and translated *) -let do_all_files classpath sources classes = - L.(debug Capture Quiet) - "Translating %d source files (%d classes)@." (String.Map.length sources) - (JBasics.ClassSet.cardinal classes) ; - let program = JClasspath.load_program classpath classes in +let do_all_files sources program = let tenv = load_tenv () in let linereader = Printer.LineReader.create () in let skip source_file = @@ -99,7 +94,7 @@ let do_all_files classpath sources classes = in let translate_source_file basename (package_opt, _) source_file = if not (skip source_file) then - do_source_file linereader classes program tenv basename package_opt source_file + do_source_file linereader program tenv basename package_opt source_file in String.Map.iteri ~f:(fun ~key:basename ~data:file_entry -> @@ -139,7 +134,11 @@ let main load_sources_and_classes = JClasspath.load_from_arguments path in if String.Map.is_empty sources then L.(die InternalError) "Failed to load any Java source code" ; - do_all_files classpath sources classes + L.(debug Capture Quiet) + "Translating %d source files (%d classes)@." (String.Map.length sources) + (JBasics.ClassSet.cardinal classes) ; + let program = JClasspath.load_program classpath classes in + do_all_files sources program let from_arguments path = main (`FromArguments path)