2012-01-23 21:23:02 -06:00
|
|
|
#[doc = "Prunes branches of the tree that are not exported"];
|
|
|
|
|
|
|
|
import rustc::syntax::ast;
|
|
|
|
import rustc::syntax::ast_util;
|
|
|
|
import rustc::middle::ast_map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-01-23 21:23:02 -06:00
|
|
|
|
|
|
|
export mk_pass;
|
|
|
|
|
|
|
|
fn mk_pass() -> pass {
|
2012-02-27 20:07:16 -06:00
|
|
|
{
|
2012-02-27 20:11:12 -06:00
|
|
|
name: "prune_unexported",
|
2012-02-27 20:07:16 -06:00
|
|
|
f: run
|
|
|
|
}
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
|
2012-03-02 20:33:25 -06:00
|
|
|
fn run(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
|
2012-01-23 21:23:02 -06:00
|
|
|
let fold = fold::fold({
|
|
|
|
fold_mod: fold_mod
|
2012-02-21 00:24:59 -06:00
|
|
|
with *fold::default_any_fold(srv)
|
2012-01-23 21:23:02 -06:00
|
|
|
});
|
2012-03-02 20:33:25 -06:00
|
|
|
fold.fold_doc(fold, doc)
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mod(fold: fold::fold<astsrv::srv>, doc: doc::moddoc) -> doc::moddoc {
|
2012-02-21 00:24:59 -06:00
|
|
|
let doc = fold::default_any_fold_mod(fold, doc);
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-02-24 15:53:28 -06:00
|
|
|
items: exported_items(fold.ctxt, doc)
|
2012-01-30 15:05:25 -06:00
|
|
|
with doc
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 17:04:36 -06:00
|
|
|
fn exported_items(srv: astsrv::srv, doc: doc::moddoc) -> [doc::itemtag] {
|
|
|
|
exported_things(
|
|
|
|
srv, doc,
|
|
|
|
exported_items_from_crate,
|
|
|
|
exported_items_from_mod
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-01-23 21:23:02 -06:00
|
|
|
fn exported_things<T>(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
doc: doc::moddoc,
|
|
|
|
from_crate: fn(astsrv::srv, doc::moddoc) -> [T],
|
|
|
|
from_mod: fn(astsrv::srv, doc::moddoc) -> [T]
|
|
|
|
) -> [T] {
|
2012-02-17 16:46:30 -06:00
|
|
|
if doc.id() == ast::crate_node_id {
|
2012-01-23 21:23:02 -06:00
|
|
|
from_crate(srv, doc)
|
|
|
|
} else {
|
|
|
|
from_mod(srv, doc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 17:04:36 -06:00
|
|
|
fn exported_items_from_crate(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
doc: doc::moddoc
|
|
|
|
) -> [doc::itemtag] {
|
|
|
|
exported_items_from(srv, doc, is_exported_from_crate)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exported_items_from_mod(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
doc: doc::moddoc
|
|
|
|
) -> [doc::itemtag] {
|
2012-02-17 16:46:30 -06:00
|
|
|
exported_items_from(srv, doc, bind is_exported_from_mod(_, doc.id(), _))
|
2012-01-28 17:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn exported_items_from(
|
2012-01-26 19:29:55 -06:00
|
|
|
srv: astsrv::srv,
|
2012-01-28 17:04:36 -06:00
|
|
|
doc: doc::moddoc,
|
|
|
|
is_exported: fn(astsrv::srv, str) -> bool
|
|
|
|
) -> [doc::itemtag] {
|
2012-02-24 15:53:28 -06:00
|
|
|
vec::filter_map(doc.items) { |itemtag|
|
2012-01-28 17:04:36 -06:00
|
|
|
let itemtag = alt itemtag {
|
|
|
|
doc::enumtag(enumdoc) {
|
|
|
|
// Also need to check variant exportedness
|
2012-01-30 15:05:25 -06:00
|
|
|
doc::enumtag({
|
2012-01-28 17:04:36 -06:00
|
|
|
variants: exported_variants_from(srv, enumdoc, is_exported)
|
2012-01-30 15:05:25 -06:00
|
|
|
with enumdoc
|
2012-01-28 17:04:36 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
_ { itemtag }
|
|
|
|
};
|
2012-02-24 17:43:57 -06:00
|
|
|
|
|
|
|
if itemtag.item().reexport || is_exported(srv, itemtag.name()) {
|
2012-01-28 17:04:36 -06:00
|
|
|
some(itemtag)
|
|
|
|
} else {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 19:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn exported_variants_from(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
doc: doc::enumdoc,
|
|
|
|
is_exported: fn(astsrv::srv, str) -> bool
|
|
|
|
) -> [doc::variantdoc] {
|
|
|
|
vec::filter_map(doc.variants) { |doc|
|
|
|
|
if is_exported(srv, doc.name) {
|
|
|
|
some(doc)
|
|
|
|
} else {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 21:23:02 -06:00
|
|
|
fn is_exported_from_mod(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
mod_id: doc::ast_id,
|
|
|
|
item_name: str
|
|
|
|
) -> bool {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
2012-01-25 23:21:21 -06:00
|
|
|
alt ctxt.ast_map.get(mod_id) {
|
2012-02-03 02:53:37 -06:00
|
|
|
ast_map::node_item(item, _) {
|
2012-01-23 21:23:02 -06:00
|
|
|
alt item.node {
|
|
|
|
ast::item_mod(m) {
|
|
|
|
ast_util::is_exported(item_name, m)
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
_ {
|
|
|
|
fail "is_exported_from_mod: not a mod";
|
|
|
|
}
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
_ { fail "is_exported_from_mod: not an item"; }
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_exported_from_crate(
|
|
|
|
srv: astsrv::srv,
|
|
|
|
item_name: str
|
|
|
|
) -> bool {
|
|
|
|
astsrv::exec(srv) {|ctxt|
|
|
|
|
ast_util::is_exported(item_name, ctxt.ast.node.module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_fns() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("mod b { export a; fn a() { } fn b() { } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()[0].fns()) == 1u;
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-01-24 01:27:26 -06:00
|
|
|
fn should_prune_unexported_fns_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export a; fn a() { } fn b() { }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().fns()) == 1u;
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-01-24 01:27:26 -06:00
|
|
|
fn should_prune_unexported_modules() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("mod a { export a; mod a { } mod b { } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()[0].mods()) == 1u;
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_modules_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export a; mod a { } mod b { }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()) == 1u;
|
2012-01-23 21:23:02 -06:00
|
|
|
}
|
2012-01-24 01:27:26 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_consts() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc(
|
|
|
|
"mod a { export a; \
|
|
|
|
const a: bool = true; \
|
|
|
|
const b: bool = true; }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()[0].consts()) == 1u;
|
2012-01-24 01:27:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_consts_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc(
|
|
|
|
"export a; const a: bool = true; const b: bool = true;");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().consts()) == 1u;
|
2012-01-24 01:27:26 -06:00
|
|
|
}
|
2012-01-26 19:29:55 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_enums_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export a; mod a { } enum b { c }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().enums()) == 0u;
|
2012-01-26 19:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_enums() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("mod a { export a; mod a { } enum b { c } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()[0].enums()) == 0u;
|
2012-01-26 19:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_variants_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export b::{}; enum b { c }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().enums()[0].variants) == 0u;
|
2012-01-26 19:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_variants() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("mod a { export b::{}; enum b { c } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::len(doc.cratemod().mods()[0].enums()[0].variants) == 0u;
|
2012-01-26 19:29:55 -06:00
|
|
|
}
|
2012-01-28 17:08:49 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_resources_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export a; mod a { } resource r(a: bool) { }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().resources());
|
2012-01-28 17:08:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_resources() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc(
|
|
|
|
"mod a { export a; mod a { } resource r(a: bool) { } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().mods()[0].resources());
|
2012-01-28 17:08:49 -06:00
|
|
|
}
|
2012-01-30 16:27:54 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_ifaces_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc("export a; mod a { } iface b { fn c(); }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().ifaces());
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-01-31 18:09:27 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_impls_from_top_mod() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = test::mk_doc(
|
|
|
|
"export a; mod a { } impl b for int { fn c() { } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().impls())
|
2012-01-31 20:32:37 -06:00
|
|
|
}
|
|
|
|
|
2012-02-02 00:41:41 -06:00
|
|
|
#[test]
|
|
|
|
fn should_prune_unexported_types() {
|
|
|
|
let doc = test::mk_doc("export a; mod a { } type b = int;");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().types());
|
2012-02-02 00:41:41 -06:00
|
|
|
}
|
|
|
|
|
2012-02-24 17:43:57 -06:00
|
|
|
#[test]
|
|
|
|
fn should_not_prune_reexports() {
|
2012-03-02 20:33:25 -06:00
|
|
|
fn mk_doc(source: str) -> doc::doc {
|
2012-02-24 17:43:57 -06:00
|
|
|
astsrv::from_str(source) {|srv|
|
|
|
|
let doc = extract::from_srv(srv, "");
|
2012-02-27 20:07:16 -06:00
|
|
|
let doc = reexport_pass::mk_pass().f(srv, doc);
|
2012-02-24 17:43:57 -06:00
|
|
|
run(srv, doc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let doc = mk_doc("import a::b; \
|
|
|
|
export b; \
|
|
|
|
mod a { fn b() { } }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_not_empty(doc.cratemod().fns());
|
2012-02-24 17:43:57 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 20:32:37 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2012-03-02 20:33:25 -06:00
|
|
|
fn mk_doc(source: str) -> doc::doc {
|
2012-02-20 23:08:19 -06:00
|
|
|
astsrv::from_str(source) {|srv|
|
|
|
|
let doc = extract::from_srv(srv, "");
|
|
|
|
run(srv, doc)
|
|
|
|
}
|
2012-01-31 20:32:37 -06:00
|
|
|
}
|
2012-03-07 18:48:57 -06:00
|
|
|
}
|