2016-07-21 12:50:15 -04:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! This pass is only used for UNIT TESTS related to incremental
|
|
|
|
//! compilation. It tests whether a particular `.o` file will be re-used
|
|
|
|
//! from a previous compilation or whether it must be regenerated.
|
|
|
|
//!
|
|
|
|
//! The user adds annotations to the crate of the following form:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! #![rustc_partition_reused(module="spike", cfg="rpass2")]
|
|
|
|
//! #![rustc_partition_translated(module="spike-x", cfg="rpass2")]
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The first indicates (in the cfg `rpass2`) that `spike.o` will be
|
|
|
|
//! reused, the second that `spike-x.o` will be recreated. If these
|
|
|
|
//! annotations are inaccurate, errors are reported.
|
|
|
|
//!
|
|
|
|
//! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
|
|
|
|
//! the HIR doesn't change as a result of the annotations, which might
|
|
|
|
//! perturb the reuse results.
|
|
|
|
|
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use syntax::ast;
|
|
|
|
|
|
|
|
use {ModuleSource, ModuleTranslation};
|
|
|
|
|
2017-03-30 15:27:27 +02:00
|
|
|
use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_TRANSLATED};
|
2016-07-21 12:50:15 -04:00
|
|
|
|
|
|
|
const MODULE: &'static str = "module";
|
|
|
|
const CFG: &'static str = "cfg";
|
|
|
|
|
2017-07-26 16:02:32 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
|
|
|
pub enum Disposition { Reused, Translated }
|
|
|
|
|
|
|
|
impl ModuleTranslation {
|
|
|
|
pub fn disposition(&self) -> (String, Disposition) {
|
|
|
|
let disposition = match self.source {
|
|
|
|
ModuleSource::Preexisting(_) => Disposition::Reused,
|
|
|
|
ModuleSource::Translated(_) => Disposition::Translated,
|
|
|
|
};
|
|
|
|
|
|
|
|
(self.name.clone(), disposition)
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 12:50:15 -04:00
|
|
|
|
2017-05-27 20:13:32 +02:00
|
|
|
pub(crate) fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-07-26 16:02:32 +02:00
|
|
|
modules: &[(String, Disposition)]) {
|
2016-07-21 12:50:15 -04:00
|
|
|
let _ignore = tcx.dep_graph.in_ignore();
|
|
|
|
|
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ams = AssertModuleSource { tcx: tcx, modules: modules };
|
2017-01-26 02:41:06 +02:00
|
|
|
for attr in &tcx.hir.krate().attrs {
|
2016-07-21 12:50:15 -04:00
|
|
|
ams.check_attr(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AssertModuleSource<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-07-26 16:02:32 +02:00
|
|
|
modules: &'a [(String, Disposition)],
|
2016-07-21 12:50:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
|
|
|
|
fn check_attr(&self, attr: &ast::Attribute) {
|
2017-03-30 15:27:27 +02:00
|
|
|
let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
|
2016-07-21 12:50:15 -04:00
|
|
|
Disposition::Reused
|
2017-03-30 15:27:27 +02:00
|
|
|
} else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
|
2016-07-21 12:50:15 -04:00
|
|
|
Disposition::Translated
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if !self.check_config(attr) {
|
|
|
|
debug!("check_attr: config does not match, ignoring attr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mname = self.field(attr, MODULE);
|
2017-07-26 16:02:32 +02:00
|
|
|
let mtrans = self.modules.iter().find(|&&(ref name, _)| name == mname.as_str());
|
2016-07-21 12:50:15 -04:00
|
|
|
let mtrans = match mtrans {
|
|
|
|
Some(m) => m,
|
|
|
|
None => {
|
|
|
|
debug!("module name `{}` not found amongst:", mname);
|
2017-07-26 16:02:32 +02:00
|
|
|
for &(ref name, ref disposition) in self.modules {
|
2016-07-21 12:50:15 -04:00
|
|
|
debug!("module named `{}` with disposition {:?}",
|
2017-07-26 16:02:32 +02:00
|
|
|
name,
|
|
|
|
disposition);
|
2016-07-21 12:50:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
self.tcx.sess.span_err(
|
|
|
|
attr.span,
|
|
|
|
&format!("no module named `{}`", mname));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-26 16:02:32 +02:00
|
|
|
let mtrans_disposition = mtrans.1;
|
2016-07-21 12:50:15 -04:00
|
|
|
if disposition != mtrans_disposition {
|
|
|
|
self.tcx.sess.span_err(
|
|
|
|
attr.span,
|
|
|
|
&format!("expected module named `{}` to be {:?} but is {:?}",
|
|
|
|
mname,
|
|
|
|
disposition,
|
|
|
|
mtrans_disposition));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 08:54:27 +00:00
|
|
|
fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
|
2017-03-03 09:23:59 +00:00
|
|
|
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
|
2016-07-21 12:50:15 -04:00
|
|
|
if item.check_name(name) {
|
|
|
|
if let Some(value) = item.value_str() {
|
2016-11-16 10:52:37 +00:00
|
|
|
return value;
|
2016-07-21 12:50:15 -04:00
|
|
|
} else {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
item.span,
|
|
|
|
&format!("associated value expected for `{}`", name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("no field `{}`", name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan for a `cfg="foo"` attribute and check whether we have a
|
|
|
|
/// cfg flag called `foo`.
|
|
|
|
fn check_config(&self, attr: &ast::Attribute) -> bool {
|
2016-10-27 06:36:56 +00:00
|
|
|
let config = &self.tcx.sess.parse_sess.config;
|
2016-07-21 12:50:15 -04:00
|
|
|
let value = self.field(attr, CFG);
|
|
|
|
debug!("check_config(config={:?}, value={:?})", config, value);
|
2016-11-15 08:54:27 +00:00
|
|
|
if config.iter().any(|&(name, _)| name == value) {
|
2016-07-21 12:50:15 -04:00
|
|
|
debug!("check_config: matched");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
debug!("check_config: no match found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|