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