]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/assert_module_sources.rs
Rollup merge of #64588 - matthewjasper:mir-address-of, r=oli-obk
[rust.git] / src / librustc_incremental / assert_module_sources.rs
1 //! This pass is only used for UNIT TESTS related to incremental
2 //! compilation. It tests whether a particular `.o` file will be re-used
3 //! from a previous compilation or whether it must be regenerated.
4 //!
5 //! The user adds annotations to the crate of the following form:
6 //!
7 //! ```
8 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
9 //! #![rustc_partition_codegened(module="spike-x", cfg="rpass2")]
10 //! ```
11 //!
12 //! The first indicates (in the cfg `rpass2`) that `spike.o` will be
13 //! reused, the second that `spike-x.o` will be recreated. If these
14 //! annotations are inaccurate, errors are reported.
15 //!
16 //! The reason that we use `cfg=...` and not `#[cfg_attr]` is so that
17 //! the HIR doesn't change as a result of the annotations, which might
18 //! perturb the reuse results.
19 //!
20 //! `#![rustc_expected_cgu_reuse(module="spike", cfg="rpass2", kind="post-lto")]
21 //! allows for doing a more fine-grained check to see if pre- or post-lto data
22 //! was re-used.
23
24 use rustc::hir::def_id::LOCAL_CRATE;
25 use rustc_session::cgu_reuse_tracker::*;
26 use rustc::mir::mono::CodegenUnitNameBuilder;
27 use rustc::ty::TyCtxt;
28 use std::collections::BTreeSet;
29 use syntax::ast;
30 use syntax::symbol::{Symbol, sym};
31
32 pub fn assert_module_sources(tcx: TyCtxt<'_>) {
33     tcx.dep_graph.with_ignore(|| {
34         if tcx.sess.opts.incremental.is_none() {
35             return;
36         }
37
38         let available_cgus = tcx
39             .collect_and_partition_mono_items(LOCAL_CRATE)
40             .1
41             .iter()
42             .map(|cgu| cgu.name())
43             .collect::<BTreeSet<Symbol>>();
44
45         let ams = AssertModuleSource {
46             tcx,
47             available_cgus
48         };
49
50         for attr in &tcx.hir().krate().attrs {
51             ams.check_attr(attr);
52         }
53     })
54 }
55
56 struct AssertModuleSource<'tcx> {
57     tcx: TyCtxt<'tcx>,
58     available_cgus: BTreeSet<Symbol>,
59 }
60
61 impl AssertModuleSource<'tcx> {
62     fn check_attr(&self, attr: &ast::Attribute) {
63         let (expected_reuse, comp_kind) = if attr.check_name(sym::rustc_partition_reused) {
64             (CguReuse::PreLto, ComparisonKind::AtLeast)
65         } else if attr.check_name(sym::rustc_partition_codegened) {
66             (CguReuse::No, ComparisonKind::Exact)
67         } else if attr.check_name(sym::rustc_expected_cgu_reuse) {
68             match &*self.field(attr, sym::kind).as_str() {
69                 "no" => (CguReuse::No, ComparisonKind::Exact),
70                 "pre-lto" => (CguReuse::PreLto, ComparisonKind::Exact),
71                 "post-lto" => (CguReuse::PostLto, ComparisonKind::Exact),
72                 "any" => (CguReuse::PreLto, ComparisonKind::AtLeast),
73                 other => {
74                     self.tcx.sess.span_fatal(
75                         attr.span,
76                         &format!("unknown cgu-reuse-kind `{}` specified", other));
77                 }
78             }
79         } else {
80             return;
81         };
82
83         if !self.tcx.sess.opts.debugging_opts.query_dep_graph {
84             self.tcx.sess.span_fatal(
85                 attr.span,
86                 &format!("found CGU-reuse attribute but `-Zquery-dep-graph` \
87                           was not specified"));
88         }
89
90         if !self.check_config(attr) {
91             debug!("check_attr: config does not match, ignoring attr");
92             return;
93         }
94
95         let user_path = self.field(attr, sym::module).to_string();
96         let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
97
98         if !user_path.starts_with(&crate_name) {
99             let msg = format!("Found malformed codegen unit name `{}`. \
100                 Codegen units names must always start with the name of the \
101                 crate (`{}` in this case).", user_path, crate_name);
102             self.tcx.sess.span_fatal(attr.span, &msg);
103         }
104
105         // Split of the "special suffix" if there is one.
106         let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind(".") {
107             (&user_path[..index], Some(&user_path[index + 1 ..]))
108         } else {
109             (&user_path[..], None)
110         };
111
112         let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
113
114         // Remove the crate name
115         assert_eq!(cgu_path_components.remove(0), crate_name);
116
117         let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
118         let cgu_name = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
119                                                        cgu_path_components,
120                                                        cgu_special_suffix);
121
122         debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
123
124         if !self.available_cgus.contains(&cgu_name) {
125             self.tcx.sess.span_err(attr.span,
126                 &format!("no module named `{}` (mangled: {}). \
127                           Available modules: {}",
128                     user_path,
129                     cgu_name,
130                     self.available_cgus
131                         .iter()
132                         .map(|cgu| cgu.to_string())
133                         .collect::<Vec<_>>()
134                         .join(", ")));
135         }
136
137         self.tcx.sess.cgu_reuse_tracker.set_expectation(&cgu_name.as_str(),
138                                                         &user_path,
139                                                         attr.span,
140                                                         expected_reuse,
141                                                         comp_kind);
142     }
143
144     fn field(&self, attr: &ast::Attribute, name: Symbol) -> ast::Name {
145         for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
146             if item.check_name(name) {
147                 if let Some(value) = item.value_str() {
148                     return value;
149                 } else {
150                     self.tcx.sess.span_fatal(
151                         item.span(),
152                         &format!("associated value expected for `{}`", name));
153                 }
154             }
155         }
156
157         self.tcx.sess.span_fatal(
158             attr.span,
159             &format!("no field `{}`", name));
160     }
161
162     /// Scan for a `cfg="foo"` attribute and check whether we have a
163     /// cfg flag called `foo`.
164     fn check_config(&self, attr: &ast::Attribute) -> bool {
165         let config = &self.tcx.sess.parse_sess.config;
166         let value = self.field(attr, sym::cfg);
167         debug!("check_config(config={:?}, value={:?})", config, value);
168         if config.iter().any(|&(name, _)| name == value) {
169             debug!("check_config: matched");
170             return true;
171         }
172         debug!("check_config: no match found");
173         return false;
174     }
175 }