Skip to content

Commit 88f007d

Browse files
stroxlerfacebook-github-bot
authored andcommitted
Rewire interprocedural to be one-Analysis [2] simplify accumulations
Summary: In the previous diff, to keep the review easy I made minimal changes to functions' top-level signatures, but preserved the inner logic which left a bunch of functions folding over just one abstract kind. In this diff I clean that up - the resulting functions are identical but shorter and easier to read. I'm not yet changing `model_t` and `result_t` to contain just a single analysis because that will be a big change, and I'd prefer to have things as clean as possible beforehand to minimize confusion. I'll stack that change on top which will further simplify the code. Reviewed By: dkgi Differential Revision: D28886237 fbshipit-source-id: 722833a8d7d0e7f8e1384dcc41eb873a0d764930
1 parent 60ba5fa commit 88f007d

File tree

1 file changed

+31
-54
lines changed

1 file changed

+31
-54
lines changed

source/interprocedural/interproceduralAnalysis.ml

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,21 @@ type initialize_result = {
2424
}
2525

2626
let initialize_models kind ~scheduler ~static_analysis_configuration ~environment ~functions ~stubs =
27-
let initialize_each
28-
{ initial_models = models; skip_overrides }
29-
(Result.Analysis { kind; analysis })
30-
=
31-
let module Analysis = (val analysis) in
32-
let { Result.initial_models = new_models; skip_overrides = new_skip_overrides } =
33-
Analysis.initialize_models
34-
~static_analysis_configuration
35-
~scheduler
36-
~environment
37-
~functions
38-
~stubs
39-
in
40-
let add_analysis_model existing model =
41-
let open Result in
42-
let package = Pkg { kind = ModelPart kind; value = model } in
43-
{ existing with models = Kind.Map.add (Kind.abstract kind) package existing.models }
44-
in
45-
let merge ~key:_ = function
46-
| `Both (existing, new_model) -> Some (add_analysis_model existing new_model)
47-
| `Left existing -> Some existing
48-
| `Right new_model -> Some (add_analysis_model Result.empty_model new_model)
49-
in
50-
{
51-
initial_models = Callable.Map.merge models new_models ~f:merge;
52-
skip_overrides = Reference.Set.union skip_overrides new_skip_overrides;
53-
}
27+
let (Result.Analysis { analysis; kind = storable_kind }) = Result.get_abstract_analysis kind in
28+
let module Analysis = (val analysis) in
29+
let { Result.initial_models = analysis_initial_models; skip_overrides } =
30+
Analysis.initialize_models
31+
~static_analysis_configuration
32+
~scheduler
33+
~environment
34+
~functions
35+
~stubs
5436
in
55-
let accumulate model kind = initialize_each model (Result.get_abstract_analysis kind) in
56-
accumulate { initial_models = Callable.Map.empty; skip_overrides = Reference.Set.empty } kind
37+
let to_model_t model =
38+
let pkg = Result.Pkg { kind = ModelPart storable_kind; value = model } in
39+
{ Result.models = Kind.Map.add kind pkg Kind.Map.empty; is_obscure = false }
40+
in
41+
{ initial_models = analysis_initial_models |> Callable.Map.map ~f:to_model_t; skip_overrides }
5742

5843

5944
let record_initial_models ~functions ~stubs models =
@@ -96,17 +81,16 @@ let get_empty_model (type a) (kind : < model : a ; .. > Result.storable_kind) :
9681
Analysis.empty_model
9782

9883

99-
let get_obscure_models analysis =
100-
let get_analysis_specific_obscure_model map abstract_analysis =
84+
let get_obscure_models abstract_analysis =
85+
let models =
10186
let (Result.Analysis { kind; analysis }) = abstract_analysis in
10287
let module Analysis = (val analysis) in
10388
let obscure_model = Analysis.obscure_model in
10489
Kind.Map.add
10590
(Kind.abstract kind)
10691
(Result.Pkg { kind = ModelPart kind; value = obscure_model })
107-
map
92+
Kind.Map.empty
10893
in
109-
let models = get_analysis_specific_obscure_model Kind.Map.empty analysis in
11094
Result.{ models; is_obscure = true }
11195

11296

@@ -200,9 +184,9 @@ let explain_non_fixpoint ~iteration ~previous ~next =
200184
let show_models models =
201185
let open Result in
202186
(* list them to make the type system do its work *)
203-
let to_string (akind, Pkg { kind = ModelPart kind; value = model }) =
187+
let to_string (abstract_kind, Pkg { kind = ModelPart kind; value = model }) =
204188
let module Analysis = (val get_analysis kind) in
205-
Format.sprintf "%s: %s" (Kind.show akind) (Analysis.show_call_model model)
189+
Format.sprintf "%s: %s" (Kind.show abstract_kind) (Analysis.show_call_model model)
206190
in
207191
Kind.Map.bindings models |> List.map ~f:to_string |> String.concat ~sep:"\n"
208192

@@ -282,7 +266,7 @@ let widen_if_necessary step callable ~old_model ~new_model result =
282266

283267
let analyze_define
284268
step
285-
analysis
269+
abstract_analysis
286270
callable
287271
environment
288272
qualifier
@@ -305,24 +289,17 @@ let analyze_define
305289
| None ->
306290
Format.asprintf "No initial model found for %a" Callable.pretty_print callable |> failwith
307291
in
308-
let new_model, results =
309-
let analyze (Result.Analysis { Result.kind; analysis }) =
292+
let models, results =
293+
try
294+
let (Result.Analysis { Result.kind; analysis }) = abstract_analysis in
310295
let open Result in
311-
let akind = Kind.abstract kind in
296+
let abstract_kind = Kind.abstract kind in
312297
let module Analysis = (val analysis) in
313298
let existing = Result.get (ModelPart kind) old_model.models in
314-
let method_result, method_model =
315-
Analysis.analyze ~callable ~environment ~qualifier ~define ~existing
316-
in
317-
( akind,
318-
Pkg { kind = ModelPart kind; value = method_model },
319-
Pkg { kind = ResultPart kind; value = method_result } )
320-
in
321-
let accumulate (models, results) analysis =
322-
let akind, model, result = analyze analysis in
323-
Result.Kind.Map.add akind model models, Result.Kind.Map.add akind result results
324-
in
325-
try accumulate Result.Kind.Map.(empty, empty) analysis with
299+
let result, model = Analysis.analyze ~callable ~environment ~qualifier ~define ~existing in
300+
( Kind.Map.add abstract_kind (Pkg { kind = ModelPart kind; value = model }) Kind.Map.empty,
301+
Kind.Map.add abstract_kind (Pkg { kind = ResultPart kind; value = result }) Kind.Map.empty )
302+
with
326303
| Analysis.ClassHierarchy.Untracked annotation ->
327304
Log.log
328305
~section:`Info
@@ -334,17 +311,17 @@ let analyze_define
334311
| Sys.Break as exn -> analysis_failed step ~exn ~message:"Hit Ctrl+C" callable
335312
| _ as exn -> analysis_failed step ~exn ~message:"Analysis failed" callable
336313
in
337-
let new_model = { Result.models = new_model; is_obscure = false } in
314+
let new_model = { Result.models; is_obscure = false } in
338315
widen_if_necessary step callable ~old_model ~new_model results
339316

340317

341318
let strip_for_callsite model =
342319
let open Result in
343320
(* list them to make the type system do its work *)
344-
let strip akind (Pkg { kind = ModelPart kind; value = model }) models =
321+
let strip abstract_kind (Pkg { kind = ModelPart kind; value = model }) models =
345322
let module Analysis = (val get_analysis kind) in
346323
let model = Analysis.strip_for_callsite model in
347-
Kind.Map.add akind (Pkg { kind = ModelPart kind; value = model }) models
324+
Kind.Map.add abstract_kind (Pkg { kind = ModelPart kind; value = model }) models
348325
in
349326
let models = Kind.Map.fold strip model.InterproceduralResult.models Kind.Map.empty in
350327
{ model with models }

0 commit comments

Comments
 (0)