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.
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.
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.
15 //! The user adds annotations to the crate of the following form:
18 //! #![rustc_partition_reused(module="spike", cfg="rpass2")]
19 //! #![rustc_partition_translated(module="spike-x", cfg="rpass2")]
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.
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.
30 use rustc
::dep_graph
::{DepNode, DepConstructor}
;
31 use rustc
::ty
::TyCtxt
;
33 use rustc
::ich
::{ATTR_PARTITION_REUSED, ATTR_PARTITION_TRANSLATED}
;
35 const MODULE
: &'
static str = "module";
36 const CFG
: &'
static str = "cfg";
38 #[derive(Debug, PartialEq, Clone, Copy)]
39 enum Disposition { Reused, Translated }
41 pub(crate) fn assert_module_sources
<'a
, 'tcx
>(tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>) {
42 let _ignore
= tcx
.dep_graph
.in_ignore();
44 if tcx
.sess
.opts
.incremental
.is_none() {
48 let ams
= AssertModuleSource { tcx }
;
49 for attr
in &tcx
.hir
.krate().attrs
{
54 struct AssertModuleSource
<'a
, 'tcx
: 'a
> {
55 tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>
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
) {
62 } else if attr
.check_name(ATTR_PARTITION_TRANSLATED
) {
63 Disposition
::Translated
68 if !self.check_config(attr
) {
69 debug
!("check_attr: config does not match, ignoring attr");
73 let mname
= self.field(attr
, MODULE
);
75 let dep_node
= DepNode
::new(self.tcx
,
76 DepConstructor
::CompileCodegenUnit(mname
.as_str()));
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(
83 &format
!("expected module named `{}` to be Reused but is Translated",
86 (Disposition
::Translated
, true) => {
87 self.tcx
.sess
.span_err(
89 &format
!("expected module named `{}` to be Translated but is Reused",
92 (Disposition
::Reused
, true) |
93 (Disposition
::Translated
, false) => {
94 // These are what we would expect.
98 self.tcx
.sess
.span_err(attr
.span
, &format
!("no module named `{}`", mname
));
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() {
108 self.tcx
.sess
.span_fatal(
110 &format
!("associated value expected for `{}`", name
));
115 self.tcx
.sess
.span_fatal(
117 &format
!("no field `{}`", name
));
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");
130 debug
!("check_config: no match found");