]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_incremental/src/assert_module_sources.rs
80448c01a262dd3085d665ff42d6c84ebdd6f48b
[rust.git] / compiler / rustc_incremental / src / 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_ast as ast;
25 use rustc_hir::def_id::LOCAL_CRATE;
26 use rustc_middle::mir::mono::CodegenUnitNameBuilder;
27 use rustc_middle::ty::TyCtxt;
28 use rustc_session::cgu_reuse_tracker::*;
29 use rustc_span::symbol::{sym, Symbol};
30 use std::collections::BTreeSet;
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().to_string())
43             .collect::<BTreeSet<String>>();
44
45         let ams = AssertModuleSource { tcx, available_cgus };
46
47         for attr in tcx.hir().krate().item.attrs {
48             ams.check_attr(attr);
49         }
50     })
51 }
52
53 struct AssertModuleSource<'tcx> {
54     tcx: TyCtxt<'tcx>,
55     available_cgus: BTreeSet<String>,
56 }
57
58 impl AssertModuleSource<'tcx> {
59     fn check_attr(&self, attr: &ast::Attribute) {
60         let (expected_reuse, comp_kind) =
61             if self.tcx.sess.check_name(attr, sym::rustc_partition_reused) {
62                 (CguReuse::PreLto, ComparisonKind::AtLeast)
63             } else if self.tcx.sess.check_name(attr, sym::rustc_partition_codegened) {
64                 (CguReuse::No, ComparisonKind::Exact)
65             } else if self.tcx.sess.check_name(attr, sym::rustc_expected_cgu_reuse) {
66                 match self.field(attr, sym::kind) {
67                     sym::no => (CguReuse::No, ComparisonKind::Exact),
68                     sym::pre_dash_lto => (CguReuse::PreLto, ComparisonKind::Exact),
69                     sym::post_dash_lto => (CguReuse::PostLto, ComparisonKind::Exact),
70                     sym::any => (CguReuse::PreLto, ComparisonKind::AtLeast),
71                     other => {
72                         self.tcx.sess.span_fatal(
73                             attr.span,
74                             &format!("unknown cgu-reuse-kind `{}` specified", other),
75                         );
76                     }
77                 }
78             } else {
79                 return;
80             };
81
82         if !self.tcx.sess.opts.debugging_opts.query_dep_graph {
83             self.tcx.sess.span_fatal(
84                 attr.span,
85                 "found CGU-reuse attribute but `-Zquery-dep-graph` was not specified",
86             );
87         }
88
89         if !self.check_config(attr) {
90             debug!("check_attr: config does not match, ignoring attr");
91             return;
92         }
93
94         let user_path = self.field(attr, sym::module).to_string();
95         let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string();
96
97         if !user_path.starts_with(&crate_name) {
98             let msg = format!(
99                 "Found malformed codegen unit name `{}`. \
100                 Codegen units names must always start with the name of the \
101                 crate (`{}` in this case).",
102                 user_path, crate_name
103             );
104             self.tcx.sess.span_fatal(attr.span, &msg);
105         }
106
107         // Split of the "special suffix" if there is one.
108         let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') {
109             (&user_path[..index], Some(&user_path[index + 1..]))
110         } else {
111             (&user_path[..], None)
112         };
113
114         let mut cgu_path_components = user_path.split('-').collect::<Vec<_>>();
115
116         // Remove the crate name
117         assert_eq!(cgu_path_components.remove(0), crate_name);
118
119         let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx);
120         let cgu_name =
121             cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix);
122
123         debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
124
125         if !self.available_cgus.contains(&*cgu_name.as_str()) {
126             self.tcx.sess.span_err(
127                 attr.span,
128                 &format!(
129                     "no module named `{}` (mangled: {}). Available modules: {}",
130                     user_path,
131                     cgu_name,
132                     self.available_cgus
133                         .iter()
134                         .map(|cgu| cgu.to_string())
135                         .collect::<Vec<_>>()
136                         .join(", ")
137                 ),
138             );
139         }
140
141         self.tcx.sess.cgu_reuse_tracker.set_expectation(
142             cgu_name,
143             &user_path,
144             attr.span,
145             expected_reuse,
146             comp_kind,
147         );
148     }
149
150     fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
151         for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
152             if item.has_name(name) {
153                 if let Some(value) = item.value_str() {
154                     return value;
155                 } else {
156                     self.tcx.sess.span_fatal(
157                         item.span(),
158                         &format!("associated value expected for `{}`", name),
159                     );
160                 }
161             }
162         }
163
164         self.tcx.sess.span_fatal(attr.span, &format!("no field `{}`", name));
165     }
166
167     /// Scan for a `cfg="foo"` attribute and check whether we have a
168     /// cfg flag called `foo`.
169     fn check_config(&self, attr: &ast::Attribute) -> bool {
170         let config = &self.tcx.sess.parse_sess.config;
171         let value = self.field(attr, sym::cfg);
172         debug!("check_config(config={:?}, value={:?})", config, value);
173         if config.iter().any(|&(name, _)| name == value) {
174             debug!("check_config: matched");
175             return true;
176         }
177         debug!("check_config: no match found");
178         false
179     }
180 }