diff --git a/plugins/cairo-lang-macro-attributes/src/lib.rs b/plugins/cairo-lang-macro-attributes/src/lib.rs index 01d268ff2..2e1b92730 100644 --- a/plugins/cairo-lang-macro-attributes/src/lib.rs +++ b/plugins/cairo-lang-macro-attributes/src/lib.rs @@ -1,6 +1,7 @@ use proc_macro::TokenStream; use quote::quote; use scarb_stable_hash::short_hash; +use syn::spanned::Spanned; use syn::{parse_macro_input, ItemFn}; /// Constructs the attribute macro implementation. @@ -14,18 +15,26 @@ pub fn attribute_macro(_args: TokenStream, input: TokenStream) -> TokenStream { let original_item_name = item.sig.ident.to_string(); let item = hide_name(item); let item_name = &item.sig.ident; + + let callback_link = format!( + "EXPANSIONS_DESERIALIZE_{}", + item_name.to_string().to_uppercase() + ); + let callback_link = syn::Ident::new(callback_link.as_str(), item.span()); + let expanded = quote! { #item #[::cairo_lang_macro::linkme::distributed_slice(::cairo_lang_macro::MACRO_DEFINITIONS_SLICE)] #[linkme(crate = ::cairo_lang_macro::linkme)] - static MACRO_DEFINITIONS_SLICE_DESERIALIZE: ::cairo_lang_macro::ExpansionDefinition = + static #callback_link: ::cairo_lang_macro::ExpansionDefinition = ::cairo_lang_macro::ExpansionDefinition{ name: #original_item_name, kind: ::cairo_lang_macro::ExpansionKind::Attr, fun: #item_name, }; }; + TokenStream::from(expanded) } @@ -55,13 +64,21 @@ pub fn post_process(_args: TokenStream, input: TokenStream) -> TokenStream { let item: ItemFn = parse_macro_input!(input as ItemFn); let item = hide_name(item); let item_name = &item.sig.ident; + + let callback_link = format!( + "POST_PROCESS_DESERIALIZE_{}", + item_name.to_string().to_uppercase() + ); + let callback_link = syn::Ident::new(callback_link.as_str(), item.span()); + let expanded = quote! { #item #[::cairo_lang_macro::linkme::distributed_slice(::cairo_lang_macro::AUX_DATA_CALLBACKS)] #[linkme(crate = ::cairo_lang_macro::linkme)] - static AUX_DATA_CALLBACK_DESERIALIZE: fn(Vec) = #item_name; + static #callback_link: fn(Vec) = #item_name; }; + TokenStream::from(expanded) } diff --git a/plugins/cairo-lang-macro/src/types/mod.rs b/plugins/cairo-lang-macro/src/types/mod.rs index 52c3bc271..ef3ce2b1f 100644 --- a/plugins/cairo-lang-macro/src/types/mod.rs +++ b/plugins/cairo-lang-macro/src/types/mod.rs @@ -96,7 +96,7 @@ impl TokenStreamMetadata { /// For instance, auxiliary data can be serialized as JSON. /// /// ``` -/// use cairo_lang_macro::{AuxData, ProcMacroResult, TokenStream, attribute_macro, post_process_callback} +/// use cairo_lang_macro::{AuxData, ProcMacroResult, TokenStream, attribute_macro, post_process} /// use serde::{Serialize, Deserialize}; /// #[derive(Debug, Serialize, Deserialize)] /// struct SomeAuxDataFormat { @@ -117,7 +117,7 @@ impl TokenStreamMetadata { /// ProcMacroResult::replace(token_stream, Some(aux_data)) /// } /// -/// #[post_process_callback] +/// #[post_process] /// pub fn callback(aux_data: Vec) { /// let aux_data = aux_data.into_iter() /// .map(|aux_data| { @@ -131,7 +131,7 @@ impl TokenStreamMetadata { /// ``` /// /// All auxiliary data emitted during compilation can be consumed -/// in the `post_process_callback` implementation. +/// in the `post_process` implementation. #[derive(Debug, Clone)] pub struct AuxData(Vec); diff --git a/scarb/tests/build_cairo_plugin.rs b/scarb/tests/build_cairo_plugin.rs index 4a4d96182..3aad802ca 100644 --- a/scarb/tests/build_cairo_plugin.rs +++ b/scarb/tests/build_cairo_plugin.rs @@ -498,6 +498,11 @@ fn can_return_aux_data_from_plugin() { .collect::>(); println!("{:?}", aux_data); } + + #[post_process] + pub fn some_no_op_callback(aux_data: Vec) { + drop(aux_data); + } "##}, );