diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2021-07-05 11:00:27 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2021-07-07 11:30:08 +0200 |
| commit | 84f259e44c97f787fc3dc5f5c596644b75f32e69 (patch) | |
| tree | 329fd717ba489941a12be9e031938e36267dee20 /src | |
| parent | c70250dfbdce688654e08cdb7a83c519f623ebda (diff) | |
| download | rust-84f259e44c97f787fc3dc5f5c596644b75f32e69.tar.gz rust-84f259e44c97f787fc3dc5f5c596644b75f32e69.zip | |
Unify macro source display
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustdoc/clean/inline.rs | 38 | ||||
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 32 | ||||
| -rw-r--r-- | src/librustdoc/clean/utils.rs | 36 |
3 files changed, 48 insertions, 58 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 1049dcfd117..f71720af7ef 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -543,39 +543,15 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St } } -fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind { - let imported_from = cx.tcx.crate_name(did.krate); - match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) { +fn build_macro(cx: &mut DocContext<'_>, def_id: DefId, name: Symbol) -> clean::ItemKind { + let imported_from = cx.tcx.crate_name(def_id.krate); + match cx.enter_resolver(|r| r.cstore().load_macro_untracked(def_id, cx.sess())) { LoadedMacro::MacroDef(item_def, _) => { if let ast::ItemKind::MacroDef(ref def) = item_def.kind { - let tts: Vec<_> = def.body.inner_tokens().into_trees().collect(); - let matchers = tts.chunks(4).map(|arm| &arm[0]); - let source = if def.macro_rules { - format!( - "macro_rules! {} {{\n{}}}", - name, - utils::render_macro_arms(matchers, ";") - ) - } else { - let vis = item_def.vis.clean(cx); - - if matchers.len() <= 1 { - format!( - "{}macro {}{} {{\n ...\n}}", - vis.to_src_with_space(cx.tcx, did), - name, - matchers.map(utils::render_macro_matcher).collect::<String>(), - ) - } else { - format!( - "{}macro {} {{\n{}}}", - vis.to_src_with_space(cx.tcx, did), - name, - utils::render_macro_arms(matchers, ";"), - ) - } - }; - clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) }) + clean::MacroItem(clean::Macro { + source: utils::display_macro_source(cx, name, def, def_id, item_def.vis), + imported_from: Some(imported_from), + }) } else { unreachable!() } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 711f2e6da41..7b359ee42c1 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2175,37 +2175,15 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) { fn clean(&self, cx: &mut DocContext<'_>) -> Item { let (item, renamed) = self; let name = renamed.unwrap_or(item.ident.name); - let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>(); - // Extract the macro's matchers. They represent the "interface" of the macro. - let matchers = tts.chunks(4).map(|arm| &arm[0]); - - let source = if item.ast.macro_rules { - format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";")) - } else { - let vis = item.vis.clean(cx); - let def_id = item.def_id.to_def_id(); - - if matchers.len() <= 1 { - format!( - "{}macro {}{} {{\n ...\n}}", - vis.to_src_with_space(cx.tcx, def_id), - name, - matchers.map(render_macro_matcher).collect::<String>(), - ) - } else { - format!( - "{}macro {} {{\n{}}}", - vis.to_src_with_space(cx.tcx, def_id), - name, - render_macro_arms(matchers, ","), - ) - } - }; + let def_id = item.def_id.to_def_id(); Item::from_hir_id_and_parts( item.hir_id(), Some(name), - MacroItem(Macro { source, imported_from: None }), + MacroItem(Macro { + source: display_macro_source(cx, name, &item.ast, def_id, &item.vis), + imported_from: None, + }), cx, ) } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index fb46b81102c..7ae602c8033 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -3,10 +3,12 @@ use crate::clean::blanket_impl::BlanketImplFinder; use crate::clean::{ inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment, PolyTrait, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding, + Visibility, }; use crate::core::DocContext; use crate::formats::item_type::ItemType; +use rustc_ast as ast; use rustc_ast::tokenstream::TokenTree; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; @@ -577,3 +579,37 @@ pub(super) fn render_macro_arms<'a>( pub(super) fn render_macro_matcher(matcher: &TokenTree) -> String { rustc_ast_pretty::pprust::tt_to_string(matcher) } + +pub(super) fn display_macro_source( + cx: &mut DocContext<'_>, + name: Symbol, + def: &ast::MacroDef, + def_id: DefId, + vis: impl Clean<Visibility>, +) -> String { + let tts: Vec<_> = def.body.inner_tokens().into_trees().collect(); + // Extract the spans of all matchers. They represent the "interface" of the macro. + let matchers = tts.chunks(4).map(|arm| &arm[0]); + + if def.macro_rules { + format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";")) + } else { + let vis = vis.clean(cx); + + if matchers.len() <= 1 { + format!( + "{}macro {}{} {{\n ...\n}}", + vis.to_src_with_space(cx.tcx, def_id), + name, + matchers.map(render_macro_matcher).collect::<String>(), + ) + } else { + format!( + "{}macro {} {{\n{}}}", + vis.to_src_with_space(cx.tcx, def_id), + name, + render_macro_arms(matchers, ","), + ) + } + } +} |
