Strip #[test] nodes during cfg processing on non-test builds.

This commit is contained in:
Jeffrey Seyfried 2016-06-01 01:27:12 +00:00
parent 0554abac63
commit 66b9ade341
4 changed files with 20 additions and 16 deletions

View File

@ -582,6 +582,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
sess.track_errors(|| {
syntax::config::strip_unconfigured_items(sess.diagnostic(),
krate,
sess.opts.test,
&mut feature_gated_cfgs)
})
})?;
@ -692,6 +693,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
features: Some(&features),
recursion_limit: sess.recursion_limit.get(),
trace_mac: sess.opts.debugging_opts.trace_macros,
should_test: sess.opts.test,
};
let mut loader = macro_import::MacroLoader::new(sess, &cstore, crate_name);
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,

View File

@ -42,16 +42,19 @@ pub trait CfgFolder: fold::Folder {
/// configuration.
pub struct StripUnconfigured<'a> {
diag: CfgDiagReal<'a, 'a>,
should_test: bool,
config: &'a ast::CrateConfig,
}
impl<'a> StripUnconfigured<'a> {
pub fn new(config: &'a ast::CrateConfig,
should_test: bool,
diagnostic: &'a Handler,
feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
-> Self {
StripUnconfigured {
config: config,
should_test: should_test,
diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
}
}
@ -96,6 +99,11 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
// configuration based on the item's attributes
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
attrs.iter().all(|attr| {
// When not compiling with --test we should not compile the #[test] functions
if !self.should_test && is_test_or_bench(attr) {
return false;
}
let mis = match attr.node.value.node {
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
_ => return true
@ -135,12 +143,12 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, should_test: bool,
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
-> ast::Crate
{
let config = &krate.config.clone();
StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate)
StripUnconfigured::new(config, should_test, diagnostic, feature_gated_cfgs).fold_crate(krate)
}
impl<T: CfgFolder> fold::Folder for T {
@ -278,6 +286,10 @@ fn is_cfg(attr: &ast::Attribute) -> bool {
attr.check_name("cfg")
}
fn is_test_or_bench(attr: &ast::Attribute) -> bool {
attr.check_name("test") || attr.check_name("bench")
}
pub trait CfgDiag {
fn emit_error<F>(&mut self, f: F) where F: FnMut(&Handler);
fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>);

View File

@ -1001,6 +1001,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
fn strip_unconfigured(&mut self) -> StripUnconfigured {
StripUnconfigured::new(&self.cx.cfg,
self.cx.ecfg.should_test,
&self.cx.parse_sess.span_diagnostic,
self.cx.feature_gated_cfgs)
}
@ -1106,6 +1107,7 @@ pub struct ExpansionConfig<'feat> {
pub features: Option<&'feat Features>,
pub recursion_limit: usize,
pub trace_mac: bool,
pub should_test: bool, // If false, strip `#[test]` nodes
}
macro_rules! feature_tests {
@ -1128,6 +1130,7 @@ impl<'feat> ExpansionConfig<'feat> {
features: None,
recursion_limit: 64,
trace_mac: false,
should_test: false,
}
}

View File

@ -81,7 +81,7 @@ pub fn modify_for_testing(sess: &ParseSess,
if should_test {
generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic)
} else {
strip_test_functions(krate)
krate
}
}
@ -306,19 +306,6 @@ fn generate_test_harness(sess: &ParseSess,
return res;
}
fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
// When not compiling with --test we should not compile the
// #[test] functions
struct StripTests;
impl config::CfgFolder for StripTests {
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
!attr::contains_name(attrs, "test") && !attr::contains_name(attrs, "bench")
}
}
StripTests.fold_crate(krate)
}
/// Craft a span that will be ignored by the stability lint's
/// call to codemap's is_internal check.
/// The expanded code calls some unstable functions in the test crate.