Skip to content

Commit

Permalink
Expand generated test harnesses and macro registries.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Sep 13, 2016
1 parent f3c2dca commit 78c0039
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,

krate = time(time_passes, "maybe building test harness", || {
syntax::test::modify_for_testing(&sess.parse_sess,
&mut resolver,
sess.opts.test,
krate,
sess.diagnostic())
Expand All @@ -700,6 +701,7 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
let num_crate_types = crate_types.len();
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
&mut resolver,
krate,
is_rustc_macro_crate,
num_crate_types,
Expand Down
30 changes: 15 additions & 15 deletions src/libsyntax/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use errors;
use errors::snippet::{SnippetData};
use config;
use entry::{self, EntryPointType};
use ext::base::{ExtCtxt, DummyResolver};
use ext::base::{ExtCtxt, Resolver};
use ext::build::AstBuilder;
use ext::expand::ExpansionConfig;
use fold::Folder;
Expand Down Expand Up @@ -70,6 +70,7 @@ struct TestCtxt<'a> {
// Traverse the crate, collecting all the test functions, eliding any
// existing main functions, and synthesizing a main test harness
pub fn modify_for_testing(sess: &ParseSess,
resolver: &mut Resolver,
should_test: bool,
krate: ast::Crate,
span_diagnostic: &errors::Handler) -> ast::Crate {
Expand All @@ -82,7 +83,7 @@ pub fn modify_for_testing(sess: &ParseSess,
"reexport_test_harness_main");

if should_test {
generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic)
generate_test_harness(sess, resolver, reexport_test_harness_main, krate, span_diagnostic)
} else {
krate
}
Expand Down Expand Up @@ -248,41 +249,39 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
}).chain(tested_submods.into_iter().map(|(r, sym)| {
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Visibility::Public, r, path)
}));
})).collect();

let reexport_mod = ast::Mod {
inner: DUMMY_SP,
items: items.collect(),
items: items,
};

let sym = token::gensym_ident("__test_reexports");
let it = P(ast::Item {
let it = cx.ext_cx.expander().fold_item(P(ast::Item {
ident: sym.clone(),
attrs: Vec::new(),
id: ast::DUMMY_NODE_ID,
node: ast::ItemKind::Mod(reexport_mod),
vis: ast::Visibility::Public,
span: DUMMY_SP,
});
})).pop().unwrap();

(it, sym)
}

fn generate_test_harness(sess: &ParseSess,
resolver: &mut Resolver,
reexport_test_harness_main: Option<InternedString>,
krate: ast::Crate,
sd: &errors::Handler) -> ast::Crate {
// Remove the entry points
let mut cleaner = EntryPointCleaner { depth: 0 };
let krate = cleaner.fold_crate(krate);

let mut resolver = DummyResolver;
let mut cx: TestCtxt = TestCtxt {
sess: sess,
span_diagnostic: sd,
ext_cx: ExtCtxt::new(sess, vec![],
ExpansionConfig::default("test".to_string()),
&mut resolver),
ext_cx: ExtCtxt::new(sess, vec![], ExpansionConfig::default("test".to_string()), resolver),
path: Vec::new(),
testfns: Vec::new(),
reexport_test_harness_main: reexport_test_harness_main,
Expand Down Expand Up @@ -511,16 +510,17 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
items: vec![import, mainfn, tests],
};
let item_ = ast::ItemKind::Mod(testmod);

let mod_ident = token::gensym_ident("__test");
let item = P(ast::Item {

let mut expander = cx.ext_cx.expander();
let item = expander.fold_item(P(ast::Item {
id: ast::DUMMY_NODE_ID,
ident: mod_ident,
attrs: vec![],
node: item_,
vis: ast::Visibility::Public,
span: DUMMY_SP,
});
})).pop().unwrap();
let reexport = cx.reexport_test_harness_main.as_ref().map(|s| {
// building `use <ident> = __test::main`
let reexport_ident = token::str_to_ident(&s);
Expand All @@ -529,14 +529,14 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
nospan(ast::ViewPathSimple(reexport_ident,
path_node(vec![mod_ident, token::str_to_ident("main")])));

P(ast::Item {
expander.fold_item(P(ast::Item {
id: ast::DUMMY_NODE_ID,
ident: keywords::Invalid.ident(),
attrs: vec![],
node: ast::ItemKind::Use(P(use_path)),
vis: ast::Visibility::Inherited,
span: DUMMY_SP
})
})).pop().unwrap()
});

debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&item));
Expand Down
23 changes: 10 additions & 13 deletions src/libsyntax_ext/rustc_macro_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use std::mem;
use errors;
use syntax::ast::{self, Ident, NodeId};
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute};
use syntax::ext::base::{ExtCtxt, DummyResolver};
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::ext::expand::ExpansionConfig;
use syntax::parse::ParseSess;
use syntax::parse::token::{self, InternedString};
use syntax::feature_gate::Features;
use syntax::fold::Folder;
use syntax::ptr::P;
use syntax_pos::{Span, DUMMY_SP};
use syntax::visit::{self, Visitor};
Expand All @@ -39,16 +40,14 @@ struct CollectCustomDerives<'a> {
}

pub fn modify(sess: &ParseSess,
resolver: &mut ::syntax::ext::base::Resolver,
mut krate: ast::Crate,
is_rustc_macro_crate: bool,
num_crate_types: usize,
handler: &errors::Handler,
features: &Features) -> ast::Crate {
let mut loader = DummyResolver;
let mut cx = ExtCtxt::new(sess,
Vec::new(),
ExpansionConfig::default("rustc_macro".to_string()),
&mut loader);
let ecfg = ExpansionConfig::default("rustc_macro".to_string());
let mut cx = ExtCtxt::new(sess, Vec::new(), ecfg, resolver);

let mut collect = CollectCustomDerives {
derives: Vec::new(),
Expand Down Expand Up @@ -268,13 +267,11 @@ fn mk_registrar(cx: &mut ExtCtxt,
i.vis = ast::Visibility::Public;
i
});
let module = cx.item_mod(span,
span,
ast::Ident::with_empty_ctxt(token::gensym("registrar")),
Vec::new(),
vec![krate, func]);
module.map(|mut i| {
let ident = ast::Ident::with_empty_ctxt(token::gensym("registrar"));
let module = cx.item_mod(span, span, ident, Vec::new(), vec![krate, func]).map(|mut i| {
i.vis = ast::Visibility::Public;
i
})
});

cx.expander().fold_item(module).pop().unwrap()
}

0 comments on commit 78c0039

Please sign in to comment.