1 //! The Rust AST Visitor. Extracts useful information and massages it into a form
2 //! usable for `clean`.
3 
4 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5 use rustc_hir as hir;
6 use rustc_hir::def::{DefKind, Res};
7 use rustc_hir::def_id::DefId;
8 use rustc_hir::Node;
9 use rustc_hir::CRATE_HIR_ID;
10 use rustc_middle::middle::privacy::AccessLevel;
11 use rustc_middle::ty::TyCtxt;
12 use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
13 use rustc_span::symbol::{kw, sym, Symbol};
14 use rustc_span::Span;
15 
16 use std::mem;
17 
18 use crate::clean::{self, cfg::Cfg, AttributesExt, NestedAttributesExt};
19 use crate::core;
20 
21 /// This module is used to store stuff from Rust's AST in a more convenient
22 /// manner (and with prettier names) before cleaning.
23 #[derive(Debug)]
24 crate struct Module<'hir> {
25     crate name: Symbol,
26     crate where_inner: Span,
27     crate mods: Vec<Module<'hir>>,
28     crate id: hir::HirId,
29     // (item, renamed)
30     crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
31     crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
32 }
33 
34 impl Module<'hir> {
new(name: Symbol, id: hir::HirId, where_inner: Span) -> Module<'hir>35     crate fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Module<'hir> {
36         Module { name, id, where_inner, mods: Vec::new(), items: Vec::new(), foreigns: Vec::new() }
37     }
38 
where_outer(&self, tcx: TyCtxt<'_>) -> Span39     crate fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
40         tcx.hir().span(self.id)
41     }
42 }
43 
44 // FIXME: Should this be replaced with tcx.def_path_str?
def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String>45 fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String> {
46     let crate_name = tcx.crate_name(did.krate).to_string();
47     let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| {
48         // extern blocks have an empty name
49         let s = elem.data.to_string();
50         if !s.is_empty() { Some(s) } else { None }
51     });
52     std::iter::once(crate_name).chain(relative).collect()
53 }
54 
inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool55 crate fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool {
56     while let Some(id) = tcx.hir().get_enclosing_scope(node) {
57         node = id;
58         if tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
59             return true;
60         }
61     }
62     false
63 }
64 
65 // Also, is there some reason that this doesn't use the 'visit'
66 // framework from syntax?.
67 
68 crate struct RustdocVisitor<'a, 'tcx> {
69     cx: &'a mut core::DocContext<'tcx>,
70     view_item_stack: FxHashSet<hir::HirId>,
71     inlining: bool,
72     /// Are the current module and all of its parents public?
73     inside_public_path: bool,
74     exact_paths: FxHashMap<DefId, Vec<String>>,
75 }
76 
77 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx>78     crate fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
79         // If the root is re-exported, terminate all recursion.
80         let mut stack = FxHashSet::default();
81         stack.insert(hir::CRATE_HIR_ID);
82         RustdocVisitor {
83             cx,
84             view_item_stack: stack,
85             inlining: false,
86             inside_public_path: true,
87             exact_paths: FxHashMap::default(),
88         }
89     }
90 
store_path(&mut self, did: DefId)91     fn store_path(&mut self, did: DefId) {
92         let tcx = self.cx.tcx;
93         self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did));
94     }
95 
visit(mut self) -> Module<'tcx>96     crate fn visit(mut self) -> Module<'tcx> {
97         let mut top_level_module = self.visit_mod_contents(
98             hir::CRATE_HIR_ID,
99             self.cx.tcx.hir().root_module(),
100             self.cx.tcx.crate_name(LOCAL_CRATE),
101         );
102 
103         // `#[macro_export] macro_rules!` items are reexported at the top level of the
104         // crate, regardless of where they're defined. We want to document the
105         // top level rexport of the macro, not its original definition, since
106         // the rexport defines the path that a user will actually see. Accordingly,
107         // we add the rexport as an item here, and then skip over the original
108         // definition in `visit_item()` below.
109         //
110         // We also skip `#[macro_export] macro_rules!` that have already been inserted,
111         // it can happen if within the same module a `#[macro_export] macro_rules!`
112         // is declared but also a reexport of itself producing two exports of the same
113         // macro in the same module.
114         let mut inserted = FxHashSet::default();
115         for export in self.cx.tcx.module_exports(CRATE_DEF_ID).unwrap_or(&[]) {
116             if let Res::Def(DefKind::Macro(_), def_id) = export.res {
117                 if let Some(local_def_id) = def_id.as_local() {
118                     if self.cx.tcx.has_attr(def_id, sym::macro_export) {
119                         if inserted.insert(def_id) {
120                             let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
121                             let item = self.cx.tcx.hir().expect_item(hir_id);
122                             top_level_module.items.push((item, None));
123                         }
124                     }
125                 }
126             }
127         }
128 
129         self.cx.cache.hidden_cfg = self
130             .cx
131             .tcx
132             .hir()
133             .attrs(CRATE_HIR_ID)
134             .iter()
135             .filter(|attr| attr.has_name(sym::doc))
136             .flat_map(|attr| attr.meta_item_list().into_iter().flatten())
137             .filter(|attr| attr.has_name(sym::cfg_hide))
138             .flat_map(|attr| {
139                 attr.meta_item_list()
140                     .unwrap_or(&[])
141                     .iter()
142                     .filter_map(|attr| {
143                         Cfg::parse(attr.meta_item()?)
144                             .map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
145                             .ok()
146                     })
147                     .collect::<Vec<_>>()
148             })
149             .collect();
150 
151         self.cx.cache.exact_paths = self.exact_paths;
152         top_level_module
153     }
154 
visit_mod_contents( &mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>, name: Symbol, ) -> Module<'tcx>155     fn visit_mod_contents(
156         &mut self,
157         id: hir::HirId,
158         m: &'tcx hir::Mod<'tcx>,
159         name: Symbol,
160     ) -> Module<'tcx> {
161         let mut om = Module::new(name, id, m.inner);
162         let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id();
163         // Keep track of if there were any private modules in the path.
164         let orig_inside_public_path = self.inside_public_path;
165         self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public();
166         for &i in m.item_ids {
167             let item = self.cx.tcx.hir().item(i);
168             self.visit_item(item, None, &mut om);
169         }
170         self.inside_public_path = orig_inside_public_path;
171         om
172     }
173 
174     /// Tries to resolve the target of a `pub use` statement and inlines the
175     /// target if it is defined locally and would not be documented otherwise,
176     /// or when it is specifically requested with `please_inline`.
177     /// (the latter is the case when the import is marked `doc(inline)`)
178     ///
179     /// Cross-crate inlining occurs later on during crate cleaning
180     /// and follows different rules.
181     ///
182     /// Returns `true` if the target has been inlined.
maybe_inline_local( &mut self, id: hir::HirId, res: Res, renamed: Option<Symbol>, glob: bool, om: &mut Module<'tcx>, please_inline: bool, ) -> bool183     fn maybe_inline_local(
184         &mut self,
185         id: hir::HirId,
186         res: Res,
187         renamed: Option<Symbol>,
188         glob: bool,
189         om: &mut Module<'tcx>,
190         please_inline: bool,
191     ) -> bool {
192         debug!("maybe_inline_local res: {:?}", res);
193 
194         let tcx = self.cx.tcx;
195         let res_did = if let Some(did) = res.opt_def_id() {
196             did
197         } else {
198             return false;
199         };
200 
201         let use_attrs = tcx.hir().attrs(id);
202         // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
203         let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
204             || use_attrs.lists(sym::doc).has_word(sym::hidden);
205 
206         // For cross-crate impl inlining we need to know whether items are
207         // reachable in documentation -- a previously unreachable item can be
208         // made reachable by cross-crate inlining which we're checking here.
209         // (this is done here because we need to know this upfront).
210         if !res_did.is_local() && !is_no_inline {
211             let attrs = clean::inline::load_attrs(self.cx, res_did);
212             let self_is_hidden = attrs.lists(sym::doc).has_word(sym::hidden);
213             if !self_is_hidden {
214                 if let Res::Def(kind, did) = res {
215                     if kind == DefKind::Mod {
216                         crate::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did)
217                     } else {
218                         // All items need to be handled here in case someone wishes to link
219                         // to them with intra-doc links
220                         self.cx.cache.access_levels.map.insert(did, AccessLevel::Public);
221                     }
222                 }
223             }
224             return false;
225         }
226 
227         let res_hir_id = match res_did.as_local() {
228             Some(n) => tcx.hir().local_def_id_to_hir_id(n),
229             None => return false,
230         };
231 
232         let is_private = !self.cx.cache.access_levels.is_public(res_did);
233         let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);
234 
235         // Only inline if requested or if the item would otherwise be stripped.
236         if (!please_inline && !is_private && !is_hidden) || is_no_inline {
237             return false;
238         }
239 
240         if !self.view_item_stack.insert(res_hir_id) {
241             return false;
242         }
243 
244         let ret = match tcx.hir().get(res_hir_id) {
245             Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => {
246                 let prev = mem::replace(&mut self.inlining, true);
247                 for &i in m.item_ids {
248                     let i = self.cx.tcx.hir().item(i);
249                     self.visit_item(i, None, om);
250                 }
251                 self.inlining = prev;
252                 true
253             }
254             Node::Item(it) if !glob => {
255                 let prev = mem::replace(&mut self.inlining, true);
256                 self.visit_item(it, renamed, om);
257                 self.inlining = prev;
258                 true
259             }
260             Node::ForeignItem(it) if !glob => {
261                 let prev = mem::replace(&mut self.inlining, true);
262                 self.visit_foreign_item(it, renamed, om);
263                 self.inlining = prev;
264                 true
265             }
266             _ => false,
267         };
268         self.view_item_stack.remove(&res_hir_id);
269         ret
270     }
271 
visit_item( &mut self, item: &'tcx hir::Item<'_>, renamed: Option<Symbol>, om: &mut Module<'tcx>, )272     fn visit_item(
273         &mut self,
274         item: &'tcx hir::Item<'_>,
275         renamed: Option<Symbol>,
276         om: &mut Module<'tcx>,
277     ) {
278         debug!("visiting item {:?}", item);
279         let name = renamed.unwrap_or(item.ident.name);
280 
281         let def_id = item.def_id.to_def_id();
282         let is_pub = self.cx.tcx.visibility(def_id).is_public();
283 
284         if is_pub {
285             self.store_path(item.def_id.to_def_id());
286         }
287 
288         match item.kind {
289             hir::ItemKind::ForeignMod { items, .. } => {
290                 for item in items {
291                     let item = self.cx.tcx.hir().foreign_item(item.id);
292                     self.visit_foreign_item(item, None, om);
293                 }
294             }
295             // If we're inlining, skip private items.
296             _ if self.inlining && !is_pub => {}
297             hir::ItemKind::GlobalAsm(..) => {}
298             hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
299             hir::ItemKind::Use(path, kind) => {
300                 let is_glob = kind == hir::UseKind::Glob;
301 
302                 // Struct and variant constructors and proc macro stubs always show up alongside
303                 // their definitions, we've already processed them so just discard these.
304                 if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = path.res {
305                     return;
306                 }
307 
308                 let attrs = self.cx.tcx.hir().attrs(item.hir_id());
309 
310                 // If there was a private module in the current path then don't bother inlining
311                 // anything as it will probably be stripped anyway.
312                 if is_pub && self.inside_public_path {
313                     let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
314                         Some(ref list) if item.has_name(sym::doc) => {
315                             list.iter().any(|i| i.has_name(sym::inline))
316                         }
317                         _ => false,
318                     });
319                     let ident = if is_glob { None } else { Some(name) };
320                     if self.maybe_inline_local(
321                         item.hir_id(),
322                         path.res,
323                         ident,
324                         is_glob,
325                         om,
326                         please_inline,
327                     ) {
328                         return;
329                     }
330                 }
331 
332                 om.items.push((item, renamed))
333             }
334             hir::ItemKind::Macro(ref macro_def) => {
335                 // `#[macro_export] macro_rules!` items are handled seperately in `visit()`,
336                 // above, since they need to be documented at the module top level. Accordingly,
337                 // we only want to handle macros if one of three conditions holds:
338                 //
339                 // 1. This macro was defined by `macro`, and thus isn't covered by the case
340                 //    above.
341                 // 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered
342                 //    by the case above.
343                 // 3. We're inlining, since a reexport where inlining has been requested
344                 //    should be inlined even if it is also documented at the top level.
345 
346                 let def_id = item.def_id.to_def_id();
347                 let is_macro_2_0 = !macro_def.macro_rules;
348                 let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export);
349 
350                 if is_macro_2_0 || nonexported || self.inlining {
351                     om.items.push((item, renamed));
352                 }
353             }
354             hir::ItemKind::Mod(ref m) => {
355                 om.mods.push(self.visit_mod_contents(item.hir_id(), m, name));
356             }
357             hir::ItemKind::Fn(..)
358             | hir::ItemKind::ExternCrate(..)
359             | hir::ItemKind::Enum(..)
360             | hir::ItemKind::Struct(..)
361             | hir::ItemKind::Union(..)
362             | hir::ItemKind::TyAlias(..)
363             | hir::ItemKind::OpaqueTy(..)
364             | hir::ItemKind::Static(..)
365             | hir::ItemKind::Trait(..)
366             | hir::ItemKind::TraitAlias(..) => om.items.push((item, renamed)),
367             hir::ItemKind::Const(..) => {
368                 // Underscore constants do not correspond to a nameable item and
369                 // so are never useful in documentation.
370                 if name != kw::Underscore {
371                     om.items.push((item, renamed));
372                 }
373             }
374             hir::ItemKind::Impl(ref impl_) => {
375                 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
376                 // them up regardless of where they're located.
377                 if !self.inlining && impl_.of_trait.is_none() {
378                     om.items.push((item, None));
379                 }
380             }
381         }
382     }
383 
visit_foreign_item( &mut self, item: &'tcx hir::ForeignItem<'_>, renamed: Option<Symbol>, om: &mut Module<'tcx>, )384     fn visit_foreign_item(
385         &mut self,
386         item: &'tcx hir::ForeignItem<'_>,
387         renamed: Option<Symbol>,
388         om: &mut Module<'tcx>,
389     ) {
390         // If inlining we only want to include public functions.
391         if !self.inlining || self.cx.tcx.visibility(item.def_id).is_public() {
392             om.foreigns.push((item, renamed));
393         }
394     }
395 }
396