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