]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/assert_module_sources.rs
Rollup merge of #41135 - japaric:unstable-docs, r=steveklabnik
[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
33 use {ModuleSource, ModuleTranslation};
34
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)]
41 enum Disposition { Reused, Translated }
42
43 pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
44                                        modules: &[ModuleTranslation]) {
45     let _ignore = tcx.dep_graph.in_ignore();
46
47     if tcx.sess.opts.incremental.is_none() {
48         return;
49     }
50
51     let ams = AssertModuleSource { tcx: tcx, modules: modules };
52     for attr in &tcx.hir.krate().attrs {
53         ams.check_attr(attr);
54     }
55 }
56
57 struct AssertModuleSource<'a, 'tcx: 'a> {
58     tcx: TyCtxt<'a, 'tcx, 'tcx>,
59     modules: &'a [ModuleTranslation],
60 }
61
62 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
63     fn check_attr(&self, attr: &ast::Attribute) {
64         let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
65             Disposition::Reused
66         } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
67             Disposition::Translated
68         } else {
69             return;
70         };
71
72         if !self.check_config(attr) {
73             debug!("check_attr: config does not match, ignoring attr");
74             return;
75         }
76
77         let mname = self.field(attr, MODULE);
78         let mtrans = self.modules.iter().find(|mtrans| *mtrans.name == *mname.as_str());
79         let mtrans = match mtrans {
80             Some(m) => m,
81             None => {
82                 debug!("module name `{}` not found amongst:", mname);
83                 for mtrans in self.modules {
84                     debug!("module named `{}` with disposition {:?}",
85                            mtrans.name,
86                            self.disposition(mtrans));
87                 }
88
89                 self.tcx.sess.span_err(
90                     attr.span,
91                     &format!("no module named `{}`", mname));
92                 return;
93             }
94         };
95
96         let mtrans_disposition = self.disposition(mtrans);
97         if disposition != mtrans_disposition {
98             self.tcx.sess.span_err(
99                 attr.span,
100                 &format!("expected module named `{}` to be {:?} but is {:?}",
101                          mname,
102                          disposition,
103                          mtrans_disposition));
104         }
105     }
106
107     fn disposition(&self, mtrans: &ModuleTranslation) -> Disposition {
108         match mtrans.source {
109             ModuleSource::Preexisting(_) => Disposition::Reused,
110             ModuleSource::Translated(_) => Disposition::Translated,
111         }
112     }
113
114     fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
115         for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
116             if item.check_name(name) {
117                 if let Some(value) = item.value_str() {
118                     return value;
119                 } else {
120                     self.tcx.sess.span_fatal(
121                         item.span,
122                         &format!("associated value expected for `{}`", name));
123                 }
124             }
125         }
126
127         self.tcx.sess.span_fatal(
128             attr.span,
129             &format!("no field `{}`", name));
130     }
131
132     /// Scan for a `cfg="foo"` attribute and check whether we have a
133     /// cfg flag called `foo`.
134     fn check_config(&self, attr: &ast::Attribute) -> bool {
135         let config = &self.tcx.sess.parse_sess.config;
136         let value = self.field(attr, CFG);
137         debug!("check_config(config={:?}, value={:?})", config, value);
138         if config.iter().any(|&(name, _)| name == value) {
139             debug!("check_config: matched");
140             return true;
141         }
142         debug!("check_config: no match found");
143         return false;
144     }
145
146 }