[report] Refactor JsonCostsPrinter module

Summary:
In the following diff, we will add `JsonConfigImpactPrinter` that will share some common code base
with `JsonCostsPrinter`.  This diff prepares the sharing.

Reviewed By: jvillard

Differential Revision: D26665070

fbshipit-source-id: 5032e0611
master
Sungkeun Cho 4 years ago committed by Facebook GitHub Bot
parent 1a5f40f25f
commit 3ba6a1e9df

@ -241,11 +241,22 @@ module IssuesJson = struct
err_log err_log
end end
type json_costs_printer_typ = module NoQualifierHashProcInfo = struct
{loc: Location.t; proc_name: Procname.t; cost_opt: CostDomain.summary option} type t = {hash: string; loc: Jsonbug_t.loc; procedure_name: string; procedure_id: string}
module JsonCostsPrinter = MakeJsonListPrinter (struct let get loc proc_name =
type elt = json_costs_printer_typ let file =
SourceFile.to_string ~force_relative:Config.report_force_relative_path loc.Location.file
in
let hash = compute_hash ~severity:"" ~bug_type:"" ~proc_name ~file ~qualifier:"" in
let loc = {Jsonbug_t.file; lnum= loc.Location.line; cnum= loc.Location.col; enum= -1} in
let procedure_name = Procname.get_method proc_name in
let procedure_id = procedure_id_of_procname proc_name in
{hash; loc; procedure_name; procedure_id}
end
module JsonCostsPrinterElt = struct
type elt = {loc: Location.t; proc_name: Procname.t; cost_opt: CostDomain.summary option}
let to_string {loc; proc_name; cost_opt} = let to_string {loc; proc_name; cost_opt} =
match cost_opt with match cost_opt with
@ -273,13 +284,13 @@ module JsonCostsPrinter = MakeJsonListPrinter (struct
Advice } Advice }
in in
let cost_item = let cost_item =
let file = let {NoQualifierHashProcInfo.hash; loc; procedure_name; procedure_id} =
SourceFile.to_string ~force_relative:Config.report_force_relative_path loc.Location.file NoQualifierHashProcInfo.get loc proc_name
in in
{ Jsonbug_t.hash= compute_hash ~severity:"" ~bug_type:"" ~proc_name ~file ~qualifier:"" { Jsonbug_t.hash
; loc= {file; lnum= loc.Location.line; cnum= loc.Location.col; enum= -1} ; loc
; procedure_name= Procname.get_method proc_name ; procedure_name
; procedure_id= procedure_id_of_procname proc_name ; procedure_id
; is_on_ui_thread ; is_on_ui_thread
; exec_cost= cost_info (CostDomain.get_cost_kind CostKind.OperationCost post).cost ; exec_cost= cost_info (CostDomain.get_cost_kind CostKind.OperationCost post).cost
; autoreleasepool_size= ; autoreleasepool_size=
@ -289,7 +300,9 @@ module JsonCostsPrinter = MakeJsonListPrinter (struct
Some (Jsonbug_j.string_of_cost_item cost_item) Some (Jsonbug_j.string_of_cost_item cost_item)
| _ -> | _ ->
None None
end) end
module JsonCostsPrinter = MakeJsonListPrinter (JsonCostsPrinterElt)
let mk_error_filter filters proc_name file error_name = let mk_error_filter filters proc_name file error_name =
(Config.write_html || not (IssueType.(equal skip_function) error_name)) (Config.write_html || not (IssueType.(equal skip_function) error_name))
@ -316,7 +329,7 @@ let write_lint_issues filters (issues_outf : Utils.outfile) linereader procname
(** Process a summary *) (** Process a summary *)
let process_summary ~costs_outf proc_name loc cost_opt err_log issues_acc = let process_summary proc_name loc ~cost:(cost_opt, costs_outf) err_log issues_acc =
write_costs proc_name loc cost_opt costs_outf ; write_costs proc_name loc cost_opt costs_outf ;
collect_issues proc_name loc err_log issues_acc collect_issues proc_name loc err_log issues_acc
@ -326,7 +339,7 @@ let process_all_summaries_and_issues ~issues_outf ~costs_outf =
let filters = Inferconfig.create_filters () in let filters = Inferconfig.create_filters () in
let all_issues = ref [] in let all_issues = ref [] in
Summary.OnDisk.iter_report_summaries_from_config ~f:(fun proc_name loc cost_opt err_log -> Summary.OnDisk.iter_report_summaries_from_config ~f:(fun proc_name loc cost_opt err_log ->
all_issues := process_summary ~costs_outf proc_name loc cost_opt err_log !all_issues ) ; all_issues := process_summary proc_name loc ~cost:(cost_opt, costs_outf) err_log !all_issues ) ;
all_issues := Issue.sort_filter_issues !all_issues ; all_issues := Issue.sort_filter_issues !all_issues ;
List.iter List.iter
~f:(fun {Issue.proc_name; proc_location; err_key; err_data} -> ~f:(fun {Issue.proc_name; proc_location; err_key; err_data} ->
@ -348,13 +361,17 @@ let write_reports ~issues_json ~costs_json =
| Some outf -> | Some outf ->
outf outf
in in
let issues_outf = mk_outfile issues_json in let open_outfile_and_fmt json =
IssuesJson.pp_open issues_outf.fmt () ; let outf = mk_outfile json in
let costs_outf = mk_outfile costs_json in IssuesJson.pp_open outf.fmt () ;
JsonCostsPrinter.pp_open costs_outf.fmt () ; outf
in
let close_fmt_and_outfile outf =
IssuesJson.pp_close outf.Utils.fmt () ;
Utils.close_outf outf
in
let issues_outf = open_outfile_and_fmt issues_json in
let costs_outf = open_outfile_and_fmt costs_json in
process_all_summaries_and_issues ~issues_outf ~costs_outf ; process_all_summaries_and_issues ~issues_outf ~costs_outf ;
JsonCostsPrinter.pp_close costs_outf.fmt () ; close_fmt_and_outfile costs_outf ;
Utils.close_outf costs_outf ; close_fmt_and_outfile issues_outf
IssuesJson.pp_close issues_outf.fmt () ;
Utils.close_outf issues_outf ;
()

Loading…
Cancel
Save