[ppx_trace] Refactor: Avoid using Ast_mapper for ppxlib 0.16 compatibility

Summary:
Use `Ppxlib.Ast_traverse.map` instead of
`Ppxlib.Selected_ast.Ast.Ast_mapper` which is included from
`Migrate_parsetree` since `Ppxlib.Selected_ast.Ast` reexports one of
the `Migrate_parsetree.Versions` modules. This change is needed to be
compatible with (ocaml-migrate-parsetree 2.0 and) ppxlib 0.16 since it
no longer re-exports the `Ast_mapper` module from
`Migrate_parsetree`. This ppxlib change is one of the headline
simplification enablers noted in the (announcement of
ocaml-migrate-parsetree
2.0)[https://discuss.ocaml.org/t/ocaml-migrate-parsetree-2-0-0/5991].

Reviewed By: jvillard

Differential Revision: D23636203

fbshipit-source-id: 71e24b46b
master
Josh Berdine 4 years ago committed by Facebook GitHub Bot
parent 936ad83650
commit 77f2b7a02f

@ -40,7 +40,6 @@
open Ppxlib open Ppxlib
open Ast_builder.Default open Ast_builder.Default
module Ast_mapper = Selected_ast.Ast.Ast_mapper
let debug = ref false let debug = ref false
@ -75,56 +74,57 @@ let vb_stack_with, vb_stack_top =
(with_, top) (with_, top)
let mapper = let mapper =
let value_binding (m : Ast_mapper.mapper) vb = object
vb_stack_with vb.pvb_pat ~f:(fun () -> inherit Ast_traverse.map as super
Ast_mapper.default_mapper.value_binding m vb )
in method! value_binding vb =
let expr (m : Ast_mapper.mapper) exp = vb_stack_with vb.pvb_pat ~f:(fun () -> super#value_binding vb)
let append_here_args args =
let mod_name = evar ~loc:Location.none "Stdlib.__MODULE__" in method! expression exp =
let fun_name = let append_here_args args =
estring ~loc:Location.none (get_fun_name (vb_stack_top ())) let mod_name = evar ~loc:Location.none "Stdlib.__MODULE__" in
let fun_name =
estring ~loc:Location.none (get_fun_name (vb_stack_top ()))
in
(Nolabel, mod_name) :: (Nolabel, fun_name) :: args
in in
(Nolabel, mod_name) :: (Nolabel, fun_name) :: args match exp.pexp_desc with
in | Pexp_extension ({txt= "debug"; loc}, PStr []) -> ebool ~loc !debug
match exp.pexp_desc with | Pexp_extension
| Pexp_extension ({txt= "debug"; loc}, PStr []) -> ebool ~loc !debug ( { txt=
| Pexp_extension ( "Trace.info" | "Trace.infok" | "Trace.printf"
( { txt= | "Trace.fprintf" | "Trace.kprintf" ) as txt
( "Trace.info" | "Trace.infok" | "Trace.printf" ; loc }
| "Trace.fprintf" | "Trace.kprintf" ) as txt , PStr [{pstr_desc= Pstr_eval (arg, []); _}] ) ->
; loc } if not !debug then eunit ~loc:exp.pexp_loc
, PStr [{pstr_desc= Pstr_eval (arg, []); _}] ) -> else
if not !debug then eunit ~loc:exp.pexp_loc let args =
else match arg.pexp_desc with
let args = | Pexp_apply (op, args) -> (Nolabel, op) :: args
match arg.pexp_desc with | _ -> [(Nolabel, arg)]
| Pexp_apply (op, args) -> (Nolabel, op) :: args in
| _ -> [(Nolabel, arg)] pexp_apply ~loc:exp.pexp_loc (evar ~loc txt)
in (append_here_args args)
pexp_apply ~loc:exp.pexp_loc (evar ~loc txt) | Pexp_extension
(append_here_args args) ( {txt= "Trace.call"; loc= call_loc}
| Pexp_extension , PStr [{pstr_desc= Pstr_eval (call_fun, []); _}] ) ->
( {txt= "Trace.call"; loc= call_loc} if not !debug then eunit ~loc:exp.pexp_loc
, PStr [{pstr_desc= Pstr_eval (call_fun, []); _}] ) -> else
if not !debug then eunit ~loc:exp.pexp_loc pexp_apply ~loc:exp.pexp_loc
else (evar ~loc:call_loc "Trace.call")
pexp_apply ~loc:exp.pexp_loc (append_here_args [(Nolabel, call_fun)])
(evar ~loc:call_loc "Trace.call") | Pexp_extension
(append_here_args [(Nolabel, call_fun)]) ( {txt= "Trace.retn"; loc= retn_loc}
| Pexp_extension , PStr [{pstr_desc= Pstr_eval (retn_fun, []); _}] ) ->
( {txt= "Trace.retn"; loc= retn_loc} if not !debug then evar ~loc:exp.pexp_loc "Stdlib.Fun.id"
, PStr [{pstr_desc= Pstr_eval (retn_fun, []); _}] ) -> else
if not !debug then evar ~loc:exp.pexp_loc "Stdlib.Fun.id" pexp_apply ~loc:exp.pexp_loc
else (evar ~loc:retn_loc "Trace.retn")
pexp_apply ~loc:exp.pexp_loc (append_here_args [(Nolabel, retn_fun)])
(evar ~loc:retn_loc "Trace.retn") | _ -> super#expression exp
(append_here_args [(Nolabel, retn_fun)]) end
| _ -> Ast_mapper.default_mapper.expr m exp
in let impl = mapper#structure
{Ast_mapper.default_mapper with expr; value_binding}
let impl = Selected_ast.Ast.map_structure mapper
;; ;;
Driver.register_transformation "trace" ~impl Driver.register_transformation "trace" ~impl

Loading…
Cancel
Save