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