Skip to content

Commit

Permalink
add Macro Exterminator
Browse files Browse the repository at this point in the history
the Macro Exterminator ensures that there are no macro invocations in
an AST. This should help make later passes confident that there aren't
hidden items, methods, expressions, etc.
  • Loading branch information
jbclements committed Jul 11, 2014
1 parent 53642ee commit c253b36
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/librustc/driver/driver.rs
Expand Up @@ -259,6 +259,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
}
);

// JBC: make CFG processing part of expansion to avoid this problem:

// strip again, in case expansion added anything with a #[cfg].
krate = time(time_passes, "configuration 2", krate, |krate|
front::config::strip_unconfigured_items(krate));
Expand All @@ -279,6 +281,9 @@ pub fn phase_2_configure_and_expand(sess: &Session,
krate.encode(&mut json).unwrap();
}

time(time_passes, "checking that all macro invocations are gone", &krate, |krate|
syntax::ext::expand::check_for_macros(&sess.parse_sess, krate));

Some((krate, map))
}

Expand All @@ -291,14 +296,14 @@ pub struct CrateAnalysis {
pub name: String,
}


/// Run the resolution, typechecking, region checking and other
/// miscellaneous analysis passes on the crate. Return various
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes(sess: Session,
krate: &ast::Crate,
ast_map: syntax::ast_map::Map,
name: String) -> CrateAnalysis {

let time_passes = sess.time_passes();

time(time_passes, "external crate/lib resolution", (), |_|
Expand Down
19 changes: 19 additions & 0 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -1151,6 +1151,25 @@ fn original_span(cx: &ExtCtxt) -> Gc<codemap::ExpnInfo> {
return einfo;
}

/// Check that there are no macro invocations left in the AST:
pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
visit::walk_crate(&mut MacroExterminator{sess:sess}, krate, ());
}

/// A visitor that ensures that no macro invocations remain in an AST.
struct MacroExterminator<'a>{
sess: &'a parse::ParseSess
}

impl<'a> visit::Visitor<()> for MacroExterminator<'a> {
fn visit_mac(&mut self, macro: &ast::Mac, _:()) {
self.sess.span_diagnostic.span_bug(macro.span,
"macro exterminator: expected AST \
with no macro invocations");
}
}


#[cfg(test)]
mod test {
use super::{pattern_bindings, expand_crate, contains_macro_escape};
Expand Down

5 comments on commit c253b36

@bors
Copy link
Contributor

@bors bors commented on c253b36 Jul 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at jbclements@c253b36

@bors
Copy link
Contributor

@bors bors commented on c253b36 Jul 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jbclements/rust/disable-default-macro-behavior = c253b36 into auto

@bors
Copy link
Contributor

@bors bors commented on c253b36 Jul 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jbclements/rust/disable-default-macro-behavior = c253b36 merged ok, testing candidate = cdd6346

@bors
Copy link
Contributor

@bors bors commented on c253b36 Jul 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = cdd6346

Please sign in to comment.