diff --git a/soroban-sdk-macros/src/attribute.rs b/soroban-sdk-macros/src/attribute.rs new file mode 100644 index 00000000..5f58473d --- /dev/null +++ b/soroban-sdk-macros/src/attribute.rs @@ -0,0 +1,10 @@ +use syn::Attribute; + +/// Returns true if the attribute is an attribute that should be preserved and +/// passed through to code generated for the item the attribute is on. +pub fn pass_through_attr_to_gen_code(attr: &Attribute) -> bool { + attr.path().is_ident("doc") + || attr.path().is_ident("cfg") + || attr.path().is_ident("allow") + || attr.path().is_ident("deny") +} diff --git a/soroban-sdk-macros/src/derive_client.rs b/soroban-sdk-macros/src/derive_client.rs index aa896c63..bc9770a9 100644 --- a/soroban-sdk-macros/src/derive_client.rs +++ b/soroban-sdk-macros/src/derive_client.rs @@ -2,7 +2,7 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; use syn::{Error, FnArg, LitStr, Path, Type, TypePath, TypeReference}; -use crate::{symbol, syn_ext}; +use crate::{attribute::pass_through_attr_to_gen_code, symbol, syn_ext}; pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStream { let ty_str = quote!(#ty).to_string(); @@ -197,7 +197,11 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) -> .unzip(); let fn_output = f.output(); let fn_try_output = f.try_output(crate_path); - let fn_attrs = f.attrs; + let fn_attrs = f + .attrs + .iter() + .filter(|attr| pass_through_attr_to_gen_code(attr)) + .collect::>(); if cfg!(not(feature = "testutils")) { quote! { #(#fn_attrs)* diff --git a/soroban-sdk-macros/src/derive_fn.rs b/soroban-sdk-macros/src/derive_fn.rs index 0c24bfe7..bc0858b3 100644 --- a/soroban-sdk-macros/src/derive_fn.rs +++ b/soroban-sdk-macros/src/derive_fn.rs @@ -1,4 +1,4 @@ -use crate::map_type::map_type; +use crate::{attribute::pass_through_attr_to_gen_code, map_type::map_type}; use itertools::MultiUnzip; use proc_macro2::TokenStream as TokenStream2; use quote::{format_ident, quote}; @@ -144,6 +144,12 @@ pub fn derive_pub_fn( None }; + // Filter attributes to those that should be passed through to the generated code. + let attrs = attrs + .iter() + .filter(|attr| pass_through_attr_to_gen_code(attr)) + .collect::>(); + // Generated code. Ok(quote! { #[doc(hidden)] diff --git a/soroban-sdk-macros/src/derive_spec_fn.rs b/soroban-sdk-macros/src/derive_spec_fn.rs index 2c0afe99..ecb9d7fc 100644 --- a/soroban-sdk-macros/src/derive_spec_fn.rs +++ b/soroban-sdk-macros/src/derive_spec_fn.rs @@ -11,6 +11,7 @@ use syn::{ ReturnType, Type, TypePath, }; +use crate::attribute::pass_through_attr_to_gen_code; use crate::{doc::docs_from_attrs, map_type::map_type, DEFAULT_XDR_RW_LIMITS}; #[allow(clippy::too_many_arguments)] @@ -162,6 +163,12 @@ pub fn derive_fn_spec( None }; + // Filter attributes to those that should be passed through to the generated code. + let attrs = attrs + .iter() + .filter(|attr| pass_through_attr_to_gen_code(attr)) + .collect::>(); + // Generated code. Ok(quote! { #[doc(hidden)] diff --git a/soroban-sdk-macros/src/lib.rs b/soroban-sdk-macros/src/lib.rs index aae0303e..e3f8c8ff 100644 --- a/soroban-sdk-macros/src/lib.rs +++ b/soroban-sdk-macros/src/lib.rs @@ -1,6 +1,7 @@ extern crate proc_macro; mod arbitrary; +mod attribute; mod derive_args; mod derive_client; mod derive_enum;