diff --git a/driver/compile.ml b/driver/compile.ml index ae7b9ccbf..7c7cf40c5 100644 --- a/driver/compile.ml +++ b/driver/compile.ml @@ -18,10 +18,6 @@ open Config open Format open Typedtree -(* Optional preprocessor *) - -let pproc = ref None - (* Initialize the search path. The current directory is always searched first, then the directories specified with the -I option (in command-line order), @@ -42,32 +38,31 @@ let initial_env () = with Not_found -> fatal_error "cannot open Pervasives.cmi" -(* Compile a .mli file *) +(* Optionally preprocess a source file *) -let interface sourcefile = - init_path(); - let prefixname = Filename.chop_extension sourcefile in - let modulename = capitalize(Filename.basename prefixname) in - let srcfile = - match !pproc with - None -> sourcefile - | Some pp -> - let tmpfile = prefixname ^ ".ppi" in - let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in - if Sys.command comm <> 0 then begin - Printf.eprintf "Preprocessing error\n"; - flush stderr; - exit 2 - end; - tmpfile - in - let ic = open_in_bin srcfile in +let pproc = ref None + +let preprocess sourcefile tmpfile = + match !pproc with + None -> sourcefile + | Some pp -> + let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in + if Sys.command comm <> 0 then begin + Printf.eprintf "Preprocessing error\n"; + flush stderr; + exit 2 + end; + tmpfile + +(* Parse a file or get a dumped syntax tree in it *) + +let parse_file inputfile parse_fun ast_magic = + let ic = open_in_bin inputfile in let is_ast_file = try - let magic = Config.ast_intf_magic_number in - let buffer = String.create (String.length magic) in - really_input ic buffer 0 (String.length magic); - buffer = magic + let buffer = String.create (String.length ast_magic) in + really_input ic buffer 0 (String.length ast_magic); + buffer = ast_magic with _ -> false in let ast = @@ -77,18 +72,28 @@ let interface sourcefile = input_value ic end else begin seek_in ic 0; - Location.input_name := srcfile; - Parse.interface (Lexing.from_channel ic) + Location.input_name := inputfile; + parse_fun (Lexing.from_channel ic) end with x -> close_in ic; raise x in close_in ic; + ast + +(* Compile a .mli file *) + +let interface sourcefile = + init_path(); + let prefixname = Filename.chop_extension sourcefile in + let modulename = capitalize(Filename.basename prefixname) in + let inputfile = preprocess sourcefile (prefixname ^ ".ppi") in + let ast = parse_file inputfile Parse.interface ast_intf_magic_number in let sg = Typemod.transl_signature (initial_env()) ast in if !Clflags.print_types then (Printtyp.signature sg; print_flush()); Env.save_signature sg modulename (prefixname ^ ".cmi"); match !pproc with None -> () - | Some _ -> remove_file srcfile + | Some _ -> remove_file inputfile (* Compile a .ml file *) @@ -100,41 +105,8 @@ let implementation sourcefile = init_path(); let prefixname = Filename.chop_extension sourcefile in let modulename = capitalize(Filename.basename prefixname) in - let srcfile = - match !pproc with - None -> sourcefile - | Some pp -> - let tmpfile = prefixname ^ ".ppo" in - let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in - if Sys.command comm <> 0 then begin - Printf.eprintf "Preprocessing error\n"; - flush stderr; - exit 2 - end; - tmpfile - in - let ic = open_in_bin srcfile in - let is_ast_file = - try - let magic = Config.ast_impl_magic_number in - let buffer = String.create (String.length magic) in - really_input ic buffer 0 (String.length magic); - buffer = magic - with _ -> false - in - let ast = - try - if is_ast_file then begin - Location.input_name := input_value ic; - input_value ic - end else begin - seek_in ic 0; - Location.input_name := srcfile; - Parse.implementation (Lexing.from_channel ic) - end - with x -> close_in ic; raise x - in - close_in ic; + let inputfile = preprocess sourcefile (prefixname ^ ".ppo") in + let ast = parse_file inputfile Parse.implementation ast_impl_magic_number in let objfile = prefixname ^ ".cmo" in let oc = open_out_bin objfile in try @@ -162,7 +134,7 @@ let implementation sourcefile = (Translmod.transl_implementation modulename str coercion)))))); begin match !pproc with None -> () - | Some _ -> remove_file srcfile + | Some _ -> remove_file inputfile end; close_out oc with x -> diff --git a/driver/optcompile.ml b/driver/optcompile.ml index b4223e81b..57c96370d 100644 --- a/driver/optcompile.ml +++ b/driver/optcompile.ml @@ -18,10 +18,6 @@ open Config open Format open Typedtree -(* Optional preprocessor *) - -let pproc = ref None - (* Initialize the search path. The current directory is always searched first, then the directories specified with the -I option (in command-line order), @@ -43,31 +39,31 @@ let initial_env () = with Not_found -> fatal_error "cannot open Pervasives.cmi" -(* Compile a .mli file *) +(* Optionally preprocess a source file *) -let interface sourcefile = - let prefixname = Filename.chop_extension sourcefile in - let modulename = capitalize(Filename.basename prefixname) in - let srcfile = - match !pproc with - None -> sourcefile - | Some pp -> - let tmpfile = prefixname ^ ".ppi" in - let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in - if Sys.command comm <> 0 then begin - Printf.eprintf "Preprocessing error\n"; - flush stderr; - exit 2 - end; - tmpfile - in - let ic = open_in_bin srcfile in +let pproc = ref None + +let preprocess sourcefile tmpfile = + match !pproc with + None -> sourcefile + | Some pp -> + let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in + if Sys.command comm <> 0 then begin + Printf.eprintf "Preprocessing error\n"; + flush stderr; + exit 2 + end; + tmpfile + +(* Parse a file or get a dumped syntax tree in it *) + +let parse_file inputfile parse_fun ast_magic = + let ic = open_in_bin inputfile in let is_ast_file = try - let magic = Config.ast_intf_magic_number in - let buffer = String.create (String.length magic) in - really_input ic buffer 0 (String.length magic); - buffer = magic + let buffer = String.create (String.length ast_magic) in + really_input ic buffer 0 (String.length ast_magic); + buffer = ast_magic with _ -> false in let ast = @@ -77,18 +73,27 @@ let interface sourcefile = input_value ic end else begin seek_in ic 0; - Location.input_name := srcfile; - Parse.interface (Lexing.from_channel ic) + Location.input_name := inputfile; + parse_fun (Lexing.from_channel ic) end with x -> close_in ic; raise x in close_in ic; + ast + +(* Compile a .mli file *) + +let interface sourcefile = + let prefixname = Filename.chop_extension sourcefile in + let modulename = capitalize(Filename.basename prefixname) in + let inputfile = preprocess sourcefile (prefixname ^ ".ppi") in + let ast = parse_file inputfile Parse.interface ast_intf_magic_number in let sg = Typemod.transl_signature (initial_env()) ast in if !Clflags.print_types then (Printtyp.signature sg; print_flush()); Env.save_signature sg modulename (prefixname ^ ".cmi"); match !pproc with None -> () - | Some _ -> remove_file srcfile + | Some _ -> remove_file inputfile (* Compile a .ml file *) @@ -99,41 +104,8 @@ let print_if flag printer arg = let implementation sourcefile = let prefixname = Filename.chop_extension sourcefile in let modulename = capitalize(Filename.basename prefixname) in - let srcfile = - match !pproc with - None -> sourcefile - | Some pp -> - let tmpfile = prefixname ^ ".ppo" in - let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in - if Sys.command comm <> 0 then begin - Printf.eprintf "Preprocessing error\n"; - flush stderr; - exit 2 - end; - tmpfile - in - let ic = open_in_bin srcfile in - let is_ast_file = - try - let magic = Config.ast_impl_magic_number in - let buffer = String.create (String.length magic) in - really_input ic buffer 0 (String.length magic); - buffer = magic - with _ -> false - in - let ast = - try - if is_ast_file then begin - Location.input_name := input_value ic; - input_value ic - end else begin - seek_in ic 0; - Location.input_name := srcfile; - Parse.implementation (Lexing.from_channel ic) - end - with x -> close_in ic; raise x - in - close_in ic; + let inputfile = preprocess sourcefile (prefixname ^ ".ppo") in + let ast = parse_file inputfile Parse.implementation ast_impl_magic_number in let (str, sg, finalenv) = Typemod.type_structure (initial_env()) ast in if !Clflags.print_types then (Printtyp.signature sg; print_flush()); let (coercion, crc) = @@ -158,7 +130,7 @@ let implementation sourcefile = Compilenv.save_unit_info (prefixname ^ ".cmx"); begin match !pproc with None -> () - | Some _ -> remove_file srcfile + | Some _ -> remove_file inputfile end let c_file name =