Skip to content

Commit 867988c

Browse files
committed
rustdoc: Show macros in documentation
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 rust-lang#3163 cc rust-lang#5605 Closes rust-lang#9954
1 parent f0cb0eb commit 867988c

File tree

7 files changed

+226
-5
lines changed

7 files changed

+226
-5
lines changed

src/librustdoc/clean.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub enum ItemEnum {
181181
VariantItem(Variant),
182182
ForeignFunctionItem(Function),
183183
ForeignStaticItem(Static),
184+
MacroItem(Macro),
184185
}
185186

186187
#[deriving(Clone, Encodable, Decodable)]
@@ -206,7 +207,8 @@ impl Clean<Item> for doctree::Module {
206207
self.fns.clean(), self.foreigns.clean().concat_vec(),
207208
self.mods.clean(), self.typedefs.clean(),
208209
self.statics.clean(), self.traits.clean(),
209-
self.impls.clean(), self.view_items.clean()].concat_vec()
210+
self.impls.clean(), self.view_items.clean(),
211+
self.macros.clean()].concat_vec()
210212
})
211213
}
212214
}
@@ -1263,3 +1265,23 @@ fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
12631265
None => None
12641266
}
12651267
}
1268+
1269+
#[deriving(Clone, Encodable, Decodable)]
1270+
pub struct Macro {
1271+
source: ~str,
1272+
}
1273+
1274+
impl Clean<Item> for doctree::Macro {
1275+
fn clean(&self) -> Item {
1276+
Item {
1277+
name: Some(self.name.clean()),
1278+
attrs: self.attrs.clean(),
1279+
source: self.where.clean(),
1280+
visibility: ast::Public.clean(),
1281+
id: self.id,
1282+
inner: MacroItem(Macro {
1283+
source: self.where.to_src(),
1284+
}),
1285+
}
1286+
}
1287+
}

src/librustdoc/doctree.rs

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Module {
3232
impls: ~[Impl],
3333
foreigns: ~[ast::ForeignMod],
3434
view_items: ~[ast::ViewItem],
35+
macros: ~[Macro],
3536
}
3637

3738
impl Module {
@@ -52,6 +53,7 @@ impl Module {
5253
impls : ~[],
5354
view_items : ~[],
5455
foreigns : ~[],
56+
macros : ~[],
5557
}
5658
}
5759
}
@@ -157,6 +159,13 @@ pub struct Impl {
157159
id: ast::NodeId,
158160
}
159161

162+
pub struct Macro {
163+
name: Ident,
164+
id: ast::NodeId,
165+
attrs: ~[ast::Attribute],
166+
where: Span,
167+
}
168+
160169
pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
161170
if sd.ctor_id.is_some() {
162171
// We are in a tuple-struct

src/librustdoc/html/render.rs

+11
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ fn shortty(item: &clean::Item) -> &'static str {
798798
clean::VariantItem(..) => "variant",
799799
clean::ForeignFunctionItem(..) => "ffi",
800800
clean::ForeignStaticItem(..) => "ffs",
801+
clean::MacroItem(..) => "macro",
801802
}
802803
}
803804

@@ -876,6 +877,7 @@ impl<'a> fmt::Show for Item<'a> {
876877
clean::StructItem(ref s) => item_struct(fmt.buf, self.item, s),
877878
clean::EnumItem(ref e) => item_enum(fmt.buf, self.item, e),
878879
clean::TypedefItem(ref t) => item_typedef(fmt.buf, self.item, t),
880+
clean::MacroItem(ref m) => item_macro(fmt.buf, self.item, m),
879881
_ => Ok(())
880882
}
881883
}
@@ -944,6 +946,8 @@ fn item_module(w: &mut Writer, cx: &Context,
944946
(_, &clean::ViewItemItem(..)) => Greater,
945947
(&clean::ModuleItem(..), _) => Less,
946948
(_, &clean::ModuleItem(..)) => Greater,
949+
(&clean::MacroItem(..), _) => Less,
950+
(_, &clean::MacroItem(..)) => Greater,
947951
(&clean::StructItem(..), _) => Less,
948952
(_, &clean::StructItem(..)) => Greater,
949953
(&clean::EnumItem(..), _) => Less,
@@ -994,6 +998,7 @@ fn item_module(w: &mut Writer, cx: &Context,
994998
clean::VariantItem(..) => "Variants",
995999
clean::ForeignFunctionItem(..) => "Foreign Functions",
9961000
clean::ForeignStaticItem(..) => "Foreign Statics",
1001+
clean::MacroItem(..) => "Macros",
9971002
}));
9981003
}
9991004

@@ -1616,3 +1621,9 @@ impl<'a> fmt::Show for Source<'a> {
16161621
Ok(())
16171622
}
16181623
}
1624+
1625+
fn item_macro(w: &mut Writer, it: &clean::Item,
1626+
t: &clean::Macro) -> fmt::Result {
1627+
if_ok!(write!(w, "<pre class='macro'>{}</pre>", t.source));
1628+
document(w, it)
1629+
}

src/librustdoc/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'a> fold::DocFolder for Stripper<'a> {
150150
}
151151
clean::ImplItem(..) => {}
152152

153-
// tymethods have no control over privacy
154-
clean::TyMethodItem(..) => {}
153+
// tymethods/macros have no control over privacy
154+
clean::MacroItem(..) | clean::TyMethodItem(..) => {}
155155
}
156156

157157
let fastreturn = match i.inner {

src/librustdoc/visit_ast.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,14 @@ impl<'a> RustdocVisitor<'a> {
280280
ast::ItemForeignMod(ref fm) => {
281281
om.foreigns.push(fm.clone());
282282
}
283-
_ => (),
283+
ast::ItemMac(ref _m) => {
284+
om.macros.push(Macro {
285+
id: item.id,
286+
attrs: item.attrs.clone(),
287+
name: item.ident,
288+
where: item.span,
289+
})
290+
}
284291
}
285292
}
286293
}

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
#[cfg(test)] pub use ops = realstd::ops;
7878
#[cfg(test)] pub use cmp = realstd::cmp;
7979

80-
mod macros;
80+
pub mod macros;
8181

8282
mod rtdeps;
8383

0 commit comments

Comments
 (0)