rework debugging to be more permanent

This commit is contained in:
Niko Matsakis 2024-05-05 09:57:38 -04:00
parent fe4ff9816a
commit 04e041b4a2
4 changed files with 37 additions and 14 deletions

View file

@ -0,0 +1,23 @@
use std::sync::OnceLock;
use proc_macro2::TokenStream;
static SALSA_DEBUG_MACRO: OnceLock<Option<String>> = OnceLock::new();
pub(crate) fn debug_enabled(input_name: impl ToString) -> bool {
let Some(env_name) = SALSA_DEBUG_MACRO.get_or_init(|| std::env::var("SALSA_DEBUG_MACRO").ok())
else {
return false;
};
let input_name = input_name.to_string();
env_name == "*" || env_name == &input_name[..]
}
pub(crate) fn dump_tokens(input_name: impl ToString, tokens: TokenStream) -> TokenStream {
if debug_enabled(input_name) {
eprintln!("{}", tokens);
}
tokens
}

View file

@ -39,6 +39,7 @@ pub(crate) fn literal(ident: &proc_macro2::Ident) -> proc_macro2::Literal {
mod accumulator;
mod configuration;
mod db;
mod debug;
mod input;
mod interned;
mod jar;

View file

@ -10,6 +10,8 @@ pub(crate) fn tracked_fn(
args: proc_macro::TokenStream,
mut item_fn: syn::ItemFn,
) -> syn::Result<TokenStream> {
let fn_ident = item_fn.sig.ident.clone();
let args: FnArgs = syn::parse(args)?;
if item_fn.sig.inputs.is_empty() {
return Err(syn::Error::new(
@ -44,14 +46,17 @@ pub(crate) fn tracked_fn(
let (config_ty, fn_struct) = fn_struct(&args, &item_fn)?;
*item_fn.block = getter_fn(&args, &mut item_fn.sig, item_fn.block.span(), &config_ty)?;
Ok(quote! {
#fn_struct
Ok(crate::debug::dump_tokens(
&fn_ident,
quote! {
#fn_struct
// we generate a `'db` lifetime that clippy
// sometimes doesn't like
#[allow(clippy::needless_lifetimes)]
#item_fn
})
// we generate a `'db` lifetime that clippy
// sometimes doesn't like
#[allow(clippy::needless_lifetimes)]
#item_fn
},
))
}
type FnArgs = Options<TrackedFn>;

View file

@ -16,13 +16,7 @@ pub(crate) fn tracked(
let tokens = SalsaStruct::with_struct(args, struct_item)
.and_then(|el| TrackedStruct(el).generate_tracked())?;
if let Ok(name) = std::env::var("NDM") {
if name == "*" || name == &struct_name.to_string()[..] {
eprintln!("{}", tokens);
}
}
Ok(tokens)
Ok(crate::debug::dump_tokens(&struct_name, tokens))
}
struct TrackedStruct(SalsaStruct<Self>);