Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pile of rustdoc issue fixes #12339

Merged
merged 6 commits into from
Feb 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use rustc::metadata::csearch;
use rustc::metadata::decoder;

use std;
use std::hashmap::HashMap;

use doctree;
use visit_ast;
Expand Down Expand Up @@ -68,17 +67,17 @@ impl<T: Clean<U>, U> Clean<~[U]> for syntax::opt_vec::OptVec<T> {
pub struct Crate {
name: ~str,
module: Option<Item>,
externs: HashMap<ast::CrateNum, ExternalCrate>,
externs: ~[(ast::CrateNum, ExternalCrate)],
}

impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
fn clean(&self) -> Crate {
use syntax::attr::find_crateid;
let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());

let mut externs = HashMap::new();
let mut externs = ~[];
cx.sess.cstore.iter_crate_data(|n, meta| {
externs.insert(n, meta.clean());
externs.push((n, meta.clean()));
});

Crate {
Expand Down Expand Up @@ -181,6 +180,7 @@ pub enum ItemEnum {
VariantItem(Variant),
ForeignFunctionItem(Function),
ForeignStaticItem(Static),
MacroItem(Macro),
}

#[deriving(Clone, Encodable, Decodable)]
Expand All @@ -206,7 +206,8 @@ impl Clean<Item> for doctree::Module {
self.fns.clean(), self.foreigns.clean().concat_vec(),
self.mods.clean(), self.typedefs.clean(),
self.statics.clean(), self.traits.clean(),
self.impls.clean(), self.view_items.clean()].concat_vec()
self.impls.clean(), self.view_items.clean(),
self.macros.clean()].concat_vec()
})
}
}
Expand Down Expand Up @@ -1263,3 +1264,23 @@ fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
None => None
}
}

#[deriving(Clone, Encodable, Decodable)]
pub struct Macro {
source: ~str,
}

impl Clean<Item> for doctree::Macro {
fn clean(&self) -> Item {
Item {
name: Some(self.name.clean()),
attrs: self.attrs.clean(),
source: self.where.clean(),
visibility: ast::Public.clean(),
id: self.id,
inner: MacroItem(Macro {
source: self.where.to_src(),
}),
}
}
}
9 changes: 9 additions & 0 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Module {
impls: ~[Impl],
foreigns: ~[ast::ForeignMod],
view_items: ~[ast::ViewItem],
macros: ~[Macro],
}

impl Module {
Expand All @@ -52,6 +53,7 @@ impl Module {
impls : ~[],
view_items : ~[],
foreigns : ~[],
macros : ~[],
}
}
}
Expand Down Expand Up @@ -157,6 +159,13 @@ pub struct Impl {
id: ast::NodeId,
}

pub struct Macro {
name: Ident,
id: ast::NodeId,
attrs: ~[ast::Attribute],
where: Span,
}

pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
if sd.ctor_id.is_some() {
// We are in a tuple-struct
Expand Down
92 changes: 60 additions & 32 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub struct Cache {
priv parent_stack: ~[ast::NodeId],
priv search_index: ~[IndexItem],
priv privmod: bool,
priv public_items: HashSet<ast::NodeId>,
}

/// Helper struct to render all source code to HTML pages
Expand Down Expand Up @@ -231,18 +232,23 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
}

// Crawl the crate to build various caches used for the output
let mut cache = Cache {
impls: HashMap::new(),
typarams: HashMap::new(),
paths: HashMap::new(),
traits: HashMap::new(),
implementors: HashMap::new(),
stack: ~[],
parent_stack: ~[],
search_index: ~[],
extern_locations: HashMap::new(),
privmod: false,
};
let mut cache = local_data::get(::analysiskey, |analysis| {
let public_items = analysis.map(|a| a.public_items.clone());
let public_items = public_items.unwrap_or(HashSet::new());
Cache {
impls: HashMap::new(),
typarams: HashMap::new(),
paths: HashMap::new(),
traits: HashMap::new(),
implementors: HashMap::new(),
stack: ~[],
parent_stack: ~[],
search_index: ~[],
extern_locations: HashMap::new(),
privmod: false,
public_items: public_items,
}
});
cache.stack.push(krate.name.clone());
krate = cache.fold_crate(krate);

Expand Down Expand Up @@ -305,7 +311,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
krate = folder.fold_crate(krate);
}

for (&n, e) in krate.externs.iter() {
for &(n, ref e) in krate.externs.iter() {
cache.extern_locations.insert(n, extern_location(e, &cx.dst));
}

Expand Down Expand Up @@ -565,8 +571,24 @@ impl DocFolder for Cache {
clean::StructItem(..) | clean::EnumItem(..) |
clean::TypedefItem(..) | clean::TraitItem(..) |
clean::FunctionItem(..) | clean::ModuleItem(..) |
clean::ForeignFunctionItem(..) | clean::VariantItem(..) => {
self.paths.insert(item.id, (self.stack.clone(), shortty(&item)));
clean::ForeignFunctionItem(..) => {
// Reexported items mean that the same id can show up twice in
// the rustdoc ast that we're looking at. We know, however, that
// a reexported item doesn't show up in the `public_items` map,
// so we can skip inserting into the paths map if there was
// already an entry present and we're not a public item.
if !self.paths.contains_key(&item.id) ||
self.public_items.contains(&item.id) {
self.paths.insert(item.id,
(self.stack.clone(), shortty(&item)));
}
}
// link variants to their parent enum because pages aren't emitted
// for each variant
clean::VariantItem(..) => {
let mut stack = self.stack.clone();
stack.pop();
self.paths.insert(item.id, (stack, "enum"));
}
_ => {}
}
Expand Down Expand Up @@ -791,6 +813,7 @@ fn shortty(item: &clean::Item) -> &'static str {
clean::VariantItem(..) => "variant",
clean::ForeignFunctionItem(..) => "ffi",
clean::ForeignStaticItem(..) => "ffs",
clean::MacroItem(..) => "macro",
}
}

Expand Down Expand Up @@ -869,6 +892,7 @@ impl<'a> fmt::Show for Item<'a> {
clean::StructItem(ref s) => item_struct(fmt.buf, self.item, s),
clean::EnumItem(ref e) => item_enum(fmt.buf, self.item, e),
clean::TypedefItem(ref t) => item_typedef(fmt.buf, self.item, t),
clean::MacroItem(ref m) => item_macro(fmt.buf, self.item, m),
_ => Ok(())
}
}
Expand Down Expand Up @@ -937,6 +961,8 @@ fn item_module(w: &mut Writer, cx: &Context,
(_, &clean::ViewItemItem(..)) => Greater,
(&clean::ModuleItem(..), _) => Less,
(_, &clean::ModuleItem(..)) => Greater,
(&clean::MacroItem(..), _) => Less,
(_, &clean::MacroItem(..)) => Greater,
(&clean::StructItem(..), _) => Less,
(_, &clean::StructItem(..)) => Greater,
(&clean::EnumItem(..), _) => Less,
Expand Down Expand Up @@ -987,6 +1013,7 @@ fn item_module(w: &mut Writer, cx: &Context,
clean::VariantItem(..) => "Variants",
clean::ForeignFunctionItem(..) => "Foreign Functions",
clean::ForeignStaticItem(..) => "Foreign Statics",
clean::MacroItem(..) => "Macros",
}));
}

Expand Down Expand Up @@ -1099,15 +1126,15 @@ fn item_trait(w: &mut Writer, it: &clean::Item,
if_ok!(write!(w, "\\{\n"));
for m in required.iter() {
if_ok!(write!(w, " "));
if_ok!(render_method(w, m.item(), true));
if_ok!(render_method(w, m.item()));
if_ok!(write!(w, ";\n"));
}
if required.len() > 0 && provided.len() > 0 {
if_ok!(w.write("\n".as_bytes()));
}
for m in provided.iter() {
if_ok!(write!(w, " "));
if_ok!(render_method(w, m.item(), true));
if_ok!(render_method(w, m.item()));
if_ok!(write!(w, " \\{ ... \\}\n"));
}
if_ok!(write!(w, "\\}"));
Expand All @@ -1121,7 +1148,7 @@ fn item_trait(w: &mut Writer, it: &clean::Item,
if_ok!(write!(w, "<h3 id='{}.{}' class='method'><code>",
shortty(m.item()),
*m.item().name.get_ref()));
if_ok!(render_method(w, m.item(), false));
if_ok!(render_method(w, m.item()));
if_ok!(write!(w, "</code></h3>"));
if_ok!(document(w, m.item()));
Ok(())
Expand Down Expand Up @@ -1176,32 +1203,27 @@ fn item_trait(w: &mut Writer, it: &clean::Item,
})
}

fn render_method(w: &mut Writer, meth: &clean::Item,
withlink: bool) -> fmt::Result {
fn render_method(w: &mut Writer, meth: &clean::Item) -> fmt::Result {
fn fun(w: &mut Writer, it: &clean::Item, purity: ast::Purity,
g: &clean::Generics, selfty: &clean::SelfTy, d: &clean::FnDecl,
withlink: bool) -> fmt::Result {
write!(w, "{}fn {withlink, select,
true{<a href='\\#{ty}.{name}'
class='fnname'>{name}</a>}
other{<span class='fnname'>{name}</span>}
}{generics}{decl}",
g: &clean::Generics, selfty: &clean::SelfTy,
d: &clean::FnDecl) -> fmt::Result {
write!(w, "{}fn <a href='\\#{ty}.{name}' class='fnname'>{name}</a>\
{generics}{decl}",
match purity {
ast::UnsafeFn => "unsafe ",
_ => "",
},
ty = shortty(it),
name = it.name.get_ref().as_slice(),
generics = *g,
decl = Method(selfty, d),
withlink = if withlink {"true"} else {"false"})
decl = Method(selfty, d))
}
match meth.inner {
clean::TyMethodItem(ref m) => {
fun(w, meth, m.purity, &m.generics, &m.self_, &m.decl, withlink)
fun(w, meth, m.purity, &m.generics, &m.self_, &m.decl)
}
clean::MethodItem(ref m) => {
fun(w, meth, m.purity, &m.generics, &m.self_, &m.decl, withlink)
fun(w, meth, m.purity, &m.generics, &m.self_, &m.decl)
}
_ => unreachable!()
}
Expand Down Expand Up @@ -1432,7 +1454,7 @@ fn render_impl(w: &mut Writer, i: &clean::Impl,
fn docmeth(w: &mut Writer, item: &clean::Item) -> io::IoResult<bool> {
if_ok!(write!(w, "<h4 id='method.{}' class='method'><code>",
*item.name.get_ref()));
if_ok!(render_method(w, item, false));
if_ok!(render_method(w, item));
if_ok!(write!(w, "</code></h4>\n"));
match item.doc_value() {
Some(s) => {
Expand Down Expand Up @@ -1609,3 +1631,9 @@ impl<'a> fmt::Show for Source<'a> {
Ok(())
}
}

fn item_macro(w: &mut Writer, it: &clean::Item,
t: &clean::Macro) -> fmt::Result {
if_ok!(write!(w, "<pre class='macro'>{}</pre>", t.source));
document(w, it)
}
2 changes: 2 additions & 0 deletions src/librustdoc/html/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,5 @@ a {
.stability.Stable { border-color: #AEC516; color: #7c8b10; }
.stability.Frozen { border-color: #009431; color: #007726; }
.stability.Locked { border-color: #0084B6; color: #00668c; }

:target { background: #FDFFD3; }
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn json_output(krate: clean::Crate, res: ~[plugins::PluginJson],
};
let crate_json = match json::from_str(crate_json_str) {
Ok(j) => j,
Err(_) => fail!("Rust generated JSON is invalid??")
Err(e) => fail!("Rust generated JSON is invalid: {:?}", e)
};

json.insert(~"crate", crate_json);
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl<'a> fold::DocFolder for Stripper<'a> {
}
clean::ImplItem(..) => {}

// tymethods have no control over privacy
clean::TyMethodItem(..) => {}
// tymethods/macros have no control over privacy
clean::MacroItem(..) | clean::TyMethodItem(..) => {}
}

let fastreturn = match i.inner {
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,14 @@ impl<'a> RustdocVisitor<'a> {
ast::ItemForeignMod(ref fm) => {
om.foreigns.push(fm.clone());
}
_ => (),
ast::ItemMac(ref _m) => {
om.macros.push(Macro {
id: item.id,
attrs: item.attrs.clone(),
name: item.ident,
where: item.span,
})
}
}
}
}
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
#[cfg(test)] pub use ops = realstd::ops;
#[cfg(test)] pub use cmp = realstd::cmp;

mod macros;
pub mod macros;

mod rtdeps;

Expand Down
Loading