Hide benchmarks behind a feature flag (#5024)

* Hide benchmarks behind a feature flag

* Propage attributes in impl_runtime_apis macro

* Bump impl_version

* Fillter cfg attributes

* Hide more things under the feature

* Fix set_block_number availability

* Rename filter_attrs -> filter_cfg_attrs

* Rename runtime_benchmarks to runtime-benchmarks
This commit is contained in:
Benjamin Kampmann
2020-03-05 10:40:55 +01:00
committed by GitHub
parent 8bf855b09d
commit ff2a36d7cb
11 changed files with 68 additions and 16 deletions
@@ -27,7 +27,7 @@ use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature,
spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature, Attribute,
ImplItem, parse::{Parse, ParseStream, Result, Error}, PathArguments, GenericArgument, TypePath,
fold::{self, Fold}, parse_quote,
};
@@ -141,7 +141,7 @@ fn extract_runtime_block_ident(trait_: &Path) -> Result<&TypePath> {
fn generate_impl_calls(
impls: &[ItemImpl],
input: &Ident
) -> Result<Vec<(Ident, Ident, TokenStream)>> {
) -> Result<Vec<(Ident, Ident, TokenStream, Vec<Attribute>)>> {
let mut impl_calls = Vec::new();
for impl_ in impls {
@@ -162,9 +162,12 @@ fn generate_impl_calls(
&impl_trait
)?;
impl_calls.push(
(impl_trait_ident.clone(), method.sig.ident.clone(), impl_call)
);
impl_calls.push((
impl_trait_ident.clone(),
method.sig.ident.clone(),
impl_call,
filter_cfg_attrs(&impl_.attrs),
));
}
}
}
@@ -178,9 +181,12 @@ fn generate_dispatch_function(impls: &[ItemImpl]) -> Result<TokenStream> {
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &data)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
.map(|(trait_, fn_name, impl_, attrs)| {
let name = prefix_function_with_trait(&trait_, &fn_name);
quote!( #name => Some(#c::Encode::encode(&{ #impl_ })), )
quote!(
#( #attrs )*
#name => Some(#c::Encode::encode(&{ #impl_ })),
)
});
Ok(quote!(
@@ -200,13 +206,14 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result<TokenStream> {
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &input)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
.map(|(trait_, fn_name, impl_, attrs)| {
let fn_name = Ident::new(
&prefix_function_with_trait(&trait_, &fn_name),
Span::call_site()
);
quote!(
#( #attrs )*
#[cfg(not(feature = "std"))]
#[no_mangle]
pub fn #fn_name(input_data: *mut u8, input_len: usize) -> u64 {
@@ -447,6 +454,7 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result<TokenStream> {
let trait_ = extend_with_runtime_decl_path(trait_);
impl_.trait_.as_mut().unwrap().1 = trait_;
impl_.attrs = filter_cfg_attrs(&impl_.attrs);
impls_prepared.push(impl_);
}
@@ -622,6 +630,8 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
}
);
input.attrs = filter_cfg_attrs(&input.attrs);
// The implementation for the `RuntimeApiImpl` is only required when compiling with
// the feature `std` or `test`.
input.attrs.push(parse_quote!( #[cfg(any(feature = "std", test))] ));
@@ -695,8 +705,12 @@ fn generate_runtime_api_versions(impls: &[ItemImpl]) -> Result<TokenStream> {
let id: Path = parse_quote!( #path ID );
let version: Path = parse_quote!( #path VERSION );
let attrs = filter_cfg_attrs(&impl_.attrs);
result.push(quote!( (#id, #version) ));
result.push(quote!(
#( #attrs )*
(#id, #version)
));
}
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
@@ -745,3 +759,32 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
)
)
}
// Filters all attributes except the cfg ones.
fn filter_cfg_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
attrs.into_iter().filter(|a| a.path.is_ident("cfg")).cloned().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filter_non_cfg_attributes() {
let cfg_std: Attribute = parse_quote!(#[cfg(feature = "std")]);
let cfg_benchmarks: Attribute = parse_quote!(#[cfg(feature = "runtime-benchmarks")]);
let attrs = vec![
cfg_std.clone(),
parse_quote!(#[derive(Debug)]),
parse_quote!(#[test]),
cfg_benchmarks.clone(),
parse_quote!(#[allow(non_camel_case_types)]),
];
let filtered = filter_cfg_attrs(&attrs);
assert_eq!(filtered.len(), 2);
assert_eq!(cfg_std, filtered[0]);
assert_eq!(cfg_benchmarks, filtered[1]);
}
}