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, Clone, Copy)]
41 pub enum Disposition { Reused, Translated }
43 impl ModuleTranslation {
44 pub fn disposition(&self) -> (String, Disposition) {
45 let disposition = match self.source {
46 ModuleSource::Preexisting(_) => Disposition::Reused,
47 ModuleSource::Translated(_) => Disposition::Translated,
50 (self.name.clone(), disposition)
54 pub(crate) fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
55 modules: &[(String, Disposition)]) {
56 let _ignore = tcx.dep_graph.in_ignore();
58 if tcx.sess.opts.incremental.is_none() {
62 let ams = AssertModuleSource { tcx: tcx, modules: modules };
63 for attr in &tcx.hir.krate().attrs {
68 struct AssertModuleSource<'a, 'tcx: 'a> {
69 tcx: TyCtxt<'a, 'tcx, 'tcx>,
70 modules: &'a [(String, Disposition)],
73 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
74 fn check_attr(&self, attr: &ast::Attribute) {
75 let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
77 } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
78 Disposition::Translated
83 if !self.check_config(attr) {
84 debug!("check_attr: config does not match, ignoring attr");
88 let mname = self.field(attr, MODULE);
89 let mtrans = self.modules.iter().find(|&&(ref name, _)| name == mname.as_str());
90 let mtrans = match mtrans {
93 debug!("module name `{}` not found amongst:", mname);
94 for &(ref name, ref disposition) in self.modules {
95 debug!("module named `{}` with disposition {:?}",
100 self.tcx.sess.span_err(
102 &format!("no module named `{}`", mname));
107 let mtrans_disposition = mtrans.1;
108 if disposition != mtrans_disposition {
109 self.tcx.sess.span_err(
111 &format!("expected module named `{}` to be {:?} but is {:?}",
114 mtrans_disposition));
118 fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
119 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
120 if item.check_name(name) {
121 if let Some(value) = item.value_str() {
124 self.tcx.sess.span_fatal(
126 &format!("associated value expected for `{}`", name));
131 self.tcx.sess.span_fatal(
133 &format!("no field `{}`", name));
136 /// Scan for a `cfg="foo"` attribute and check whether we have a
137 /// cfg flag called `foo`.
138 fn check_config(&self, attr: &ast::Attribute) -> bool {
139 let config = &self.tcx.sess.parse_sess.config;
140 let value = self.field(attr, CFG);
141 debug!("check_config(config={:?}, value={:?})", config, value);
142 if config.iter().any(|&(name, _)| name == value) {
143 debug!("check_config: matched");
146 debug!("check_config: no match found");