Skip to content

Commit

Permalink
rustdoc: Show macros in documentation
Browse files Browse the repository at this point in the history
Any macro tagged with #[macro_export] will be showed in the documentation for
that module. This also documents all the existing macros inside of std::macros.

Closes #3163
cc #5605
Closes #9954
  • Loading branch information
alexcrichton committed Feb 19, 2014
1 parent f0cb0eb commit 867988c
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 5 deletions.
24 changes: 23 additions & 1 deletion src/librustdoc/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub enum ItemEnum {
VariantItem(Variant),
ForeignFunctionItem(Function),
ForeignStaticItem(Static),
MacroItem(Macro),
}

#[deriving(Clone, Encodable, Decodable)]
Expand All @@ -206,7 +207,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 +1265,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
11 changes: 11 additions & 0 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ fn shortty(item: &clean::Item) -> &'static str {
clean::VariantItem(..) => "variant",
clean::ForeignFunctionItem(..) => "ffi",
clean::ForeignStaticItem(..) => "ffs",
clean::MacroItem(..) => "macro",
}
}

Expand Down Expand Up @@ -876,6 +877,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 @@ -944,6 +946,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 @@ -994,6 +998,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 @@ -1616,3 +1621,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)
}
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

0 comments on commit 867988c

Please sign in to comment.