Debian Codegraph @

lib/cErrors.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Pp
12
13 (* Errors *)
14
15 exception Anomaly of string option * Pp.t (* System errors *)
16
17 let _ =
18 let pr = function
19 | Anomaly (s, pp) -> Some ("\"Anomaly: " ^ string_of_ppcmds pp ^ "\"")
20 | _ -> None
21 in
22 Printexc.register_printer pr
23
24 let anomaly ?loc ?info ?label pp =
25 let info = Option.default Exninfo.null info in
26 let info = Option.cata (Loc.add_loc info) info loc in
27 Exninfo.iraise (Anomaly (label, pp), info)
28
29 exception UserError of Pp.t (* User errors *)
30
31 (* We register Rocq Errors with the global printer here as they will be
32 printed if Dynlink.loadfile raises UserError in the module
33 intializers, and in some other cases. We should make this more
34 principled. *)
35 let _ = Printexc.register_printer (function
36 | UserError msg ->
37 Some (Format.asprintf "@[Rocq Error: %a@]" Pp.pp_with msg)
38 | _ -> None)
39
40 let user_err ?loc ?info strm =
41 let info = Option.default Exninfo.null info in
42 let info = Option.cata (Loc.add_loc info) info loc in
43 Exninfo.iraise (UserError strm, info)
44
45 (** Only anomalies should reach the bottom of the handler stack.
46 In usual situation, the [handle_stack] is treated as it if was always
47 non-empty with [print_anomaly] as its bottom handler. *)
48
49 let where = function
50 | None -> mt ()
51 | Some s ->
52 str "in " ++ str s ++ str ":" ++ spc ()
53
54 let raw_anomaly e = match e with
55 | Anomaly (s, pps) ->
56 where s ++ pps
57 | Assert_failure _ | Match_failure _ ->
58 str (Printexc.to_string e) ++ str "."
59 | _ ->
60 str "Uncaught exception " ++ str (Printexc.to_string e) ++ str "."
61
62 let print_backtrace e = match Exninfo.get_backtrace e with
63 | None -> None
64 | Some bt ->
65 let bt = str (Exninfo.backtrace_to_string bt) in
66 Some (hov 0 bt)
67
68 let print_anomaly askreport e =
69 if askreport then
70 hov 0 (str "Anomaly" ++ spc () ++ quote (raw_anomaly e)) ++ spc () ++
71 hov 0 (str "Please report at " ++ str Coq_config.wwwbugtracker ++ str ".")
72 else
73 hov 0 (raw_anomaly e)
74
75 let handle_stack = ref []
76
77 let register_handler h = handle_stack := h::!handle_stack
78
79 let is_handled e =
80 let is_handled_by h = Option.has_some (h e) in
81 match e with
82 | Anomaly _ -> false (* anomaly is considered unhandled *)
83 | _ -> List.exists is_handled_by !handle_stack
84
85 let is_async = function
86 | Control.Timeout | Sys.Break | Out_of_memory | Stack_overflow -> true
87 | e -> Memprof_coq.is_interrupted()
88
89 let exit_code e = if is_handled e then 1 else 129
90
91 let is_sync_anomaly e = not (is_async e) && not (is_handled e)
92
93 (** Printing of additional error info, from Exninfo *)
94 let additional_error_info_handler = ref []
95
96 let register_additional_error_info (f : Exninfo.info -> Pp.t option) =
97 additional_error_info_handler := f :: !additional_error_info_handler
98
99 let () = register_additional_error_info print_backtrace
100
101 (** [print_gen] is a general exception printer which tries successively
102 all the handlers of a list, and finally a [bottom] handler if all
103 others have failed *)
104
105 let rec print_gen ~anomaly stk e =
106 match stk with
107 | [] ->
108 print_anomaly anomaly e
109 | h::stk' ->
110 match h e with
111 | Some err_msg -> err_msg
112 | None -> print_gen ~anomaly stk' e
113
114 let print_extra info =
115 let pp =
116 CList.map_filter (fun f -> f info) !additional_error_info_handler
117 in
118 if CList.is_empty pp then mt()
119 else fnl() ++ prlist_with_sep fnl (fun x -> x) pp
120
121 let print_gen ~anomaly (e, info) =
122 let extra_msg = print_extra info in
123 try
124 print_gen ~anomaly !handle_stack e ++ extra_msg
125 with exn ->
126 let exn, info = Exninfo.capture exn in
127 (* exception in error printer *)
128 str "<in exception printer>:" ++ spc() ++ print_anomaly anomaly exn ++ print_extra info ++ fnl() ++
129 str "<original exception>:" ++ spc() ++ print_anomaly false e ++ extra_msg
130
131 let print_quickfix = function
132 | [] -> mt ()
133 | qf -> fnl () ++ Quickfix.print qf
134
135 (** The standard exception printer *)
136 let iprint (e, info) =
137 print_gen ~anomaly:true (e,info) ++
138 if !Flags.test_mode then
139 Result.fold ~ok:print_quickfix ~error:(print_gen ~anomaly:true) (Quickfix.from_exception e)
140 else
141 mt ()
142
143 let print e =
144 iprint (e, Exninfo.info e)
145
146 (** Same as [print], except that the "Please report" part of an anomaly
147 isn't printed (used in Ltac debugging). *)
148 let iprint_no_report (e, info) =
149 print_gen ~anomaly:false (e,info)
150 let print_no_report e = iprint_no_report (e, Exninfo.info e)
151
152 (** Predefined handlers **)
153
154 let _ = register_handler begin function
155 | UserError pps ->
156 Some pps
157 | _ -> None
158 end
159
160 (** Critical exceptions should not be caught and ignored by mistake
161 by inner functions during a [vernacinterp]. They should be handled
162 only at the very end of interp, to be displayed to the user. *)
163
164 [@@@ocaml.warning "-52"]
165 let noncritical = function
166 | Sys.Break | Out_of_memory | Stack_overflow
167 | Assert_failure _ | Match_failure _ | Anomaly _
168 | Control.Timeout -> false
169 | Invalid_argument "equal: functional value" -> false
170 | _ -> not (Memprof_coq.is_interrupted ())
171 [@@@ocaml.warning "+52"]
172
173 (* This should be in exninfo, but noncritical is here... *)
174 let to_result ~f x =
175 try Ok (f x)
176 with exn when noncritical exn ->
177 let iexn = Exninfo.capture exn in
178 Error iexn