diff --git a/components/salsa-2022-macros/src/debug.rs b/components/salsa-2022-macros/src/debug.rs new file mode 100644 index 00000000..ed9e2a8b --- /dev/null +++ b/components/salsa-2022-macros/src/debug.rs @@ -0,0 +1,23 @@ +use std::sync::OnceLock; + +use proc_macro2::TokenStream; + +static SALSA_DEBUG_MACRO: OnceLock> = 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 +} diff --git a/components/salsa-2022-macros/src/lib.rs b/components/salsa-2022-macros/src/lib.rs index a1476b07..0fcdccaf 100644 --- a/components/salsa-2022-macros/src/lib.rs +++ b/components/salsa-2022-macros/src/lib.rs @@ -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; diff --git a/components/salsa-2022-macros/src/tracked_fn.rs b/components/salsa-2022-macros/src/tracked_fn.rs index e7a03ccb..cfd3d9aa 100644 --- a/components/salsa-2022-macros/src/tracked_fn.rs +++ b/components/salsa-2022-macros/src/tracked_fn.rs @@ -10,6 +10,8 @@ pub(crate) fn tracked_fn( args: proc_macro::TokenStream, mut item_fn: syn::ItemFn, ) -> syn::Result { + 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; diff --git a/components/salsa-2022-macros/src/tracked_struct.rs b/components/salsa-2022-macros/src/tracked_struct.rs index 3c41e3d3..479867d9 100644 --- a/components/salsa-2022-macros/src/tracked_struct.rs +++ b/components/salsa-2022-macros/src/tracked_struct.rs @@ -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);