Skip to content

Commit 7ee81dd

Browse files
Add new unstable --generate-macro-expansion rustdoc command line flag
1 parent d00d3d0 commit 7ee81dd

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)]
@@ -786,6 +788,7 @@ impl Options {
786788
let show_type_layout = matches.opt_present("show-type-layout");
787789
let nocapture = matches.opt_present("nocapture");
788790
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
791+
let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
789792
let extern_html_root_takes_precedence =
790793
matches.opt_present("extern-html-root-takes-precedence");
791794
let html_no_source = matches.opt_present("html-no-source");
@@ -801,6 +804,13 @@ impl Options {
801804
.with_note("`--generate-link-to-definition` option will be ignored")
802805
.emit();
803806
}
807+
if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
808+
dcx.struct_warn(
809+
"`--generate-macro-expansion` option can only be used with HTML output format",
810+
)
811+
.with_note("`--generate-macro-expansion` option will be ignored")
812+
.emit();
813+
}
804814

805815
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
806816
let with_examples = matches.opt_strs("with-examples");
@@ -882,6 +892,7 @@ impl Options {
882892
unstable_features,
883893
emit,
884894
generate_link_to_definition,
895+
generate_macro_expansion,
885896
call_locations,
886897
no_emit_shared: false,
887898
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
@@ -479,6 +479,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
479479
generate_redirect_map,
480480
show_type_layout,
481481
generate_link_to_definition,
482+
generate_macro_expansion,
482483
call_locations,
483484
no_emit_shared,
484485
html_no_source,
@@ -543,6 +544,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
543544
&src_root,
544545
include_sources,
545546
generate_link_to_definition,
547+
generate_macro_expansion,
546548
);
547549

548550
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
@@ -704,6 +704,14 @@ fn opts() -> Vec<RustcOptGroup> {
704704
"removed, see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information",
705705
"[rust]",
706706
),
707+
opt(
708+
Unstable,
709+
Flag,
710+
"",
711+
"generate-macro-expansion",
712+
"Add possibility to expand macros in the HTML source code pages",
713+
"",
714+
),
707715
]
708716
}
709717

0 commit comments

Comments
 (0)