]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/assert_module_sources.rs
New upstream version 1.22.1+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::dep_graph::{DepNode, DepConstructor};
31 use rustc::ty::TyCtxt;
32 use syntax::ast;
33 use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_TRANSLATED};
34
35 const MODULE: &'static str = "module";
36 const CFG: &'static str = "cfg";
37
38 #[derive(Debug, PartialEq, Clone, Copy)]
39 enum Disposition { Reused, Translated }
40
41 pub(crate) fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
42 let _ignore = tcx.dep_graph.in_ignore();
43
44 if tcx.sess.opts.incremental.is_none() {
45 return;
46 }
47
48 let ams = AssertModuleSource { tcx };
49 for attr in &tcx.hir.krate().attrs {
50 ams.check_attr(attr);
51 }
52 }
53
54 struct AssertModuleSource<'a, 'tcx: 'a> {
55 tcx: TyCtxt<'a, 'tcx, 'tcx>
56 }
57
58 impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
59 fn check_attr(&self, attr: &ast::Attribute) {
60 let disposition = if attr.check_name(ATTR_PARTITION_REUSED) {
61 Disposition::Reused
62 } else if attr.check_name(ATTR_PARTITION_TRANSLATED) {
63 Disposition::Translated
64 } else {
65 return;
66 };
67
68 if !self.check_config(attr) {
69 debug!("check_attr: config does not match, ignoring attr");
70 return;
71 }
72
73 let mname = self.field(attr, MODULE);
74
75 let dep_node = DepNode::new(self.tcx,
76 DepConstructor::CompileCodegenUnit(mname.as_str()));
77
78 if let Some(loaded_from_cache) = self.tcx.dep_graph.was_loaded_from_cache(&dep_node) {
79 match (disposition, loaded_from_cache) {
80 (Disposition::Reused, false) => {
81 self.tcx.sess.span_err(
82 attr.span,
83 &format!("expected module named `{}` to be Reused but is Translated",
84 mname));
85 }
86 (Disposition::Translated, true) => {
87 self.tcx.sess.span_err(
88 attr.span,
89 &format!("expected module named `{}` to be Translated but is Reused",
90 mname));
91 }
92 (Disposition::Reused, true) |
93 (Disposition::Translated, false) => {
94 // These are what we would expect.
95 }
96 }
97 } else {
98 self.tcx.sess.span_err(attr.span, &format!("no module named `{}`", mname));
99 }
100 }
101
102 fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
103 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
104 if item.check_name(name) {
105 if let Some(value) = item.value_str() {
106 return value;
107 } else {
108 self.tcx.sess.span_fatal(
109 item.span,
110 &format!("associated value expected for `{}`", name));
111 }
112 }
113 }
114
115 self.tcx.sess.span_fatal(
116 attr.span,
117 &format!("no field `{}`", name));
118 }
119
120 /// Scan for a `cfg="foo"` attribute and check whether we have a
121 /// cfg flag called `foo`.
122 fn check_config(&self, attr: &ast::Attribute) -> bool {
123 let config = &self.tcx.sess.parse_sess.config;
124 let value = self.field(attr, CFG);
125 debug!("check_config(config={:?}, value={:?})", config, value);
126 if config.iter().any(|&(name, _)| name == value) {
127 debug!("check_config: matched");
128 return true;
129 }
130 debug!("check_config: no match found");
131 return false;
132 }
133
134 }