]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/visit_ast.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / librustdoc / visit_ast.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Rust AST Visitor. Extracts useful information and massages it into a form
12//! usable for clean
13
9346a6ac 14use std::mem;
1a4d82fc
JJ
15
16use syntax::abi;
17use syntax::ast;
b039eaaf 18use syntax::attr;
3157f602 19use syntax_pos::Span;
1a4d82fc 20
54a0048b 21use rustc::hir::map as hir_map;
a7813a04 22use rustc::hir::def::Def;
cc61c64b 23use rustc::hir::def_id::{DefId, LOCAL_CRATE};
ea8adc8c 24use rustc::middle::cstore::{LoadedMacro, CrateStore};
a7813a04 25use rustc::middle::privacy::AccessLevel;
476ff2be 26use rustc::util::nodemap::FxHashSet;
1a4d82fc 27
54a0048b 28use rustc::hir;
e9174d1e 29
1a4d82fc 30use core;
476ff2be 31use clean::{self, AttributesExt, NestedAttributesExt};
1a4d82fc
JJ
32use doctree::*;
33
34// looks to me like the first two of these are actually
35// output parameters, maybe only mutated once; perhaps
36// better simply to have the visit method return a tuple
37// containing them?
38
39// also, is there some reason that this doesn't use the 'visit'
40// framework from syntax?
41
42pub struct RustdocVisitor<'a, 'tcx: 'a> {
ea8adc8c 43 cstore: &'tcx CrateStore,
1a4d82fc 44 pub module: Module,
9cc50fc6 45 pub attrs: hir::HirVec<ast::Attribute>,
62682a34 46 pub cx: &'a core::DocContext<'a, 'tcx>,
476ff2be
SL
47 view_item_stack: FxHashSet<ast::NodeId>,
48 inlining: bool,
49 /// Is the current module and all of its parents public?
50 inside_public_path: bool,
cc61c64b 51 reexported_macros: FxHashSet<DefId>,
1a4d82fc
JJ
52}
53
54impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
ea8adc8c
XL
55 pub fn new(cstore: &'tcx CrateStore,
56 cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
1a4d82fc 57 // If the root is reexported, terminate all recursion.
476ff2be 58 let mut stack = FxHashSet();
1a4d82fc
JJ
59 stack.insert(ast::CRATE_NODE_ID);
60 RustdocVisitor {
61 module: Module::new(None),
9cc50fc6 62 attrs: hir::HirVec::new(),
3b2f2976 63 cx,
1a4d82fc 64 view_item_stack: stack,
476ff2be
SL
65 inlining: false,
66 inside_public_path: true,
cc61c64b 67 reexported_macros: FxHashSet(),
ea8adc8c 68 cstore,
1a4d82fc
JJ
69 }
70 }
71
72 fn stability(&self, id: ast::NodeId) -> Option<attr::Stability> {
32a655c1 73 self.cx.tcx.hir.opt_local_def_id(id)
476ff2be 74 .and_then(|def_id| self.cx.tcx.lookup_stability(def_id)).cloned()
1a4d82fc
JJ
75 }
76
9cc50fc6 77 fn deprecation(&self, id: ast::NodeId) -> Option<attr::Deprecation> {
32a655c1 78 self.cx.tcx.hir.opt_local_def_id(id)
476ff2be 79 .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id))
9cc50fc6
SL
80 }
81
e9174d1e 82 pub fn visit(&mut self, krate: &hir::Crate) {
1a4d82fc
JJ
83 self.attrs = krate.attrs.clone();
84
85 self.module = self.visit_mod_contents(krate.span,
86 krate.attrs.clone(),
e9174d1e 87 hir::Public,
1a4d82fc
JJ
88 ast::CRATE_NODE_ID,
89 &krate.module,
90 None);
91 // attach the crate's exported macros to the top-level module:
476ff2be 92 let macro_exports: Vec<_> =
32a655c1 93 krate.exported_macros.iter().map(|def| self.visit_local_macro(def)).collect();
476ff2be 94 self.module.macros.extend(macro_exports);
1a4d82fc
JJ
95 self.module.is_crate = true;
96 }
97
b039eaaf
SL
98 pub fn visit_variant_data(&mut self, item: &hir::Item,
99 name: ast::Name, sd: &hir::VariantData,
e9174d1e 100 generics: &hir::Generics) -> Struct {
1a4d82fc
JJ
101 debug!("Visiting struct");
102 let struct_type = struct_type_from_def(&*sd);
103 Struct {
104 id: item.id,
3b2f2976
XL
105 struct_type,
106 name,
54a0048b 107 vis: item.vis.clone(),
1a4d82fc 108 stab: self.stability(item.id),
9cc50fc6 109 depr: self.deprecation(item.id),
1a4d82fc
JJ
110 attrs: item.attrs.clone(),
111 generics: generics.clone(),
b039eaaf 112 fields: sd.fields().iter().cloned().collect(),
1a4d82fc
JJ
113 whence: item.span
114 }
115 }
116
9e0c209e
SL
117 pub fn visit_union_data(&mut self, item: &hir::Item,
118 name: ast::Name, sd: &hir::VariantData,
119 generics: &hir::Generics) -> Union {
120 debug!("Visiting union");
121 let struct_type = struct_type_from_def(&*sd);
122 Union {
123 id: item.id,
3b2f2976
XL
124 struct_type,
125 name,
9e0c209e
SL
126 vis: item.vis.clone(),
127 stab: self.stability(item.id),
128 depr: self.deprecation(item.id),
129 attrs: item.attrs.clone(),
130 generics: generics.clone(),
131 fields: sd.fields().iter().cloned().collect(),
132 whence: item.span
133 }
134 }
135
e9174d1e 136 pub fn visit_enum_def(&mut self, it: &hir::Item,
b039eaaf 137 name: ast::Name, def: &hir::EnumDef,
e9174d1e 138 params: &hir::Generics) -> Enum {
1a4d82fc
JJ
139 debug!("Visiting enum");
140 Enum {
3b2f2976 141 name,
1a4d82fc
JJ
142 variants: def.variants.iter().map(|v| Variant {
143 name: v.node.name,
144 attrs: v.node.attrs.clone(),
b039eaaf 145 stab: self.stability(v.node.data.id()),
9cc50fc6 146 depr: self.deprecation(v.node.data.id()),
b039eaaf 147 def: v.node.data.clone(),
1a4d82fc
JJ
148 whence: v.span,
149 }).collect(),
54a0048b 150 vis: it.vis.clone(),
1a4d82fc 151 stab: self.stability(it.id),
9cc50fc6 152 depr: self.deprecation(it.id),
1a4d82fc
JJ
153 generics: params.clone(),
154 attrs: it.attrs.clone(),
155 id: it.id,
156 whence: it.span,
157 }
158 }
159
e9174d1e 160 pub fn visit_fn(&mut self, item: &hir::Item,
b039eaaf 161 name: ast::Name, fd: &hir::FnDecl,
e9174d1e
SL
162 unsafety: &hir::Unsafety,
163 constness: hir::Constness,
62682a34 164 abi: &abi::Abi,
32a655c1
SL
165 gen: &hir::Generics,
166 body: hir::BodyId) -> Function {
1a4d82fc
JJ
167 debug!("Visiting fn");
168 Function {
169 id: item.id,
54a0048b 170 vis: item.vis.clone(),
1a4d82fc 171 stab: self.stability(item.id),
9cc50fc6 172 depr: self.deprecation(item.id),
1a4d82fc
JJ
173 attrs: item.attrs.clone(),
174 decl: fd.clone(),
3b2f2976 175 name,
1a4d82fc
JJ
176 whence: item.span,
177 generics: gen.clone(),
178 unsafety: *unsafety,
3b2f2976 179 constness,
9346a6ac 180 abi: *abi,
3b2f2976 181 body,
1a4d82fc
JJ
182 }
183 }
184
9cc50fc6 185 pub fn visit_mod_contents(&mut self, span: Span, attrs: hir::HirVec<ast::Attribute>,
e9174d1e
SL
186 vis: hir::Visibility, id: ast::NodeId,
187 m: &hir::Mod,
b039eaaf 188 name: Option<ast::Name>) -> Module {
1a4d82fc 189 let mut om = Module::new(name);
1a4d82fc
JJ
190 om.where_outer = span;
191 om.where_inner = m.inner;
192 om.attrs = attrs;
54a0048b 193 om.vis = vis.clone();
1a4d82fc 194 om.stab = self.stability(id);
9cc50fc6 195 om.depr = self.deprecation(id);
1a4d82fc 196 om.id = id;
476ff2be
SL
197 // Keep track of if there were any private modules in the path.
198 let orig_inside_public_path = self.inside_public_path;
199 self.inside_public_path &= vis == hir::Public;
92a42be0 200 for i in &m.item_ids {
32a655c1 201 let item = self.cx.tcx.hir.expect_item(i.id);
92a42be0 202 self.visit_item(item, None, &mut om);
1a4d82fc 203 }
476ff2be 204 self.inside_public_path = orig_inside_public_path;
ea8adc8c
XL
205 let def_id = self.cx.tcx.hir.local_def_id(id);
206 if let Some(exports) = self.cx.tcx.module_exports(def_id) {
207 for export in exports.iter() {
8bb4bdeb 208 if let Def::Macro(def_id, ..) = export.def {
cc61c64b 209 if def_id.krate == LOCAL_CRATE || self.reexported_macros.contains(&def_id) {
476ff2be
SL
210 continue // These are `krate.exported_macros`, handled in `self.visit()`.
211 }
cc61c64b 212
ea8adc8c
XL
213 let imported_from = self.cx.tcx.original_crate_name(def_id.krate);
214 let def = match self.cstore.load_macro_untracked(def_id, self.cx.sess()) {
8bb4bdeb 215 LoadedMacro::MacroDef(macro_def) => macro_def,
476ff2be
SL
216 // FIXME(jseyfried): document proc macro reexports
217 LoadedMacro::ProcMacro(..) => continue,
218 };
219
7cac9316
XL
220 let matchers = if let ast::ItemKind::MacroDef(ref def) = def.node {
221 let tts: Vec<_> = def.stream().into_trees().collect();
8bb4bdeb
XL
222 tts.chunks(4).map(|arm| arm[0].span()).collect()
223 } else {
224 unreachable!()
225 };
cc61c64b 226
476ff2be 227 om.macros.push(Macro {
3b2f2976 228 def_id,
476ff2be
SL
229 attrs: def.attrs.clone().into(),
230 name: def.ident.name,
231 whence: def.span,
3b2f2976 232 matchers,
476ff2be
SL
233 stab: self.stability(def.id),
234 depr: self.deprecation(def.id),
32a655c1 235 imported_from: Some(imported_from),
476ff2be 236 })
1a4d82fc
JJ
237 }
238 }
239 }
476ff2be 240 om
1a4d82fc
JJ
241 }
242
54a0048b
SL
243 /// Tries to resolve the target of a `pub use` statement and inlines the
244 /// target if it is defined locally and would not be documented otherwise,
245 /// or when it is specifically requested with `please_inline`.
246 /// (the latter is the case when the import is marked `doc(inline)`)
247 ///
248 /// Cross-crate inlining occurs later on during crate cleaning
249 /// and follows different rules.
250 ///
251 /// Returns true if the target has been inlined.
476ff2be
SL
252 fn maybe_inline_local(&mut self,
253 id: ast::NodeId,
254 def: Def,
255 renamed: Option<ast::Name>,
256 glob: bool,
257 om: &mut Module,
258 please_inline: bool) -> bool {
54a0048b
SL
259
260 fn inherits_doc_hidden(cx: &core::DocContext, mut node: ast::NodeId) -> bool {
32a655c1 261 while let Some(id) = cx.tcx.hir.get_enclosing_scope(node) {
54a0048b 262 node = id;
32a655c1 263 if cx.tcx.hir.attrs(node).lists("doc").has_word("hidden") {
54a0048b
SL
264 return true;
265 }
266 if node == ast::CRATE_NODE_ID {
267 break;
268 }
269 }
270 false
271 }
272
cc61c64b
XL
273 debug!("maybe_inline_local def: {:?}", def);
274
476ff2be
SL
275 let tcx = self.cx.tcx;
276 if def == Def::Err {
277 return false;
278 }
a7813a04 279 let def_did = def.def_id();
54a0048b 280
32a655c1 281 let use_attrs = tcx.hir.attrs(id);
3157f602 282 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
476ff2be
SL
283 let is_no_inline = use_attrs.lists("doc").has_word("no_inline") ||
284 use_attrs.lists("doc").has_word("hidden");
a7813a04 285
cc61c64b
XL
286 // Memoize the non-inlined `pub use`'d macros so we don't push an extra
287 // declaration in `visit_mod_contents()`
288 if !def_did.is_local() {
289 if let Def::Macro(did, _) = def {
290 if please_inline { return true }
291 debug!("memoizing non-inlined macro export: {:?}", def);
292 self.reexported_macros.insert(did);
293 return false;
294 }
295 }
296
a7813a04
XL
297 // For cross-crate impl inlining we need to know whether items are
298 // reachable in documentation - a previously nonreachable item can be
299 // made reachable by cross-crate inlining which we're checking here.
300 // (this is done here because we need to know this upfront)
3157f602 301 if !def_did.is_local() && !is_no_inline {
476ff2be
SL
302 let attrs = clean::inline::load_attrs(self.cx, def_did);
303 let self_is_hidden = attrs.lists("doc").has_word("hidden");
3157f602 304 match def {
a7813a04
XL
305 Def::Trait(did) |
306 Def::Struct(did) |
9e0c209e 307 Def::Union(did) |
a7813a04
XL
308 Def::Enum(did) |
309 Def::TyAlias(did) if !self_is_hidden => {
310 self.cx.access_levels.borrow_mut().map.insert(did, AccessLevel::Public);
311 },
312 Def::Mod(did) => if !self_is_hidden {
313 ::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did);
314 },
315 _ => {},
316 }
cc61c64b 317
a7813a04
XL
318 return false
319 }
320
32a655c1 321 let def_node_id = match tcx.hir.as_local_node_id(def_did) {
a7813a04
XL
322 Some(n) => n, None => return false
323 };
54a0048b 324
a7813a04 325 let is_private = !self.cx.access_levels.borrow().is_public(def_did);
54a0048b 326 let is_hidden = inherits_doc_hidden(self.cx, def_node_id);
54a0048b
SL
327
328 // Only inline if requested or if the item would otherwise be stripped
329 if (!please_inline && !is_private && !is_hidden) || is_no_inline {
1a4d82fc
JJ
330 return false
331 }
54a0048b 332
b039eaaf 333 if !self.view_item_stack.insert(def_node_id) { return false }
1a4d82fc 334
32a655c1 335 let ret = match tcx.hir.get(def_node_id) {
041b39d2 336 hir_map::NodeItem(&hir::Item { node: hir::ItemMod(ref m), .. }) if glob => {
476ff2be 337 let prev = mem::replace(&mut self.inlining, true);
041b39d2
XL
338 for i in &m.item_ids {
339 let i = self.cx.tcx.hir.expect_item(i.id);
340 self.visit_item(i, None, om);
1a4d82fc 341 }
476ff2be 342 self.inlining = prev;
041b39d2
XL
343 true
344 }
345 hir_map::NodeItem(it) if !glob => {
346 let prev = mem::replace(&mut self.inlining, true);
347 self.visit_item(it, renamed, om);
348 self.inlining = prev;
1a4d82fc
JJ
349 true
350 }
351 _ => false,
352 };
b039eaaf 353 self.view_item_stack.remove(&def_node_id);
c30ab7b3 354 ret
1a4d82fc
JJ
355 }
356
e9174d1e 357 pub fn visit_item(&mut self, item: &hir::Item,
b039eaaf 358 renamed: Option<ast::Name>, om: &mut Module) {
1a4d82fc 359 debug!("Visiting item {:?}", item);
b039eaaf 360 let name = renamed.unwrap_or(item.name);
1a4d82fc 361 match item.node {
476ff2be
SL
362 hir::ItemForeignMod(ref fm) => {
363 // If inlining we only want to include public functions.
364 om.foreigns.push(if self.inlining {
365 hir::ForeignMod {
366 abi: fm.abi,
367 items: fm.items.iter().filter(|i| i.vis == hir::Public).cloned().collect(),
368 }
369 } else {
370 fm.clone()
371 });
372 }
373 // If we're inlining, skip private items.
374 _ if self.inlining && item.vis != hir::Public => {}
cc61c64b 375 hir::ItemGlobalAsm(..) => {}
e9174d1e 376 hir::ItemExternCrate(ref p) => {
ea8adc8c 377 let def_id = self.cx.tcx.hir.local_def_id(item.id);
85aaf69f 378 om.extern_crates.push(ExternCrate {
ea8adc8c 379 cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
9e0c209e 380 .unwrap_or(LOCAL_CRATE),
3b2f2976 381 name,
54a0048b
SL
382 path: p.map(|x|x.to_string()),
383 vis: item.vis.clone(),
85aaf69f
SL
384 attrs: item.attrs.clone(),
385 whence: item.span,
386 })
387 }
476ff2be
SL
388 hir::ItemUse(_, hir::UseKind::ListStem) => {}
389 hir::ItemUse(ref path, kind) => {
390 let is_glob = kind == hir::UseKind::Glob;
391
392 // If there was a private module in the current path then don't bother inlining
393 // anything as it will probably be stripped anyway.
394 if item.vis == hir::Public && self.inside_public_path {
85aaf69f
SL
395 let please_inline = item.attrs.iter().any(|item| {
396 match item.meta_item_list() {
cc61c64b 397 Some(ref list) if item.check_name("doc") => {
9e0c209e 398 list.iter().any(|i| i.check_name("inline"))
85aaf69f 399 }
54a0048b 400 _ => false,
85aaf69f
SL
401 }
402 });
476ff2be
SL
403 let name = if is_glob { None } else { Some(name) };
404 if self.maybe_inline_local(item.id,
405 path.def,
406 name,
407 is_glob,
408 om,
409 please_inline) {
410 return;
85aaf69f 411 }
476ff2be
SL
412 }
413
85aaf69f 414 om.imports.push(Import {
3b2f2976 415 name,
85aaf69f 416 id: item.id,
54a0048b 417 vis: item.vis.clone(),
85aaf69f 418 attrs: item.attrs.clone(),
476ff2be
SL
419 path: (**path).clone(),
420 glob: is_glob,
85aaf69f
SL
421 whence: item.span,
422 });
423 }
e9174d1e 424 hir::ItemMod(ref m) => {
1a4d82fc
JJ
425 om.mods.push(self.visit_mod_contents(item.span,
426 item.attrs.clone(),
54a0048b 427 item.vis.clone(),
1a4d82fc
JJ
428 item.id,
429 m,
430 Some(name)));
431 },
e9174d1e 432 hir::ItemEnum(ref ed, ref gen) =>
1a4d82fc 433 om.enums.push(self.visit_enum_def(item, name, ed, gen)),
e9174d1e 434 hir::ItemStruct(ref sd, ref gen) =>
b039eaaf 435 om.structs.push(self.visit_variant_data(item, name, sd, gen)),
9e0c209e
SL
436 hir::ItemUnion(ref sd, ref gen) =>
437 om.unions.push(self.visit_union_data(item, name, sd, gen)),
32a655c1 438 hir::ItemFn(ref fd, ref unsafety, constness, ref abi, ref gen, body) =>
62682a34 439 om.fns.push(self.visit_fn(item, name, &**fd, unsafety,
32a655c1 440 constness, abi, gen, body)),
e9174d1e 441 hir::ItemTy(ref ty, ref gen) => {
1a4d82fc
JJ
442 let t = Typedef {
443 ty: ty.clone(),
444 gen: gen.clone(),
3b2f2976 445 name,
1a4d82fc
JJ
446 id: item.id,
447 attrs: item.attrs.clone(),
448 whence: item.span,
54a0048b 449 vis: item.vis.clone(),
1a4d82fc 450 stab: self.stability(item.id),
9cc50fc6 451 depr: self.deprecation(item.id),
1a4d82fc
JJ
452 };
453 om.typedefs.push(t);
454 },
e9174d1e 455 hir::ItemStatic(ref ty, ref mut_, ref exp) => {
1a4d82fc
JJ
456 let s = Static {
457 type_: ty.clone(),
458 mutability: mut_.clone(),
459 expr: exp.clone(),
460 id: item.id,
3b2f2976 461 name,
1a4d82fc
JJ
462 attrs: item.attrs.clone(),
463 whence: item.span,
54a0048b 464 vis: item.vis.clone(),
1a4d82fc 465 stab: self.stability(item.id),
9cc50fc6 466 depr: self.deprecation(item.id),
1a4d82fc
JJ
467 };
468 om.statics.push(s);
469 },
e9174d1e 470 hir::ItemConst(ref ty, ref exp) => {
1a4d82fc
JJ
471 let s = Constant {
472 type_: ty.clone(),
473 expr: exp.clone(),
474 id: item.id,
3b2f2976 475 name,
1a4d82fc
JJ
476 attrs: item.attrs.clone(),
477 whence: item.span,
54a0048b 478 vis: item.vis.clone(),
1a4d82fc 479 stab: self.stability(item.id),
9cc50fc6 480 depr: self.deprecation(item.id),
1a4d82fc
JJ
481 };
482 om.constants.push(s);
483 },
abe05a73 484 hir::ItemTrait(_, unsafety, ref gen, ref b, ref item_ids) => {
32a655c1
SL
485 let items = item_ids.iter()
486 .map(|ti| self.cx.tcx.hir.trait_item(ti.id).clone())
487 .collect();
1a4d82fc 488 let t = Trait {
3b2f2976
XL
489 unsafety,
490 name,
491 items,
1a4d82fc 492 generics: gen.clone(),
85aaf69f 493 bounds: b.iter().cloned().collect(),
1a4d82fc
JJ
494 id: item.id,
495 attrs: item.attrs.clone(),
496 whence: item.span,
54a0048b 497 vis: item.vis.clone(),
1a4d82fc 498 stab: self.stability(item.id),
9cc50fc6 499 depr: self.deprecation(item.id),
1a4d82fc
JJ
500 };
501 om.traits.push(t);
502 },
476ff2be 503
7cac9316
XL
504 hir::ItemImpl(unsafety,
505 polarity,
506 defaultness,
507 ref gen,
508 ref tr,
509 ref ty,
510 ref item_ids) => {
476ff2be
SL
511 // Don't duplicate impls when inlining, we'll pick them up
512 // regardless of where they're located.
513 if !self.inlining {
514 let items = item_ids.iter()
32a655c1 515 .map(|ii| self.cx.tcx.hir.impl_item(ii.id).clone())
476ff2be
SL
516 .collect();
517 let i = Impl {
3b2f2976
XL
518 unsafety,
519 polarity,
520 defaultness,
476ff2be
SL
521 generics: gen.clone(),
522 trait_: tr.clone(),
523 for_: ty.clone(),
3b2f2976 524 items,
476ff2be
SL
525 attrs: item.attrs.clone(),
526 id: item.id,
527 whence: item.span,
528 vis: item.vis.clone(),
529 stab: self.stability(item.id),
530 depr: self.deprecation(item.id),
531 };
9346a6ac
AL
532 om.impls.push(i);
533 }
1a4d82fc 534 },
abe05a73 535 hir::ItemAutoImpl(unsafety, ref trait_ref) => {
476ff2be
SL
536 // See comment above about ItemImpl.
537 if !self.inlining {
abe05a73 538 let i = AutoImpl {
3b2f2976 539 unsafety,
476ff2be
SL
540 trait_: trait_ref.clone(),
541 id: item.id,
542 attrs: item.attrs.clone(),
543 whence: item.span,
544 };
9346a6ac
AL
545 om.def_traits.push(i);
546 }
c34b1796 547 }
1a4d82fc
JJ
548 }
549 }
550
551 // convert each exported_macro into a doc item
32a655c1 552 fn visit_local_macro(&self, def: &hir::MacroDef) -> Macro {
8bb4bdeb 553 let tts = def.body.trees().collect::<Vec<_>>();
92a42be0 554 // Extract the spans of all matchers. They represent the "interface" of the macro.
8bb4bdeb 555 let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
92a42be0 556
1a4d82fc 557 Macro {
32a655c1 558 def_id: self.cx.tcx.hir.local_def_id(def.id),
1a4d82fc 559 attrs: def.attrs.clone(),
b039eaaf 560 name: def.name,
1a4d82fc 561 whence: def.span,
3b2f2976 562 matchers,
1a4d82fc 563 stab: self.stability(def.id),
9cc50fc6 564 depr: self.deprecation(def.id),
32a655c1 565 imported_from: None,
1a4d82fc
JJ
566 }
567 }
568}