Conditionally compile items declared as statements. Issue #489

This commit is contained in:
Brian Anderson 2011-06-30 13:04:02 -07:00
parent 6346a67cbd
commit 39fccf3bc7
2 changed files with 43 additions and 1 deletions

View File

@ -11,7 +11,8 @@ export strip_unconfigured_items;
fn strip_unconfigured_items(@ast::crate crate) -> @ast::crate {
auto cfg = crate.node.config;
auto precursor = rec(fold_mod = bind fold_mod(cfg, _, _)
auto precursor = rec(fold_mod = bind fold_mod(cfg, _, _),
fold_block = bind fold_block(cfg, _, _)
with *fold::default_ast_fold());
auto fold = fold::make_fold(precursor);
@ -38,6 +39,34 @@ fn fold_mod(&ast::crate_cfg cfg, &ast::_mod m,
items=vec::map(fld.fold_item, filtered_items));
}
fn filter_stmt(&ast::crate_cfg cfg,
&@ast::stmt stmt) -> option::t[@ast::stmt] {
alt (stmt.node) {
case (ast::stmt_decl(?decl, _)) {
alt (decl.node) {
case (ast::decl_item(?item)) {
if (in_cfg(cfg, item)) {
option::some(stmt)
} else {
option::none
}
}
case (_) { option::some(stmt) }
}
}
case (_) { option::some(stmt) }
}
}
fn fold_block(&ast::crate_cfg cfg, &ast::block_ b,
fold::ast_fold fld) -> ast::block_ {
auto filter = bind filter_stmt(cfg, _);
auto filtered_stmts = vec::filter_map(filter, b.stmts);
ret rec(stmts=vec::map(fld.fold_stmt, filtered_stmts),
expr=option::map(fld.fold_expr, b.expr),
id=b.id);
}
// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(&ast::crate_cfg cfg, &@ast::item item) -> bool {

View File

@ -70,5 +70,18 @@ fn main() {
assert b;
let t x = true;
let tg y = bar;
test_in_fn_ctxt();
}
fn test_in_fn_ctxt() {
#[cfg(bogus)]
fn f() { fail }
fn f() {}
f();
#[cfg(bogus)]
const int i = 0;
const int i = 1;
assert i == 1;
}