Skip to content

Commit

Permalink
add derive macros' helper attributes to doc output
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Sep 26, 2018
1 parent 869ebc4 commit 27429d9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/librustdoc/clean/inline.rs
Expand Up @@ -13,7 +13,7 @@
use std::iter::once;

use syntax::ast;
use syntax::ext::base::MacroKind;
use syntax::ext::base::{MacroKind, SyntaxExtension};
use syntax_pos::Span;

use rustc::hir;
Expand Down Expand Up @@ -465,8 +465,14 @@ fn build_macro(cx: &DocContext, did: DefId, name: ast::Name) -> clean::ItemEnum
})
}
LoadedMacro::ProcMacro(ext) => {
let helpers = match &*ext {
&SyntaxExtension::ProcMacroDerive(_, ref syms, ..) => { syms.clean(cx) }
_ => Vec::new(),
};

clean::ProcMacroItem(clean::ProcMacro {
kind: ext.kind(),
helpers,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -3793,6 +3793,7 @@ impl Clean<Item> for doctree::Macro {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct ProcMacro {
pub kind: MacroKind,
pub helpers: Vec<String>,
}

impl Clean<Item> for doctree::ProcMacro {
Expand All @@ -3807,6 +3808,7 @@ impl Clean<Item> for doctree::ProcMacro {
def_id: cx.tcx.hir.local_def_id(self.id),
inner: ProcMacroItem(ProcMacro {
kind: self.kind,
helpers: self.helpers.clean(cx),
}),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctree.rs
Expand Up @@ -271,6 +271,7 @@ pub struct ProcMacro {
pub name: Name,
pub id: NodeId,
pub kind: MacroKind,
pub helpers: Vec<Name>,
pub attrs: hir::HirVec<ast::Attribute>,
pub whence: Span,
pub stab: Option<attr::Stability>,
Expand Down
8 changes: 8 additions & 0 deletions src/librustdoc/html/render.rs
Expand Up @@ -4626,6 +4626,14 @@ fn item_proc_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, m: &c
MacroKind::Derive => {
write!(w, "<pre class='rust derive'>")?;
write!(w, "#[derive({})]", name)?;
if !m.helpers.is_empty() {
writeln!(w, "\n{{")?;
writeln!(w, " // Attributes available to this derive:")?;
for attr in &m.helpers {
writeln!(w, " #[{}]", attr)?;
}
write!(w, "}}")?;
}
write!(w, "</pre>")?;
}
_ => {}
Expand Down
16 changes: 16 additions & 0 deletions src/librustdoc/visit_ast.rs
Expand Up @@ -197,10 +197,26 @@ impl<'a, 'tcx, 'rcx, 'cstore> RustdocVisitor<'a, 'tcx, 'rcx, 'cstore> {
name
};

let mut helpers = Vec::new();
for mi in item.attrs.lists("proc_macro_derive") {
if !mi.check_name("attributes") {
continue;
}

if let Some(list) = mi.meta_item_list() {
for inner_mi in list {
if let Some(name) = inner_mi.name() {
helpers.push(name);
}
}
}
}

om.proc_macros.push(ProcMacro {
name,
id: item.id,
kind,
helpers,
attrs: item.attrs.clone(),
whence: item.span,
stab: self.stability(item.id),
Expand Down

0 comments on commit 27429d9

Please sign in to comment.