Skip to content

Commit 1de1360

Browse files
committed
-Zunpretty: allow passing several printing modes as comma-separated list.
Modes that had a comma in them use "+" now: -Zunpretty=expanded,hygiene => -Zunpretty=expanded+hygiene You can print several modes back to back like this: -Zunpretty=expanded+hygiene,mir,hir,everybody_loops
1 parent c0a54cc commit 1de1360

File tree

13 files changed

+150
-138
lines changed

13 files changed

+150
-138
lines changed

compiler/rustc_driver/src/lib.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -333,28 +333,30 @@ fn run_compiler(
333333
let early_exit = || sess.compile_status().map(|_| None);
334334
queries.parse()?;
335335

336-
if let Some(ppm) = &sess.opts.pretty {
337-
if ppm.needs_ast_map() {
338-
queries.global_ctxt()?.peek_mut().enter(|tcx| {
339-
let expanded_crate = queries.expansion()?.take().0;
340-
pretty::print_after_hir_lowering(
341-
tcx,
342-
compiler.input(),
343-
&expanded_crate,
344-
*ppm,
336+
if let Some(ppms) = &sess.opts.pretty {
337+
for ppm in ppms {
338+
if ppm.needs_ast_map() {
339+
queries.global_ctxt()?.peek_mut().enter(|tcx| {
340+
let expanded_crate = queries.expansion()?.take().0;
341+
pretty::print_after_hir_lowering(
342+
tcx,
343+
compiler.input(),
344+
&expanded_crate,
345+
ppms,
346+
compiler.output_file().as_ref().map(|p| &**p),
347+
);
348+
Ok(())
349+
})?;
350+
} else {
351+
let krate = queries.parse()?.take();
352+
pretty::print_after_parsing(
353+
sess,
354+
&compiler.input(),
355+
&krate,
356+
ppms,
345357
compiler.output_file().as_ref().map(|p| &**p),
346358
);
347-
Ok(())
348-
})?;
349-
} else {
350-
let krate = queries.parse()?.take();
351-
pretty::print_after_parsing(
352-
sess,
353-
&compiler.input(),
354-
&krate,
355-
*ppm,
356-
compiler.output_file().as_ref().map(|p| &**p),
357-
);
359+
}
358360
}
359361
trace!("finished pretty-printing");
360362
return early_exit();

compiler/rustc_driver/src/pretty.rs

+62-56
Original file line numberDiff line numberDiff line change
@@ -386,52 +386,15 @@ pub fn print_after_parsing(
386386
sess: &Session,
387387
input: &Input,
388388
krate: &ast::Crate,
389-
ppm: PpMode,
390-
ofile: Option<&Path>,
391-
) {
392-
let (src, src_name) = get_source(input, sess);
393-
394-
let out = if let Source(s) = ppm {
395-
// Silently ignores an identified node.
396-
call_with_pp_support(&s, sess, None, move |annotation| {
397-
debug!("pretty printing source code {:?}", s);
398-
let sess = annotation.sess();
399-
let parse = &sess.parse_sess;
400-
pprust::print_crate(
401-
sess.source_map(),
402-
krate,
403-
src_name,
404-
src,
405-
annotation.pp_ann(),
406-
false,
407-
parse.edition,
408-
)
409-
})
410-
} else {
411-
unreachable!()
412-
};
413-
414-
write_or_print(&out, ofile);
415-
}
416-
417-
pub fn print_after_hir_lowering<'tcx>(
418-
tcx: TyCtxt<'tcx>,
419-
input: &Input,
420-
krate: &ast::Crate,
421-
ppm: PpMode,
389+
ppms: &Vec<PpMode>,
422390
ofile: Option<&Path>,
423391
) {
424-
if ppm.needs_analysis() {
425-
abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
426-
return;
427-
}
428-
429-
let (src, src_name) = get_source(input, tcx.sess);
392+
ppms.iter().for_each(|ppm| {
393+
let (src, src_name) = get_source(input, sess);
430394

431-
let out = match ppm {
432-
Source(s) => {
395+
let out = if let Source(s) = ppm {
433396
// Silently ignores an identified node.
434-
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
397+
call_with_pp_support(&s, sess, None, move |annotation| {
435398
debug!("pretty printing source code {:?}", s);
436399
let sess = annotation.sess();
437400
let parse = &sess.parse_sess;
@@ -441,28 +404,71 @@ pub fn print_after_hir_lowering<'tcx>(
441404
src_name,
442405
src,
443406
annotation.pp_ann(),
444-
true,
407+
false,
445408
parse.edition,
446409
)
447410
})
411+
} else {
412+
unreachable!()
413+
};
414+
415+
write_or_print(&out, ofile);
416+
});
417+
}
418+
419+
pub fn print_after_hir_lowering<'tcx>(
420+
tcx: TyCtxt<'tcx>,
421+
input: &Input,
422+
krate: &ast::Crate,
423+
ppms: &Vec<PpMode>,
424+
ofile: Option<&Path>,
425+
) {
426+
ppms.iter().for_each(|ppm| {
427+
if ppm.needs_analysis() {
428+
abort_on_err(print_with_analysis(tcx, *ppm, ofile), tcx.sess);
429+
return;
448430
}
449431

450-
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
451-
debug!("pretty printing HIR {:?}", s);
452-
let sess = annotation.sess();
453-
let sm = sess.source_map();
454-
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
455-
}),
432+
let (src, src_name) = get_source(input, tcx.sess);
433+
434+
let out = match ppm {
435+
Source(s) => {
436+
// Silently ignores an identified node.
437+
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
438+
debug!("pretty printing source code {:?}", s);
439+
let sess = annotation.sess();
440+
let parse = &sess.parse_sess;
441+
pprust::print_crate(
442+
sess.source_map(),
443+
krate,
444+
src_name,
445+
src,
446+
annotation.pp_ann(),
447+
true,
448+
parse.edition,
449+
)
450+
})
451+
}
456452

457-
HirTree => call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
458-
debug!("pretty printing HIR tree");
459-
format!("{:#?}", krate)
460-
}),
453+
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
454+
debug!("pretty printing HIR {:?}", s);
455+
let sess = annotation.sess();
456+
let sm = sess.source_map();
457+
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
458+
}),
459+
460+
HirTree => {
461+
call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
462+
debug!("pretty printing HIR tree");
463+
format!("{:#?}", krate)
464+
})
465+
}
461466

462-
_ => unreachable!(),
463-
};
467+
_ => unreachable!(),
468+
};
464469

465-
write_or_print(&out, ofile);
470+
write_or_print(&out, ofile);
471+
});
466472
}
467473

468474
// In an ideal world, this would be a public function called by the driver after

compiler/rustc_interface/src/passes.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,11 @@ fn configure_and_expand_inner<'a>(
350350
rustc_builtin_macros::test_harness::inject(&sess, &mut resolver, &mut krate)
351351
});
352352

353-
if let Some(PpMode::Source(PpSourceMode::EveryBodyLoops)) = sess.opts.pretty {
354-
tracing::debug!("replacing bodies with loop {{}}");
355-
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
353+
if let Some(ppmodes) = &sess.opts.pretty {
354+
if ppmodes.contains(&PpMode::Source(PpSourceMode::EveryBodyLoops)) {
355+
tracing::debug!("replacing bodies with loop {{}}");
356+
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
357+
}
356358
}
357359

358360
let has_proc_macro_decls = sess.time("AST_validation", || {

compiler/rustc_session/src/config.rs

+48-46
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
11781178
"Pretty-print the input instead of compiling;
11791179
valid types are: `normal` (un-annotated source),
11801180
`expanded` (crates expanded), or
1181-
`expanded,identified` (fully parenthesized, AST nodes with IDs).",
1181+
`expanded+identified` (fully parenthesized, AST nodes with IDs).",
11821182
"TYPE",
11831183
),
11841184
opt::multi_s(
@@ -2056,49 +2056,51 @@ fn parse_pretty(
20562056
matches: &getopts::Matches,
20572057
debugging_opts: &DebuggingOptions,
20582058
efmt: ErrorOutputType,
2059-
) -> Option<PpMode> {
2060-
fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMode {
2059+
) -> Option<Vec<PpMode>> {
2060+
fn parse_pretty_inner(efmt: ErrorOutputType, list: &str, extended: bool) -> Vec<PpMode> {
20612061
use PpMode::*;
2062-
let first = match (name, extended) {
2063-
("normal", _) => Source(PpSourceMode::Normal),
2064-
("identified", _) => Source(PpSourceMode::Identified),
2065-
("everybody_loops", true) => Source(PpSourceMode::EveryBodyLoops),
2066-
("expanded", _) => Source(PpSourceMode::Expanded),
2067-
("expanded,identified", _) => Source(PpSourceMode::ExpandedIdentified),
2068-
("expanded,hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
2069-
("hir", true) => Hir(PpHirMode::Normal),
2070-
("hir,identified", true) => Hir(PpHirMode::Identified),
2071-
("hir,typed", true) => Hir(PpHirMode::Typed),
2072-
("hir-tree", true) => HirTree,
2073-
("mir", true) => Mir,
2074-
("mir-cfg", true) => MirCFG,
2075-
_ => {
2076-
if extended {
2077-
early_error(
2078-
efmt,
2079-
&format!(
2080-
"argument to `unpretty` must be one of `normal`, \
2081-
`expanded`, `identified`, `expanded,identified`, \
2082-
`expanded,hygiene`, `everybody_loops`, \
2083-
`hir`, `hir,identified`, `hir,typed`, `hir-tree`, \
2084-
`mir` or `mir-cfg`; got {}",
2085-
name
2086-
),
2087-
);
2088-
} else {
2089-
early_error(
2090-
efmt,
2091-
&format!(
2092-
"argument to `pretty` must be one of `normal`, \
2093-
`expanded`, `identified`, or `expanded,identified`; got {}",
2094-
name
2095-
),
2096-
);
2062+
list.split(",").map(|ppmode| {
2063+
let first = match (ppmode, extended) {
2064+
("normal", _) => Source(PpSourceMode::Normal),
2065+
("identified", _) => Source(PpSourceMode::Identified),
2066+
("everybody_loops", true) => Source(PpSourceMode::EveryBodyLoops),
2067+
("expanded", _) => Source(PpSourceMode::Expanded),
2068+
("expanded+identified", _) => Source(PpSourceMode::ExpandedIdentified),
2069+
("expanded+hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
2070+
("hir", true) => Hir(PpHirMode::Normal),
2071+
("hir+identified", true) => Hir(PpHirMode::Identified),
2072+
("hir+typed", true) => Hir(PpHirMode::Typed),
2073+
("hir-tree", true) => HirTree,
2074+
("mir", true) => Mir,
2075+
("mir-cfg", true) => MirCFG,
2076+
_ => {
2077+
if extended {
2078+
early_error(
2079+
efmt,
2080+
&format!(
2081+
"argument to `unpretty` must be one of `normal`, \
2082+
`expanded`, `identified`, `expanded+identified`, \
2083+
`expanded+hygiene`, `everybody_loops`, \
2084+
`hir`, `hir+identified`, `hir+typed`, `hir-tree`, \
2085+
`mir` or `mir-cfg`; got {}",
2086+
ppmode
2087+
),
2088+
);
2089+
} else {
2090+
early_error(
2091+
efmt,
2092+
&format!(
2093+
"argument to `pretty` must be a comma-separated list or one of `normal`, \
2094+
`expanded`, `identified`, or `expanded+identified`; got {}",
2095+
ppmode
2096+
),
2097+
);
2098+
}
20972099
}
2098-
}
2099-
};
2100-
tracing::debug!("got unpretty option: {:?}", first);
2101-
first
2100+
};
2101+
tracing::debug!("got unpretty option: {:?}", first);
2102+
first
2103+
}).collect()
21022104
}
21032105

21042106
if debugging_opts.unstable_options {
@@ -2227,19 +2229,19 @@ pub enum PpSourceMode {
22272229
Expanded,
22282230
/// `--pretty=identified`
22292231
Identified,
2230-
/// `--pretty=expanded,identified`
2232+
/// `--pretty=expanded+identified`
22312233
ExpandedIdentified,
2232-
/// `--pretty=expanded,hygiene`
2234+
/// `--pretty=expanded+hygiene`
22332235
ExpandedHygiene,
22342236
}
22352237

22362238
#[derive(Copy, Clone, PartialEq, Debug)]
22372239
pub enum PpHirMode {
22382240
/// `-Zunpretty=hir`
22392241
Normal,
2240-
/// `-Zunpretty=hir,identified`
2242+
/// `-Zunpretty=hir+identified`
22412243
Identified,
2242-
/// `-Zunpretty=hir,typed`
2244+
/// `-Zunpretty=hir+typed`
22432245
Typed,
22442246
}
22452247

compiler/rustc_session/src/options.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ top_level_options!(
147147
// by the compiler.
148148
json_artifact_notifications: bool [TRACKED],
149149

150-
pretty: Option<PpMode> [UNTRACKED],
150+
pretty: Option<Vec<PpMode>> [UNTRACKED],
151151
}
152152
);
153153

@@ -1153,11 +1153,11 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11531153
unpretty: Option<String> = (None, parse_unpretty, [UNTRACKED],
11541154
"present the input source, unstable (and less-pretty) variants;
11551155
valid types are any of the types for `--pretty`, as well as:
1156-
`expanded`, `expanded,identified`,
1157-
`expanded,hygiene` (with internal representations),
1156+
`expanded`, `expanded+identified`,
1157+
`expanded+hygiene` (with internal representations),
11581158
`everybody_loops` (all function bodies replaced with `loop {}`),
1159-
`hir` (the HIR), `hir,identified`,
1160-
`hir,typed` (HIR with types for each node),
1159+
`hir` (the HIR), `hir+identified`,
1160+
`hir+typed` (HIR with types for each node),
11611161
`hir-tree` (dump the raw HIR),
11621162
`mir` (the MIR), or `mir-cfg` (graphviz formatted MIR)"),
11631163
unsound_mir_opts: bool = (false, parse_bool, [TRACKED],

src/test/pretty/issue-4264.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[macro_use]
44
extern crate std;
55
// pretty-compare-only
6-
// pretty-mode:hir,typed
6+
// pretty-mode:hir+typed
77
// pp-exact:issue-4264.pp
88

99
// #4264 fixed-length vector types

src/test/pretty/issue-4264.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// pretty-compare-only
2-
// pretty-mode:hir,typed
2+
// pretty-mode:hir+typed
33
// pp-exact:issue-4264.pp
44

55
// #4264 fixed-length vector types

src/test/ui/hygiene/unpretty-debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// check-pass
2-
// compile-flags: -Zunpretty=expanded,hygiene
2+
// compile-flags: -Zunpretty=expanded+hygiene
33

44
// Don't break whenever Symbol numbering changes
55
// normalize-stdout-test "\d+#" -> "0#"

src/test/ui/hygiene/unpretty-debug.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// check-pass
2-
// compile-flags: -Zunpretty=expanded,hygiene
2+
// compile-flags: -Zunpretty=expanded+hygiene
33

44
// Don't break whenever Symbol numbering changes
55
// normalize-stdout-test "\d+#" -> "0#"

src/test/ui/proc-macro/meta-macro-hygiene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// aux-build:make-macro.rs
33
// aux-build:meta-macro.rs
44
// edition:2018
5-
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene -Z trim-diagnostic-paths=no
5+
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded+hygiene -Z trim-diagnostic-paths=no
66
// check-pass
77
// normalize-stdout-test "\d+#" -> "0#"
88
//

0 commit comments

Comments
 (0)