Skip to content

Commit 0ab05e9

Browse files
committed
Compiler: control warnings. Allowing to mute certain warnings.
1 parent e6c8a9a commit 0ab05e9

File tree

37 files changed

+447
-214
lines changed

37 files changed

+447
-214
lines changed

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ type t =
8080
; effects : Config.effects_backend
8181
}
8282

83+
let set_param =
84+
let doc = "Set compiler options." in
85+
let all = List.map (Config.Param.all ()) ~f:(fun (x, _, _) -> x, x) in
86+
let pair = Arg.(pair ~sep:'=' (enum all) string) in
87+
let parser s =
88+
match Arg.conv_parser pair s with
89+
| Ok (k, v) -> (
90+
match
91+
List.find ~f:(fun (k', _, _) -> String.equal k k') (Config.Param.all ())
92+
with
93+
| _, _, valid -> (
94+
match valid v with
95+
| Ok () -> Ok (k, v)
96+
| Error msg -> Error (`Msg ("Unexpected VALUE after [=], " ^ msg))))
97+
| Error _ as e -> e
98+
in
99+
let printer = Arg.conv_printer pair in
100+
let c = Arg.conv (parser, printer) in
101+
Arg.(value & opt_all (list c) [] & info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
102+
83103
let wrap_with_fun_conv =
84104
let conv s =
85105
if String.equal s ""
@@ -180,14 +200,6 @@ let options =
180200
in
181201
Arg.(value & opt wrap_with_fun_conv `Iife & info [ "wrap-with-fun" ] ~doc)
182202
in
183-
let set_param =
184-
let doc = "Set compiler options." in
185-
let all = List.map (Config.Param.all ()) ~f:(fun (x, _) -> x, x) in
186-
Arg.(
187-
value
188-
& opt_all (list (pair ~sep:'=' (enum all) string)) []
189-
& info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
190-
in
191203
let set_env =
192204
let doc = "Set environment variable statically." in
193205
Arg.(
@@ -502,14 +514,6 @@ let options_runtime_only =
502514
in
503515
Arg.(value & opt wrap_with_fun_conv `Iife & info [ "wrap-with-fun" ] ~doc)
504516
in
505-
let set_param =
506-
let doc = "Set compiler options." in
507-
let all = List.map (Config.Param.all ()) ~f:(fun (x, _) -> x, x) in
508-
Arg.(
509-
value
510-
& opt_all (list (pair ~sep:'=' (enum all) string)) []
511-
& info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
512-
in
513517
let set_env =
514518
let doc = "Set environment variable statically." in
515519
Arg.(

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ let run
226226
let check_debug (one : Parse_bytecode.one) =
227227
if Option.is_some source_map && Parse_bytecode.Debug.is_empty one.debug
228228
then
229-
warn
230-
"Warning: '--source-map' is enabled but the bytecode program was compiled with \
231-
no debugging information.\n\
232-
Warning: Consider passing '-g' option to ocamlc.\n\
229+
Warning.warn
230+
`Missing_debug_event
231+
"'--source-map' is enabled but the bytecode program was compiled with no \
232+
debugging information.\n\
233+
Consider passing '-g' option to ocamlc.\n\
233234
%!"
234235
in
235236
let pseudo_fs_instr prim debug cmis =

compiler/bin-js_of_ocaml/js_of_ocaml.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open Js_of_ocaml_compiler
2323

2424
let () =
2525
Sys.catch_break true;
26-
let argv = Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv in
26+
let argv = Sys.argv in
2727
let argv =
2828
let like_arg x = String.length x > 0 && Char.equal x.[0] '-' in
2929
let like_command x =
@@ -59,11 +59,8 @@ let () =
5959
])
6060
with
6161
| Ok (`Ok () | `Help | `Version) ->
62-
if !warnings > 0 && !werror
63-
then (
64-
Format.eprintf "%s: all warnings being treated as errors@." Sys.argv.(0);
65-
exit 1)
66-
else exit 0
62+
Warning.process_warnings ();
63+
exit 0
6764
| Error `Term -> exit 1
6865
| Error `Parse -> exit Cmdliner.Cmd.Exit.cli_error
6966
| Error `Exn -> ()

compiler/bin-jsoo_minify/jsoo_minify.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ let main =
9292
Cmdliner.Cmd.v Cmd_arg.info t
9393

9494
let (_ : int) =
95-
try
96-
Cmdliner.Cmd.eval
97-
~catch:false
98-
~argv:(Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv)
99-
main
100-
with
95+
try Cmdliner.Cmd.eval ~catch:false ~argv:Sys.argv main with
10196
| (Match_failure _ | Assert_failure _ | Not_found) as exc ->
10297
let backtrace = Printexc.get_backtrace () in
10398
Format.eprintf

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ type t =
6666
; shape_files : string list
6767
}
6868

69+
let set_param =
70+
let doc = "Set compiler options." in
71+
let all = List.map (Config.Param.all ()) ~f:(fun (x, _, _) -> x, x) in
72+
let pair = Arg.(pair ~sep:'=' (enum all) string) in
73+
let parser s =
74+
match Arg.conv_parser pair s with
75+
| Ok (k, v) -> (
76+
match
77+
List.find ~f:(fun (k', _, _) -> String.equal k k') (Config.Param.all ())
78+
with
79+
| _, _, valid -> (
80+
match valid v with
81+
| Ok () -> Ok (k, v)
82+
| Error msg -> Error (`Msg ("Unexpected VALUE after [=], " ^ msg))))
83+
| Error _ as e -> e
84+
in
85+
let printer = Arg.conv_printer pair in
86+
let c = Arg.conv (parser, printer) in
87+
Arg.(value & opt_all (list c) [] & info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
88+
6989
let options () =
7090
let runtime_files =
7191
let doc = "Link JavaScript and WebAssembly files [$(docv)]. " in
@@ -110,14 +130,6 @@ let options () =
110130
let doc = "root dir for source map." in
111131
Arg.(value & opt (some string) None & info [ "source-map-root" ] ~doc)
112132
in
113-
let set_param =
114-
let doc = "Set compiler options." in
115-
let all = List.map (Config.Param.all ()) ~f:(fun (x, _) -> x, x) in
116-
Arg.(
117-
value
118-
& opt_all (list (pair ~sep:'=' (enum all) string)) []
119-
& info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
120-
in
121133
let include_dirs =
122134
let doc = "Add [$(docv)] to the list of include directories." in
123135
Arg.(value & opt_all string [] & info [ "I" ] ~docv:"DIR" ~doc)
@@ -232,14 +244,6 @@ let options_runtime_only () =
232244
let doc = "Add [$(docv)] to the list of include directories." in
233245
Arg.(value & opt_all string [] & info [ "I" ] ~docv:"DIR" ~doc)
234246
in
235-
let set_param =
236-
let doc = "Set compiler options." in
237-
let all = List.map (Config.Param.all ()) ~f:(fun (x, _) -> x, x) in
238-
Arg.(
239-
value
240-
& opt_all (list (pair ~sep:'=' (enum all) string)) []
241-
& info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
242-
in
243247
let effects =
244248
let doc =
245249
"Select an implementation of effect handlers. [$(docv)] should be one of $(b,jspi) \

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,10 @@ let run
414414
&& Parse_bytecode.Debug.is_empty one.debug
415415
&& not (Code.is_empty one.code)
416416
then
417-
warn
418-
"Warning: '--source-map' is enabled but the bytecode program was compiled with \
419-
no debugging information.\n\
417+
Warning.warn
418+
`Missing_debug_event
419+
"'--source-map' is enabled but the bytecode program was compiled with no \
420+
debugging information.\n\
420421
Warning: Consider passing '-g' option to ocamlc.\n\
421422
%!"
422423
in

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let check_js_file fname =
1717
let freenames = StringSet.diff freenames Reserved.provided in
1818
if not (StringSet.is_empty freenames)
1919
then (
20-
Format.eprintf "warning: free variables in %S@." fname;
20+
Format.eprintf "Warning: free variables in %S@." fname;
2121
Format.eprintf "vars: %s@." (String.concat ~sep:", " (StringSet.elements freenames));
2222
exit 2);
2323
()

compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open Js_of_ocaml_compiler
2121

2222
let () =
2323
Sys.catch_break true;
24-
let argv = Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv in
24+
let argv = Sys.argv in
2525
let argv =
2626
let like_arg x = String.length x > 0 && Char.equal x.[0] '-' in
2727
let like_command x =
@@ -57,11 +57,8 @@ let () =
5757
])
5858
with
5959
| Ok (`Ok () | `Help | `Version) ->
60-
if !warnings > 0 && !werror
61-
then (
62-
Format.eprintf "%s: all warnings being treated as errors@." Sys.argv.(0);
63-
exit 1)
64-
else exit 0
60+
Warning.process_warnings ();
61+
exit 0
6562
| Error `Term -> exit 1
6663
| Error `Parse -> exit Cmdliner.Cmd.Exit.cli_error
6764
| Error `Exn -> ()

compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
open Js_of_ocaml_compiler.Stdlib
2121

2222
let (_ : int) =
23-
try
24-
Cmdliner.Cmd.eval
25-
~catch:false
26-
~argv:(Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv)
27-
Link_wasm.command
28-
with
23+
try Cmdliner.Cmd.eval ~catch:false ~argv:Sys.argv Link_wasm.command with
2924
| (Match_failure _ | Assert_failure _ | Not_found) as exc ->
3025
let backtrace = Printexc.get_backtrace () in
3126
Format.eprintf

compiler/lib-cmdline/arg.ml

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type t =
3131
; optim : string list on_off
3232
; quiet : bool
3333
; werror : bool
34+
; warnings : (bool * Warning.t) list
3435
; custom_header : string option
3536
}
3637

@@ -61,6 +62,35 @@ let disable =
6162
in
6263
Term.(const List.flatten $ arg))
6364

65+
let parse_warning s =
66+
let err s = `Msg (Printf.sprintf "Unknown warning %s" s) in
67+
if String.is_empty s
68+
then Error (err s)
69+
else
70+
match Warning.parse s with
71+
| Some n -> Ok (true, n)
72+
| None -> (
73+
match String.drop_prefix ~prefix:"no-" s with
74+
| Some n -> (
75+
match Warning.parse n with
76+
| Some n -> Ok (false, n)
77+
| None -> Error (err n))
78+
| None -> Error (err s))
79+
80+
let print_warning fmt (b, w) =
81+
Format.fprintf
82+
fmt
83+
"%s%s"
84+
(match b with
85+
| true -> ""
86+
| false -> "")
87+
(Warning.name w)
88+
89+
let warnings : (bool * Warning.t) list Term.t =
90+
let doc = "Enable or disable the warnings specified by the argument [$(docv)]." in
91+
let c : 'a Arg.conv = Arg.conv ~docv:"" (parse_warning, print_warning) in
92+
Arg.(value & opt_all c [] & info [ "w" ] ~docv:"WARN" ~doc)
93+
6494
let pretty =
6595
let doc = "Pretty print the output." in
6696
Arg.(value & flag & info [ "pretty" ] ~doc)
@@ -91,7 +121,19 @@ let custom_header =
91121
let t =
92122
lazy
93123
Term.(
94-
const (fun debug enable disable pretty debuginfo noinline quiet werror c_header ->
124+
const
125+
(fun
126+
debug
127+
enable
128+
disable
129+
pretty
130+
debuginfo
131+
noinline
132+
quiet
133+
(warnings : (bool * Warning.t) list)
134+
werror
135+
c_header
136+
->
95137
let enable = if pretty then "pretty" :: enable else enable in
96138
let enable = if debuginfo then "debuginfo" :: enable else enable in
97139
let disable = if noinline then "inline" :: disable else disable in
@@ -104,6 +146,7 @@ let t =
104146
let disable = disable_if_pretty "share" disable in
105147
{ debug = { enable = debug; disable = [] }
106148
; optim = { enable; disable }
149+
; warnings
107150
; quiet
108151
; werror
109152
; custom_header = c_header
@@ -115,6 +158,7 @@ let t =
115158
$ debuginfo
116159
$ noinline
117160
$ is_quiet
161+
$ warnings
118162
$ is_werror
119163
$ custom_header)
120164

@@ -125,5 +169,8 @@ let on_off on off t =
125169
let eval t =
126170
Config.Flag.(on_off enable disable t.optim);
127171
Debug.(on_off enable disable t.debug);
128-
quiet := t.quiet;
129-
werror := t.werror
172+
List.iter t.warnings ~f:(function
173+
| true, w -> Warning.enable w
174+
| false, w -> Warning.disable w);
175+
Warning.quiet := t.quiet;
176+
Warning.werror := t.werror

0 commit comments

Comments
 (0)