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::dep_graph::{DepNode, DepConstructor};
31 use rustc::mir::mono::CodegenUnit;
32 use rustc::ty::TyCtxt;
34 use syntax_pos::symbol::Symbol;
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 enum Disposition { Reused, Translated }
43 pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
44 tcx.dep_graph.with_ignore(|| {
45 if tcx.sess.opts.incremental.is_none() {
49 let ams = AssertModuleSource { tcx };
50 for attr in &tcx.hir.krate().attrs {
56 struct AssertModuleSource<'a, 'tcx: 'a> {
57 tcx: TyCtxt<'a, 'tcx, 'tcx>
60 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
61 fn check_attr(&self, attr: &ast::Attribute) {
62 let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
64 } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
65 Disposition::Translated
70 if !self.check_config(attr) {
71 debug!("check_attr: config does not match, ignoring attr");
75 let mname = self.field(attr, MODULE);
76 let mangled_cgu_name = CodegenUnit::mangle_name(&mname.as_str());
77 let mangled_cgu_name = Symbol::intern(&mangled_cgu_name).as_interned_str();
79 let dep_node = DepNode::new(self.tcx,
80 DepConstructor::CompileCodegenUnit(mangled_cgu_name));
82 if let Some(loaded_from_cache) = self.tcx.dep_graph.was_loaded_from_cache(&dep_node) {
83 match (disposition, loaded_from_cache) {
84 (Disposition::Reused, false) => {
85 self.tcx.sess.span_err(
87 &format!("expected module named `{}` to be Reused but is Translated",
90 (Disposition::Translated, true) => {
91 self.tcx.sess.span_err(
93 &format!("expected module named `{}` to be Translated but is Reused",
96 (Disposition::Reused, true) |
97 (Disposition::Translated, false) => {
98 // These are what we would expect.
102 self.tcx.sess.span_err(attr.span, &format!("no module named `{}`", mname));
106 fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
107 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
108 if item.check_name(name) {
109 if let Some(value) = item.value_str() {
112 self.tcx.sess.span_fatal(
114 &format!("associated value expected for `{}`", name));
119 self.tcx.sess.span_fatal(
121 &format!("no field `{}`", name));
124 /// Scan for a `cfg="foo"` attribute and check whether we have a
125 /// cfg flag called `foo`.
126 fn check_config(&self, attr: &ast::Attribute) -> bool {
127 let config = &self.tcx.sess.parse_sess.config;
128 let value = self.field(attr, CFG);
129 debug!("check_config(config={:?}, value={:?})", config, value);
130 if config.iter().any(|&(name, _)| name == value) {
131 debug!("check_config: matched");
134 debug!("check_config: no match found");