1. HongboZhang
  2. ocaml

Commits

frisch  committed d155702

Add support for -ppx in ocamldep (and reuse preprocessing code in Pparse).

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12769f963ae5c-01c2-4b8c-9fe0-0dff7051ff02

  • Participants
  • Parent commits 6e090de
  • Branches master

Comments (0)

Files changed (5)

File .depend

View file
     parsing/location.cmi parsing/lexer.cmi
 parsing/lexer.cmx : utils/warnings.cmx parsing/parser.cmx utils/misc.cmx \
     parsing/location.cmx parsing/lexer.cmi
+parsing/linenum.cmo : utils/misc.cmi
+parsing/linenum.cmx : utils/misc.cmx
 parsing/location.cmo : utils/warnings.cmi utils/terminfo.cmi \
     parsing/location.cmi
 parsing/location.cmx : utils/warnings.cmx utils/terminfo.cmx \
     parsing/longident.cmi parsing/location.cmi typing/ident.cmi \
     typing/env.cmi parsing/asttypes.cmi
 typing/typedtreeIter.cmi : typing/typedtree.cmi parsing/asttypes.cmi
-typing/typedtreeMap.cmi : typing/typedtree.cmi parsing/asttypes.cmi
+typing/typedtreeMap.cmi : typing/typedtree.cmi
 typing/typemod.cmi : typing/types.cmi typing/typedtree.cmi \
     parsing/parsetree.cmi parsing/longident.cmi parsing/location.cmi \
     typing/includemod.cmi typing/ident.cmi typing/env.cmi
 typing/typedtree.cmx : typing/types.cmx typing/primitive.cmx typing/path.cmx \
     utils/misc.cmx parsing/longident.cmx parsing/location.cmx \
     typing/ident.cmx typing/env.cmx parsing/asttypes.cmi typing/typedtree.cmi
-typing/typedtreeIter.cmo : typing/typedtree.cmi utils/misc.cmi \
-    parsing/asttypes.cmi typing/typedtreeIter.cmi
-typing/typedtreeIter.cmx : typing/typedtree.cmx utils/misc.cmx \
-    parsing/asttypes.cmi typing/typedtreeIter.cmi
+typing/typedtreeIter.cmo : typing/typedtree.cmi parsing/asttypes.cmi \
+    typing/typedtreeIter.cmi
+typing/typedtreeIter.cmx : typing/typedtree.cmx parsing/asttypes.cmi \
+    typing/typedtreeIter.cmi
 typing/typedtreeMap.cmo : typing/typedtree.cmi utils/misc.cmi \
     parsing/asttypes.cmi typing/typedtreeMap.cmi
 typing/typedtreeMap.cmx : typing/typedtree.cmx utils/misc.cmx \

File tools/Makefile.shared

View file
 CAMLDEP_OBJ=depend.cmo ocamldep.cmo
 CAMLDEP_IMPORTS=misc.cmo config.cmo clflags.cmo terminfo.cmo \
   warnings.cmo location.cmo longident.cmo \
-  syntaxerr.cmo parser.cmo lexer.cmo parse.cmo
+  syntaxerr.cmo parser.cmo lexer.cmo parse.cmo \
+  ccomp.cmo pparse.cmo
 
 ocamldep: depend.cmi $(CAMLDEP_OBJ)
 	$(CAMLC) $(LINKFLAGS) -o ocamldep $(CAMLDEP_IMPORTS) $(CAMLDEP_OBJ)

File tools/depend.ml

View file
 and add_use_file bv top_phrs =
   ignore (List.fold_left add_top_phrase bv top_phrs)
 
+and add_implementation bv l =
+  ignore (add_structure bv l)
+
 and add_top_phrase bv = function
   | Ptop_def str -> add_structure bv str
   | Ptop_dir (_, _) -> bv

File tools/depend.mli

View file
 val add_use_file : StringSet.t -> Parsetree.toplevel_phrase list -> unit
 
 val add_signature : StringSet.t -> Parsetree.signature -> unit
+
+val add_implementation : StringSet.t -> Parsetree.structure -> unit

File tools/ocamldep.ml

View file
     deps;
   print_char '\n'
 
-(* Optionally preprocess a source file *)
-
-let preprocessor = ref None
-
-exception Preprocessing_error
-
-let preprocess sourcefile =
-  match !preprocessor with
-    None -> sourcefile
-  | Some pp ->
-      flush Pervasives.stdout;
-      let tmpfile = Filename.temp_file "camlpp" "" in
-      let comm = Printf.sprintf "%s %s > %s" pp sourcefile tmpfile in
-      if Sys.command comm <> 0 then begin
-        Misc.remove_file tmpfile;
-        raise Preprocessing_error
-      end;
-      tmpfile
-
-let remove_preprocessed inputfile =
-  match !preprocessor with
-    None -> ()
-  | Some _ -> Misc.remove_file inputfile
-
-(* Parse a file or get a dumped syntax tree in it *)
-
-let is_ast_file ic ast_magic =
-  try
-    let buffer = Misc.input_bytes ic (String.length ast_magic) in
-    if buffer = ast_magic then true
-    else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then
-      failwith "OCaml and preprocessor have incompatible versions"
-    else false
-  with End_of_file -> false
-
-let parse_use_file ic =
-  if is_ast_file ic Config.ast_impl_magic_number then
-    let _source_file = input_value ic in
-    [Ptop_def (input_value ic : Parsetree.structure)]
-  else begin
-    seek_in ic 0;
-    let lb = Lexing.from_channel ic in
-    Location.init lb !Location.input_name;
-    Parse.use_file lb
-  end
-
-let parse_interface ic =
-  if is_ast_file ic Config.ast_intf_magic_number then
-    let _source_file = input_value ic in
-    (input_value ic : Parsetree.signature)
-  else begin
-    seek_in ic 0;
-    let lb = Lexing.from_channel ic in
-    Location.init lb !Location.input_name;
-    Parse.interface lb
-  end
 
 (* Process one file *)
 
         Syntaxerr.report_error err
     | Sys_error msg ->
         Format.fprintf Format.err_formatter "@[I/O error:@ %s@]@." msg
-    | Preprocessing_error ->
+    | Pparse.Error ->
         Format.fprintf Format.err_formatter "@[Preprocessing error on file %s@]@."
             source_file
     | x -> raise x
 
-let read_parse_and_extract parse_function extract_function source_file =
+let read_parse_and_extract parse_function extract_function magic source_file =
   Depend.free_structure_names := Depend.StringSet.empty;
   try
-    let input_file = preprocess source_file in
-    let ic = open_in_bin input_file in
-    try
-      let ast = parse_function ic in
-      extract_function Depend.StringSet.empty ast;
-      !Depend.free_structure_names
-    with x ->
-      close_in ic; remove_preprocessed input_file; raise x
+    let input_file = Pparse.preprocess source_file in
+    let ast = Pparse.file Format.err_formatter input_file parse_function magic in
+    extract_function Depend.StringSet.empty ast;
+    !Depend.free_structure_names
   with x ->
     report_err source_file x;
     Depend.StringSet.empty
 
 let ml_file_dependencies source_file =
   let extracted_deps = read_parse_and_extract
-    parse_use_file Depend.add_use_file source_file
+      Parse.implementation Depend.add_implementation Config.ast_impl_magic_number source_file
   in
   if !sort_files then
     files := (source_file, ML, !Depend.free_structure_names) :: !files
 
 let mli_file_dependencies source_file =
   let extracted_deps = read_parse_and_extract
-    parse_interface Depend.add_signature source_file in
+      Parse.interface Depend.add_signature Config.ast_intf_magic_number source_file
+  in
   if !sort_files then
     files := (source_file, MLI, extracted_deps) :: !files
   else
              "  Generate dependencies on all files (not accommodating for make shortcomings)";
      "-one-line", Arg.Set one_line,
              "  Output one line per file, regardless of the length";
-     "-pp", Arg.String(fun s -> preprocessor := Some s),
+     "-pp", Arg.String(fun s -> Clflags.preprocessor := Some s),
          "<cmd> Pipe sources through preprocessor <cmd>";
+    "-ppx", Arg.String(fun s -> Clflags.ppx := s :: !Clflags.ppx),
+         "<cmd>  Pipe abstract syntax trees through preprocessor <cmd>";
      "-slash", Arg.Set force_slash,
             "   (Windows) Use forward slash / instead of backslash \\ in file paths";
      "-version", Arg.Unit print_version,