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
::ty
::TyCtxt
;
32 use syntax
::parse
::token
::InternedString
;
34 use {ModuleSource, ModuleTranslation}
;
36 const PARTITION_REUSED
: &'
static str = "rustc_partition_reused";
37 const PARTITION_TRANSLATED
: &'
static str = "rustc_partition_translated";
39 const MODULE
: &'
static str = "module";
40 const CFG
: &'
static str = "cfg";
42 #[derive(Debug, PartialEq)]
43 enum Disposition { Reused, Translated }
45 pub fn assert_module_sources
<'a
, 'tcx
>(tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
46 modules
: &[ModuleTranslation
]) {
47 let _ignore
= tcx
.dep_graph
.in_ignore();
49 if tcx
.sess
.opts
.incremental
.is_none() {
53 let ams
= AssertModuleSource { tcx: tcx, modules: modules }
;
54 for attr
in &tcx
.map
.krate().attrs
{
59 struct AssertModuleSource
<'a
, 'tcx
: 'a
> {
60 tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
61 modules
: &'a
[ModuleTranslation
],
64 impl<'a
, 'tcx
> AssertModuleSource
<'a
, 'tcx
> {
65 fn check_attr(&self, attr
: &ast
::Attribute
) {
66 let disposition
= if attr
.check_name(PARTITION_REUSED
) {
68 } else if attr
.check_name(PARTITION_TRANSLATED
) {
69 Disposition
::Translated
74 if !self.check_config(attr
) {
75 debug
!("check_attr: config does not match, ignoring attr");
79 let mname
= self.field(attr
, MODULE
);
80 let mtrans
= self.modules
.iter().find(|mtrans
| &mtrans
.name
[..] == &mname
[..]);
81 let mtrans
= match mtrans
{
84 debug
!("module name `{}` not found amongst:", mname
);
85 for mtrans
in self.modules
{
86 debug
!("module named `{}` with disposition {:?}",
88 self.disposition(mtrans
));
91 self.tcx
.sess
.span_err(
93 &format
!("no module named `{}`", mname
));
98 let mtrans_disposition
= self.disposition(mtrans
);
99 if disposition
!= mtrans_disposition
{
100 self.tcx
.sess
.span_err(
102 &format
!("expected module named `{}` to be {:?} but is {:?}",
105 mtrans_disposition
));
109 fn disposition(&self, mtrans
: &ModuleTranslation
) -> Disposition
{
110 match mtrans
.source
{
111 ModuleSource
::Preexisting(_
) => Disposition
::Reused
,
112 ModuleSource
::Translated(_
) => Disposition
::Translated
,
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() {
122 self.tcx
.sess
.span_fatal(
124 &format
!("associated value expected for `{}`", name
));
129 self.tcx
.sess
.span_fatal(
131 &format
!("no field `{}`", name
));
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
.sess
.parse_sess
.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");
144 debug
!("check_config: no match found");