Skip to content

Commit c253b36

Browse files
committed
add Macro Exterminator
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.
1 parent 53642ee commit c253b36

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/librustc/driver/driver.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
259259
}
260260
);
261261

262+
// JBC: make CFG processing part of expansion to avoid this problem:
263+
262264
// strip again, in case expansion added anything with a #[cfg].
263265
krate = time(time_passes, "configuration 2", krate, |krate|
264266
front::config::strip_unconfigured_items(krate));
@@ -279,6 +281,9 @@ pub fn phase_2_configure_and_expand(sess: &Session,
279281
krate.encode(&mut json).unwrap();
280282
}
281283

284+
time(time_passes, "checking that all macro invocations are gone", &krate, |krate|
285+
syntax::ext::expand::check_for_macros(&sess.parse_sess, krate));
286+
282287
Some((krate, map))
283288
}
284289

@@ -291,14 +296,14 @@ pub struct CrateAnalysis {
291296
pub name: String,
292297
}
293298

299+
294300
/// Run the resolution, typechecking, region checking and other
295301
/// miscellaneous analysis passes on the crate. Return various
296302
/// structures carrying the results of the analysis.
297303
pub fn phase_3_run_analysis_passes(sess: Session,
298304
krate: &ast::Crate,
299305
ast_map: syntax::ast_map::Map,
300306
name: String) -> CrateAnalysis {
301-
302307
let time_passes = sess.time_passes();
303308

304309
time(time_passes, "external crate/lib resolution", (), |_|

src/libsyntax/ext/expand.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,25 @@ fn original_span(cx: &ExtCtxt) -> Gc<codemap::ExpnInfo> {
11511151
return einfo;
11521152
}
11531153

1154+
/// Check that there are no macro invocations left in the AST:
1155+
pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
1156+
visit::walk_crate(&mut MacroExterminator{sess:sess}, krate, ());
1157+
}
1158+
1159+
/// A visitor that ensures that no macro invocations remain in an AST.
1160+
struct MacroExterminator<'a>{
1161+
sess: &'a parse::ParseSess
1162+
}
1163+
1164+
impl<'a> visit::Visitor<()> for MacroExterminator<'a> {
1165+
fn visit_mac(&mut self, macro: &ast::Mac, _:()) {
1166+
self.sess.span_diagnostic.span_bug(macro.span,
1167+
"macro exterminator: expected AST \
1168+
with no macro invocations");
1169+
}
1170+
}
1171+
1172+
11541173
#[cfg(test)]
11551174
mod test {
11561175
use super::{pattern_bindings, expand_crate, contains_macro_escape};

0 commit comments

Comments
 (0)