]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/json/import_finder.rs
Port pgo.sh to Python
[rust.git] / src / librustdoc / json / import_finder.rs
1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_hir::def_id::DefId;
3
4 use crate::{
5     clean::{self, Import, ImportSource, Item},
6     fold::DocFolder,
7 };
8
9 /// Get the id's of all items that are `pub use`d in the crate.
10 ///
11 /// We need this to know if a stripped module is `pub use mod::*`, to decide
12 /// if it needs to be kept in the index, despite being stripped.
13 ///
14 /// See [#100973](https://github.com/rust-lang/rust/issues/100973) and
15 /// [#101103](https://github.com/rust-lang/rust/issues/101103) for times when
16 /// this information is needed.
17 pub(crate) fn get_imports(krate: clean::Crate) -> (clean::Crate, FxHashSet<DefId>) {
18     let mut finder = ImportFinder { imported: FxHashSet::default() };
19     let krate = finder.fold_crate(krate);
20     (krate, finder.imported)
21 }
22
23 struct ImportFinder {
24     imported: FxHashSet<DefId>,
25 }
26
27 impl DocFolder for ImportFinder {
28     fn fold_item(&mut self, i: Item) -> Option<Item> {
29         match *i.kind {
30             clean::ImportItem(Import { source: ImportSource { did: Some(did), .. }, .. }) => {
31                 self.imported.insert(did);
32                 Some(i)
33             }
34
35             _ => Some(self.fold_item_recur(i)),
36         }
37     }
38 }