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