|
| 1 | +use itertools::Itertools; |
| 2 | +use proc_macro::TokenStream; |
| 3 | +use quote::{quote, ToTokens}; |
| 4 | +use syn::{ |
| 5 | + parse::{Parse, Parser}, |
| 6 | + Expr, Meta, MetaNameValue, Token, Type, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Values from parsed options shared between `#[divan::bench]` and |
| 10 | +/// `#[divan::bench_group]`. |
| 11 | +/// |
| 12 | +/// The `crate` option is not included because it is only needed to get proper |
| 13 | +/// access to `__private`. |
| 14 | +#[derive(Default)] |
| 15 | +pub(crate) struct AttrOptions { |
| 16 | + pub(crate) types: Option<GenericTypes>, |
| 17 | + pub(crate) crate_: bool, |
| 18 | + pub(crate) other_args: Vec<Meta>, |
| 19 | +} |
| 20 | + |
| 21 | +#[allow(unreachable_code)] |
| 22 | +impl AttrOptions { |
| 23 | + pub fn parse(tokens: TokenStream) -> Result<Self, TokenStream> { |
| 24 | + let mut attr_options = Self::default(); |
| 25 | + |
| 26 | + let attr_parser = syn::meta::parser(|meta| { |
| 27 | + let Some(ident) = meta.path.get_ident() else { |
| 28 | + return Err(meta.error("Unexpected attribute")); |
| 29 | + }; |
| 30 | + |
| 31 | + let ident_name = ident.to_string(); |
| 32 | + let ident_name = ident_name.strip_prefix("r#").unwrap_or(&ident_name); |
| 33 | + |
| 34 | + match ident_name { |
| 35 | + // Divan accepts type syntax that is not parseable into syn::Meta out of the box, |
| 36 | + // so we parse and rebuild the arguments manually. |
| 37 | + "types" => { |
| 38 | + attr_options.types = Some(meta.value()?.parse()?); |
| 39 | + } |
| 40 | + "crate" => { |
| 41 | + attr_options.crate_ = true; |
| 42 | + meta.value()?.parse::<Expr>()?; // Discard the value |
| 43 | + } |
| 44 | + "min_time" | "max_time" | "sample_size" | "sample_count" | "skip_ext_time" => { |
| 45 | + // These arguments are ignored for codspeed runs |
| 46 | + meta.value()?.parse::<Expr>()?; // Discard the value |
| 47 | + } |
| 48 | + _ => { |
| 49 | + let path = meta.path.clone(); |
| 50 | + let parsed_meta = if meta.input.is_empty() { |
| 51 | + Meta::Path(path) |
| 52 | + } else { |
| 53 | + let value: syn::Expr = meta.value()?.parse()?; |
| 54 | + Meta::NameValue(MetaNameValue { |
| 55 | + path, |
| 56 | + eq_token: Default::default(), |
| 57 | + value: Expr::Verbatim(value.into_token_stream()), |
| 58 | + }) |
| 59 | + }; |
| 60 | + |
| 61 | + attr_options.other_args.push(parsed_meta); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Ok(()) |
| 66 | + }); |
| 67 | + |
| 68 | + match attr_parser.parse(tokens) { |
| 69 | + Ok(()) => {} |
| 70 | + Err(error) => return Err(error.into_compile_error().into()), |
| 71 | + } |
| 72 | + |
| 73 | + Ok(attr_options) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Generic types over which to instantiate benchmark functions. |
| 78 | +pub(crate) enum GenericTypes { |
| 79 | + /// List of types, e.g. `[i32, String, ()]`. |
| 80 | + List(Vec<proc_macro2::TokenStream>), |
| 81 | +} |
| 82 | + |
| 83 | +impl Parse for GenericTypes { |
| 84 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 85 | + let content; |
| 86 | + syn::bracketed!(content in input); |
| 87 | + |
| 88 | + Ok(Self::List( |
| 89 | + content |
| 90 | + .parse_terminated(Type::parse, Token![,])? |
| 91 | + .into_iter() |
| 92 | + .map(|ty| ty.into_token_stream()) |
| 93 | + .collect(), |
| 94 | + )) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl ToTokens for GenericTypes { |
| 99 | + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 100 | + match self { |
| 101 | + Self::List(list) => { |
| 102 | + let type_tokens = list.iter().cloned().map_into::<proc_macro2::TokenStream>(); |
| 103 | + tokens.extend(quote! { [ #(#type_tokens),* ] }); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments