How to change all the elements of a box in a compiler plugin?

I am trying to build a syntax extension that extends an attribute into calls. Before:

#[flame] fn flamed() { .. } 

After:

 fn flamed() { flame::start_guard("flamed"); .. } 

This is already working . However, I would also like it to work if I have the #[flame] attribute at the box level (for example, #![flame] ). Is this possible, and if so, how?

+5
source share
1 answer

Comment by @huon

Have you tried to catch ItemKind :: Mod in github.com/llogiq/flamer/blob / ... and repeat its contents (recursively)?

there was a spot on - I just added a fixation that handles trendy and roady elements while skipping them. I will also probably add code for functions to handle internal elements and fns.

The code is as follows:

 fn flame_item(i: &Item) -> Item { let base = i.clone(); Item { node: match i.node { ItemKind::Mod(ref m) => ItemKind::Mod(flame_mod(m)), ItemKind::Trait(unsafety, ref generic, ref bounds, ref tis) => ItemKind::Trait(unsafety, generic.clone(), bounds.clone(), flame_items(tis)), .. // other item types as usual: items, traitimpls, implitems _ => return base }, ..base } } fn flame_mod(m: &Mod) -> Mod { Mod { inner: m.inner, items: m.items.iter().map(|i| P(flame_item(i))).collect() } } fn flame_items(items: &[TraitItem]) -> Vec<TraitItem> { items.iter().map(flame_trait_item).collect() } 
+2
source

All Articles