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")]
|
2018-05-08 16:10:16 +03:00
|
|
|
//! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
|
2016-07-21 12:50:15 -04: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-07-10 19:16:22 +02:00
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2017-09-28 11:58:45 +02:00
|
|
|
use rustc::dep_graph::{DepNode, DepConstructor};
|
2018-08-14 17:55:22 +02:00
|
|
|
use rustc::mir::mono::CodegenUnitNameBuilder;
|
2016-07-21 12:50:15 -04:00
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use syntax::ast;
|
2018-05-08 16:10:16 +03:00
|
|
|
use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_CODEGENED};
|
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)]
|
2018-05-08 16:10:16 +03:00
|
|
|
enum Disposition { Reused, Codegened }
|
2017-07-26 16:02:32 +02:00
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2017-12-28 06:05:45 +01:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 12:50:15 -04:00
|
|
|
|
2017-12-28 06:05:45 +01:00
|
|
|
let ams = AssertModuleSource { tcx };
|
|
|
|
for attr in &tcx.hir.krate().attrs {
|
|
|
|
ams.check_attr(attr);
|
|
|
|
}
|
|
|
|
})
|
2016-07-21 12:50:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AssertModuleSource<'a, 'tcx: 'a> {
|
2017-09-28 11:58:45 +02:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>
|
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
|
2018-05-08 16:10:16 +03:00
|
|
|
} else if attr.check_name(ATTR_PARTITION_CODEGENED) {
|
|
|
|
Disposition::Codegened
|
2016-07-21 12:50:15 -04:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if !self.check_config(attr) {
|
|
|
|
debug!("check_attr: config does not match, ignoring attr");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-10 19:16:22 +02: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 12:55:47 +02:00
|
|
|
let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
|
2018-07-10 19:16:22 +02:00
|
|
|
|
|
|
|
// Remove the crate name
|
|
|
|
assert_eq!(cgu_path_components.remove(0), crate_name);
|
|
|
|
|
2018-08-14 17:55:22 +02: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 19:16:22 +02:00
|
|
|
|
|
|
|
debug!("mapping '{}' to cgu name '{}'", self.field(attr, MODULE), cgu_name);
|
2016-07-21 12:50:15 -04:00
|
|
|
|
2017-09-28 11:58:45 +02:00
|
|
|
let dep_node = DepNode::new(self.tcx,
|
2018-07-10 19:16:22 +02:00
|
|
|
DepConstructor::CompileCodegenUnit(cgu_name));
|
2016-07-21 12:50:15 -04:00
|
|
|
|
2017-09-28 11:58:45 +02:00
|
|
|
if let Some(loaded_from_cache) = self.tcx.dep_graph.was_loaded_from_cache(&dep_node) {
|
|
|
|
match (disposition, loaded_from_cache) {
|
|
|
|
(Disposition::Reused, false) => {
|
|
|
|
self.tcx.sess.span_err(
|
|
|
|
attr.span,
|
2018-05-08 16:10:16 +03:00
|
|
|
&format!("expected module named `{}` to be Reused but is Codegened",
|
2018-07-10 19:16:22 +02:00
|
|
|
user_path));
|
2017-09-28 11:58:45 +02:00
|
|
|
}
|
2018-05-08 16:10:16 +03:00
|
|
|
(Disposition::Codegened, true) => {
|
2017-09-28 11:58:45 +02:00
|
|
|
self.tcx.sess.span_err(
|
|
|
|
attr.span,
|
2018-05-08 16:10:16 +03:00
|
|
|
&format!("expected module named `{}` to be Codegened but is Reused",
|
2018-07-10 19:16:22 +02:00
|
|
|
user_path));
|
2017-09-28 11:58:45 +02:00
|
|
|
}
|
|
|
|
(Disposition::Reused, true) |
|
2018-05-08 16:10:16 +03:00
|
|
|
(Disposition::Codegened, false) => {
|
2017-09-28 11:58:45 +02:00
|
|
|
// These are what we would expect.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-10 19:16:22 +02:00
|
|
|
let available_cgus = self.tcx
|
|
|
|
.collect_and_partition_mono_items(LOCAL_CRATE)
|
|
|
|
.1
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| format!("{}", cgu.name()))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ");
|
|
|
|
|
|
|
|
self.tcx.sess.span_err(attr.span,
|
|
|
|
&format!("no module named `{}` (mangled: {}).\nAvailable modules: {}",
|
|
|
|
user_path,
|
|
|
|
cgu_name,
|
|
|
|
available_cgus));
|
2016-07-21 12:50:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|