]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/passes/propagate_doc_cfg.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / librustdoc / passes / propagate_doc_cfg.rs
1 // Copyright 2017 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 std::sync::Arc;
12
13 use clean::{Crate, Item};
14 use clean::cfg::Cfg;
15 use fold::DocFolder;
16
17 pub fn propagate_doc_cfg(cr: Crate) -> Crate {
18 CfgPropagator { parent_cfg: None }.fold_crate(cr)
19 }
20
21 struct CfgPropagator {
22 parent_cfg: Option<Arc<Cfg>>,
23 }
24
25 impl DocFolder for CfgPropagator {
26 fn fold_item(&mut self, mut item: Item) -> Option<Item> {
27 let old_parent_cfg = self.parent_cfg.clone();
28
29 let new_cfg = match (self.parent_cfg.take(), item.attrs.cfg.take()) {
30 (None, None) => None,
31 (Some(rc), None) | (None, Some(rc)) => Some(rc),
32 (Some(mut a), Some(b)) => {
33 let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc));
34 *Arc::make_mut(&mut a) &= b;
35 Some(a)
36 }
37 };
38 self.parent_cfg = new_cfg.clone();
39 item.attrs.cfg = new_cfg;
40
41 let result = self.fold_item_recur(item);
42 self.parent_cfg = old_parent_cfg;
43
44 result
45 }
46 }