Skip to content

Commit

Permalink
Generate *.cms files for merlin (ocaml-flambda#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
poechsel authored Mar 29, 2023
1 parent 7c76ce3 commit f9ccb00
Show file tree
Hide file tree
Showing 21 changed files with 283 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*.lib
*.dll
*.la
*.cm[ioxat]
*.cm[ioxats]
*.cmx[as]
*.cmti
*.cmsi
*.annot
*.exe
*.exe.manifest
Expand Down
50 changes: 50 additions & 0 deletions ocaml/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ typing/cmt2annot.cmo : \
typing/envaux.cmi \
typing/env.cmi \
file_formats/cmt_format.cmi \
file_formats/cms_format.cmi \
parsing/asttypes.cmi \
typing/annot.cmi
typing/cmt2annot.cmx : \
Expand All @@ -644,6 +645,7 @@ typing/cmt2annot.cmx : \
typing/envaux.cmx \
typing/env.cmx \
file_formats/cmt_format.cmx \
file_formats/cms_format.cmx \
parsing/asttypes.cmi \
typing/annot.cmi
typing/ctype.cmo : \
Expand Down Expand Up @@ -910,6 +912,7 @@ typing/includemod.cmo : \
utils/diffing.cmi \
typing/ctype.cmi \
file_formats/cmt_format.cmi \
file_formats/cms_format.cmi \
parsing/builtin_attributes.cmi \
typing/btype.cmi \
typing/includemod.cmi
Expand All @@ -933,6 +936,7 @@ typing/includemod.cmx : \
utils/diffing.cmx \
typing/ctype.cmx \
file_formats/cmt_format.cmx \
file_formats/cms_format.cmx \
parsing/builtin_attributes.cmx \
typing/btype.cmx \
typing/includemod.cmi
Expand Down Expand Up @@ -1460,6 +1464,7 @@ typing/typeclass.cmo : \
typing/env.cmi \
typing/ctype.cmi \
file_formats/cmt_format.cmi \
file_formats/cms_format.cmi \
utils/clflags.cmi \
parsing/builtin_attributes.cmi \
typing/btype.cmi \
Expand Down Expand Up @@ -1488,6 +1493,7 @@ typing/typeclass.cmx : \
typing/env.cmx \
typing/ctype.cmx \
file_formats/cmt_format.cmx \
file_formats/cms_format.cmx \
utils/clflags.cmx \
parsing/builtin_attributes.cmx \
typing/btype.cmx \
Expand Down Expand Up @@ -1534,6 +1540,7 @@ typing/typecore.cmo : \
typing/env.cmi \
typing/ctype.cmi \
file_formats/cmt_format.cmi \
file_formats/cms_format.cmi \
utils/clflags.cmi \
parsing/builtin_attributes.cmi \
typing/btype.cmi \
Expand Down Expand Up @@ -1569,6 +1576,7 @@ typing/typecore.cmx : \
typing/env.cmx \
typing/ctype.cmx \
file_formats/cmt_format.cmx \
file_formats/cms_format.cmx \
utils/clflags.cmx \
parsing/builtin_attributes.cmx \
typing/btype.cmx \
Expand Down Expand Up @@ -1811,6 +1819,7 @@ typing/typemod.cmo : \
utils/config.cmi \
utils/compilation_unit.cmi \
file_formats/cmt_format.cmi \
file_formats/cms_format.cmi \
typing/cmt2annot.cmo \
file_formats/cmi_format.cmi \
utils/clflags.cmi \
Expand Down Expand Up @@ -1850,6 +1859,7 @@ typing/typemod.cmx : \
utils/config.cmx \
utils/compilation_unit.cmx \
file_formats/cmt_format.cmx \
file_formats/cms_format.cmx \
typing/cmt2annot.cmx \
file_formats/cmi_format.cmx \
utils/clflags.cmx \
Expand Down Expand Up @@ -4187,6 +4197,22 @@ file_formats/cmt_format.cmo : \
file_formats/cmi_format.cmi \
utils/clflags.cmi \
file_formats/cmt_format.cmi
file_formats/cms_format.cmo : \
typing/types.cmi \
typing/typedtree.cmi \
typing/tast_mapper.cmi \
typing/shape.cmi \
utils/misc.cmi \
parsing/location.cmi \
utils/load_path.cmi \
parsing/lexer.cmi \
utils/import_info.cmi \
typing/env.cmi \
utils/config.cmi \
utils/compilation_unit.cmi \
file_formats/cmi_format.cmi \
utils/clflags.cmi \
file_formats/cms_format.cmi
file_formats/cmt_format.cmx : \
typing/types.cmx \
typing/typedtree.cmx \
Expand All @@ -4203,6 +4229,22 @@ file_formats/cmt_format.cmx : \
file_formats/cmi_format.cmx \
utils/clflags.cmx \
file_formats/cmt_format.cmi
file_formats/cms_format.cmx : \
typing/types.cmx \
typing/typedtree.cmx \
typing/tast_mapper.cmx \
typing/shape.cmx \
utils/misc.cmx \
parsing/location.cmx \
utils/load_path.cmx \
parsing/lexer.cmx \
utils/import_info.cmx \
typing/env.cmx \
utils/config.cmx \
utils/compilation_unit.cmx \
file_formats/cmi_format.cmx \
utils/clflags.cmx \
file_formats/cms_format.cmi
file_formats/cmt_format.cmi : \
typing/types.cmi \
typing/typedtree.cmi \
Expand All @@ -4212,6 +4254,14 @@ file_formats/cmt_format.cmi : \
typing/env.cmi \
utils/compilation_unit.cmi \
file_formats/cmi_format.cmi
file_formats/cms_format.cmi : \
typing/types.cmi \
typing/typedtree.cmi \
typing/shape.cmi \
parsing/location.cmi \
typing/env.cmi \
utils/compilation_unit.cmi \
file_formats/cmi_format.cmi
file_formats/cmx_format.cmi : \
lambda/lambda.cmi \
utils/import_info.cmi \
Expand Down
1 change: 1 addition & 0 deletions ocaml/compilerlibs/Makefile.compilerlibs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ TYPING = \
typing/tast_mapper.cmo \
typing/stypes.cmo \
file_formats/cmt_format.cmo \
file_formats/cms_format.cmo \
typing/cmt2annot.cmo \
typing/untypeast.cmo \
typing/includemod.cmo \
Expand Down
1 change: 1 addition & 0 deletions ocaml/driver/compenv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ let read_one_param ppf position name v =
match name with
| "g" -> set "g" [ Clflags.debug ] v
| "bin-annot" -> set "bin-annot" [ Clflags.binary_annotations ] v
| "bin-annot-cms" -> set "bin-annot-cms" [ Clflags.binary_annotations_cms ] v
| "afl-instrument" -> set "afl-instrument" [ Clflags.afl_instrument ] v
| "afl-inst-ratio" ->
int_setter ppf "afl-inst-ratio" afl_inst_ratio v
Expand Down
8 changes: 8 additions & 0 deletions ocaml/driver/main_args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ let mk_binannot f =
"-bin-annot", Arg.Unit f, " Save typedtree in <filename>.cmt"
;;

let mk_binannot_cms f =
"-bin-annot-cms", Arg.Unit f, " Save shapes in <filename>.cms"
;;

let mk_c f =
"-c", Arg.Unit f, " Compile only (do not link)"
;;
Expand Down Expand Up @@ -1031,6 +1035,7 @@ module type Compiler_options = sig
val _a : unit -> unit
val _annot : unit -> unit
val _binannot : unit -> unit
val _binannot_cms : unit -> unit
val _c : unit -> unit
val _cc : string -> unit
val _cclib : string -> unit
Expand Down Expand Up @@ -1229,6 +1234,7 @@ struct
mk_absname F._absname;
mk_annot F._annot;
mk_binannot F._binannot;
mk_binannot_cms F._binannot_cms;
mk_c F._c;
mk_cc F._cc;
mk_cclib F._cclib;
Expand Down Expand Up @@ -1420,6 +1426,7 @@ struct
mk_afl_inst_ratio F._afl_inst_ratio;
mk_annot F._annot;
mk_binannot F._binannot;
mk_binannot_cms F._binannot_cms;
mk_inline_branch_factor F._inline_branch_factor;
mk_c F._c;
mk_cc F._cc;
Expand Down Expand Up @@ -1947,6 +1954,7 @@ module Default = struct
let _args = Arg.read_arg
let _args0 = Arg.read_arg0
let _binannot = set binary_annotations
let _binannot_cms = set binary_annotations_cms
let _c = set compile_only
let _cc s = c_compiler := (Some s)
let _cclib s = Compenv.defer (ProcessObjects (Misc.rev_split_words s))
Expand Down
1 change: 1 addition & 0 deletions ocaml/driver/main_args.mli
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module type Compiler_options = sig
val _a : unit -> unit
val _annot : unit -> unit
val _binannot : unit -> unit
val _binannot_cms : unit -> unit
val _c : unit -> unit
val _cc : string -> unit
val _cclib : string -> unit
Expand Down
3 changes: 2 additions & 1 deletion ocaml/dune
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
ident path primitive shape types btype oprint subst predef datarepr
cmi_format persistent_env env type_immediacy errortrace
typedtree printtyped ctype printtyp includeclass mtype envaux includecore
tast_iterator tast_mapper signature_group cmt_format untypeast
tast_iterator tast_mapper signature_group cmt_format cms_format untypeast
includemod includemod_errorprinter
typetexp patterns printpat parmatch stypes typedecl typeopt rec_check
typecore
Expand Down Expand Up @@ -293,6 +293,7 @@
(tast_iterator.mli as compiler-libs/tast_iterator.mli)
(tast_mapper.mli as compiler-libs/tast_mapper.mli)
(cmt_format.mli as compiler-libs/cmt_format.mli)
(cms_format.mli as compiler-libs/cms_format.mli)
(untypeast.mli as compiler-libs/untypeast.mli)
(includemod.mli as compiler-libs/includemod.mli)
(typetexp.mli as compiler-libs/typetexp.mli)
Expand Down
77 changes: 77 additions & 0 deletions ocaml/file_formats/cms_format.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Mark Shinwell and Leo White, Jane Street Europe *)
(* Fabrice Le Fessant, INRIA Saclay *)
(* *)
(* Copyright 2023 Jane Street Group LLC *)
(* Copyright 2012 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)

(** cms and cmsi files format. *)

let read_magic_number ic =
let len_magic_number = String.length Config.cms_magic_number in
really_input_string ic len_magic_number

type cms_infos = {
cms_modname : Compilation_unit.t;
cms_comments : (string * Location.t) list;
cms_sourcefile : string option;
cms_builddir : string;
cms_source_digest : Digest.t option;
cms_uid_to_loc : Location.t Shape.Uid.Tbl.t;
cms_uid_to_attributes : Parsetree.attributes Shape.Uid.Tbl.t;
cms_impl_shape : Shape.t option; (* None for mli *)
}

type error =
Not_a_shape of string

exception Error of error

let input_cms ic = (input_value ic : cms_infos)

let output_cms oc cms =
output_string oc Config.cms_magic_number;
output_value oc (cms : cms_infos)

let read filename =
let ic = open_in_bin filename in
Misc.try_finally
~always:(fun () -> close_in ic)
(fun () ->
let magic_number = read_magic_number ic in
if magic_number = Config.cms_magic_number then
input_cms ic
else
raise (Error (Not_a_shape filename))
)

let save_cms filename modname sourcefile shape =
if (!Clflags.binary_annotations_cms && not !Clflags.print_types) then begin
Misc.output_to_file_via_temporary
~mode:[Open_binary] filename
(fun _temp_file_name oc ->
let source_digest = Option.map Digest.file sourcefile in
let cms = {
cms_modname = modname;
cms_comments = Lexer.comments ();
cms_sourcefile = sourcefile;
cms_builddir = Location.rewrite_absolute_path (Sys.getcwd ());
cms_source_digest = source_digest;
cms_uid_to_loc = Env.get_uid_to_loc_tbl ();
cms_uid_to_attributes = Env.get_uid_to_attributes_tbl ();
cms_impl_shape = shape;
} in
output_cms oc cms)
end

let clear () = ()
54 changes: 54 additions & 0 deletions ocaml/file_formats/cms_format.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Mark Shinwell and Leo White, Jane Street Europe *)
(* Fabrice Le Fessant, INRIA Saclay *)
(* *)
(* Copyright 2023 Jane Street Group LLC *)
(* Copyright 2012 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)

(** cms and cmsi files format. *)

type cms_infos = {
cms_modname : Compilation_unit.t;
cms_comments : (string * Location.t) list;
cms_sourcefile : string option;
cms_builddir : string;
cms_source_digest : string option;
cms_uid_to_loc : Location.t Shape.Uid.Tbl.t;
cms_uid_to_attributes : Parsetree.attributes Shape.Uid.Tbl.t;
cms_impl_shape : Shape.t option; (* None for mli *)
}

type error =
Not_a_shape of string

exception Error of error

(** [read filename] opens filename, and extract the cms_infos. It
can be used with .cms and .cmsi files.
*)
val read : string -> cms_infos

(** [save_cms filename modname sourcefile shape]
writes a cms(i) file. *)
val save_cms :
string -> (* filename.cms to generate *)
Compilation_unit.t -> (* module name *)
string option -> (* source file *)
Shape.t option ->
unit

(* Miscellaneous functions *)

val read_magic_number : in_channel -> string

val clear : unit -> unit
Loading

0 comments on commit f9ccb00

Please sign in to comment.