Skip to content

Commit a79a2ff

Browse files
Add new unstable --generate-macro-expansion rustdoc command line flag
1 parent d7907fa commit a79a2ff

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

src/librustdoc/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ pub(crate) struct RenderOptions {
302302
pub(crate) parts_out_dir: Option<PathToParts>,
303303
/// disable minification of CSS/JS
304304
pub(crate) disable_minification: bool,
305+
/// If `true`, HTML source pages will generate the possibility to expand macros.
306+
pub(crate) generate_macro_expansion: bool,
305307
}
306308

307309
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -778,6 +780,7 @@ impl Options {
778780
let show_type_layout = matches.opt_present("show-type-layout");
779781
let nocapture = matches.opt_present("nocapture");
780782
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
783+
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
781784
let extern_html_root_takes_precedence =
782785
matches.opt_present("extern-html-root-takes-precedence");
783786
let html_no_source = matches.opt_present("html-no-source");
@@ -793,6 +796,13 @@ impl Options {
793796
.with_note("`--generate-link-to-definition` option will be ignored")
794797
.emit();
795798
}
799+
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
800+
dcx.struct_warn(
801+
"`--generate-macro-expansion` option can only be used with HTML output format",
802+
)
803+
.with_note("`--generate-macro-expansion` option will be ignored")
804+
.emit();
805+
}
796806

797807
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
798808
let with_examples = matches.opt_strs("with-examples");
@@ -872,6 +882,7 @@ impl Options {
872882
unstable_features,
873883
emit,
874884
generate_link_to_definition,
885+
generate_macro_expansion,
875886
call_locations,
876887
no_emit_shared: false,
877888
html_no_source,

src/librustdoc/html/highlight.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fn get_expansion<'a, W: Write>(
281281
&& let Some(expanded_code) = expanded_codes.iter().find(|code| code.start_line == line)
282282
{
283283
let (closing, reopening) = if let Some(current_class) = token_handler.current_class
284-
&& let class = current_class.as_html()
284+
&& let class = current_class.as_html()
285285
&& !class.is_empty()
286286
{
287287
("</span>", format!("<span class=\"{class}\">"))
@@ -321,11 +321,15 @@ fn end_expansion<W: Write>(token_handler: &mut TokenHandler<'_, '_, W>, level: u
321321
}
322322
let mut out = String::new();
323323
let mut end = String::new();
324-
for (tag, class) in token_handler.closing_tags.iter().skip(token_handler.closing_tags.len() - level) {
324+
for (tag, class) in
325+
token_handler.closing_tags.iter().skip(token_handler.closing_tags.len() - level)
326+
{
325327
out.push_str(tag);
326328
end.push_str(&format!("<span class=\"{}\">", class.as_html()));
327329
}
328-
token_handler.pending_elems.push((Cow::Owned(format!("</span></span>{out}{end}")), Some(Class::Expansion)));
330+
token_handler
331+
.pending_elems
332+
.push((Cow::Owned(format!("</span></span>{out}{end}")), Some(Class::Expansion)));
329333
}
330334

331335
#[derive(Clone, Copy)]
@@ -399,8 +403,7 @@ pub(super) fn write_code(
399403
.href_context
400404
.as_ref()
401405
.and_then(|c| c.context.shared.expanded_codes.get(&c.file_span.lo()));
402-
let mut current_expansion =
403-
get_expansion(&mut token_handler, expanded_codes, line);
406+
let mut current_expansion = get_expansion(&mut token_handler, expanded_codes, line);
404407
token_handler.write_pending_elems(None);
405408
let mut level = 0;
406409

@@ -440,8 +443,7 @@ pub(super) fn write_code(
440443
.push((Cow::Borrowed(text), Some(Class::Backline(line))));
441444
}
442445
if current_expansion.is_none() {
443-
current_expansion =
444-
get_expansion(&mut token_handler, expanded_codes, line);
446+
current_expansion = get_expansion(&mut token_handler, expanded_codes, line);
445447
}
446448
} else {
447449
token_handler.pending_elems.push((Cow::Borrowed(text), class));
@@ -887,7 +889,9 @@ impl<'src> Classifier<'src> {
887889
) {
888890
let lookahead = self.peek();
889891
let file_span = self.file_span;
890-
let no_highlight = |sink: &mut dyn FnMut(_, _)| sink(new_span(before, text, file_span), Highlight::Token { text, class: None });
892+
let no_highlight = |sink: &mut dyn FnMut(_, _)| {
893+
sink(new_span(before, text, file_span), Highlight::Token { text, class: None })
894+
};
891895
let whitespace = |sink: &mut dyn FnMut(_, _)| {
892896
let mut start = 0u32;
893897
for part in text.split('\n').intersperse("\n").filter(|s| !s.is_empty()) {
@@ -1053,7 +1057,10 @@ impl<'src> Classifier<'src> {
10531057
TokenKind::CloseBracket => {
10541058
if self.in_attribute {
10551059
self.in_attribute = false;
1056-
sink(new_span(before, text, file_span), Highlight::Token { text: "]", class: None });
1060+
sink(
1061+
new_span(before, text, file_span),
1062+
Highlight::Token { text: "]", class: None },
1063+
);
10571064
sink(DUMMY_SP, Highlight::ExitSpan);
10581065
return;
10591066
}

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
485485
generate_redirect_map,
486486
show_type_layout,
487487
generate_link_to_definition,
488+
generate_macro_expansion,
488489
call_locations,
489490
no_emit_shared,
490491
html_no_source,
@@ -549,6 +550,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
549550
&src_root,
550551
include_sources,
551552
generate_link_to_definition,
553+
generate_macro_expansion,
552554
);
553555

554556
let (sender, receiver) = channel();

src/librustdoc/html/render/span_map.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub(crate) fn collect_spans_and_sources(
6060
src_root: &Path,
6161
include_sources: bool,
6262
generate_link_to_definition: bool,
63+
generate_macro_expansion: bool,
6364
) -> (
6465
FxIndexMap<PathBuf, String>,
6566
FxHashMap<Span, LinkFromSrc>,
@@ -69,7 +70,9 @@ pub(crate) fn collect_spans_and_sources(
6970
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
7071
let mut expanded_visitor = ExpandedCodeVisitor { tcx, expanded_codes: Vec::new() };
7172

72-
tcx.hir_walk_toplevel_module(&mut expanded_visitor);
73+
if generate_macro_expansion {
74+
tcx.hir_walk_toplevel_module(&mut expanded_visitor);
75+
}
7376
if generate_link_to_definition {
7477
tcx.hir_walk_toplevel_module(&mut visitor);
7578
}
@@ -350,10 +353,7 @@ impl<'tcx> ExpandedCodeVisitor<'tcx> {
350353
}
351354
} else {
352355
// We add a new item.
353-
self.expanded_codes.push(ExpandedCodeInfo {
354-
span: new_span,
355-
code: f(self.tcx),
356-
});
356+
self.expanded_codes.push(ExpandedCodeInfo { span: new_span, code: f(self.tcx) });
357357
}
358358
}
359359

src/librustdoc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@ fn opts() -> Vec<RustcOptGroup> {
711711
"removed, see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information",
712712
"[rust]",
713713
),
714+
opt(
715+
Unstable,
716+
Flag,
717+
"",
718+
"generate-macro-expansion",
719+
"Add possibility to expand macros in the HTML source code pages",
720+
"",
721+
),
714722
]
715723
}
716724

0 commit comments

Comments
 (0)