Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize runtime of #[cargo_test_macro] #7146

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions crates/cargo-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ description = "Helper proc-macro for Cargo's testsuite."

[lib]
proc-macro = true

[dependencies]
quote = "0.6"
syn = { version = "0.15", features = ["full"] }
69 changes: 51 additions & 18 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,59 @@
extern crate proc_macro;

use quote::{quote, ToTokens};
use syn::{parse::Parser, *};
use proc_macro::*;

#[proc_macro_attribute]
pub fn cargo_test(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut fn_def = parse_macro_input!(item as ItemFn);
pub fn cargo_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
let span = Span::call_site();
let mut ret = TokenStream::new();
ret.extend(Some(TokenTree::from(Punct::new('#', Spacing::Alone))));
let test = TokenTree::from(Ident::new("test", span));
ret.extend(Some(TokenTree::from(Group::new(
Delimiter::Bracket,
test.into(),
))));

let attr = quote! {
#[test]
};
fn_def
.attrs
.extend(Attribute::parse_outer.parse2(attr).unwrap());
for token in item {
let group = match token {
TokenTree::Group(g) => {
if g.delimiter() == Delimiter::Brace {
g
} else {
ret.extend(Some(TokenTree::Group(g)));
continue;
}
}
other => {
ret.extend(Some(other));
continue;
}
};

let stmt = quote! {
let _test_guard = crate::support::paths::init_root();
};
fn_def.block.stmts.insert(0, parse2(stmt).unwrap());
let mut new_body = vec![
TokenTree::from(Ident::new("let", span)),
TokenTree::from(Ident::new("_test_guard", span)),
TokenTree::from(Punct::new('=', Spacing::Alone)),
TokenTree::from(Ident::new("crate", span)),
TokenTree::from(Punct::new(':', Spacing::Joint)),
TokenTree::from(Punct::new(':', Spacing::Alone)),
TokenTree::from(Ident::new("support", span)),
TokenTree::from(Punct::new(':', Spacing::Joint)),
TokenTree::from(Punct::new(':', Spacing::Alone)),
TokenTree::from(Ident::new("paths", span)),
TokenTree::from(Punct::new(':', Spacing::Joint)),
TokenTree::from(Punct::new(':', Spacing::Alone)),
TokenTree::from(Ident::new("init_root", span)),
TokenTree::from(Group::new(Delimiter::Parenthesis, TokenStream::new())),
TokenTree::from(Punct::new(';', Spacing::Alone)),
]
.into_iter()
.collect::<TokenStream>();
new_body.extend(group.stream());
ret.extend(Some(TokenTree::from(Group::new(
group.delimiter(),
new_body,
))));
}

fn_def.into_token_stream().into()
return ret;
}