Skip to content

Commit 8464902

Browse files
Add new unstable --generate-macro-expansion rustdoc command line flag
1 parent 6bb4f14 commit 8464902

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
@@ -305,6 +305,8 @@ pub(crate) struct RenderOptions {
305305
pub(crate) parts_out_dir: Option<PathToParts>,
306306
/// disable minification of CSS/JS
307307
pub(crate) disable_minification: bool,
308+
/// If `true`, HTML source pages will generate the possibility to expand macros.
309+
pub(crate) generate_macro_expansion: bool,
308310
}
309311

310312
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -783,6 +785,7 @@ impl Options {
783785
let show_type_layout = matches.opt_present("show-type-layout");
784786
let nocapture = matches.opt_present("nocapture");
785787
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
788+
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
786789
let extern_html_root_takes_precedence =
787790
matches.opt_present("extern-html-root-takes-precedence");
788791
let html_no_source = matches.opt_present("html-no-source");
@@ -798,6 +801,13 @@ impl Options {
798801
.with_note("`--generate-link-to-definition` option will be ignored")
799802
.emit();
800803
}
804+
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
805+
dcx.struct_warn(
806+
"`--generate-macro-expansion` option can only be used with HTML output format",
807+
)
808+
.with_note("`--generate-macro-expansion` option will be ignored")
809+
.emit();
810+
}
801811

802812
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
803813
let with_examples = matches.opt_strs("with-examples");
@@ -878,6 +888,7 @@ impl Options {
878888
unstable_features,
879889
emit,
880890
generate_link_to_definition,
891+
generate_macro_expansion,
881892
call_locations,
882893
no_emit_shared: false,
883894
html_no_source,

src/librustdoc/html/highlight.rs

Lines changed: 16 additions & 9 deletions
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));
@@ -880,7 +882,9 @@ impl<'src> Classifier<'src> {
880882
) {
881883
let lookahead = self.peek();
882884
let file_span = self.file_span;
883-
let no_highlight = |sink: &mut dyn FnMut(_, _)| sink(new_span(before, text, file_span), Highlight::Token { text, class: None });
885+
let no_highlight = |sink: &mut dyn FnMut(_, _)| {
886+
sink(new_span(before, text, file_span), Highlight::Token { text, class: None })
887+
};
884888
let whitespace = |sink: &mut dyn FnMut(_, _)| {
885889
let mut start = 0u32;
886890
for part in text.split('\n').intersperse("\n").filter(|s| !s.is_empty()) {
@@ -1046,7 +1050,10 @@ impl<'src> Classifier<'src> {
10461050
TokenKind::CloseBracket => {
10471051
if self.in_attribute {
10481052
self.in_attribute = false;
1049-
sink(new_span(before, text, file_span), Highlight::Token { text: "]", class: None });
1053+
sink(
1054+
new_span(before, text, file_span),
1055+
Highlight::Token { text: "]", class: None },
1056+
);
10501057
sink(DUMMY_SP, Highlight::ExitSpan);
10511058
return;
10521059
}

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
488488
generate_redirect_map,
489489
show_type_layout,
490490
generate_link_to_definition,
491+
generate_macro_expansion,
491492
call_locations,
492493
no_emit_shared,
493494
html_no_source,
@@ -552,6 +553,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
552553
&src_root,
553554
include_sources,
554555
generate_link_to_definition,
556+
generate_macro_expansion,
555557
);
556558

557559
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
@@ -707,6 +707,14 @@ fn opts() -> Vec<RustcOptGroup> {
707707
"removed, see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information",
708708
"[rust]",
709709
),
710+
opt(
711+
Unstable,
712+
Flag,
713+
"",
714+
"generate-macro-expansion",
715+
"Add possibility to expand macros in the HTML source code pages",
716+
"",
717+
),
710718
]
711719
}
712720

0 commit comments

Comments
 (0)