Skip to content

Commit 96ce177

Browse files
authored
Compiler: new -Werror flag to turn wanrings into errors (#1222)
* Compiler: new -Werror flag to turn wanrings into errors * Changes * Fix fmt
1 parent 6589602 commit 96ce177

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Compiler: be more consistent dealing with js vs ocaml strings (#984)
99
* Compiler: Compiler: add BigInt to provided symbols (fix #1168) (#1191)
1010
* Compiler: use globalThis, drop joo_global_object #1193
11+
* Compiler: new -Werror flag to turn wanrings into errors (#1222)
1112
* Lib: add messageEvent to Dom_html (#1164)
1213
* Lib: add PerformanceObserver API (#1164)
1314
* Lib: add CSSStyleDeclaration.{setProperty, getPropertyValue, getPropertyPriority, removeProperty} (#1170)

compiler/bin-js_of_ocaml/js_of_ocaml.ml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,29 @@ let _ =
4545
| _ -> argv
4646
in
4747
try
48-
Cmdliner.Term.eval_choice
49-
~catch:false
50-
~argv
51-
Compile.command_main
52-
[ Link.command
53-
; Build_fs.command
54-
; Build_runtime.command
55-
; Print_runtime.command
56-
; Check_runtime.command
57-
; Compile.command
58-
]
48+
match
49+
Cmdliner.Term.eval_choice
50+
~catch:false
51+
~argv
52+
Compile.command_main
53+
[ Link.command
54+
; Build_fs.command
55+
; Build_runtime.command
56+
; Print_runtime.command
57+
; Check_runtime.command
58+
; Compile.command
59+
]
60+
with
61+
| `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
67+
| `Error `Term -> exit 1
68+
| `Error `Parse -> exit 124
69+
| `Error `Exn -> ()
70+
(* should not happen *)
5971
with
6072
| (Match_failure _ | Assert_failure _ | Not_found) as exc ->
6173
let backtrace = Printexc.get_backtrace () in
@@ -65,7 +77,7 @@ let _ =
6577
Sys.argv.(0);
6678
Format.eprintf "Error: %s@." (Printexc.to_string exc);
6779
prerr_string backtrace;
68-
exit 1
80+
exit 125
6981
| Magic_number.Bad_magic_number s ->
7082
Format.eprintf "%s: Error: Not an ocaml bytecode file@." Sys.argv.(0);
7183
Format.eprintf "%s: Error: Invalid magic number %S@." Sys.argv.(0) s;

compiler/lib-cmdline/arg.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type t =
3030
{ debug : string list on_off
3131
; optim : string list on_off
3232
; quiet : bool
33+
; werror : bool
3334
; custom_header : string option
3435
}
3536

@@ -73,6 +74,10 @@ let is_quiet =
7374
let doc = "suppress non-error messages." in
7475
Arg.(value & flag & info [ "quiet"; "q" ] ~doc)
7576

77+
let is_werror =
78+
let doc = "turn all warnings into errors." in
79+
Arg.(value & flag & info [ "Werror" ] ~doc)
80+
7681
let custom_header =
7782
let doc =
7883
"Provide a custom header for the generated JavaScript file, useful for making the \
@@ -82,7 +87,7 @@ let custom_header =
8287

8388
let t =
8489
Term.(
85-
pure (fun debug enable disable pretty debuginfo noinline quiet c_header ->
90+
pure (fun debug enable disable pretty debuginfo noinline quiet werror c_header ->
8691
let enable = if pretty then "pretty" :: enable else enable in
8792
let enable = if debuginfo then "debuginfo" :: enable else enable in
8893
let disable = if noinline then "inline" :: disable else disable in
@@ -94,6 +99,7 @@ let t =
9499
{ debug = { enable = debug; disable = [] }
95100
; optim = { enable; disable }
96101
; quiet
102+
; werror
97103
; custom_header = c_header
98104
})
99105
$ debug
@@ -103,6 +109,7 @@ let t =
103109
$ debuginfo
104110
$ noinline
105111
$ is_quiet
112+
$ is_werror
106113
$ custom_header)
107114

108115
let on_off on off t =
@@ -112,4 +119,5 @@ let on_off on off t =
112119
let eval t =
113120
Config.Flag.(on_off enable disable t.optim);
114121
Debug.(on_off enable disable t.debug);
115-
quiet := t.quiet
122+
quiet := t.quiet;
123+
werror := t.werror

compiler/lib-cmdline/arg.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type t =
2626
{ debug : string list on_off
2727
; optim : string list on_off
2828
; quiet : bool
29+
; werror : bool
2930
; custom_header : string option
3031
}
3132

compiler/lib/stdlib.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ include Int_replace_polymorphic_compare
6666

6767
let quiet = ref false
6868

69-
let warn fmt = Format.ksprintf (fun s -> if not !quiet then Format.eprintf "%s%!" s) fmt
69+
let werror = ref false
70+
71+
let warnings = ref 0
72+
73+
let warn fmt =
74+
Format.ksprintf
75+
(fun s ->
76+
incr warnings;
77+
if not !quiet then Format.eprintf "%s%!" s)
78+
fmt
7079

7180
let fail = ref true
7281

toplevel/examples/lwt_toplevel/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
(action
8888
(run
8989
%{bin:js_of_ocaml}
90+
--Werror
9091
-I
9192
%{lib:reactiveData:.}
9293
-I

0 commit comments

Comments
 (0)