Skip to content

Commit c1d4e57

Browse files
davidpichardiefacebook-github-bot
authored andcommitted
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
1 parent 3554101 commit c1d4e57

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

infer/src/java/jClasspath.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ type program =
249249

250250
let get_classmap program = program.classmap
251251

252+
let mem_classmap cn program = JBasics.ClassMap.mem cn program.classmap
253+
252254
let get_classpath_channel program = program.classpath.channel
253255

254256
let get_models program = program.models

infer/src/java/jClasspath.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type program
3333

3434
val get_classmap : program -> classmap
3535

36+
val mem_classmap : JBasics.class_name -> program -> bool
37+
3638
val get_models : program -> classmap
3739

3840
val cleanup : program -> unit

infer/src/java/jFrontend.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ let create_icfg source_file linereader program tenv icfg cn node =
162162
Javalib.m_iter translate node
163163

164164

165-
(* returns true for the set of classes that are selected to be translated *)
166-
let should_capture classes package_opt source_basename node =
165+
(* returns true for the set of classes that are selected to be translated in the given
166+
progam *)
167+
let should_capture program package_opt source_basename node =
167168
let classname = Javalib.get_name node in
168169
let match_package pkg cn =
169170
match JTransType.package_to_string (JBasics.cn_package cn) with
@@ -172,7 +173,7 @@ let should_capture classes package_opt source_basename node =
172173
| Some found_pkg ->
173174
String.equal found_pkg pkg
174175
in
175-
if JBasics.ClassSet.mem classname classes then
176+
if JClasspath.mem_classmap classname program then
176177
match Javalib.get_sourcefile node with
177178
| None ->
178179
false
@@ -186,17 +187,17 @@ let should_capture classes package_opt source_basename node =
186187

187188

188189
(* Computes the control - flow graph and call - graph of a given source file.
189-
In the standard - mode, it translated all the classes that correspond to this
190+
In the standard - mode, it translated all the classes of [program] that correspond to this
190191
source file. *)
191-
let compute_source_icfg linereader classes program tenv source_basename package_opt source_file =
192+
let compute_source_icfg linereader program tenv source_basename package_opt source_file =
192193
let icfg = {JContext.cfg= Cfg.create (); tenv} in
193194
let select test procedure cn node =
194195
if test node then try procedure cn node with Bir.Subroutine -> ()
195196
in
196197
let () =
197198
JBasics.ClassMap.iter
198199
(select
199-
(should_capture classes package_opt source_basename)
200+
(should_capture program package_opt source_basename)
200201
(create_icfg source_file linereader program tenv icfg))
201202
(JClasspath.get_classmap program)
202203
in

infer/src/java/jFrontend.mli

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ val is_classname_cached : JBasics.class_name -> bool
2020

2121
val compute_source_icfg :
2222
Printer.LineReader.t
23-
-> JBasics.ClassSet.t
2423
-> JClasspath.program
2524
-> Tenv.t
2625
-> string
2726
-> string option
2827
-> SourceFile.t
2928
-> Cfg.t
30-
(** [compute_cfg linereader classes program tenv source_basename source_file] create the control
31-
flow graph for the file [source_file] by translating all the classes in [program] originating
32-
from [source_file] *)
29+
(** [compute_cfg linereader program tenv source_basename source_file] create the control flow graph
30+
for the file [source_file] by translating all the classes in [program] originating from
31+
[source_file] *)
3332

3433
val compute_class_icfg :
3534
SourceFile.t

infer/src/java/jMain.ml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ let store_icfg source_file cfg =
2727

2828
(* Given a source file, its code is translated, and the call-graph, control-flow-graph and type *)
2929
(* environment are obtained and saved. *)
30-
let do_source_file linereader classes program tenv source_basename package_opt source_file =
30+
let do_source_file linereader program tenv source_basename package_opt source_file =
3131
L.(debug Capture Medium) "@\nfilename: %a (%s)@." SourceFile.pp source_file source_basename ;
3232
init_global_state source_file ;
3333
let cfg =
34-
JFrontend.compute_source_icfg linereader classes program tenv source_basename package_opt
35-
source_file
34+
JFrontend.compute_source_icfg linereader program tenv source_basename package_opt source_file
3635
in
3736
store_icfg source_file cfg
3837

@@ -81,11 +80,7 @@ let store_callee_attributes tenv program =
8180

8281

8382
(* The program is loaded and translated *)
84-
let do_all_files classpath sources classes =
85-
L.(debug Capture Quiet)
86-
"Translating %d source files (%d classes)@." (String.Map.length sources)
87-
(JBasics.ClassSet.cardinal classes) ;
88-
let program = JClasspath.load_program classpath classes in
83+
let do_all_files sources program =
8984
let tenv = load_tenv () in
9085
let linereader = Printer.LineReader.create () in
9186
let skip source_file =
@@ -99,7 +94,7 @@ let do_all_files classpath sources classes =
9994
in
10095
let translate_source_file basename (package_opt, _) source_file =
10196
if not (skip source_file) then
102-
do_source_file linereader classes program tenv basename package_opt source_file
97+
do_source_file linereader program tenv basename package_opt source_file
10398
in
10499
String.Map.iteri
105100
~f:(fun ~key:basename ~data:file_entry ->
@@ -139,7 +134,11 @@ let main load_sources_and_classes =
139134
JClasspath.load_from_arguments path
140135
in
141136
if String.Map.is_empty sources then L.(die InternalError) "Failed to load any Java source code" ;
142-
do_all_files classpath sources classes
137+
L.(debug Capture Quiet)
138+
"Translating %d source files (%d classes)@." (String.Map.length sources)
139+
(JBasics.ClassSet.cardinal classes) ;
140+
let program = JClasspath.load_program classpath classes in
141+
do_all_files sources program
143142

144143

145144
let from_arguments path = main (`FromArguments path)

0 commit comments

Comments
 (0)