Skip to content

Commit 49a5ec0

Browse files
Add new unstable --generate-macro-expansion rustdoc command line flag
1 parent 9a31036 commit 49a5ec0

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

src/librustdoc/config.rs

+11
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ pub(crate) struct RenderOptions {
307307
pub(crate) parts_out_dir: Option<PathToParts>,
308308
/// disable minification of CSS/JS
309309
pub(crate) disable_minification: bool,
310+
/// If `true`, HTML source pages will generate the possibility to expand macros.
311+
pub(crate) generate_macro_expansion: bool,
310312
}
311313

312314
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -775,6 +777,7 @@ impl Options {
775777
let show_type_layout = matches.opt_present("show-type-layout");
776778
let nocapture = matches.opt_present("nocapture");
777779
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
780+
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
778781
let extern_html_root_takes_precedence =
779782
matches.opt_present("extern-html-root-takes-precedence");
780783
let html_no_source = matches.opt_present("html-no-source");
@@ -790,6 +793,13 @@ impl Options {
790793
.with_note("`--generate-link-to-definition` option will be ignored")
791794
.emit();
792795
}
796+
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
797+
dcx.struct_warn(
798+
"`--generate-macro-expansion` option can only be used with HTML output format",
799+
)
800+
.with_note("`--generate-macro-expansion` option will be ignored")
801+
.emit();
802+
}
793803

794804
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
795805
let with_examples = matches.opt_strs("with-examples");
@@ -870,6 +880,7 @@ impl Options {
870880
unstable_features,
871881
emit,
872882
generate_link_to_definition,
883+
generate_macro_expansion,
873884
call_locations,
874885
no_emit_shared: false,
875886
html_no_source,

src/librustdoc/html/highlight.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn get_expansion<'a, W: Write>(
274274
&& let Some(expanded_code) = expanded_codes.iter().find(|code| code.start_line == line)
275275
{
276276
let (closing, reopening) = if let Some(current_class) = token_handler.current_class
277-
&& let class = current_class.as_html()
277+
&& let class = current_class.as_html()
278278
&& !class.is_empty()
279279
{
280280
("</span>", format!("<span class=\"{class}\">"))
@@ -314,11 +314,15 @@ fn end_expansion<W: Write>(token_handler: &mut TokenHandler<'_, '_, W>, level: u
314314
}
315315
let mut out = String::new();
316316
let mut end = String::new();
317-
for (tag, class) in token_handler.closing_tags.iter().skip(token_handler.closing_tags.len() - level) {
317+
for (tag, class) in
318+
token_handler.closing_tags.iter().skip(token_handler.closing_tags.len() - level)
319+
{
318320
out.push_str(tag);
319321
end.push_str(&format!("<span class=\"{}\">", class.as_html()));
320322
}
321-
token_handler.pending_elems.push((Cow::Owned(format!("</span></span>{out}{end}")), Some(Class::Expansion)));
323+
token_handler
324+
.pending_elems
325+
.push((Cow::Owned(format!("</span></span>{out}{end}")), Some(Class::Expansion)));
322326
}
323327

324328
#[derive(Clone, Copy)]
@@ -392,8 +396,7 @@ pub(super) fn write_code(
392396
.href_context
393397
.as_ref()
394398
.and_then(|c| c.context.shared.expanded_codes.get(&c.file_span.lo()));
395-
let mut current_expansion =
396-
get_expansion(&mut token_handler, expanded_codes, line);
399+
let mut current_expansion = get_expansion(&mut token_handler, expanded_codes, line);
397400
token_handler.write_pending_elems(None);
398401
let mut level = 0;
399402

@@ -433,8 +436,7 @@ pub(super) fn write_code(
433436
.push((Cow::Borrowed(text), Some(Class::Backline(line))));
434437
}
435438
if current_expansion.is_none() {
436-
current_expansion =
437-
get_expansion(&mut token_handler, expanded_codes, line);
439+
current_expansion = get_expansion(&mut token_handler, expanded_codes, line);
438440
}
439441
} else {
440442
token_handler.pending_elems.push((Cow::Borrowed(text), class));
@@ -879,7 +881,9 @@ impl<'src> Classifier<'src> {
879881
) {
880882
let lookahead = self.peek();
881883
let file_span = self.file_span;
882-
let no_highlight = |sink: &mut dyn FnMut(_, _)| sink(new_span(before, text, file_span), Highlight::Token { text, class: None });
884+
let no_highlight = |sink: &mut dyn FnMut(_, _)| {
885+
sink(new_span(before, text, file_span), Highlight::Token { text, class: None })
886+
};
883887
let whitespace = |sink: &mut dyn FnMut(_, _)| {
884888
let mut start = 0u32;
885889
for part in text.split('\n').intersperse("\n").filter(|s| !s.is_empty()) {
@@ -1044,7 +1048,10 @@ impl<'src> Classifier<'src> {
10441048
TokenKind::CloseBracket => {
10451049
if self.in_attribute {
10461050
self.in_attribute = false;
1047-
sink(new_span(before, text, file_span), Highlight::Token { text: "]", class: None });
1051+
sink(
1052+
new_span(before, text, file_span),
1053+
Highlight::Token { text: "]", class: None },
1054+
);
10481055
sink(DUMMY_SP, Highlight::ExitSpan);
10491056
return;
10501057
}

src/librustdoc/html/render/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
484484
generate_redirect_map,
485485
show_type_layout,
486486
generate_link_to_definition,
487+
generate_macro_expansion,
487488
call_locations,
488489
no_emit_shared,
489490
html_no_source,
@@ -548,6 +549,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
548549
&src_root,
549550
include_sources,
550551
generate_link_to_definition,
552+
generate_macro_expansion,
551553
);
552554

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

src/librustdoc/html/render/span_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub(crate) fn collect_spans_and_sources(
5959
src_root: &Path,
6060
include_sources: bool,
6161
generate_link_to_definition: bool,
62+
generate_macro_expansion: bool,
6263
) -> (
6364
FxIndexMap<PathBuf, String>,
6465
FxHashMap<Span, LinkFromSrc>,
@@ -68,7 +69,9 @@ pub(crate) fn collect_spans_and_sources(
6869
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
6970
let mut expanded_visitor = ExpandedCodeVisitor { tcx, expanded_codes: Vec::new() };
7071

71-
tcx.hir().walk_toplevel_module(&mut expanded_visitor);
72+
if generate_macro_expansion {
73+
tcx.hir().walk_toplevel_module(&mut expanded_visitor);
74+
}
7275
if generate_link_to_definition {
7376
tcx.hir().walk_toplevel_module(&mut visitor);
7477
}
@@ -352,10 +355,7 @@ impl<'tcx> ExpandedCodeVisitor<'tcx> {
352355
}
353356
} else {
354357
// We add a new item.
355-
self.expanded_codes.push(ExpandedCodeInfo {
356-
span: new_span,
357-
code: f(self.tcx),
358-
});
358+
self.expanded_codes.push(ExpandedCodeInfo { span: new_span, code: f(self.tcx) });
359359
}
360360
}
361361

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@ fn opts() -> Vec<RustcOptGroup> {
700700
"removed, see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information",
701701
"[rust]",
702702
),
703+
opt(
704+
Unstable,
705+
Flag,
706+
"",
707+
"generate-macro-expansion",
708+
"Add possibility to expand macros in the HTML source code pages",
709+
"",
710+
),
703711
]
704712
}
705713

0 commit comments

Comments
 (0)