]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/strip_hidden.rs
rustdoc: remove unused CSS `#main-content > table td`
[rust.git] / src / librustdoc / passes / strip_hidden.rs
1 //! Strip all doc(hidden) items from the output.
2 use rustc_span::symbol::sym;
3 use std::mem;
4
5 use crate::clean;
6 use crate::clean::{Item, ItemIdSet, NestedAttributesExt};
7 use crate::core::DocContext;
8 use crate::fold::{strip_item, DocFolder};
9 use crate::passes::{ImplStripper, Pass};
10
11 pub(crate) const STRIP_HIDDEN: Pass = Pass {
12     name: "strip-hidden",
13     run: strip_hidden,
14     description: "strips all `#[doc(hidden)]` items from the output",
15 };
16
17 /// Strip items marked `#[doc(hidden)]`
18 pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
19     let mut retained = ItemIdSet::default();
20     let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
21
22     // strip all #[doc(hidden)] items
23     let krate = {
24         let mut stripper = Stripper { retained: &mut retained, update_retained: true };
25         stripper.fold_crate(krate)
26     };
27
28     // strip all impls referencing stripped items
29     let mut stripper = ImplStripper {
30         retained: &retained,
31         cache: &cx.cache,
32         is_json_output,
33         document_private: cx.render_options.document_private,
34     };
35     stripper.fold_crate(krate)
36 }
37
38 struct Stripper<'a> {
39     retained: &'a mut ItemIdSet,
40     update_retained: bool,
41 }
42
43 impl<'a> DocFolder for Stripper<'a> {
44     fn fold_item(&mut self, i: Item) -> Option<Item> {
45         if i.attrs.lists(sym::doc).has_word(sym::hidden) {
46             debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
47             // Use a dedicated hidden item for fields, variants, and modules.
48             // We need to keep private fields and variants, so that the docs
49             // can show a placeholder "// some variants omitted". We need to keep
50             // private modules, because they can contain impl blocks, and impl
51             // block privacy is inherited from the type and trait, not from the
52             // module it's defined in. Both of these are marked "stripped," and
53             // not included in the final docs, but since they still have an effect
54             // on the final doc, cannot be completely removed from the Clean IR.
55             match *i.kind {
56                 clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
57                     // We need to recurse into stripped modules to
58                     // strip things like impl methods but when doing so
59                     // we must not add any items to the `retained` set.
60                     let old = mem::replace(&mut self.update_retained, false);
61                     let ret = strip_item(self.fold_item_recur(i));
62                     self.update_retained = old;
63                     return Some(ret);
64                 }
65                 _ => return None,
66             }
67         } else {
68             if self.update_retained {
69                 self.retained.insert(i.item_id);
70             }
71         }
72         Some(self.fold_item_recur(i))
73     }
74 }