Skip to content

Commit aea1bd0

Browse files
handle proc-macros as macros instead of functions
1 parent f49f6e7 commit aea1bd0

File tree

11 files changed

+243
-50
lines changed

11 files changed

+243
-50
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,14 @@ pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name, visited: &mut FxHa
105105
record_extern_fqn(cx, did, clean::TypeKind::Const);
106106
clean::ConstantItem(build_const(cx, did))
107107
}
108-
// FIXME(misdreavus): if attributes/derives come down here we should probably document them
109-
// separately
110-
Def::Macro(did, MacroKind::Bang) => {
111-
record_extern_fqn(cx, did, clean::TypeKind::Macro);
112-
if let Some(mac) = build_macro(cx, did, name) {
113-
clean::MacroItem(mac)
114-
} else {
115-
return None;
108+
Def::Macro(did, mac_kind) => {
109+
match mac_kind {
110+
MacroKind::Bang => record_extern_fqn(cx, did, clean::TypeKind::Macro),
111+
MacroKind::Attr => record_extern_fqn(cx, did, clean::TypeKind::Attr),
112+
MacroKind::Derive => record_extern_fqn(cx, did, clean::TypeKind::Derive),
113+
MacroKind::ProcMacroStub => return None,
116114
}
115+
build_macro(cx, did, name)
117116
}
118117
_ => return None,
119118
};
@@ -442,31 +441,35 @@ fn build_static(cx: &DocContext, did: DefId, mutable: bool) -> clean::Static {
442441
}
443442
}
444443

445-
fn build_macro(cx: &DocContext, did: DefId, name: ast::Name) -> Option<clean::Macro> {
444+
fn build_macro(cx: &DocContext, did: DefId, name: ast::Name) -> clean::ItemEnum {
446445
let imported_from = cx.tcx.original_crate_name(did.krate);
447-
let def = match cx.cstore.load_macro_untracked(did, cx.sess()) {
448-
LoadedMacro::MacroDef(macro_def) => macro_def,
449-
// FIXME(jseyfried): document proc macro re-exports
450-
LoadedMacro::ProcMacro(..) => return None,
451-
};
452-
453-
let matchers: hir::HirVec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.node {
454-
let tts: Vec<_> = def.stream().into_trees().collect();
455-
tts.chunks(4).map(|arm| arm[0].span()).collect()
456-
} else {
457-
unreachable!()
458-
};
459-
460-
let source = format!("macro_rules! {} {{\n{}}}",
461-
name.clean(cx),
462-
matchers.iter().map(|span| {
463-
format!(" {} => {{ ... }};\n", span.to_src(cx))
464-
}).collect::<String>());
446+
match cx.cstore.load_macro_untracked(did, cx.sess()) {
447+
LoadedMacro::MacroDef(def) => {
448+
let matchers: hir::HirVec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.node {
449+
let tts: Vec<_> = def.stream().into_trees().collect();
450+
tts.chunks(4).map(|arm| arm[0].span()).collect()
451+
} else {
452+
unreachable!()
453+
};
454+
455+
let source = format!("macro_rules! {} {{\n{}}}",
456+
name.clean(cx),
457+
matchers.iter().map(|span| {
458+
format!(" {} => {{ ... }};\n", span.to_src(cx))
459+
}).collect::<String>());
460+
461+
clean::MacroItem(clean::Macro {
462+
source,
463+
imported_from: Some(imported_from).clean(cx),
464+
})
465+
}
466+
LoadedMacro::ProcMacro(ext) => {
467+
clean::ProcMacroItem(clean::ProcMacro {
468+
kind: ext.kind(),
469+
})
470+
}
471+
}
465472

466-
Some(clean::Macro {
467-
source,
468-
imported_from: Some(imported_from).clean(cx),
469-
})
470473
}
471474

472475
/// A trait's generics clause actually contains all of the predicates for all of

src/librustdoc/clean/mod.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use self::Visibility::{Public, Inherited};
2121
use rustc_target::spec::abi::Abi;
2222
use syntax::ast::{self, AttrStyle, Ident};
2323
use syntax::attr;
24+
use syntax::ext::base::MacroKind;
2425
use syntax::source_map::{dummy_spanned, Spanned};
2526
use syntax::ptr::P;
2627
use syntax::symbol::keywords::{self, Keyword};
@@ -527,6 +528,7 @@ pub enum ItemEnum {
527528
/// `type`s from an extern block
528529
ForeignTypeItem,
529530
MacroItem(Macro),
531+
ProcMacroItem(ProcMacro),
530532
PrimitiveItem(PrimitiveType),
531533
AssociatedConstItem(Type, Option<String>),
532534
AssociatedTypeItem(Vec<GenericBound>, Option<Type>),
@@ -588,6 +590,7 @@ impl Clean<Item> for doctree::Module {
588590
items.extend(self.traits.iter().map(|x| x.clean(cx)));
589591
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
590592
items.extend(self.macros.iter().map(|x| x.clean(cx)));
593+
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
591594

592595
// determine if we should display the inner contents or
593596
// the outer `mod` item for the source code.
@@ -2189,6 +2192,8 @@ pub enum TypeKind {
21892192
Typedef,
21902193
Foreign,
21912194
Macro,
2195+
Attr,
2196+
Derive,
21922197
}
21932198

21942199
pub trait GetDefId {
@@ -3725,7 +3730,12 @@ pub fn register_def(cx: &DocContext, def: Def) -> DefId {
37253730
Def::Static(i, _) => (i, TypeKind::Static),
37263731
Def::Variant(i) => (cx.tcx.parent_def_id(i).expect("cannot get parent def id"),
37273732
TypeKind::Enum),
3728-
Def::Macro(i, _) => (i, TypeKind::Macro),
3733+
Def::Macro(i, mac_kind) => match mac_kind {
3734+
MacroKind::Bang => (i, TypeKind::Macro),
3735+
MacroKind::Attr => (i, TypeKind::Attr),
3736+
MacroKind::Derive => (i, TypeKind::Derive),
3737+
MacroKind::ProcMacroStub => unreachable!(),
3738+
},
37293739
Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
37303740
Def::SelfTy(_, Some(impl_def_id)) => {
37313741
return impl_def_id
@@ -3780,6 +3790,28 @@ impl Clean<Item> for doctree::Macro {
37803790
}
37813791
}
37823792

3793+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
3794+
pub struct ProcMacro {
3795+
pub kind: MacroKind,
3796+
}
3797+
3798+
impl Clean<Item> for doctree::ProcMacro {
3799+
fn clean(&self, cx: &DocContext) -> Item {
3800+
Item {
3801+
name: Some(self.name.clean(cx)),
3802+
attrs: self.attrs.clean(cx),
3803+
source: self.whence.clean(cx),
3804+
visibility: Some(Public),
3805+
stability: self.stab.clean(cx),
3806+
deprecation: self.depr.clean(cx),
3807+
def_id: cx.tcx.hir.local_def_id(self.id),
3808+
inner: ProcMacroItem(ProcMacro {
3809+
kind: self.kind,
3810+
}),
3811+
}
3812+
}
3813+
}
3814+
37833815
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
37843816
pub struct Stability {
37853817
pub level: stability::StabilityLevel,

src/librustdoc/doctree.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use self::StructType::*;
1515
use syntax::ast;
1616
use syntax::ast::{Name, NodeId};
1717
use syntax::attr;
18+
use syntax::ext::base::MacroKind;
1819
use syntax::ptr::P;
1920
use syntax::source_map::Spanned;
2021
use syntax_pos::{self, Span};
@@ -46,6 +47,7 @@ pub struct Module {
4647
pub impls: Vec<Impl>,
4748
pub foreigns: Vec<hir::ForeignMod>,
4849
pub macros: Vec<Macro>,
50+
pub proc_macros: Vec<ProcMacro>,
4951
pub is_crate: bool,
5052
}
5153

@@ -75,6 +77,7 @@ impl Module {
7577
impls : Vec::new(),
7678
foreigns : Vec::new(),
7779
macros : Vec::new(),
80+
proc_macros: Vec::new(),
7881
is_crate : false,
7982
}
8083
}
@@ -264,6 +267,16 @@ pub struct Import {
264267
pub whence: Span,
265268
}
266269

270+
pub struct ProcMacro {
271+
pub name: Name,
272+
pub id: NodeId,
273+
pub kind: MacroKind,
274+
pub attrs: hir::HirVec<ast::Attribute>,
275+
pub whence: Span,
276+
pub stab: Option<attr::Stability>,
277+
pub depr: Option<attr::Deprecation>,
278+
}
279+
267280
pub fn struct_type_from_def(vdata: &hir::VariantData) -> StructType {
268281
match *vdata {
269282
hir::VariantData::Struct(..) => Plain,

src/librustdoc/html/item_type.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Item types.
1212
1313
use std::fmt;
14+
use syntax::ext::base::MacroKind;
1415
use clean;
1516

1617
/// Item type. Corresponds to `clean::ItemEnum` variants.
@@ -19,6 +20,11 @@ use clean;
1920
/// discriminants. JavaScript then is used to decode them into the original value.
2021
/// Consequently, every change to this type should be synchronized to
2122
/// the `itemTypes` mapping table in `static/main.js`.
23+
///
24+
/// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
25+
/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
26+
/// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
27+
/// ordering based on a helper function inside `item_module`, in the same file.
2228
#[derive(Copy, PartialEq, Clone, Debug)]
2329
pub enum ItemType {
2430
Module = 0,
@@ -44,6 +50,8 @@ pub enum ItemType {
4450
ForeignType = 20,
4551
Keyword = 21,
4652
Existential = 22,
53+
ProcAttribute = 23,
54+
ProcDerive = 24,
4755
}
4856

4957

@@ -88,6 +96,12 @@ impl<'a> From<&'a clean::Item> for ItemType {
8896
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
8997
clean::ForeignTypeItem => ItemType::ForeignType,
9098
clean::KeywordItem(..) => ItemType::Keyword,
99+
clean::ProcMacroItem(ref mac) => match mac.kind {
100+
MacroKind::Bang => ItemType::Macro,
101+
MacroKind::Attr => ItemType::ProcAttribute,
102+
MacroKind::Derive => ItemType::ProcDerive,
103+
MacroKind::ProcMacroStub => unreachable!(),
104+
}
91105
clean::StrippedItem(..) => unreachable!(),
92106
}
93107
}
@@ -107,7 +121,9 @@ impl From<clean::TypeKind> for ItemType {
107121
clean::TypeKind::Variant => ItemType::Variant,
108122
clean::TypeKind::Typedef => ItemType::Typedef,
109123
clean::TypeKind::Foreign => ItemType::ForeignType,
110-
clean::TypeKind::Macro => ItemType::Macro,
124+
clean::TypeKind::Macro => ItemType::Macro,
125+
clean::TypeKind::Attr => ItemType::ProcAttribute,
126+
clean::TypeKind::Derive => ItemType::ProcDerive,
111127
}
112128
}
113129
}
@@ -138,6 +154,8 @@ impl ItemType {
138154
ItemType::ForeignType => "foreigntype",
139155
ItemType::Keyword => "keyword",
140156
ItemType::Existential => "existential",
157+
ItemType::ProcAttribute => "attr",
158+
ItemType::ProcDerive => "derive",
141159
}
142160
}
143161

@@ -166,7 +184,9 @@ impl ItemType {
166184
ItemType::Constant |
167185
ItemType::AssociatedConst => NameSpace::Value,
168186

169-
ItemType::Macro => NameSpace::Macro,
187+
ItemType::Macro |
188+
ItemType::ProcAttribute |
189+
ItemType::ProcDerive => NameSpace::Macro,
170190

171191
ItemType::Keyword => NameSpace::Keyword,
172192
}

src/librustdoc/html/render.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use externalfiles::ExternalHtml;
5656

5757
use serialize::json::{ToJson, Json, as_json};
5858
use syntax::ast;
59+
use syntax::ext::base::MacroKind;
5960
use syntax::source_map::FileName;
6061
use syntax::feature_gate::UnstableFeatures;
6162
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
@@ -2155,6 +2156,12 @@ impl<'a> fmt::Display for Item<'a> {
21552156
clean::EnumItem(..) => write!(fmt, "Enum ")?,
21562157
clean::TypedefItem(..) => write!(fmt, "Type Definition ")?,
21572158
clean::MacroItem(..) => write!(fmt, "Macro ")?,
2159+
clean::ProcMacroItem(ref mac) => match mac.kind {
2160+
MacroKind::Bang => write!(fmt, "Macro ")?,
2161+
MacroKind::Attr => write!(fmt, "Attribute Macro ")?,
2162+
MacroKind::Derive => write!(fmt, "Derive Macro ")?,
2163+
MacroKind::ProcMacroStub => unreachable!(),
2164+
}
21582165
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
21592166
clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
21602167
clean::ConstantItem(..) => write!(fmt, "Constant ")?,
@@ -2191,6 +2198,7 @@ impl<'a> fmt::Display for Item<'a> {
21912198
clean::EnumItem(ref e) => item_enum(fmt, self.cx, self.item, e),
21922199
clean::TypedefItem(ref t, _) => item_typedef(fmt, self.cx, self.item, t),
21932200
clean::MacroItem(ref m) => item_macro(fmt, self.cx, self.item, m),
2201+
clean::ProcMacroItem(ref m) => item_proc_macro(fmt, self.cx, self.item, m),
21942202
clean::PrimitiveItem(ref p) => item_primitive(fmt, self.cx, self.item, p),
21952203
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) =>
21962204
item_static(fmt, self.cx, self.item, i),
@@ -4523,6 +4531,8 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
45234531
ItemType::ForeignType => ("foreign-types", "Foreign Types"),
45244532
ItemType::Keyword => ("keywords", "Keywords"),
45254533
ItemType::Existential => ("existentials", "Existentials"),
4534+
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
4535+
ItemType::ProcDerive => ("derives", "Derive Macros"),
45264536
}
45274537
}
45284538

@@ -4598,6 +4608,17 @@ fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
45984608
document(w, cx, it)
45994609
}
46004610

4611+
fn item_proc_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, m: &clean::ProcMacro)
4612+
-> fmt::Result
4613+
{
4614+
if m.kind == MacroKind::Bang {
4615+
write!(w, "<pre class='rust macro'>")?;
4616+
write!(w, "{}!() {{ /* proc-macro */ }}", it.name.as_ref().unwrap())?;
4617+
write!(w, "</pre>")?;
4618+
}
4619+
document(w, cx, it)
4620+
}
4621+
46014622
fn item_primitive(w: &mut fmt::Formatter, cx: &Context,
46024623
it: &clean::Item,
46034624
_p: &clean::PrimitiveType) -> fmt::Result {

src/librustdoc/html/static/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"associatedconstant",
4040
"union",
4141
"foreigntype",
42-
"keyword"];
42+
"keyword",
43+
"existential",
44+
"attr",
45+
"derive"];
4346

4447
var search_input = document.getElementsByClassName('search-input')[0];
4548

src/librustdoc/html/static/themes/dark.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ pre {
124124
.content .highlighted.tymethod { background-color: #4950ed; }
125125
.content .highlighted.type { background-color: #38902c; }
126126
.content .highlighted.foreigntype { background-color: #b200d6; }
127+
.content .highlighted.attr,
128+
.content .highlighted.derive,
127129
.content .highlighted.macro { background-color: #217d1c; }
128130
.content .highlighted.constant,
129131
.content .highlighted.static { background-color: #0063cc; }
@@ -134,6 +136,8 @@ pre {
134136
.content span.struct, .content a.struct, .block a.current.struct { color: #2dbfb8; }
135137
.content span.type, .content a.type, .block a.current.type { color: #ff7f00; }
136138
.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #dd7de8; }
139+
.content span.attr, .content a.attr, .block a.current.attr,
140+
.content span.derive, .content a.derive, .block a.current.derive,
137141
.content span.macro, .content a.macro, .block a.current.macro { color: #09bd00; }
138142
.content span.union, .content a.union, .block a.current.union { color: #a6ae37; }
139143
.content span.constant, .content a.constant, .block a.current.constant,

src/librustdoc/html/static/themes/light.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ pre {
124124
.content .highlighted.tymethod { background-color: #c6afb3; }
125125
.content .highlighted.type { background-color: #ffc891; }
126126
.content .highlighted.foreigntype { background-color: #f5c4ff; }
127+
.content .highlighted.attr,
128+
.content .highlighted.derive,
127129
.content .highlighted.macro { background-color: #8ce488; }
128130
.content .highlighted.constant,
129131
.content .highlighted.static { background-color: #c3e0ff; }
@@ -134,6 +136,8 @@ pre {
134136
.content span.struct, .content a.struct, .block a.current.struct { color: #ad448e; }
135137
.content span.type, .content a.type, .block a.current.type { color: #ba5d00; }
136138
.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #cd00e2; }
139+
.content span.attr, .content a.attr, .block a.current.attr,
140+
.content span.derive, .content a.derive, .block a.current.derive,
137141
.content span.macro, .content a.macro, .block a.current.macro { color: #068000; }
138142
.content span.union, .content a.union, .block a.current.union { color: #767b27; }
139143
.content span.constant, .content a.constant, .block a.current.constant,

src/librustdoc/passes/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ impl<'a> fold::DocFolder for Stripper<'a> {
249249
// tymethods/macros have no control over privacy
250250
clean::MacroItem(..) | clean::TyMethodItem(..) => {}
251251

252+
// Proc-macros are always public
253+
clean::ProcMacroItem(..) => {}
254+
252255
// Primitives are never stripped
253256
clean::PrimitiveItem(..) => {}
254257

0 commit comments

Comments
 (0)