]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/strip_hidden.rs
68c1231fc6f7c58e7266ee3e3c52708fa36e7b68
[rust.git] / src / librustdoc / passes / strip_hidden.rs
1 // Copyright 2012-2014 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 use rustc::util::nodemap::DefIdSet;
12 use std::mem;
13
14 use clean::{self, AttributesExt, NestedAttributesExt};
15 use clean::Item;
16 use plugins;
17 use fold;
18 use fold::DocFolder;
19 use fold::FoldItem::Strip;
20 use passes::ImplStripper;
21
22 /// Strip items marked `#[doc(hidden)]`
23 pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
24     let mut retained = DefIdSet();
25
26     // strip all #[doc(hidden)] items
27     let krate = {
28         let mut stripper = Stripper{ retained: &mut retained, update_retained: true };
29         stripper.fold_crate(krate)
30     };
31
32     // strip all impls referencing stripped items
33     let mut stripper = ImplStripper { retained: &retained };
34     stripper.fold_crate(krate)
35 }
36
37 struct Stripper<'a> {
38     retained: &'a mut DefIdSet,
39     update_retained: bool,
40 }
41
42 impl<'a> fold::DocFolder for Stripper<'a> {
43     fn fold_item(&mut self, i: Item) -> Option<Item> {
44         if i.attrs.lists("doc").has_word("hidden") {
45             debug!("found one in strip_hidden; removing");
46             // use a dedicated hidden item for given item type if any
47             match i.inner {
48                 clean::StructFieldItem(..) | clean::ModuleItem(..) => {
49                     // We need to recurse into stripped modules to
50                     // strip things like impl methods but when doing so
51                     // we must not add any items to the `retained` set.
52                     let old = mem::replace(&mut self.update_retained, false);
53                     let ret = Strip(self.fold_item_recur(i).unwrap()).fold();
54                     self.update_retained = old;
55                     return ret;
56                 }
57                 _ => return None,
58             }
59         } else {
60             if self.update_retained {
61                 self.retained.insert(i.def_id);
62             }
63         }
64         self.fold_item_recur(i)
65     }
66 }