2016-07-21 16:50:15 +00:00
|
|
|
//! 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")]
|
2018-05-08 13:10:16 +00:00
|
|
|
//! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
|
2016-07-21 16:50:15 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! 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.
|
2018-09-18 14:33:24 +00:00
|
|
|
//!
|
|
|
|
//! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]
|
|
|
|
//! allows for doing a more fine-grained check to see if pre- or post-lto data
|
|
|
|
//! was re-used.
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2018-07-10 17:16:22 +00:00
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2018-09-18 14:33:24 +00:00
|
|
|
use rustc::dep_graph::cgu_reuse_tracker::*;
|
2018-08-14 15:55:22 +00:00
|
|
|
use rustc::mir::mono::CodegenUnitNameBuilder;
|
2016-07-21 16:50:15 +00:00
|
|
|
use rustc::ty::TyCtxt;
|
2018-09-18 14:33:24 +00:00
|
|
|
use std::collections::BTreeSet;
|
2016-07-21 16:50:15 +00:00
|
|
|
use syntax::ast;
|
2019-05-08 03:21:18 +00:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2018-09-18 14:33:24 +00:00
|
|
|
use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_CODEGENED,
|
|
|
|
ATTR_EXPECTED_CGU_REUSE};
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2019-05-08 03:21:18 +00:00
|
|
|
const MODULE: Symbol = sym::module;
|
|
|
|
const CFG: Symbol = sym::cfg;
|
|
|
|
const KIND: Symbol = sym::kind;
|
2017-07-26 14:02:32 +00:00
|
|
|
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn assert_module_sources(tcx: TyCtxt<'_>) {
|
2017-12-28 05:05:45 +00:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2018-09-18 14:33:24 +00:00
|
|
|
let available_cgus = tcx
|
|
|
|
.collect_and_partition_mono_items(LOCAL_CRATE)
|
|
|
|
.1
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| format!("{}", cgu.name()))
|
|
|
|
.collect::<BTreeSet<String>>();
|
|
|
|
|
|
|
|
let ams = AssertModuleSource {
|
|
|
|
tcx,
|
|
|
|
available_cgus
|
|
|
|
};
|
|
|
|
|
2018-12-04 12:45:36 +00:00
|
|
|
for attr in &tcx.hir().krate().attrs {
|
2017-12-28 05:05:45 +00:00
|
|
|
ams.check_attr(attr);
|
|
|
|
}
|
|
|
|
})
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct AssertModuleSource<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-09-18 14:33:24 +00:00
|
|
|
available_cgus: BTreeSet<String>,
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl AssertModuleSource<'tcx> {
|
2016-07-21 16:50:15 +00:00
|
|
|
fn check_attr(&self, attr: &ast::Attribute) {
|
2018-09-18 14:33:24 +00:00
|
|
|
let (expected_reuse, comp_kind) = if attr.check_name(ATTR_PARTITION_REUSED) {
|
|
|
|
(CguReuse::PreLto, ComparisonKind::AtLeast)
|
2018-05-08 13:10:16 +00:00
|
|
|
} else if attr.check_name(ATTR_PARTITION_CODEGENED) {
|
2018-09-18 14:33:24 +00:00
|
|
|
(CguReuse::No, ComparisonKind::Exact)
|
|
|
|
} else if attr.check_name(ATTR_EXPECTED_CGU_REUSE) {
|
|
|
|
match &self.field(attr, KIND).as_str()[..] {
|
|
|
|
"no" => (CguReuse::No, ComparisonKind::Exact),
|
|
|
|
"pre-lto" => (CguReuse::PreLto, ComparisonKind::Exact),
|
|
|
|
"post-lto" => (CguReuse::PostLto, ComparisonKind::Exact),
|
|
|
|
"any" => (CguReuse::PreLto, ComparisonKind::AtLeast),
|
|
|
|
other => {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("unknown cgu-reuse-kind `{}` specified", other));
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 16:50:15 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2018-09-18 14:33:24 +00:00
|
|
|
if !self.tcx.sess.opts.debugging_opts.query_dep_graph {
|
|
|
|
self.tcx.sess.span_fatal(
|
|
|
|
attr.span,
|
|
|
|
&format!("found CGU-reuse attribute but `-Zquery-dep-graph` \
|
|
|
|
was not specified"));
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:50:15 +00:00
|
|
|
if !self.check_config(attr) {
|
|
|
|
debug!("check_attr: config does not match, ignoring attr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:16:22 +00:00
|
|
|
let user_path = self.field(attr, MODULE).as_str().to_string();
|
|
|
|
let crate_name = self.tcx.crate_name(LOCAL_CRATE).as_str().to_string();
|
|
|
|
|
|
|
|
if !user_path.starts_with(&crate_name) {
|
|
|
|
let msg = format!("Found malformed codegen unit name `{}`. \
|
|
|
|
Codegen units names must always start with the name of the \
|
|
|
|
crate (`{}` in this case).", user_path, crate_name);
|
|
|
|
self.tcx.sess.span_fatal(attr.span, &msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split of the "special suffix" if there is one.
|
|
|
|
let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind(".") {
|
|
|
|
(&user_path[..index], Some(&user_path[index + 1 ..]))
|
|
|
|
} else {
|
|
|
|
(&user_path[..], None)
|
|
|
|
};
|
|
|
|
|
2018-08-22 10:55:47 +00:00
|
|
|
let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
// Remove the crate name
|
|
|
|
assert_eq!(cgu_path_components.remove(0), crate_name);
|
|
|
|
|
2018-08-14 15:55:22 +00:00
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
|
|
|
|
let cgu_name = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
|
|
|
|
cgu_path_components,
|
|
|
|
cgu_special_suffix);
|
2018-07-10 17:16:22 +00:00
|
|
|
|
|
|
|
debug!("mapping '{}' to cgu name '{}'", self.field(attr, MODULE), cgu_name);
|
2016-07-21 16:50:15 +00:00
|
|
|
|
2018-09-18 14:33:24 +00:00
|
|
|
if !self.available_cgus.contains(&cgu_name.as_str()[..]) {
|
2018-07-10 17:16:22 +00:00
|
|
|
self.tcx.sess.span_err(attr.span,
|
2018-09-18 14:33:24 +00:00
|
|
|
&format!("no module named `{}` (mangled: {}). \
|
|
|
|
Available modules: {}",
|
2018-07-10 17:16:22 +00:00
|
|
|
user_path,
|
|
|
|
cgu_name,
|
2018-09-18 14:33:24 +00:00
|
|
|
self.available_cgus
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")));
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
2018-09-18 14:33:24 +00:00
|
|
|
|
|
|
|
self.tcx.sess.cgu_reuse_tracker.set_expectation(&cgu_name.as_str(),
|
|
|
|
&user_path,
|
|
|
|
attr.span,
|
|
|
|
expected_reuse,
|
|
|
|
comp_kind);
|
2016-07-21 16:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 03:21:18 +00:00
|
|
|
fn field(&self, attr: &ast::Attribute, name: Symbol) -> 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 16:50:15 +00: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 16:50:15 +00:00
|
|
|
} else {
|
|
|
|
self.tcx.sess.span_fatal(
|
2019-03-03 17:56:24 +00:00
|
|
|
item.span(),
|
2016-07-21 16:50:15 +00:00
|
|
|
&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 16:50:15 +00: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 16:50:15 +00:00
|
|
|
debug!("check_config: matched");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
debug!("check_config: no match found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|