summaryrefslogtreecommitdiff
path: root/compiler/Errors.ml
blob: 6e2de7e13d28daf1a90f00d7b06fd5ca65aed647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
let log = Logging.errors_log

let span_to_string (span : Meta.span) =
  let raw_span = span.span in
  let file = match raw_span.file with Virtual s | Local s -> s in
  let loc_to_string (l : Meta.loc) : string =
    string_of_int l.line ^ ":" ^ string_of_int l.col
  in
  "Source: '" ^ file ^ "', lines "
  ^ loc_to_string raw_span.beg_loc
  ^ "-"
  ^ loc_to_string raw_span.end_loc

let format_error_message (span : Meta.span option) (msg : string) =
  let span =
    match span with None -> "" | Some span -> "\n" ^ span_to_string span
  in
  msg ^ span

let format_error_message_with_file_line (file : string) (line : int)
    (span : Meta.span option) (msg : string) =
  "In file " ^ file ^ ", line " ^ string_of_int line ^ ":\n"
  ^ format_error_message span msg

exception CFailure of (Meta.span option * string)

let error_list : (Meta.span option * string) list ref = ref []

let push_error (span : Meta.span option) (msg : string) =
  error_list := (span, msg) :: !error_list

(** Register an error, and throw an exception if [throw] is true *)
let save_error (file : string) (line : int) ?(throw : bool = false)
    (span : Meta.span option) (msg : string) =
  push_error span msg;
  if !Config.fail_hard && throw then (
    let msg = format_error_message_with_file_line file line span msg in
    log#serror (msg ^ "\n");
    raise (Failure msg))

let craise_opt_span (file : string) (line : int) (span : Meta.span option)
    (msg : string) =
  if !Config.fail_hard then (
    let msg = format_error_message_with_file_line file line span msg in
    log#serror (msg ^ "\n");
    raise (Failure (format_error_message_with_file_line file line span msg)))
  else
    let () = push_error span msg in
    raise (CFailure (span, msg))

let craise (file : string) (line : int) (span : Meta.span) (msg : string) =
  craise_opt_span file line (Some span) msg

let cassert_opt_span (file : string) (line : int) (b : bool)
    (span : Meta.span option) (msg : string) =
  if not b then craise_opt_span file line span msg

let cassert (file : string) (line : int) (b : bool) (span : Meta.span)
    (msg : string) =
  cassert_opt_span file line b (Some span) msg

let sanity_check (file : string) (line : int) b span =
  cassert file line b span "Internal error, please file an issue"

let sanity_check_opt_span (file : string) (line : int) b span =
  cassert_opt_span file line b span "Internal error, please file an issue"

let internal_error (file : string) (line : int) span =
  craise file line span "Internal error, please file an issue"

let exec_raise = craise
let exec_assert = cassert