]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/passes/propagate_doc_cfg.rs
New upstream version 1.30.0~beta.7+dfsg1
[rustc.git] / src / librustdoc / passes / propagate_doc_cfg.rs
CommitLineData
3b2f2976
XL
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
94b46f34 11use std::sync::Arc;
3b2f2976
XL
12
13use clean::{Crate, Item};
14use clean::cfg::Cfg;
15use fold::DocFolder;
b7449926
XL
16use passes::Pass;
17
18pub const PROPAGATE_DOC_CFG: Pass =
19 Pass::late("propagate-doc-cfg", propagate_doc_cfg,
20 "propagates `#[doc(cfg(...))]` to child items");
3b2f2976 21
8faf50e0 22pub fn propagate_doc_cfg(cr: Crate) -> Crate {
3b2f2976
XL
23 CfgPropagator { parent_cfg: None }.fold_crate(cr)
24}
25
26struct CfgPropagator {
94b46f34 27 parent_cfg: Option<Arc<Cfg>>,
3b2f2976
XL
28}
29
30impl DocFolder for CfgPropagator {
31 fn fold_item(&mut self, mut item: Item) -> Option<Item> {
32 let old_parent_cfg = self.parent_cfg.clone();
33
34 let new_cfg = match (self.parent_cfg.take(), item.attrs.cfg.take()) {
35 (None, None) => None,
36 (Some(rc), None) | (None, Some(rc)) => Some(rc),
37 (Some(mut a), Some(b)) => {
94b46f34
XL
38 let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc));
39 *Arc::make_mut(&mut a) &= b;
3b2f2976
XL
40 Some(a)
41 }
42 };
43 self.parent_cfg = new_cfg.clone();
44 item.attrs.cfg = new_cfg;
45
46 let result = self.fold_item_recur(item);
47 self.parent_cfg = old_parent_cfg;
48
49 result
50 }
51}