[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) let create_json_bug ~qualifier ~line ~file ~source_file ~trace ~(item : Jsonbug_t.item)
~(issue_type : IssueType.t) = ~(issue_type : IssueType.t) =
{ Jsonbug_j.bug_type= issue_type.unique_id { Jsonbug_t.bug_type= issue_type.unique_id
; qualifier ; qualifier
; severity= IssueType.string_of_severity Advice ; severity= IssueType.string_of_severity Advice
; line ; line
@ -177,7 +177,8 @@ let to_map key_func report =
~init:String.Map.empty report ~init:String.Map.empty report
module CostItem = struct module Cost = struct
module CostItem = struct
type t = type t =
{ cost_item: Jsonbug_t.cost_item { cost_item: Jsonbug_t.cost_item
; polynomial: Polynomials.NonNegativePolynomial.t option ; polynomial: Polynomials.NonNegativePolynomial.t option
@ -232,9 +233,9 @@ module CostItem = struct
in in
Format.fprintf fmt "Cost of %s is %t (degree is %a)" procedure_name pp_cost Format.fprintf fmt "Cost of %s is %t (degree is %a)" procedure_name pp_cost
(pp_degree ~only_bigO:false) curr_item (pp_degree ~only_bigO:false) curr_item
end end
let polynomial_traces issue_type = function let polynomial_traces issue_type = function
| None -> | None ->
[] []
| Some (Val (_, degree_term)) -> | Some (Val (_, degree_term)) ->
@ -247,7 +248,7 @@ let polynomial_traces issue_type = function
[("", Polynomials.TopTraces.make_err_trace traces)] [("", Polynomials.TopTraces.make_err_trace traces)]
let issue_of_cost kind CostIssues.{complexity_increase_issue; unreachable_issue; infinite_issue} let issue_of_cost kind CostIssues.{complexity_increase_issue; unreachable_issue; infinite_issue}
~delta ~prev_item:({CostItem.degree_with_term= prev_degree_with_term} as prev_item) ~delta ~prev_item:({CostItem.degree_with_term= prev_degree_with_term} as prev_item)
~curr_item: ~curr_item:
({CostItem.cost_item= cost_info; degree_with_term= curr_degree_with_term} as curr_item) = ({CostItem.cost_item= cost_info; degree_with_term= curr_degree_with_term} as curr_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 else if String.equal method_name Procname.Java.class_initializer_method_name then
Format.pp_print_string f "class initializer" Format.pp_print_string f "class initializer"
else 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 in
Format.asprintf "%s of %t has %a from %a to %a. %s%a" Format.asprintf "%s of %t has %a from %a to %a. %s%a"
(CostKind.to_complexity_string kind) (CostKind.to_complexity_string kind)
@ -324,12 +327,13 @@ let issue_of_cost kind CostIssues.{complexity_increase_issue; unreachable_issue;
else None else None
(** Differential of cost reports, based on degree variations. Compare degree_before (DB), and (** Differential of cost reports, based on degree variations. Compare degree_before (DB), and
degree_after (DA): degree_after (DA):
- DB > DA => fixed - DB > DA => fixed
- DB < DA => introduced *) - 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) = let fold_aux kind issue_spec ~key:_ ~data (left, both, right) =
match data with match data with
| `Both (current, previous) -> | `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)) (to_map (get_previous_costs ~extract_cost_f))
~f:(fold_aux kind issue_spec) ~init:acc ) ~f:(fold_aux kind issue_spec) ~init:acc )
CostIssues.enabled_cost_map ([], [], []) CostIssues.enabled_cost_map ([], [], [])
end
module ConfigImpactItem = struct module ConfigImpactItem = struct
module UncheckedCallee = ConfigImpactAnalysis.UncheckedCallee module UncheckedCallee = ConfigImpactAnalysis.UncheckedCallee
@ -444,7 +448,7 @@ module ConfigImpactItem = struct
else None 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) = ~(previous_config_impact : Jsonbug_t.config_impact_report) =
let fold_aux ~key:_ ~data ((acc_introduced, acc_fixed) as acc) = let fold_aux ~key:_ ~data ((acc_introduced, acc_fixed) as acc) =
match data with match data with
@ -474,11 +478,8 @@ module ConfigImpactItem = struct
Map.fold2 ~init:([], []) current_map previous_map ~f:fold_aux Map.fold2 ~init:([], []) current_map previous_map ~f:fold_aux
end end
(** Set operations should keep duplicated issues with identical hashes *) module Report = struct
let of_reports ~(current_report : Jsonbug_t.report) ~(previous_report : Jsonbug_t.report) let issues_of_reports ~current_report ~previous_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 fold_aux ~key:_ ~data (left, both, right) = let fold_aux ~key:_ ~data (left, both, right) =
match data with match data with
| `Left left' -> | `Left left' ->
@ -488,22 +489,32 @@ let of_reports ~(current_report : Jsonbug_t.report) ~(previous_report : Jsonbug_
| `Right right' -> | `Right right' ->
(left, both, List.rev_append right' right) (left, both, List.rev_append right' right)
in in
let introduced, preexisting, fixed =
let key_func (issue : Jsonbug_t.jsonbug) = issue.hash in let key_func (issue : Jsonbug_t.jsonbug) = issue.hash in
let to_map = to_map key_func in let to_map = to_map key_func in
Map.fold2 (to_map current_report) (to_map previous_report) ~f:fold_aux ~init:([], [], []) 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 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 = 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 in
{ introduced= { introduced=
List.rev_append introduced_costs (dedup introduced) combine_all ~report:introduced ~cost:introduced_costs ~config_impact:introduced_config_impact
|> List.rev_append introduced_config_impact ; fixed= combine_all ~report:fixed ~cost:fixed_costs ~config_impact:fixed_config_impact
; fixed= List.rev_append fixed_costs (dedup fixed) |> List.rev_append fixed_config_impact ; preexisting= combine_all ~report:preexisting ~cost:preexisting_costs ~config_impact:[]
; preexisting= List.rev_append preexisting_costs (dedup preexisting) ; costs_summary= CostsSummary.to_json ~current_costs ~previous_costs }
; costs_summary }
let to_files {introduced; fixed; preexisting; costs_summary} destdir = let to_files {introduced; fixed; preexisting; costs_summary} destdir =

@ -13,7 +13,7 @@ type t =
; preexisting: Jsonbug_t.report ; preexisting: Jsonbug_t.report
; costs_summary: Yojson.Basic.t } ; costs_summary: Yojson.Basic.t }
val of_reports : val issues_of_reports :
current_report:Jsonbug_t.report current_report:Jsonbug_t.report
-> previous_report:Jsonbug_t.report -> previous_report:Jsonbug_t.report
-> current_costs:Jsonbug_t.costs_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 previous_config_impact = load_config_impact previous_config_impact_fname in
let diff = let diff =
let unfiltered_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 ~current_config_impact ~previous_config_impact
in in
(* FIXME(T54950303) replace use of filtering with deduplicate *) (* 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 current_config_impact = [] in
let previous_config_impact = [] in let previous_config_impact = [] in
let diff = 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 ~current_config_impact ~previous_config_impact
in in
let diff' = let diff' =

@ -32,7 +32,7 @@ let current_config_impact = []
let previous_config_impact = [] let previous_config_impact = []
let diff = 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 ~current_config_impact ~previous_config_impact

Loading…
Cancel
Save