1 //! Strip all doc(hidden) items from the output.
2 use rustc_span::symbol::sym;
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};
11 pub(crate) const STRIP_HIDDEN: Pass = Pass {
14 description: "strips all `#[doc(hidden)]` items from the output",
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();
21 // strip all #[doc(hidden)] items
23 let mut stripper = Stripper { retained: &mut retained, update_retained: true };
24 stripper.fold_crate(krate)
27 // strip all impls referencing stripped items
28 let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
29 stripper.fold_crate(krate)
33 retained: &'a mut ItemIdSet,
34 update_retained: bool,
37 impl<'a> DocFolder for Stripper<'a> {
38 fn fold_item(&mut self, i: Item) -> Option<Item> {
39 if i.attrs.lists(sym::doc).has_word(sym::hidden) {
40 debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
41 // Use a dedicated hidden item for fields, variants, and modules.
42 // We need to keep private fields and variants, so that the docs
43 // can show a placeholder "// some variants omitted". We need to keep
44 // private modules, because they can contain impl blocks, and impl
45 // block privacy is inherited from the type and trait, not from the
46 // module it's defined in. Both of these are marked "stripped," and
47 // not included in the final docs, but since they still have an effect
48 // on the final doc, cannot be completely removed from the Clean IR.
50 clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
51 // We need to recurse into stripped modules to
52 // strip things like impl methods but when doing so
53 // we must not add any items to the `retained` set.
54 let old = mem::replace(&mut self.update_retained, false);
55 let ret = strip_item(self.fold_item_recur(i));
56 self.update_retained = old;
62 if self.update_retained {
63 self.retained.insert(i.item_id);
66 Some(self.fold_item_recur(i))