Support #[cfg] on methods

This commit is contained in:
Brian Anderson 2012-11-01 15:48:37 -07:00
parent 768247f393
commit 3edccc311e
3 changed files with 64 additions and 1 deletions

View File

@ -12,6 +12,7 @@ export noop_fold_pat;
export noop_fold_mod;
export noop_fold_ty;
export noop_fold_block;
export noop_fold_item_underscore;
export wrap;
export fold_ty_param;
export fold_ty_params;

View File

@ -27,6 +27,7 @@ fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
@{fold_mod: |a,b| fold_mod(ctxt, a, b),
fold_block: fold::wrap(|a,b| fold_block(ctxt, a, b) ),
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
.. *fold::default_ast_fold()};
let fold = fold::make_fold(precursor);
@ -79,6 +80,22 @@ fn fold_foreign_mod(cx: ctxt, nm: ast::foreign_mod,
};
}
fn fold_item_underscore(cx: ctxt, item: ast::item_, fld: fold::ast_fold) -> ast::item_ {
let item = match item {
ast::item_impl(a, b, c, Some(methods)) => {
let methods = methods.filter(|m| method_in_cfg(cx, *m) );
ast::item_impl(a, b, c, Some(methods))
}
ast::item_trait(a, b, ref methods) => {
let methods = methods.filter(|m| trait_method_in_cfg(cx, m) );
ast::item_trait(a, b, methods)
}
_ => item
};
fold::noop_fold_item_underscore(item, fld)
}
fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
Option<@ast::stmt> {
match stmt.node {
@ -118,6 +135,17 @@ fn view_item_in_cfg(cx: ctxt, item: @ast::view_item) -> bool {
return cx.in_cfg(item.attrs);
}
fn method_in_cfg(cx: ctxt, meth: @ast::method) -> bool {
return cx.in_cfg(meth.attrs);
}
fn trait_method_in_cfg(cx: ctxt, meth: &ast::trait_method) -> bool {
match *meth {
ast::required(ref meth) => cx.in_cfg(meth.attrs),
ast::provided(@ref meth) => cx.in_cfg(meth.attrs)
}
}
// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(cfg: ast::crate_cfg, attrs: ~[ast::attribute]) -> bool {

View File

@ -1,3 +1,7 @@
// Crate use statements
#[cfg(bogus)]
use flippity;
#[cfg(bogus)]
const b: bool = false;
@ -115,4 +119,34 @@ mod test_use_statements {
#[cfg(bogus)]
use flippity_foo;
}
}
}
mod test_methods {
struct Foo {
bar: uint
}
impl Foo: Fooable {
#[cfg(bogus)]
static fn what() { }
static fn what() { }
#[cfg(bogus)]
fn the() { }
fn the() { }
}
trait Fooable {
#[cfg(bogus)]
static fn what();
static fn what();
#[cfg(bogus)]
fn the();
fn the();
}
}