Skip to main content

rustc_passes/
fake_doc_items.rs

1//! Collecting fake doc items. This query is used by rustdoc to document attributes, keywords and
2//! primitives.
3
4use rustc_attr_ir::{DocAttribute, find_attr};
5use rustc_middle::query::{LocalCrate, Providers};
6use rustc_middle::ty::TyCtxt;
7use rustc_span::def_id::{DefId, LOCAL_CRATE};
8use rustc_span::sym;
9
10/// Traverse and collect the fake doc items in the current crate
11fn fake_doc_items(tcx: TyCtxt<'_>, _: LocalCrate) -> Vec<DefId> {
12    let mut fake_doc_items = Vec::new();
13
14    // Optimization: can this crate even define fake doc items?
15    let features = tcx.features().enabled_features();
16    if features.contains(&sym::rustc_attrs) || features.contains(&sym::rustdoc_internals) {
17        // Collect fake doc items in this crate.
18        for id in tcx.hir_root_module().item_ids {
19            let id = id.hir_id();
20            if {
        {
            'done:
                {
                for i in ::rustc_attr_ir::HasAttrs::get_attrs(id, &tcx) {
                    #[allow(unused_imports)]
                    use ::rustc_attr_ir::AttributeKind::*;
                    let i: &::rustc_attr_ir::Attribute = i;
                    match i {
                        ::rustc_attr_ir::Attribute::Parsed(RustcDocPrimitive(..) |
                            Doc(DocAttribute { keyword: Some(..), .. }) |
                            Doc(DocAttribute { attribute: Some(..), .. })) => {
                            break 'done Some(());
                        }
                        ::rustc_attr_ir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(
21                tcx,
22                id,
23                RustcDocPrimitive(..)
24                    | Doc(DocAttribute { keyword: Some(..), .. })
25                    | Doc(DocAttribute { attribute: Some(..), .. })
26            ) {
27                fake_doc_items.push(id.expect_owner().to_def_id());
28            }
29        }
30    }
31
32    fake_doc_items
33}
34
35/// Traverse and collect all the fake doc items in all crates.
36fn all_fake_doc_items(tcx: TyCtxt<'_>, (): ()) -> Vec<DefId> {
37    let mut fake_doc_items = Vec::new();
38
39    // Collect fake doc items in visible crates.
40    for cnum in tcx
41        .crates(())
42        .iter()
43        .copied()
44        .filter(|cnum| tcx.is_user_visible_dep(*cnum))
45        .chain(std::iter::once(LOCAL_CRATE))
46    {
47        fake_doc_items.extend_from_slice(tcx.fake_doc_items(cnum))
48    }
49
50    fake_doc_items
51}
52
53pub(crate) fn provide(providers: &mut Providers) {
54    providers.fake_doc_items = fake_doc_items;
55    providers.all_fake_doc_items = all_fake_doc_items;
56}