[sledge] Use a cookie instead of env var to enable ppx_trace

Summary:
Passing info that affects the build through environment variables is
bad since it is not very visible, e.g. in log files. Also, those
settings are not recorded in .merlin files, leading to merlin using
the default configuration.

This diff changes ppx_trace to use a 'cookie' instead of an
environment variable, and configures dune to set this cookie based on
the selected profile. This has the benefit of using command line
arguments such as `--cookie 'ppx_trace_enabled="1"'` which appear in
build logs and .merlin files.

Reviewed By: jvillard

Differential Revision: D20590515

fbshipit-source-id: d2daceaa3
master
Josh Berdine 5 years ago committed by Facebook GitHub Bot
parent 1ce5eb8033
commit 31dd2884f6

@ -5,7 +5,10 @@
(library (library
(name ppx_sledge) (name ppx_sledge)
(kind ppx_rewriter) (kind
(ppx_rewriter
(cookies
(ppx_trace_enabled %{env:PPX_TRACE_ENABLED=0}))))
(libraries ppx_compare ppx_expect ppx_hash ppx_here ppx_inline_test ppx_let (libraries ppx_compare ppx_expect ppx_hash ppx_here ppx_inline_test ppx_let
ppx_sexp_conv ppx_sexp_value ppx_trace) ppx_sexp_conv ppx_sexp_value ppx_trace)
(preprocess no_preprocessing)) (preprocess no_preprocessing))

@ -7,16 +7,19 @@
(** Extension point rewriter for debug trace logging (** Extension point rewriter for debug trace logging
This ppx rewriter declares a [--debug] command line option, to be passed This ppx rewriter reads a cookie to determine whether to rewrite in
by the build system in debug but not optimized build modes. Setting the "debug" mode or not. To enable "debug" mode, pass
[PPX_TRACE_ENABLED] environment variable to [1] or [true] has the same [--cookie 'ppx_trace_enabled="1"'] (or with [true] instead or [1]).
effect as passing [--debug].
named "ppx_trace_enabled" declares a [--debug] command line option, to
be passed by the build system in debug but not optimized build modes.
Setting the [PPX_TRACE_ENABLED] environment variable to [1] or [true]
has the same effect as passing [--debug].
It rewrites [\[%Trace.info f\]] to a call It rewrites [\[%Trace.info f\]] to a call
[\[Trace.info mod_name fun_name f\]] where [mod_name] and [fun_name] are [\[Trace.info mod_name fun_name f\]] where [mod_name] and [fun_name] are
the enclosing module and function names in the parsetree. This is only the enclosing module and function names in the parsetree. This is only
done in debug mode, if [--debug] is not passed, then [\[%Trace.info f\]] done in debug mode, otherwise [\[%Trace.info f\]] is rewritten to [()].
is rewritten to [()].
Similarly, [\[%Trace.call\]] is rewritten to a call to [Trace.call] or Similarly, [\[%Trace.call\]] is rewritten to a call to [Trace.call] or
[()], and [\[%Trace.retn\]] to a call to [Trace.retn] or [Fn.id]. [()], and [\[%Trace.retn\]] to a call to [Trace.retn] or [Fn.id].
@ -38,20 +41,20 @@
only in debug mode. only in debug mode.
Additionally, [\[%debug\]] is rewritten to the compile-time boolean Additionally, [\[%debug\]] is rewritten to the compile-time boolean
constant determined by whether or not [--debug] is passed. *) constant indicating if rewriting was done in debug mode. *)
open Ppxlib open Ppxlib
open Ast_builder.Default open Ast_builder.Default
module Ast_mapper = Selected_ast.Ast.Ast_mapper module Ast_mapper = Selected_ast.Ast.Ast_mapper
let debug = let debug = ref false
ref
( match Sys.getenv_opt "PPX_TRACE_ENABLED" with
| Some ("1" | "true") -> true
| _ -> false )
;; ;;
Driver.add_arg "--debug" (Arg.Set debug) ~doc:"Enable debug tracing output" Driver.Cookies.add_simple_handler "ppx_trace_enabled" Ast_pattern.__
~f:(function
| Some {pexp_desc= Pexp_constant (Pconst_string (("1" | "true"), _))} ->
debug := true
| _ -> () )
let rec get_fun_name pat = let rec get_fun_name pat =
match pat.ppat_desc with match pat.ppat_desc with

Loading…
Cancel
Save