]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/assert_module_sources.rs
Changed issue number to 36105
[rust.git] / src / librustc_trans / 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::ty::TyCtxt;
31 use syntax::ast;
32 use syntax::attr::AttrMetaMethods;
33 use syntax::parse::token::InternedString;
34
35 use {ModuleSource, ModuleTranslation};
36
37 const PARTITION_REUSED: &'static str = "rustc_partition_reused";
38 const PARTITION_TRANSLATED: &'static str = "rustc_partition_translated";
39
40 const MODULE: &'static str = "module";
41 const CFG: &'static str = "cfg";
42
43 #[derive(Debug, PartialEq)]
44 enum Disposition { Reused, Translated }
45
46 pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
47                                        modules: &[ModuleTranslation]) {
48     let _ignore = tcx.dep_graph.in_ignore();
49
50     if tcx.sess.opts.incremental.is_none() {
51         return;
52     }
53
54     let ams = AssertModuleSource { tcx: tcx, modules: modules };
55     for attr in &tcx.map.krate().attrs {
56         ams.check_attr(attr);
57     }
58 }
59
60 struct AssertModuleSource<'a, 'tcx: 'a> {
61     tcx: TyCtxt<'a, 'tcx, 'tcx>,
62     modules: &'a [ModuleTranslation],
63 }
64
65 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
66     fn check_attr(&self, attr: &ast::Attribute) {
67         let disposition = if attr.check_name(PARTITION_REUSED) {
68             Disposition::Reused
69         } else if attr.check_name(PARTITION_TRANSLATED) {
70             Disposition::Translated
71         } else {
72             return;
73         };
74
75         if !self.check_config(attr) {
76             debug!("check_attr: config does not match, ignoring attr");
77             return;
78         }
79
80         let mname = self.field(attr, MODULE);
81         let mtrans = self.modules.iter().find(|mtrans| &mtrans.name[..] == &mname[..]);
82         let mtrans = match mtrans {
83             Some(m) => m,
84             None => {
85                 debug!("module name `{}` not found amongst:", mname);
86                 for mtrans in self.modules {
87                     debug!("module named `{}` with disposition {:?}",
88                            mtrans.name,
89                            self.disposition(mtrans));
90                 }
91
92                 self.tcx.sess.span_err(
93                     attr.span,
94                     &format!("no module named `{}`", mname));
95                 return;
96             }
97         };
98
99         let mtrans_disposition = self.disposition(mtrans);
100         if disposition != mtrans_disposition {
101             self.tcx.sess.span_err(
102                 attr.span,
103                 &format!("expected module named `{}` to be {:?} but is {:?}",
104                          mname,
105                          disposition,
106                          mtrans_disposition));
107         }
108     }
109
110     fn disposition(&self, mtrans: &ModuleTranslation) -> Disposition {
111         match mtrans.source {
112             ModuleSource::Preexisting(_) => Disposition::Reused,
113             ModuleSource::Translated(_) => Disposition::Translated,
114         }
115     }
116
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() {
121                     return value;
122                 } else {
123                     self.tcx.sess.span_fatal(
124                         item.span,
125                         &format!("associated value expected for `{}`", name));
126                 }
127             }
128         }
129
130         self.tcx.sess.span_fatal(
131             attr.span,
132             &format!("no field `{}`", name));
133     }
134
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");
143             return true;
144         }
145         debug!("check_config: no match found");
146         return false;
147     }
148
149 }