add simple ast dimension diagnostic

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-25 01:41:31 +00:00
parent 2100618d47
commit 8bb69eb81d
3 changed files with 35 additions and 0 deletions

View file

@ -1,5 +1,8 @@
use std::{any::Any, panic};
/// Export debug proc_macros
pub use conduit_macros::recursion_depth;
/// Export all of the ancillary tools from here as well.
pub use crate::utils::debug::*;

28
src/macros/debug.rs Normal file
View file

@ -0,0 +1,28 @@
use std::cmp;
use proc_macro::TokenStream;
use syn::{parse_macro_input, AttributeArgs, Item};
pub(super) fn recursion_depth(args: TokenStream, item_: TokenStream) -> TokenStream {
let item = item_.clone();
let item = parse_macro_input!(item as Item);
let _args = parse_macro_input!(args as AttributeArgs);
let mut best: usize = 0;
let mut count: usize = 0;
// think you'd find a fancy recursive ast visitor? think again
let tree = format!("{item:#?}");
for line in tree.lines() {
let trim = line.trim_start_matches(' ');
let diff = line.len().saturating_sub(trim.len());
let level = diff / 4;
best = cmp::max(level, best);
count = count.saturating_add(1);
}
println!("--- Recursion Diagnostic ---");
println!("DEPTH: {best}");
println!("LENGTH: {count}");
item_
}

View file

@ -1,5 +1,6 @@
mod admin;
mod cargo;
mod debug;
mod rustc;
mod utils;
@ -13,5 +14,8 @@ pub fn admin_command_dispatch(args: TokenStream, input: TokenStream) -> TokenStr
#[proc_macro_attribute]
pub fn cargo_manifest(args: TokenStream, input: TokenStream) -> TokenStream { cargo::manifest(args, input) }
#[proc_macro_attribute]
pub fn recursion_depth(args: TokenStream, input: TokenStream) -> TokenStream { debug::recursion_depth(args, input) }
#[proc_macro]
pub fn rustc_flags_capture(args: TokenStream) -> TokenStream { rustc::flags_capture(args) }