Skip to content

Commit 771f79d

Browse files
authored
Revised handling of unknown warnings (#554)
`-W<some unknown warning>` is now (by default) a warning (not a fatal error) and `-Wno-<some unknown warning>` is ignored. This is what GCC and Clang do. This commit also introduces the `unknown-warning-option` warning, so that warnings about unknown warnings can be ignored (`-Wno-unknown-warning-option`) or turned into an error (`-Werror=unknown-warning-option`). This is what Clang does. Closes: #552
1 parent a3f52d4 commit 771f79d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

cparser/Diagnostics.ml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ let reset () = num_errors := 0; num_warnings := 0
5454

5555
exception Abort
5656

57+
(* Locations are (filename, line number) pairs *)
58+
59+
let no_loc = ("", -1)
60+
61+
let file_loc file = (file,-10)
62+
5763
(* [fatal_error_raw] is identical to [fatal_error], except it uses [Printf]
5864
to print its message, as opposed to [Format], and does not automatically
5965
introduce indentation and a final dot into the message. This is useful
@@ -106,6 +112,7 @@ type warning_type =
106112
| Non_linear_cond_expr
107113
| Invalid_UTF8
108114
| Dollar_in_identifier
115+
| Unknown_warning
109116

110117
(* List of all warnings with default status.
111118
"true" means the warning is active by default.
@@ -143,7 +150,8 @@ let all_warnings =
143150
(Reduced_alignment, false);
144151
(Non_linear_cond_expr, false);
145152
(Invalid_UTF8, true);
146-
(Dollar_in_identifier, false)
153+
(Dollar_in_identifier, false);
154+
(Unknown_warning, true)
147155
]
148156

149157
(* List of active warnings *)
@@ -188,6 +196,7 @@ let string_of_warning = function
188196
| Non_linear_cond_expr -> "non-linear-cond-expr"
189197
| Invalid_UTF8 -> "invalid-utf8"
190198
| Dollar_in_identifier -> "dollar-in-identifier-extension"
199+
| Unknown_warning -> "unknown-warning-option"
191200

192201
(* Activate the given warning *)
193202
let activate_warning w () =
@@ -363,13 +372,15 @@ let check_errors () =
363372
raise Abort
364373
end
365374

375+
let unknown_warning w =
376+
warning no_loc Unknown_warning "Unknown warning option %s, ignored." w
366377

367378
let error_option w =
368379
let key = string_of_warning w in
369380
[Exact ("-W"^key), Unit (activate_warning w);
370381
Exact ("-Wno-"^key), Unit (deactivate_warning w);
371382
Exact ("-Werror="^key), Unit (warning_as_error w);
372-
Exact ("-Wno-error="^key), Unit ( warning_not_as_error w)]
383+
Exact ("-Wno-error="^key), Unit (warning_not_as_error w)]
373384

374385
let warning_options =
375386
List.concat (List.map (fun (w, active) -> error_option w) all_warnings) @
@@ -379,6 +390,8 @@ let warning_options =
379390
Exact ("-Werror"), Unit werror;
380391
Exact ("-Wall"), Unit wall;
381392
Exact ("-w"), Unit wnothing;
393+
_Regexp("-Wno-.*$"), Ignore;
394+
_Regexp("-W.*$"), Self unknown_warning;
382395
longopt_int ("-fmax-errors") ((:=) max_error);
383396
Exact("-fno-diagnostics-show-option"),Unset diagnostics_show_option;
384397
Exact("-fdiagnostics-show-option"),Set diagnostics_show_option;
@@ -425,9 +438,5 @@ let crash exn =
425438
exit 2
426439
end
427440

428-
let no_loc = ("", -1)
429-
430-
let file_loc file = (file,-10)
431-
432441
let active_warning ty =
433442
fst (classify_warning ty) <> SuppressedMsg

cparser/Diagnostics.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type warning_type =
5959
| Non_linear_cond_expr (** condition that cannot be linearized *)
6060
| Invalid_UTF8 (** invalid UTF-8 encoding *)
6161
| Dollar_in_identifier (** '$' sign in identifier *)
62+
| Unknown_warning (** unknown warning in '-W' option *)
6263

6364
val warning : (string * int) -> warning_type -> ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
6465
(** [warning (f,c) w fmt arg1 ... argN] formats the arguments [arg1] to [argN] as warining according to

0 commit comments

Comments
 (0)