Skip to content

Commit 423f312

Browse files
authored
flambda-backend: Refactor -extension and -standard flags (#398)
* Refactor extension handling in Clflags Move all extension handling function into a separate module * Compiler flag -standard disables all extensions Regardless of the position of -standard flag relative to -extension flags, -standard disables extensions that were enabled before or after it. * Fix testsuite * Add extension and standard to OCAMLPARAM * Rename flag -standard to -no-extensions * Rename standard to no-extensions in OCAMLPARAM * Improve help text for -no-extensions * Print available extensions in help text for -extension
1 parent 585e023 commit 423f312

File tree

8 files changed

+56
-34
lines changed

8 files changed

+56
-34
lines changed

driver/compenv.ml

+4
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ let read_one_param ppf position name v =
469469
| Some pass -> set_save_ir_after pass true
470470
end
471471

472+
| "extension" -> Clflags.Extension.enable v
473+
| "no-extensions" ->
474+
if check_bool ppf "no-extensions" v then Clflags.Extension.disable_all ()
475+
472476
| _ ->
473477
if !warnings_for_discarded_params &&
474478
not (List.mem name !can_discard) then begin

driver/main_args.ml

+16-8
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,19 @@ let mk_dump_into_file f =
716716
;;
717717

718718
let mk_extension f =
719-
"-extension", Arg.String f, "<extension> Enable the extension"
719+
let available_extensions =
720+
Clflags.Extension.(List.map to_string all)
721+
in
722+
"-extension", Arg.Symbol (available_extensions, f),
723+
"<extension> Enable the extension (may be specified more than once)"
720724
;;
721725

722-
let mk_standard f =
723-
"-standard", Arg.Unit f, " Disable all default extensions"
726+
let mk_no_extensions f =
727+
"-no-extensions", Arg.Unit f,
728+
" Disable all extensions, wherever they are specified; this flag\n\
729+
\ overrides the -extension flag (whether specified before or after this\n\
730+
\ flag), disables any extensions that are enabled by default, and\n\
731+
\ ignores any extensions requested in OCAMLPARAM."
724732
;;
725733

726734
let mk_dparsetree f =
@@ -1031,7 +1039,7 @@ module type Compiler_options = sig
10311039
val _match_context_rows : int -> unit
10321040
val _dtimings : unit -> unit
10331041
val _dprofile : unit -> unit
1034-
val _standard : unit -> unit
1042+
val _no_extensions : unit -> unit
10351043
val _dump_into_file : unit -> unit
10361044

10371045
val _args: string -> string array
@@ -1283,7 +1291,7 @@ struct
12831291
mk_dcamlprimc F._dcamlprimc;
12841292
mk_dtimings F._dtimings;
12851293
mk_dprofile F._dprofile;
1286-
mk_standard F._standard;
1294+
mk_no_extensions F._no_extensions;
12871295
mk_dump_into_file F._dump_into_file;
12881296
mk_extension F._extension;
12891297

@@ -1510,7 +1518,7 @@ struct
15101518
mk_dstartup F._dstartup;
15111519
mk_dtimings F._dtimings;
15121520
mk_dprofile F._dprofile;
1513-
mk_standard F._standard;
1521+
mk_no_extensions F._no_extensions;
15141522
mk_dump_into_file F._dump_into_file;
15151523
mk_dump_pass F._dump_pass;
15161524
mk_extension F._extension;
@@ -1769,7 +1777,7 @@ module Default = struct
17691777
let _unsafe = set unsafe
17701778
let _warn_error s = Warnings.parse_options true s
17711779
let _warn_help = Warnings.help_warnings
1772-
let _extension s = add_extension s
1780+
let _extension s = Extension.enable s
17731781
end
17741782

17751783
module Native = struct
@@ -1884,7 +1892,7 @@ module Default = struct
18841892
let _config_var = Misc.show_config_variable_and_exit
18851893
let _dprofile () = profile_columns := Profile.all_columns
18861894
let _dtimings () = profile_columns := [`Time]
1887-
let _standard = set_standard
1895+
let _no_extensions = Extension.disable_all
18881896
let _dump_into_file = set dump_into_file
18891897
let _for_pack s = for_package := (Some s)
18901898
let _g = set debug

driver/main_args.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module type Compiler_options = sig
119119
val _match_context_rows : int -> unit
120120
val _dtimings : unit -> unit
121121
val _dprofile : unit -> unit
122-
val _standard : unit -> unit
122+
val _no_extensions : unit -> unit
123123
val _dump_into_file : unit -> unit
124124

125125
val _args: string -> string array

testsuite/tests/comprehensions/comprehensions.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* TEST
2-
flags = "-extension Comprehensions"
2+
flags = "-extension comprehensions"
33
* expect
44
*)
55
(*Type checking tests.*)

typing/typecore.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type error =
127127
| Probe_name_format of string
128128
| Probe_name_undefined of string
129129
| Probe_is_enabled_format
130-
| Extension_not_enabled of Clflags.extension
130+
| Extension_not_enabled of Clflags.Extension.t
131131
| Literal_overflow of string
132132
| Unknown_literal of string * char
133133
| Illegal_letrec_pat
@@ -3776,12 +3776,12 @@ and type_expect_
37763776
| Pexp_extension (({ txt = ("extension.list_comprehension"
37773777
| "extension.arr_comprehension"); _ },
37783778
_ ) as extension) ->
3779-
if Clflags.is_extension_enabled Clflags.Comprehensions then
3779+
if Clflags.Extension.(is_enabled Comprehensions) then
37803780
let ext_expr = Extensions.extension_expr_of_payload ~loc extension in
37813781
type_extension ~loc ~env ~ty_expected ~sexp ext_expr
37823782
else
37833783
raise
3784-
(Error (loc, env, Extension_not_enabled(Clflags.Comprehensions)))
3784+
(Error (loc, env, Extension_not_enabled(Clflags.Extension.Comprehensions)))
37853785
| Pexp_extension ext ->
37863786
raise (Error_forward (Builtin_attributes.error_of_extension ext))
37873787

@@ -5739,8 +5739,8 @@ let report_error ~loc env = function
57395739
Location.errorf ~loc
57405740
"%%probe_is_enabled points must specify a single probe name as a \
57415741
string literal"
5742-
| Extension_not_enabled(ext) ->
5743-
let name = Clflags.string_of_extension ext in
5742+
| Extension_not_enabled ext ->
5743+
let name = Clflags.Extension.to_string ext in
57445744
Location.errorf ~loc
57455745
"Extension %s must be enabled to use this feature." name
57465746
| Literal_overflow ty ->

typing/typecore.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ type error =
185185
| Probe_name_undefined of string
186186
(* CR-soon mshinwell: Use an inlined record *)
187187
| Probe_is_enabled_format
188-
| Extension_not_enabled of Clflags.extension
188+
| Extension_not_enabled of Clflags.Extension.t
189189
| Literal_overflow of string
190190
| Unknown_literal of string * char
191191
| Illegal_letrec_pat

utils/clflags.ml

+20-12
Original file line numberDiff line numberDiff line change
@@ -375,23 +375,31 @@ let set_dumped_pass s enabled =
375375
dumped_passes_list := dumped_passes
376376
end
377377

378-
type extension = Comprehensions
378+
module Extension = struct
379+
type t = Comprehensions
379380

380-
let extensions = ref ([] : extension list) (* -extensions *)
381-
let set_standard () = extensions := []
381+
let all = [ Comprehensions ]
382382

383-
let string_of_extension = function
384-
| Comprehensions -> "comprehensions"
383+
let extensions = ref ([] : t list) (* -extension *)
384+
let equal Comprehensions Comprehensions = true
385385

386-
let extension_of_string = function
387-
| "comprehensions" -> Comprehensions
388-
| extn -> raise (Arg.Bad(Printf.sprintf "Extension %s is not known" extn))
386+
let disable_all_extensions = ref false (* -no-extensions *)
387+
let disable_all () = disable_all_extensions := true
389388

390-
let add_extension extn =
391-
let extension = extension_of_string (String.lowercase_ascii extn) in
392-
extensions := extension::!extensions
389+
let to_string = function
390+
| Comprehensions -> "comprehensions"
391+
392+
let of_string = function
393+
| "comprehensions" -> Comprehensions
394+
| extn -> raise (Arg.Bad(Printf.sprintf "Extension %s is not known" extn))
393395

394-
let is_extension_enabled ext = List.mem ext !extensions
396+
let enable extn =
397+
let t = of_string (String.lowercase_ascii extn) in
398+
if not (List.exists (equal t) !extensions) then
399+
extensions := t :: !extensions
400+
401+
let is_enabled ext = not !disable_all_extensions && List.mem ext !extensions
402+
end
395403

396404
let dump_into_file = ref false (* -dump-into-file *)
397405

utils/clflags.mli

+8-6
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,14 @@ val set_dumped_pass : string -> bool -> unit
201201

202202
val dump_into_file : bool ref
203203

204-
type extension = Comprehensions
205-
val extensions : extension list ref
206-
val set_standard : unit -> unit
207-
val add_extension: string -> unit
208-
val is_extension_enabled: extension -> bool
209-
val string_of_extension: extension -> string
204+
module Extension : sig
205+
type t = Comprehensions
206+
val enable : string -> unit
207+
val is_enabled : t -> bool
208+
val to_string : t -> string
209+
val all : t list
210+
val disable_all : unit -> unit
211+
end
210212

211213
(* Support for flags that can also be set from an environment variable *)
212214
type 'a env_reader = {

0 commit comments

Comments
 (0)