Skip to content

Commit

Permalink
Create command for printing magic numbers (#117)
Browse files Browse the repository at this point in the history
* Create command for printing magic numbers

* Fix ci failures
  • Loading branch information
liam923 authored Nov 1, 2024
1 parent b0cc0c7 commit 25f1a7f
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flambda-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
working-directory: merlin-jst
run: |
export MERLIN_TEST_OCAML_PATH=$GITHUB_WORKSPACE/flambda-backend/_install
opam exec -- dune runtest -p merlin-lib,dot-merlin-reader,merlin
opam exec -- dune runtest -p merlin-lib,dot-merlin-reader,merlin,ocaml-index
4 changes: 3 additions & 1 deletion src/commands/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
(flags
:standard
-open Ocaml_parsing
-open Ocaml_utils
-open Merlin_utils
-open Merlin_kernel)
(libraries
merlin-lib.ocaml_parsing
merlin-lib.utils
merlin-lib.kernel
merlin-lib.query_protocol
merlin-lib.query_commands))
merlin-lib.query_commands
merlin-lib.ocaml_utils))
5 changes: 5 additions & 0 deletions src/commands/new_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,11 @@ let all_commands =
run buffer (Query_protocol.Signature_help sh)
end;
(* Used only for testing *)
command "version" ~spec:[] ~default:() ~doc:"Print version information"
begin
fun buffer () -> run buffer Query_protocol.Version
end;
(* Used only for testing *)
command "dump"
~spec:
[ arg "-what"
Expand Down
6 changes: 5 additions & 1 deletion src/commands/query_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,8 @@ let json_of_response (type a) (query : a t) (response : a) : json =
let with_file = scope = `Project in
`List (List.map locations ~f:(fun loc -> with_location ~with_file loc []))
| Signature_help _, s -> json_of_signature_help s
| Version, version -> `String version
| Version, (version, magic_numbers) ->
`Assoc
[ ("version", `String version);
("magicNumbers", Config.Magic_numbers.to_json magic_numbers)
]
4 changes: 2 additions & 2 deletions src/frontend/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(name query_protocol)
(public_name merlin-lib.query_protocol)
(modules query_protocol)
(flags :standard -open Merlin_utils -open Merlin_kernel -open Ocaml_parsing -open Merlin_kernel)
(libraries merlin_kernel merlin_utils ocaml_parsing))
(flags :standard -open Merlin_utils -open Merlin_kernel -open Ocaml_parsing -open Ocaml_utils)
(libraries merlin_kernel merlin_utils ocaml_parsing ocaml_utils))

(library
(name query_commands)
Expand Down
8 changes: 6 additions & 2 deletions src/frontend/query_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -978,5 +978,9 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a = function
}
| None -> None)
| Version ->
Printf.sprintf "The Merlin toolkit version %s, for Ocaml %s\n"
Merlin_config.version Sys.ocaml_version
let version =
Printf.sprintf "The Merlin toolkit version %s, for Ocaml %s\n"
Merlin_config.version Sys.ocaml_version
in
let magic_numbers = Config.Magic_numbers.current in
(version, magic_numbers)
2 changes: 1 addition & 1 deletion src/frontend/query_protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,4 @@ type _ t =
(** In current version, Merlin only uses the parameter [position] to answer
signature_help queries. The additionnal parameters are described in the
LSP protocol and might enable finer behaviour in the future. *)
| Version : string t
| Version : (string * Config.Magic_numbers.t) t
2 changes: 1 addition & 1 deletion src/ocaml-index/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(name ocaml_index)
(public_name ocaml-index)
(package ocaml-index)
(libraries lib ocaml_typing ocaml_utils merlin_index_format)
(libraries lib ocaml_typing ocaml_utils merlin_index_format merlin_utils yojson)
(flags
:standard
-open Ocaml_typing
Expand Down
15 changes: 13 additions & 2 deletions src/ocaml-index/bin/ocaml_index.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ let rewrite_root = ref false
let store_shapes = ref false
let do_not_use_cmt_loadpath = ref false

type command = Aggregate | Dump | Dump_file_stats | Stats | Gather_shapes
type command =
| Aggregate
| Dump
| Dump_file_stats
| Stats
| Gather_shapes
| Magic_numbers

let parse_command = function
| "aggregate" -> Some Aggregate
| "dump" -> Some Dump
| "dump-file-stats" -> Some Dump_file_stats
| "stats" -> Some Stats
| "gather-shapes" -> Some Gather_shapes
| "magic-numbers" -> Some Magic_numbers
| _ -> None

let command = ref None
Expand Down Expand Up @@ -128,5 +135,9 @@ let () =
(Hashtbl.length cu_shape)
(Option.value ~default:"none" root_directory))
(List.rev !input_files_rev)
| _ -> Printf.printf "Nothing to do.\n%!");
| Some Magic_numbers ->
let json = Config.Magic_numbers.(to_json current) in
Yojson.Basic.to_channel stdout json;
print_newline ()
| None -> Printf.printf "Nothing to do.\n%!");
exit 0
35 changes: 35 additions & 0 deletions src/ocaml/utils/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,38 @@ let reserved_header_bits = 8
let runtime5 = true

let merlin = true

module Magic_numbers = struct
type t =
{ cmi_magic_number : string;
ast_intf_magic_number : string;
ast_impl_magic_number : string;
cmt_magic_number : string;
cms_magic_number : string;
index_magic_number : string
}

let current =
{ cmi_magic_number;
ast_intf_magic_number;
ast_impl_magic_number;
cmt_magic_number;
cms_magic_number;
index_magic_number
}

let to_json t =
let nums =
[ ("cmi_magic_number", t.cmi_magic_number);
("ast_intf_magic_number", t.ast_intf_magic_number);
("ast_impl_magic_number", t.ast_impl_magic_number);
("cmt_magic_number", t.cmt_magic_number);
("cms_magic_number", t.cms_magic_number);
("index_magic_number", t.index_magic_number)
]
in
`Assoc
(List.map
(fun (key, value) -> (key, Merlin_utils.Std.Json.string value))
nums)
end
15 changes: 15 additions & 0 deletions src/ocaml/utils/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ val runtime5 : bool

val merlin : bool

module Magic_numbers : sig
type t =
{ cmi_magic_number : string;
ast_intf_magic_number : string;
ast_impl_magic_number : string;
cmt_magic_number : string;
cms_magic_number : string;
index_magic_number : string
}

val current : t

val to_json : t -> Std.json
end

(**/**)
2 changes: 1 addition & 1 deletion src/ocaml/utils/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name ocaml_utils)
(public_name merlin-lib.ocaml_utils)
(libraries merlin_config merlin_utils)
(libraries merlin_utils)
(flags :standard -open Merlin_utils))
27 changes: 27 additions & 0 deletions tests/test-dirs/version.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$ $MERLIN single version | revert-newlines | jq .value
{
"version": "The Merlin toolkit version %VERSION%, for Ocaml 5.2.0\n",
"magicNumbers": {
"cmi_magic_number": "Caml1999I552",
"ast_intf_magic_number": "Caml1999N552",
"ast_impl_magic_number": "Caml1999M552",
"cmt_magic_number": "Caml1999T552",
"cms_magic_number": "Caml1999S552",
"index_magic_number": "Merl2023I552"
}
}

$ ocaml-index magic-numbers | jq
{
"cmi_magic_number": "Caml1999I552",
"ast_intf_magic_number": "Caml1999N552",
"ast_impl_magic_number": "Caml1999M552",
"cmt_magic_number": "Caml1999T552",
"cms_magic_number": "Caml1999S552",
"index_magic_number": "Merl2023I552"
}

Verify there is no difference between Merlin and Ocaml-index
$ $MERLIN single version | revert-newlines | jq --sort-keys .value.magicNumbers > merlin-magic-numbers.json
$ ocaml-index magic-numbers | jq --sort-keys > ocaml-index-magic-numbers.json
$ diff merlin-magic-numbers.json ocaml-index-magic-numbers.json

0 comments on commit 25f1a7f

Please sign in to comment.