Skip to content

Commit 7b8c5aa

Browse files
committed
implement macros
1 parent ad9933e commit 7b8c5aa

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,8 +2354,30 @@ impl Clean<Item> for doctree::ForeignItem<'_> {
23542354
}
23552355

23562356
impl Clean<Item> for hir::MacroDef<'_> {
2357-
fn clean(&self, _cx: &DocContext<'_>) -> Item {
2358-
unimplemented!()
2357+
fn clean(&self, cx: &DocContext<'_>) -> Item {
2358+
let tts: Vec<_> = self.ast.body.inner_tokens().trees().collect();
2359+
// Extract the spans of all matchers. They represent the "interface" of the macro.
2360+
let matchers = tts.chunks(4).map(|arm| arm[0].span());
2361+
2362+
Item {
2363+
name: Some(self.ident.name.to_string()), // TODO: this should store a Symbol
2364+
attrs: self.attrs.clean(cx),
2365+
source: self.span.clean(cx),
2366+
visibility: Public,
2367+
stability: cx.stability(self.hir_id),
2368+
deprecation: cx.deprecation(self.hir_id).clean(cx),
2369+
def_id: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
2370+
kind: MacroItem(Macro {
2371+
source: format!(
2372+
"macro_rules! {} {{\n{}}}",
2373+
self.ident.name,
2374+
matchers
2375+
.map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
2376+
.collect::<String>()
2377+
),
2378+
imported_from: None,
2379+
}),
2380+
}
23592381
}
23602382
}
23612383

0 commit comments

Comments
 (0)