|
| 1 | +use std::fs; |
| 2 | + |
| 3 | +use profile::StopWatch; |
| 4 | +use rustc_hash::FxHashMap; |
| 5 | +use syntax::{ |
| 6 | + ast::{self, NameOwner}, |
| 7 | + AstNode, |
| 8 | +}; |
| 9 | +use test_utils::{project_dir, skip_slow_tests}; |
| 10 | + |
| 11 | +use crate::{ast_to_token_tree, MacroRules}; |
| 12 | + |
| 13 | +#[test] |
| 14 | +fn parse_a_lot_macro_rules() { |
| 15 | + if skip_slow_tests() { |
| 16 | + return; |
| 17 | + } |
| 18 | + let rules = macro_rules_fixtures_tt(); |
| 19 | + let mut time = StopWatch::start(); |
| 20 | + for (_, tt) in rules { |
| 21 | + MacroRules::parse(&tt).unwrap(); |
| 22 | + } |
| 23 | + println!("elapsed time : {}", time.elapsed()); |
| 24 | +} |
| 25 | + |
| 26 | +#[test] |
| 27 | +fn expand_a_lot_macro_rules() { |
| 28 | + if skip_slow_tests() { |
| 29 | + return; |
| 30 | + } |
| 31 | + let rules = macro_rules_fixtures(); |
| 32 | + let invocations = invocation_fixtures(); |
| 33 | + |
| 34 | + let mut time = StopWatch::start(); |
| 35 | + for (idx, (id, tt)) in invocations.into_iter().enumerate() { |
| 36 | + let res = rules[&id].expand(&tt); |
| 37 | + if let Some(err) = res.err { |
| 38 | + println!("Faile to expand [{}] {}: error = {}, content = {}", idx, id, err, tt); |
| 39 | + } |
| 40 | + } |
| 41 | + println!("elapsed time : {}", time.elapsed()); |
| 42 | +} |
| 43 | + |
| 44 | +fn macro_rules_fixtures() -> FxHashMap<String, MacroRules> { |
| 45 | + macro_rules_fixtures_tt() |
| 46 | + .into_iter() |
| 47 | + .map(|(id, tt)| (id, MacroRules::parse(&tt).unwrap())) |
| 48 | + .collect() |
| 49 | +} |
| 50 | + |
| 51 | +fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> { |
| 52 | + let rules = macro_rules_fixtures_ast(); |
| 53 | + rules |
| 54 | + .into_iter() |
| 55 | + .map(|(id, rule)| { |
| 56 | + let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap(); |
| 57 | + (id, def_tt) |
| 58 | + }) |
| 59 | + .collect() |
| 60 | +} |
| 61 | + |
| 62 | +fn macro_rules_fixtures_ast() -> FxHashMap<String, ast::MacroRules> { |
| 63 | + let file = project_dir().join("crates/mbe/test_data/fixture-rules.rs"); |
| 64 | + let big_file = fs::read_to_string(file).unwrap(); |
| 65 | + let source_file = ast::SourceFile::parse(&big_file).ok().unwrap(); |
| 66 | + |
| 67 | + source_file |
| 68 | + .syntax() |
| 69 | + .descendants() |
| 70 | + .filter_map(ast::MacroRules::cast) |
| 71 | + .map(|it| (it.name().unwrap().to_string(), it)) |
| 72 | + .collect() |
| 73 | +} |
| 74 | + |
| 75 | +fn invocation_fixtures() -> FxHashMap<String, tt::Subtree> { |
| 76 | + let file = project_dir().join("crates/mbe/test_data/fixture-invocations.rs"); |
| 77 | + let big_file = fs::read_to_string(file).unwrap(); |
| 78 | + // Replace $crate to crate, since $crate may transform to crate name in realilty |
| 79 | + let big_file = big_file.replace("$crate", "crate"); |
| 80 | + |
| 81 | + let source_file = ast::SourceFile::parse(&big_file).ok().unwrap(); |
| 82 | + |
| 83 | + source_file |
| 84 | + .syntax() |
| 85 | + .descendants() |
| 86 | + .filter_map(ast::MacroCall::cast) |
| 87 | + .map(|it| { |
| 88 | + let id = it.path().unwrap().to_string(); |
| 89 | + let (invocation_tt, _) = ast_to_token_tree(&it.token_tree().unwrap()).unwrap(); |
| 90 | + (id, invocation_tt) |
| 91 | + }) |
| 92 | + .collect() |
| 93 | +} |
0 commit comments