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;
32 use syntax::attr::AttrMetaMethods;
33 use syntax::parse::token::InternedString;
35 use {ModuleSource, ModuleTranslation};
37 const PARTITION_REUSED: &'static str = "rustc_partition_reused";
38 const PARTITION_TRANSLATED: &'static str = "rustc_partition_translated";
40 const MODULE: &'static str = "module";
41 const CFG: &'static str = "cfg";
43 #[derive(Debug, PartialEq)]
44 enum Disposition { Reused, Translated }
46 pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
47 modules: &[ModuleTranslation]) {
48 let _ignore = tcx.dep_graph.in_ignore();
50 if tcx.sess.opts.incremental.is_none() {
54 let ams = AssertModuleSource { tcx: tcx, modules: modules };
55 for attr in &tcx.map.krate().attrs {
60 struct AssertModuleSource<'a, 'tcx: 'a> {
61 tcx: TyCtxt<'a, 'tcx, 'tcx>,
62 modules: &'a [ModuleTranslation],
65 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
66 fn check_attr(&self, attr: &ast::Attribute) {
67 let disposition = if attr.check_name(PARTITION_REUSED) {
69 } else if attr.check_name(PARTITION_TRANSLATED) {
70 Disposition::Translated
75 if !self.check_config(attr) {
76 debug!("check_attr: config does not match, ignoring attr");
80 let mname = self.field(attr, MODULE);
81 let mtrans = self.modules.iter().find(|mtrans| &mtrans.name[..] == &mname[..]);
82 let mtrans = match mtrans {
85 debug!("module name `{}` not found amongst:", mname);
86 for mtrans in self.modules {
87 debug!("module named `{}` with disposition {:?}",
89 self.disposition(mtrans));
92 self.tcx.sess.span_err(
94 &format!("no module named `{}`", mname));
99 let mtrans_disposition = self.disposition(mtrans);
100 if disposition != mtrans_disposition {
101 self.tcx.sess.span_err(
103 &format!("expected module named `{}` to be {:?} but is {:?}",
106 mtrans_disposition));
110 fn disposition(&self, mtrans: &ModuleTranslation) -> Disposition {
111 match mtrans.source {
112 ModuleSource::Preexisting(_) => Disposition::Reused,
113 ModuleSource::Translated(_) => Disposition::Translated,
117 fn field(&self, attr: &ast::Attribute, name: &str) -> InternedString {
118 for item in attr.meta_item_list().unwrap_or(&[]) {
119 if item.check_name(name) {
120 if let Some(value) = item.value_str() {
123 self.tcx.sess.span_fatal(
125 &format!("associated value expected for `{}`", name));
130 self.tcx.sess.span_fatal(
132 &format!("no field `{}`", name));
135 /// Scan for a `cfg="foo"` attribute and check whether we have a
136 /// cfg flag called `foo`.
137 fn check_config(&self, attr: &ast::Attribute) -> bool {
138 let config = &self.tcx.map.krate().config;
139 let value = self.field(attr, CFG);
140 debug!("check_config(config={:?}, value={:?})", config, value);
141 if config.iter().any(|c| c.check_name(&value[..])) {
142 debug!("check_config: matched");
145 debug!("check_config: no match found");