[differential] Modularize cost and reuse common funcs

Reviewed By: jvillard

Differential Revision: D26843701

fbshipit-source-id: 46e8ac551
master
Ezgi Çiçek 4 years ago committed by Facebook GitHub Bot
parent 3ad19cd83d
commit a7ada8d4af

@ -57,7 +57,7 @@ let dedup (issues : Jsonbug_t.jsonbug list) =
let create_json_bug ~qualifier ~line ~file ~source_file ~trace ~(item : Jsonbug_t.item)
~(issue_type : IssueType.t) =
{ Jsonbug_j.bug_type= issue_type.unique_id
{ Jsonbug_t.bug_type= issue_type.unique_id
; qualifier
; severity= IssueType.string_of_severity Advice
; line
@ -177,6 +177,7 @@ let to_map key_func report =
~init:String.Map.empty report
module Cost = struct
module CostItem = struct
type t =
{ cost_item: Jsonbug_t.cost_item
@ -287,7 +288,9 @@ let issue_of_cost kind CostIssues.{complexity_increase_issue; unreachable_issue;
else if String.equal method_name Procname.Java.class_initializer_method_name then
Format.pp_print_string f "class initializer"
else
Format.fprintf f "%a" (MarkupFormatter.wrap_monospaced Format.pp_print_string) method_name
Format.fprintf f "%a"
(MarkupFormatter.wrap_monospaced Format.pp_print_string)
method_name
in
Format.asprintf "%s of %t has %a from %a to %a. %s%a"
(CostKind.to_complexity_string kind)
@ -329,7 +332,8 @@ let issue_of_cost kind CostIssues.{complexity_increase_issue; unreachable_issue;
- DB > DA => fixed
- DB < DA => introduced *)
let of_costs ~(current_costs : Jsonbug_t.costs_report) ~(previous_costs : Jsonbug_t.costs_report) =
let issues_of_reports ~(current_costs : Jsonbug_t.costs_report)
~(previous_costs : Jsonbug_t.costs_report) =
let fold_aux kind issue_spec ~key:_ ~data (left, both, right) =
match data with
| `Both (current, previous) ->
@ -392,7 +396,7 @@ let of_costs ~(current_costs : Jsonbug_t.costs_report) ~(previous_costs : Jsonbu
(to_map (get_previous_costs ~extract_cost_f))
~f:(fold_aux kind issue_spec) ~init:acc )
CostIssues.enabled_cost_map ([], [], [])
end
module ConfigImpactItem = struct
module UncheckedCallee = ConfigImpactAnalysis.UncheckedCallee
@ -444,7 +448,7 @@ module ConfigImpactItem = struct
else None
let issues_of ~(current_config_impact : Jsonbug_t.config_impact_report)
let issues_of_reports ~(current_config_impact : Jsonbug_t.config_impact_report)
~(previous_config_impact : Jsonbug_t.config_impact_report) =
let fold_aux ~key:_ ~data ((acc_introduced, acc_fixed) as acc) =
match data with
@ -474,11 +478,8 @@ module ConfigImpactItem = struct
Map.fold2 ~init:([], []) current_map previous_map ~f:fold_aux
end
(** Set operations should keep duplicated issues with identical hashes *)
let of_reports ~(current_report : Jsonbug_t.report) ~(previous_report : Jsonbug_t.report)
~(current_costs : Jsonbug_t.costs_report) ~(previous_costs : Jsonbug_t.costs_report)
~(current_config_impact : Jsonbug_t.config_impact_report)
~(previous_config_impact : Jsonbug_t.config_impact_report) : t =
module Report = struct
let issues_of_reports ~current_report ~previous_report =
let fold_aux ~key:_ ~data (left, both, right) =
match data with
| `Left left' ->
@ -488,22 +489,32 @@ let of_reports ~(current_report : Jsonbug_t.report) ~(previous_report : Jsonbug_
| `Right right' ->
(left, both, List.rev_append right' right)
in
let introduced, preexisting, fixed =
let key_func (issue : Jsonbug_t.jsonbug) = issue.hash in
let to_map = to_map key_func in
Map.fold2 (to_map current_report) (to_map previous_report) ~f:fold_aux ~init:([], [], [])
end
let combine_all ~report ~cost ~config_impact =
dedup report |> List.rev_append cost |> List.rev_append config_impact
(** Set operations should keep duplicated issues with identical hashes *)
let issues_of_reports ~(current_report : Jsonbug_t.report) ~(previous_report : Jsonbug_t.report)
~(current_costs : Jsonbug_t.costs_report) ~(previous_costs : Jsonbug_t.costs_report)
~(current_config_impact : Jsonbug_t.config_impact_report)
~(previous_config_impact : Jsonbug_t.config_impact_report) : t =
let introduced, preexisting, fixed = Report.issues_of_reports ~current_report ~previous_report in
let introduced_costs, preexisting_costs, fixed_costs =
Cost.issues_of_reports ~current_costs ~previous_costs
in
let costs_summary = CostsSummary.to_json ~current_costs ~previous_costs in
let introduced_costs, preexisting_costs, fixed_costs = of_costs ~current_costs ~previous_costs in
let introduced_config_impact, fixed_config_impact =
ConfigImpactItem.issues_of ~current_config_impact ~previous_config_impact
ConfigImpactItem.issues_of_reports ~current_config_impact ~previous_config_impact
in
{ introduced=
List.rev_append introduced_costs (dedup introduced)
|> List.rev_append introduced_config_impact
; fixed= List.rev_append fixed_costs (dedup fixed) |> List.rev_append fixed_config_impact
; preexisting= List.rev_append preexisting_costs (dedup preexisting)
; costs_summary }
combine_all ~report:introduced ~cost:introduced_costs ~config_impact:introduced_config_impact
; fixed= combine_all ~report:fixed ~cost:fixed_costs ~config_impact:fixed_config_impact
; preexisting= combine_all ~report:preexisting ~cost:preexisting_costs ~config_impact:[]
; costs_summary= CostsSummary.to_json ~current_costs ~previous_costs }
let to_files {introduced; fixed; preexisting; costs_summary} destdir =

@ -13,7 +13,7 @@ type t =
; preexisting: Jsonbug_t.report
; costs_summary: Yojson.Basic.t }
val of_reports :
val issues_of_reports :
current_report:Jsonbug_t.report
-> previous_report:Jsonbug_t.report
-> current_costs:Jsonbug_t.costs_report

@ -26,7 +26,7 @@ let reportdiff ~current_report:current_report_fname ~previous_report:previous_re
let previous_config_impact = load_config_impact previous_config_impact_fname in
let diff =
let unfiltered_diff =
Differential.of_reports ~current_report ~previous_report ~current_costs ~previous_costs
Differential.issues_of_reports ~current_report ~previous_report ~current_costs ~previous_costs
~current_config_impact ~previous_config_impact
in
(* FIXME(T54950303) replace use of filtering with deduplicate *)

@ -171,7 +171,7 @@ let test_skip_duplicated_types_on_filenames =
let current_config_impact = [] in
let previous_config_impact = [] in
let diff =
Differential.of_reports ~current_report ~previous_report ~current_costs ~previous_costs
Differential.issues_of_reports ~current_report ~previous_report ~current_costs ~previous_costs
~current_config_impact ~previous_config_impact
in
let diff' =

@ -32,7 +32,7 @@ let current_config_impact = []
let previous_config_impact = []
let diff =
Differential.of_reports ~current_report ~previous_report ~current_costs ~previous_costs
Differential.issues_of_reports ~current_report ~previous_report ~current_costs ~previous_costs
~current_config_impact ~previous_config_impact

Loading…
Cancel
Save