]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/assert_module_sources.rs
Rollup merge of #50257 - estebank:fix-49560, r=nikomatsakis
[rust.git] / src / librustc_incremental / assert_module_sources.rs
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.
4 //
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.
10
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.
14 //!
15 //! The user adds annotations to the crate of the following form:
16 //!
17 //! ```
18 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
19 //! #![rustc_partition_translated(module="spike-x", cfg="rpass2")]
20 //! ```
21 //!
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.
25 //!
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.
29
30 use rustc::dep_graph::{DepNode, DepConstructor};
31 use rustc::mir::mono::CodegenUnit;
32 use rustc::ty::TyCtxt;
33 use syntax::ast;
34 use syntax_pos::symbol::Symbol;
35 use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_TRANSLATED};
36
37 const MODULE: &'static str = "module";
38 const CFG: &'static str = "cfg";
39
40 #[derive(Debug, PartialEq, Clone, Copy)]
41 enum Disposition { Reused, Translated }
42
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() {
46             return;
47         }
48
49         let ams = AssertModuleSource { tcx };
50         for attr in &tcx.hir.krate().attrs {
51             ams.check_attr(attr);
52         }
53     })
54 }
55
56 struct AssertModuleSource<'a, 'tcx: 'a> {
57     tcx: TyCtxt<'a, 'tcx, 'tcx>
58 }
59
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) {
63             Disposition::Reused
64         } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
65             Disposition::Translated
66         } else {
67             return;
68         };
69
70         if !self.check_config(attr) {
71             debug!("check_attr: config does not match, ignoring attr");
72             return;
73         }
74
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();
78
79         let dep_node = DepNode::new(self.tcx,
80                                     DepConstructor::CompileCodegenUnit(mangled_cgu_name));
81
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(
86                         attr.span,
87                         &format!("expected module named `{}` to be Reused but is Translated",
88                                  mname));
89                 }
90                 (Disposition::Translated, true) => {
91                     self.tcx.sess.span_err(
92                         attr.span,
93                         &format!("expected module named `{}` to be Translated but is Reused",
94                                  mname));
95                 }
96                 (Disposition::Reused, true) |
97                 (Disposition::Translated, false) => {
98                     // These are what we would expect.
99                 }
100             }
101         } else {
102             self.tcx.sess.span_err(attr.span, &format!("no module named `{}`", mname));
103         }
104     }
105
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() {
110                     return value;
111                 } else {
112                     self.tcx.sess.span_fatal(
113                         item.span,
114                         &format!("associated value expected for `{}`", name));
115                 }
116             }
117         }
118
119         self.tcx.sess.span_fatal(
120             attr.span,
121             &format!("no field `{}`", name));
122     }
123
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");
132             return true;
133         }
134         debug!("check_config: no match found");
135         return false;
136     }
137
138 }