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.
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.
13 use clean
::{Crate, Item}
;
17 pub fn propagate_doc_cfg(cr
: Crate
) -> Crate
{
18 CfgPropagator { parent_cfg: None }
.fold_crate(cr
)
21 struct CfgPropagator
{
22 parent_cfg
: Option
<Arc
<Cfg
>>,
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();
29 let new_cfg
= match (self.parent_cfg
.take(), item
.attrs
.cfg
.take()) {
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
;
38 self.parent_cfg
= new_cfg
.clone();
39 item
.attrs
.cfg
= new_cfg
;
41 let result
= self.fold_item_recur(item
);
42 self.parent_cfg
= old_parent_cfg
;