Skip to content

Commit 1c375a1

Browse files
jvillardfacebook-github-bot
authored andcommitted
[log] die more appropriately
Summary: - failwith police: no more `failwith`. Instead, use `Logging.die`. - Introduce the `SimpleLogging` module for dying from modules where `Logging` cannot be used (usually because that would create a cyclic dependency). - always log backtraces, and show backtraces on the console except for usage errors - Also point out in the log file where the toplevel executions of infer happen Reviewed By: jeremydubreil Differential Revision: D5726362 fbshipit-source-id: d7a01fc
1 parent 3e660b0 commit 1c375a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+449
-294
lines changed

infer/src/IR/Cfg.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let check_cfg_connectedness cfg =
8484
let nodes = Procdesc.get_nodes pd in
8585
(* TODO (T20302015): also check the CFGs for the C-like procedures *)
8686
if not Config.keep_going && Typ.Procname.is_java pname && List.exists ~f:broken_node nodes then
87-
failwithf "Broken CFG on %a" Typ.Procname.pp pname
87+
L.(die InternalError) "Broken CFG on %a" Typ.Procname.pp pname
8888
in
8989
let pdescs = get_all_procs cfg in
9090
List.iter ~f:do_pdesc pdescs

infer/src/IR/HilExp.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ let of_sil ~include_array_indexes ~f_resolve_id exp typ =
162162
| Some access_path
163163
-> AccessPath access_path
164164
| None
165-
-> failwithf "Couldn't convert var expression %a to access path" Exp.pp exp
165+
-> L.(die InternalError) "Couldn't convert var expression %a to access path" Exp.pp exp
166166
in
167167
of_sil_ exp typ
168168

infer/src/IR/HilInstr.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ let of_sil ~include_array_indexes ~f_resolve_id (instr: Sil.instr) =
8484
| ap :: _
8585
-> ap
8686
| []
87-
-> invalid_argf "Invalid pointer arithmetic expression %a used as LHS" Exp.pp lhs_exp
88-
)
87+
-> L.(die InternalError)
88+
"Invalid pointer arithmetic expression %a used as LHS" Exp.pp lhs_exp )
8989
| _
90-
-> invalid_argf "Non-assignable LHS expression %a" Exp.pp lhs_exp
90+
-> L.(die InternalError) "Non-assignable LHS expression %a" Exp.pp lhs_exp
9191
in
9292
Instr (Assign (lhs_access_path, exp_of_sil rhs_exp typ, loc))
9393
| Call (ret_opt, call_exp, formals, loc, call_flags)
@@ -99,7 +99,7 @@ let of_sil ~include_array_indexes ~f_resolve_id (instr: Sil.instr) =
9999
| AccessPath access_path
100100
-> Indirect access_path
101101
| call_exp
102-
-> invalid_argf "Unexpected call expression %a" HilExp.pp call_exp
102+
-> L.(die InternalError) "Unexpected call expression %a" HilExp.pp call_exp
103103
in
104104
let formals = List.map ~f:(fun (exp, typ) -> exp_of_sil exp typ) formals in
105105
Instr (Call (hil_ret, hil_call, formals, call_flags, loc))

infer/src/IR/IntLit.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*)
1010
open! IStd
1111
module F = Format
12+
module L = Logging
1213

1314
(** signed and unsigned integer literals *)
1415
type t = bool * Int64.t * bool
@@ -108,7 +109,7 @@ let sub i1 i2 = add i1 (neg i2)
108109
let shift_left (unsigned1, i1, ptr1) (_, i2, _) =
109110
match Int64.to_int i2 with
110111
| None
111-
-> failwithf "Shifting failed with operand %a" Int64.pp i2
112+
-> L.(die InternalError) "Shifting failed with operand %a" Int64.pp i2
112113
| Some i2
113114
-> if i2 < 0 || i2 >= 64 then raise OversizedShift ;
114115
let res = Int64.shift_left i1 i2 in
@@ -117,7 +118,7 @@ let shift_left (unsigned1, i1, ptr1) (_, i2, _) =
117118
let shift_right (unsigned1, i1, ptr1) (_, i2, _) =
118119
match Int64.to_int i2 with
119120
| None
120-
-> failwithf "Shifting failed with operand %a" Int64.pp i2
121+
-> L.(die InternalError) "Shifting failed with operand %a" Int64.pp i2
121122
| Some i2
122123
-> if i2 < 0 || i2 >= 64 then raise OversizedShift ;
123124
let res = Int64.shift_right i1 i2 in

infer/src/IR/Pvar.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ let get_translation_unit pvar =
227227
| Global_var (tu, _, _, _)
228228
-> tu
229229
| _
230-
-> invalid_argf "Expected a global variable"
230+
-> L.(die InternalError) "Expected a global variable"
231231

232232
let is_compile_constant pvar = match pvar.pv_kind with Global_var (_, b, _, _) -> b | _ -> false
233233

infer/src/IR/QualifiedCppName.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*)
99

1010
open! IStd
11+
module L = Logging
1112

1213
(* internally it uses reversed list to store qualified name, for example: ["get", "shared_ptr<int>", "std"]*)
1314
type t = string list [@@deriving compare]
@@ -27,13 +28,13 @@ let strip_template_args quals =
2728
let append_template_args_to_last quals ~args =
2829
match quals with
2930
| [last; _] when String.contains last '<'
30-
-> failwithf
31+
-> L.(die InternalError)
3132
"expected qualified name without template args, but got %s, the last qualifier of %s" last
3233
(String.concat ~sep:", " quals)
3334
| last :: rest
3435
-> (last ^ args) :: rest
3536
| []
36-
-> failwith "expected non-empty qualified name"
37+
-> L.(die InternalError) "expected non-empty qualified name"
3738

3839
let to_list = List.rev
3940

@@ -82,7 +83,7 @@ module Match = struct
8283
List.iter colon_splits ~f:(fun s ->
8384
(* Filter out the '<' in operator< and operator<= *)
8485
if not (String.is_prefix s ~prefix:"operator<") && String.contains s '<' then
85-
failwithf "Unexpected template in fuzzy qualified name %s." qual_name ) ;
86+
L.(die InternalError) "Unexpected template in fuzzy qualified name %s." qual_name ) ;
8687
of_qual_string qual_name
8788

8889
let of_fuzzy_qual_names fuzzy_qual_names =

infer/src/absint/AbstractInterpreter.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct
9999
else
100100
let visit_count' = old_state.visit_count + 1 in
101101
if visit_count' > Config.max_widens then
102-
failwithf
102+
L.(die InternalError)
103103
"Exceeded max widening threshold %d while analyzing %a. Please check your widening operator or increase the threshold"
104104
Config.max_widens Typ.Procname.pp (Procdesc.get_proc_name pdesc) ;
105105
update_inv_map widened_pre visit_count'

infer/src/backend/DifferentialFilters.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*)
99

1010
open! IStd
11+
module L = Logging
1112

1213
module FileRenamings = struct
1314
type renaming = {current: string; previous: string} [@@deriving compare]
@@ -45,7 +46,7 @@ module FileRenamings = struct
4546
| _
4647
-> raise (Yojson.Json_error "not a record")
4748
with Yojson.Json_error err ->
48-
failwithf
49+
L.(die UserError)
4950
"Error parsing file renamings: %s@\nExpected JSON object of the following form: '%s', but instead got: '%s'"
5051
err "{\"current\": \"aaa.java\", \"previous\": \"BBB.java\"}"
5152
(Yojson.Basic.to_string assoc)
@@ -54,7 +55,7 @@ module FileRenamings = struct
5455
| `List json_renamings
5556
-> List.map ~f:renaming_of_assoc json_renamings
5657
| _
57-
-> failwithf "Expected JSON list but got '%s'" input
58+
-> L.(die UserError) "Expected JSON list but got '%s'" input
5859

5960
let from_json_file file : t = from_json (In_channel.read_all file)
6061

infer/src/backend/InferPrint.ml

+12-11
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ module IssuesJson = struct
331331
-> (err_data.loc.Location.file, 0)
332332
in
333333
if SourceFile.is_invalid source_file then
334-
failwithf "Invalid source file for %a %[email protected]: %a@." IssueType.pp key.err_name
334+
L.(die InternalError)
335+
"Invalid source file for %a %[email protected]: %a@." IssueType.pp key.err_name
335336
Localise.pp_error_desc key.err_desc Errlog.pp_loc_trace err_data.loc_trace ;
336337
let should_report_source_file =
337338
not (SourceFile.is_infer_model source_file) || Config.debug_mode || Config.debug_exceptions
@@ -741,39 +742,39 @@ let pp_issues_in_format (format_kind, (outf: Utils.outfile)) =
741742
| Csv
742743
-> IssuesCsv.pp_issues_of_error_log outf.fmt
743744
| Tests
744-
-> failwith "Print issues as tests is not implemented"
745+
-> L.(die InternalError) "Print issues as tests is not implemented"
745746
| Text
746747
-> IssuesTxt.pp_issues_of_error_log outf.fmt
747748
| Latex
748-
-> failwith "Printing issues in latex is not implemented"
749+
-> L.(die InternalError) "Printing issues in latex is not implemented"
749750

750751
let pp_procs_in_format (format_kind, (outf: Utils.outfile)) =
751752
match format_kind with
752753
| Csv
753754
-> ProcsCsv.pp_summary outf.fmt
754755
| Json | Latex | Tests | Text
755-
-> failwith "Printing procs in json/latex/tests/text is not implemented"
756+
-> L.(die InternalError) "Printing procs in json/latex/tests/text is not implemented"
756757

757758
let pp_calls_in_format (format_kind, (outf: Utils.outfile)) =
758759
match format_kind with
759760
| Csv
760761
-> CallsCsv.pp_calls outf.fmt
761762
| Json | Tests | Text | Latex
762-
-> failwith "Printing calls in json/tests/text/latex is not implemented"
763+
-> L.(die InternalError) "Printing calls in json/tests/text/latex is not implemented"
763764

764765
let pp_stats_in_format (format_kind, _) =
765766
match format_kind with
766767
| Csv
767768
-> Stats.process_summary
768769
| Json | Tests | Text | Latex
769-
-> failwith "Printing stats in json/tests/text/latex is not implemented"
770+
-> L.(die InternalError) "Printing stats in json/tests/text/latex is not implemented"
770771

771772
let pp_summary_in_format (format_kind, (outf: Utils.outfile)) =
772773
match format_kind with
773774
| Latex
774775
-> Summary.write_summary_latex outf.fmt
775776
| Json | Csv | Tests | Text
776-
-> failwith "Printing summary in json/csv/tests/text is not implemented"
777+
-> L.(die InternalError) "Printing summary in json/csv/tests/text is not implemented"
777778

778779
let pp_issues_of_error_log error_filter linereader proc_loc_opt procname err_log bug_format_list =
779780
let pp_issues_in_format format =
@@ -848,11 +849,11 @@ let pp_json_report_by_report_kind formats_by_report_kind fname =
848849
| Text
849850
-> pp_text_of_report outf.fmt report
850851
| Json
851-
-> failwith "Printing issues from json does not support json output"
852+
-> L.(die InternalError) "Printing issues from json does not support json output"
852853
| Csv
853-
-> failwith "Printing issues from json does not support csv output"
854+
-> L.(die InternalError) "Printing issues from json does not support csv output"
854855
| Latex
855-
-> failwith "Printing issues from json does not support latex output"
856+
-> L.(die InternalError) "Printing issues from json does not support latex output"
856857
in
857858
List.iter ~f:pp_json_issue format_list
858859
in
@@ -869,7 +870,7 @@ let pp_json_report_by_report_kind formats_by_report_kind fname =
869870
in
870871
List.iter ~f:pp_report_by_report_kind formats_by_report_kind
871872
| Error error
872-
-> failwithf "Error reading '%s': %s" fname error
873+
-> L.(die UserError) "Error reading '%s': %s" fname error
873874

874875
let pp_lint_issues_by_report_kind formats_by_report_kind error_filter linereader procname error_log =
875876
let pp_summary_by_report_kind (report_kind, format_list) =

infer/src/backend/StatsAggregator.ml

+12-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*)
99
open! IStd
1010
open! PVariant
11+
module L = Logging
1112

1213
let aggregated_stats_filename = "aggregated_stats.json"
1314

@@ -51,22 +52,18 @@ let find_stats_files_in_dir dir =
5152
{frontend_paths; backend_paths; reporting_paths}
5253

5354
let load_data_from_infer_deps file =
55+
let error msg = Printf.sprintf ("Error reading '%s': " ^^ msg) file in
5456
let extract_target_and_path line =
55-
match Str.split_delim (Str.regexp (Str.quote "\t")) line with
57+
match String.split ~on:'\t' line with
5658
| target :: _ :: path :: _
57-
-> if dir_exists path then (target, path)
58-
else raise (Failure ("path '" ^ path ^ "' is not a valid directory"))
59+
-> if dir_exists path then Ok (target, path)
60+
else Error (error "path '%s' is not a valid directory" path)
5961
| _
60-
-> raise (Failure "malformed input")
62+
-> Error (error "malformed input")
6163
in
62-
let lines = Utils.read_file file in
63-
try
64-
match lines with
65-
| Ok l
66-
-> Ok (List.map ~f:extract_target_and_path l)
67-
| Error error
68-
-> raise (Failure (Printf.sprintf "Error reading '%s': %s" file error))
69-
with Failure msg -> Error msg
64+
let parse_lines lines = List.map lines ~f:extract_target_and_path |> Result.all in
65+
Utils.read_file file |> Result.map_error ~f:(fun msg -> error "%s" msg)
66+
|> Result.bind ~f:parse_lines
7067

7168
let collect_all_stats_files () =
7269
let infer_out = Config.results_dir in
@@ -138,7 +135,9 @@ let aggregate_stats_by_target tp =
138135
let generate_files () =
139136
let infer_out = Config.results_dir in
140137
let stats_files = collect_all_stats_files () in
141-
let origin = match stats_files with Ok origin -> origin | Error e -> failwith e in
138+
let origin =
139+
match stats_files with Ok origin -> origin | Error e -> L.(die InternalError) "%s" e
140+
in
142141
let aggregated_frontend_stats_dir = Filename.concat infer_out Config.frontend_stats_dir_name in
143142
let aggregated_backend_stats_dir = Filename.concat infer_out Config.backend_stats_dir_name in
144143
let aggregated_reporting_stats_dir = Filename.concat infer_out Config.reporting_stats_dir_name in

infer/src/backend/builtin.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*)
99

1010
open! IStd
11+
module L = Logging
1112

1213
(** Module for builtin functions with their symbolic execution handler *)
1314

@@ -34,7 +35,7 @@ let builtin_functions = Typ.Procname.Hash.create 4
3435
let check_register_populated () =
3536
(* check if BuiltinDefn were loaded before accessing register *)
3637
if Int.equal (Typ.Procname.Hash.length builtin_functions) 0 then
37-
failwith "Builtins were not initialized"
38+
L.(die InternalError) "Builtins were not initialized"
3839

3940
(** check if the function is a builtin *)
4041
let is_registered name =

infer/src/backend/callbacks.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let iterate_callbacks call_graph exe_env =
126126
let analyze_proc_name pname =
127127
match Ondemand.get_proc_desc pname with
128128
| None
129-
-> failwithf "Could not find proc desc for %a" Typ.Procname.pp pname
129+
-> L.(die InternalError) "Could not find proc desc for %a" Typ.Procname.pp pname
130130
| Some pdesc
131131
-> ignore (Ondemand.analyze_proc_desc pdesc pdesc)
132132
in

infer/src/backend/crashcontext.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let frame_id_of_summary stacktree =
2525
let short_name = List.hd_exn (Str.split (Str.regexp "(") stacktree.Stacktree_j.method_name) in
2626
match stacktree.Stacktree_j.location with
2727
| None
28-
-> failwith
28+
-> L.(die InternalError)
2929
"Attempted to take signature of a frame without location information. This is undefined."
3030
| Some {line= Some line_num; file}
3131
-> F.sprintf "%s(%s:%d)" short_name (Filename.basename file) line_num

infer/src/backend/exe_env.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ let java_global_tenv =
149149
( lazy
150150
( match Tenv.load_from_file DB.global_tenv_fname with
151151
| None
152-
-> failwithf "Could not load the global tenv at path %s@."
153-
(DB.filename_to_string DB.global_tenv_fname)
152+
-> L.(die InternalError)
153+
"Could not load the global tenv at path '%s'" (DB.filename_to_string DB.global_tenv_fname)
154154
| Some tenv
155155
-> tenv ) )
156156

@@ -166,10 +166,11 @@ let get_tenv exe_env proc_name =
166166
| Some tenv
167167
-> tenv
168168
| None
169-
-> failwithf "get_tenv: tenv not found for %a in file %s" Typ.Procname.pp proc_name
169+
-> L.(die InternalError)
170+
"get_tenv: tenv not found for %a in file '%s'" Typ.Procname.pp proc_name
170171
(DB.filename_to_string file_data.tenv_file) )
171172
| None
172-
-> failwithf "get_tenv: file_data not found for %a" Typ.Procname.pp proc_name
173+
-> L.(die InternalError) "get_tenv: file_data not found for %a" Typ.Procname.pp proc_name
173174

174175
(** return the cfg associated to the procedure *)
175176
let get_cfg exe_env pname =

infer/src/backend/infer.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ let () =
118118
-> (* at least one report must be passed in input to compute differential *)
119119
( match (Config.report_current, Config.report_previous) with
120120
| None, None
121-
-> failwith "Expected at least one argument among 'report-current' and 'report-previous'\n"
121+
-> L.(die UserError)
122+
"Expected at least one argument among 'report-current' and 'report-previous'"
122123
| _
123124
-> () ) ;
124125
ReportDiff.reportdiff ~current_report:Config.report_current

infer/src/backend/inferconfig.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ module OverridesMatcher = struct
178178
-> is_subtype mp.class_name
179179
&& Option.value_map ~f:(match_method language proc_name) ~default:false mp.method_name
180180
| _
181-
-> failwith "Expecting method pattern"
181+
-> L.(die UserError) "Expecting method pattern"
182182
in
183183
List.exists ~f:is_matching patterns
184184
end
@@ -229,7 +229,9 @@ let patterns_of_json_with_key (json_key, json) =
229229
| `String s
230230
-> s :: accu
231231
| _
232-
-> failwith ("Unrecognised parameters in " ^ Yojson.Basic.to_string (`Assoc assoc))
232+
-> L.(die UserError)
233+
"Unrecognised parameters in %s"
234+
(Yojson.Basic.to_string (`Assoc assoc))
233235
in
234236
List.rev (List.fold ~f:collect ~init:[] l)
235237
in
@@ -244,7 +246,7 @@ let patterns_of_json_with_key (json_key, json) =
244246
| key, _ when String.equal key "language"
245247
-> mp
246248
| _
247-
-> failwith ("Fails to parse " ^ Yojson.Basic.to_string (`Assoc assoc))
249+
-> L.(die UserError) "Failed to parse %s" (Yojson.Basic.to_string (`Assoc assoc))
248250
in
249251
List.fold ~f:loop ~init:default_method_pattern assoc
250252
and create_string_contains assoc =
@@ -254,7 +256,7 @@ let patterns_of_json_with_key (json_key, json) =
254256
| key, _ when String.equal key "language"
255257
-> sc
256258
| _
257-
-> failwith ("Fails to parse " ^ Yojson.Basic.to_string (`Assoc assoc))
259+
-> L.(die UserError) "Failed to parse %s" (Yojson.Basic.to_string (`Assoc assoc))
258260
in
259261
List.fold ~f:loop ~init:default_source_contains assoc
260262
in

0 commit comments

Comments
 (0)