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
master
David Pichardie 5 years ago committed by Facebook Github Bot
parent 3554101ece
commit c1d4e57561

@ -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

@ -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

@ -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

@ -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

@ -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)

Loading…
Cancel
Save