Debian Codegraph @

lib/cWarnings.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 Util
12
13 type status =
14 Disabled | Enabled | AsError
15
16 type warning =
17 { wname : string;
18 wparents : category list;
19 pp : Pp.t
20 ; default : status
21 ; hybrid : bool
22 (* also counts as a category *)
23 ; mutable current : status
24 }
25
26 and category = {
27 name : string;
28 parents : category list;
29 warning : warning option;
30 }
31
32 let category_of_warning w = {
33 name = w.wname;
34 parents = w.wparents;
35 warning = Some w;
36 }
37
38 (* Some values of type [category] should not be exposed as such in the interface. *)
39 let is_category x = match x.warning with
40 | None -> true
41 | Some {hybrid} -> hybrid
42
43 let graph : category String.Map.t ref = ref String.Map.empty
44
45 let revgraph : category list String.Map.t ref = ref String.Map.empty
46 (** Map from categories to the warnings and categories they contain, including transitively. *)
47
48 type 'a elt =
49 | There of 'a
50 | NotThere
51 | OtherType
52
53 let get_category name =
54 match String.Map.find_opt name !graph with
55 | None -> NotThere
56 | Some v ->
57 if is_category v then There v
58 else OtherType
59
60 let get_warning name =
61 match String.Map.find_opt name !graph with
62 | None -> NotThere
63 | Some v -> match v.warning with
64 | None -> OtherType
65 | Some v -> There v
66
67 let warning_status w = w.current
68
69 type _ tag = ..
70
71 type w = W : Loc.t option * 'a tag * 'a -> w
72 exception WarnError of w
73
74 type 'a quickfix = loc:Loc.t -> 'a -> Quickfix.t list
75 (* XXX accept optional loc? In principle we could have a loc in the
76 ['a] while the warning as a whole is unlocated but not clear if
77 that ever really happens. *)
78
79 module DMap = PolyMap.Make (struct type nonrec 'a tag = 'a tag = .. end)
80 module PrintMap = DMap.Map(struct type 'a t = 'a -> Pp.t end)
81 module QFMap = DMap.Map(struct type 'a t = 'a quickfix list end)
82
83 module Msg = struct
84 type 'a t = {
85 tag : 'a DMap.onetag;
86 warning : warning;
87 }
88 end
89 type 'a msg = 'a Msg.t
90
91 let printers = ref PrintMap.empty
92 let quickfixes = ref QFMap.empty
93
94 let wrap_pp w pp =
95 let open Pp in
96 fun x -> pp x ++ spc () ++ str "[" ++ w.pp ++ str "]"
97
98 let register_printer { Msg.tag; warning} pp =
99 let pp x = wrap_pp warning pp x in
100 printers := PrintMap.add tag pp !printers
101
102 let register_quickfix { Msg.tag } qf =
103 quickfixes := QFMap.update tag (fun v -> Some (qf :: Option.default [] v)) !quickfixes
104
105 let create_msg warning () =
106 let v = { Msg.tag = DMap.make(); warning; } in
107 v
108
109 let print (W (_loc, tag, w)) =
110 let pp = try PrintMap.find tag !printers with Not_found -> assert false in
111 pp w
112
113 let quickfix (W (loc, tag, w)) =
114 match loc with
115 | None -> []
116 | Some loc ->
117 let pp = try QFMap.find tag !quickfixes with Not_found -> [] in
118 List.concat_map (fun f -> f ~loc w) pp
119
120 let () = CErrors.register_handler (function
121 | WarnError w -> Some (print w)
122 | _ -> None)
123
124 let () = Quickfix.register (function
125 | WarnError w -> quickfix w
126 | _ -> [])
127
128 let warn { Msg.tag; warning } ?loc v =
129 let tag = DMap.tag_of_onetag tag in
130 let w = W (loc,tag,v) in
131 match warning_status warning with
132 | Disabled -> ()
133 | AsError -> Loc.raise ?loc (WarnError w)
134 | Enabled -> Feedback.msg_warning ?loc ~quickfix:(quickfix w) (print w)
135
136 (** Flag handling *)
137
138 let flags = ref ""
139
140 let get_flags () = !flags
141
142 let iter_warnings f =
143 !graph |> String.Map.iter (fun _ w -> Option.iter f w.warning)
144
145 let reset_default_warnings () =
146 iter_warnings (fun w -> w.current <- w.default)
147
148 let set_all_warnings_status status =
149 iter_warnings (fun w -> w.current <- status)
150
151 type flag =
152 | All
153 | Other of string
154
155 let is_none_keyword s = CString.equal s "none"
156
157 let parse_flag s =
158 if String.length s > 1 then
159 let status, name = match String.get s 0 with
160 | '+' -> (AsError, String.sub s 1 (String.length s - 1))
161 | '-' -> (Disabled, String.sub s 1 (String.length s - 1))
162 | _ -> (Enabled, s)
163 in
164 match name with
165 | "all" -> status, All
166 | "none" ->
167 if status = Enabled then Disabled, All
168 else CErrors.user_err
169 Pp.(str "Invalid warnings flag: \"none\" used with " ++ str (String.make 1 s.[0]))
170 | _ -> status, Other name
171 else CErrors.user_err Pp.(str "Invalid warnings flag")
172
173 let string_of_flag (status,name) =
174 let name = match name with
175 | All -> "all"
176 | Other s -> s
177 in
178 match status with
179 | AsError -> "+" ^ name
180 | Disabled -> "-" ^ name
181 | Enabled -> name
182
183 let string_of_flags flags =
184 String.concat "," (List.map string_of_flag flags)
185
186 let set_status ~name status = match name with
187 | All ->
188 set_all_warnings_status status
189 | Other name ->
190 begin match String.Map.find_opt name !graph with
191 | None ->
192 (* unknown warning or category *)
193 ()
194 | Some elt ->
195 let () = Option.iter (fun w -> w.current <- status) elt.warning in
196 let () =
197 if is_category elt then begin
198 let contents = String.Map.get name !revgraph in
199 List.iter (fun elt -> Option.iter (fun w -> w.current <- status) elt.warning) contents
200 end
201 in
202 ()
203 end
204
205 let split_flags s =
206 let reg = Str.regexp "[ ,]+" in Str.split reg s
207
208 (** [cut_before_all_rev] removes all flags subsumed by a later occurrence of the
209 "all" flag, and reverses the list. *)
210 let rec cut_before_all_rev acc = function
211 | [] -> acc
212 | (_, All as w) :: warnings -> cut_before_all_rev [w] warnings
213 | w :: warnings -> cut_before_all_rev (w::acc) warnings
214
215 let cut_before_all_rev warnings = cut_before_all_rev [] warnings
216
217 (** [uniquize_flags_rev] removes flags that are subsumed by later occurrences of
218 themselves or their categories, and reverses the list. *)
219 let uniquize_flags_rev flags =
220 let rec aux acc visited = function
221 | (_, All as flag) :: flags ->
222 assert (List.is_empty flags);
223 flag :: acc
224
225 | (_,Other name as flag)::flags ->
226 if CString.Set.mem name visited then aux acc visited flags else
227 let visited =
228 match String.Map.find_opt name !revgraph with
229 | Some contents ->
230 List.fold_left (fun v w ->
231 CString.Set.add w.name v)
232 visited contents
233 | None ->
234 (* Maybe a known non-category warning, maybe an unknown warning or category *)
235 visited
236 in
237 aux (flag::acc) (CString.Set.add name visited) flags
238 | [] -> acc
239 in
240 aux [] CString.Set.empty flags
241
242 (** [normalize_flags] removes redundant warnings. Unknown warnings are kept
243 because they may be declared in a plugin that will be linked later. *)
244 let normalize_flags warnings =
245 let warnings = cut_before_all_rev warnings in
246 uniquize_flags_rev warnings
247
248 let flags_of_string s = List.map parse_flag (split_flags s)
249
250 let normalize_flags_string s =
251 if is_none_keyword s then s
252 else
253 let flags = flags_of_string s in
254 let flags = normalize_flags flags in
255 string_of_flags flags
256
257 let parse_warnings items =
258 CList.iter (fun (status, name) -> set_status ~name status) items
259
260 (* For compatibility, we accept "none" *)
261 let parse_flags s =
262 if is_none_keyword s then begin
263 set_all_warnings_status Disabled;
264 "none"
265 end
266 else begin
267 let flags = flags_of_string s in
268 let flags = normalize_flags flags in
269 parse_warnings flags;
270 string_of_flags flags
271 end
272
273 let set_flags s =
274 reset_default_warnings (); let s = parse_flags s in flags := s
275
276 (* Remark: [warn] does not need to start with a comma, but if present
277 it won't hurt (",," is normalized into ","). *)
278 let with_warn warn (f:'b -> 'a) x =
279 let s = get_flags () in
280 Util.try_finally (fun x -> set_flags (s^","^warn);f x) x set_flags s
281
282 let is_used_name name =
283 String.equal name "none" ||
284 String.Map.mem name !graph
285
286 let check_fresh_name name =
287 if is_used_name name then
288 CErrors.anomaly Pp.(str "Already used warning name " ++ str name ++ str ".")
289
290 let rec add_in_categories_rec v visited {name; parents} =
291 if String.Set.mem name visited then visited
292 else
293 let visited = String.Set.add name visited in
294 revgraph := String.Map.update name (function
295 | None -> assert false
296 | Some l -> Some (v :: l))
297 !revgraph;
298 List.fold_left (add_in_categories_rec v) visited parents
299
300 let add_in_categories v cats =
301 let _ : String.Set.t =
302 List.fold_left (add_in_categories_rec v) String.Set.empty cats
303 in
304 ()
305
306 let check_cat_list cats =
307 assert (not (List.is_empty cats));
308 assert (not (List.exists (fun cat -> String.equal cat.name "default") cats))
309
310 (* We could re-normalize flags but it only matters if a warning will
311 be added to the new category so we just normalize at that time. *)
312 let create_category ~from ~name () =
313 let v = {
314 name = name;
315 parents = from;
316 warning = None;
317 }
318 in
319 graph := String.Map.add name v !graph;
320 revgraph := String.Map.add name [] !revgraph;
321 add_in_categories v from;
322 v
323
324 let all = create_category ~from:[] ~name:"all" ()
325
326 let default_cat = create_category ~from:[all] ~name:"default" ()
327
328 let create_category ?(from=[all]) ~name () =
329 check_fresh_name name;
330 check_cat_list from;
331 create_category ~from ~name ()
332
333 let add_pp name pp =
334 let open Pp in
335 if ismt pp then str name
336 else str name ++ str "," ++ pp
337
338 let rec make_cat_pp visited suffix cat =
339 let name = cat.name in
340 if String.Set.mem name visited then visited, suffix
341 else
342 let visited = String.Set.add name visited in
343 if String.equal cat.name "all" then
344 visited, suffix
345 else
346 let visited, pp = make_cats_pp visited suffix cat.parents in
347 visited, add_pp name pp
348
349 and make_cats_pp visited suffix cats =
350 List.fold_left (fun (visited,pp) cat -> make_cat_pp visited pp cat)
351 (visited, suffix)
352 cats
353
354 let make_warning_pp ~from name =
355 let _, pp = make_cats_pp String.Set.empty (Pp.mt()) from in
356 add_pp name pp
357
358 (* Adds a warning to the [warnings] and [categories] tables. We then reparse the
359 warning flags string, because the warning being created might have been set
360 already. *)
361 let create_gen ?(from=[all]) ?(default=Enabled) ~name ~hybrid () =
362 check_fresh_name name;
363 check_cat_list from;
364 let from = if default <> Disabled then default_cat :: from else from in
365 let pp = make_warning_pp ~from name in
366 let w = {
367 wname = name;
368 default = default;
369 wparents = from;
370 current = default;
371 hybrid;
372 pp;
373 }
374 in
375 let elt = category_of_warning w in
376 let () = graph := String.Map.add name elt !graph in
377 let () = if hybrid then revgraph := String.Map.add name [] !revgraph in
378 add_in_categories elt from;
379 (* We re-parse and also re-normalize the flags, because the category of the
380 new warning is now known. *)
381 set_flags !flags;
382 elt, w
383
384 let create_warning ?from ?default ~name () =
385 let _, w = create_gen ?from ?default ~name ~hybrid:false () in
386 w
387
388 let create_hybrid ?from ?default ~name () =
389 create_gen ?from ?default ~name ~hybrid:true ()
390
391 let create_in w ?quickfix pp =
392 let msg = create_msg w () in
393 let () = register_printer msg pp in
394 let () = quickfix |> Option.iter (register_quickfix msg) in
395 fun ?loc x -> warn ?loc msg x
396
397 let create ~name ?category ?default ?quickfix pp =
398 let from = Option.map (fun x -> [x]) category in
399 let w = create_warning ?from ?default ~name () in
400 create_in w ?quickfix pp
401
402 let warn_unknown_warnings = create ~name:"unknown-warning" Pp.(fun flags ->
403 str "Could not enable unknown " ++
404 str (CString.plural (List.length flags) "warning") ++ spc() ++
405 prlist_with_sep spc str flags)
406
407 let override_unknown_warning = ref false
408
409 let warn_unknown_warnings ?loc flags =
410 if not !override_unknown_warning then warn_unknown_warnings ?loc flags
411
412 let check_unknown_warnings flags =
413 let flags = flags_of_string flags in
414 let flags = List.filter_map (function
415 | Disabled, _ | _, All -> None
416 | (Enabled|AsError), Other name ->
417 if String.Map.mem name !graph then None
418 else Some name)
419 flags
420 in
421 if not (List.is_empty flags) then
422 warn_unknown_warnings flags
423
424 module CoreCategories = struct
425
426 let make name = create_category ~name ()
427
428 (* Update the list of core categories in
429 doc/sphinx/proof-engine/vernacular-commands.rst
430 when adding a new category here. *)
431
432 let automation = make "automation"
433 let bytecode_compiler = make "bytecode-compiler"
434 let coercions = make "coercions"
435 let deprecated = make "deprecated"
436 let extraction = make "extraction"
437 let filesystem = make "filesystem"
438 let fixpoints = make "fixpoints"
439 let fragile = make "fragile"
440 let funind = make "funind"
441 let implicits = make "implicits"
442 let ltac = make "ltac"
443 let ltac2 = make "ltac2"
444 let native_compiler = make "native-compiler"
445 let numbers = make "numbers"
446 let parsing = make "parsing"
447 let pedantic = make "pedantic"
448 let records = make "records"
449 let rewrite_rules = make "rewrite-rules"
450 let ssr = make "ssr"
451 let syntax = make "syntax"
452 let tactics = make "tactics"
453 let user_warn = make "user-warn"
454 let vernacular = make "vernacular"
455 let internal = make "internal"
456
457 end
458
459 let warn_ignored_coqlib = create ~name:"boot-ignored-coqlib" ~category:CoreCategories.fragile
460 Pp.(fun () -> str Boot.Env.ignored_coqlib_msg)
461
462 (* loc doesn't make sense for this warning and gets in the way *)
463 let warn_ignored_coqlib () = warn_ignored_coqlib ()