1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This pass is only used for UNIT TESTS related to incremental
12 //! compilation. It tests whether a particular `.o` file will be re-used
13 //! from a previous compilation or whether it must be regenerated.
15 //! The user adds annotations to the crate of the following form:
18 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
19 //! #![rustc_partition_translated(module="spike-x", cfg="rpass2")]
22 //! The first indicates (in the cfg `rpass2`) that `spike.o` will be
23 //! reused, the second that `spike-x.o` will be recreated. If these
24 //! annotations are inaccurate, errors are reported.
26 //! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
27 //! the HIR doesn't change as a result of the annotations, which might
28 //! perturb the reuse results.
30 use rustc::ty::TyCtxt;
33 use {ModuleSource, ModuleTranslation};
35 use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_TRANSLATED};
37 const MODULE: &'static str = "module";
38 const CFG: &'static str = "cfg";
40 #[derive(Debug, PartialEq)]
41 enum Disposition { Reused, Translated }
43 pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
44 modules: &[ModuleTranslation]) {
45 let _ignore = tcx.dep_graph.in_ignore();
47 if tcx.sess.opts.incremental.is_none() {
51 let ams = AssertModuleSource { tcx: tcx, modules: modules };
52 for attr in &tcx.hir.krate().attrs {
57 struct AssertModuleSource<'a, 'tcx: 'a> {
58 tcx: TyCtxt<'a, 'tcx, 'tcx>,
59 modules: &'a [ModuleTranslation],
62 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
63 fn check_attr(&self, attr: &ast::Attribute) {
64 let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
66 } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
67 Disposition::Translated
72 if !self.check_config(attr) {
73 debug!("check_attr: config does not match, ignoring attr");
77 let mname = self.field(attr, MODULE);
78 let mtrans = self.modules.iter().find(|mtrans| *mtrans.name == *mname.as_str());
79 let mtrans = match mtrans {
82 debug!("module name `{}` not found amongst:", mname);
83 for mtrans in self.modules {
84 debug!("module named `{}` with disposition {:?}",
86 self.disposition(mtrans));
89 self.tcx.sess.span_err(
91 &format!("no module named `{}`", mname));
96 let mtrans_disposition = self.disposition(mtrans);
97 if disposition != mtrans_disposition {
98 self.tcx.sess.span_err(
100 &format!("expected module named `{}` to be {:?} but is {:?}",
103 mtrans_disposition));
107 fn disposition(&self, mtrans: &ModuleTranslation) -> Disposition {
108 match mtrans.source {
109 ModuleSource::Preexisting(_) => Disposition::Reused,
110 ModuleSource::Translated(_) => Disposition::Translated,
114 fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
115 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
116 if item.check_name(name) {
117 if let Some(value) = item.value_str() {
120 self.tcx.sess.span_fatal(
122 &format!("associated value expected for `{}`", name));
127 self.tcx.sess.span_fatal(
129 &format!("no field `{}`", name));
132 /// Scan for a `cfg="foo"` attribute and check whether we have a
133 /// cfg flag called `foo`.
134 fn check_config(&self, attr: &ast::Attribute) -> bool {
135 let config = &self.tcx.sess.parse_sess.config;
136 let value = self.field(attr, CFG);
137 debug!("check_config(config={:?}, value={:?})", config, value);
138 if config.iter().any(|&(name, _)| name == value) {
139 debug!("check_config: matched");
142 debug!("check_config: no match found");