Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Zunpretty: allow passing several printing modes as comma-separated list #82540

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,28 +333,30 @@ fn run_compiler(
let early_exit = || sess.compile_status().map(|_| None);
queries.parse()?;

if let Some(ppm) = &sess.opts.pretty {
if ppm.needs_ast_map() {
queries.global_ctxt()?.peek_mut().enter(|tcx| {
let expanded_crate = queries.expansion()?.take().0;
pretty::print_after_hir_lowering(
tcx,
compiler.input(),
&expanded_crate,
*ppm,
if let Some(ppms) = &sess.opts.pretty {
for ppm in ppms {
if ppm.needs_ast_map() {
queries.global_ctxt()?.peek_mut().enter(|tcx| {
let expanded_crate = queries.expansion()?.take().0;
pretty::print_after_hir_lowering(
tcx,
compiler.input(),
&expanded_crate,
ppms,
compiler.output_file().as_ref().map(|p| &**p),
);
Ok(())
})?;
} else {
let krate = queries.parse()?.take();
pretty::print_after_parsing(
sess,
&compiler.input(),
&krate,
ppms,
compiler.output_file().as_ref().map(|p| &**p),
);
Ok(())
})?;
} else {
let krate = queries.parse()?.take();
pretty::print_after_parsing(
sess,
&compiler.input(),
&krate,
*ppm,
compiler.output_file().as_ref().map(|p| &**p),
);
}
}
trace!("finished pretty-printing");
return early_exit();
Expand Down
118 changes: 62 additions & 56 deletions compiler/rustc_driver/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,52 +386,15 @@ pub fn print_after_parsing(
sess: &Session,
input: &Input,
krate: &ast::Crate,
ppm: PpMode,
ofile: Option<&Path>,
) {
let (src, src_name) = get_source(input, sess);

let out = if let Source(s) = ppm {
// Silently ignores an identified node.
call_with_pp_support(&s, sess, None, move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
let parse = &sess.parse_sess;
pprust::print_crate(
sess.source_map(),
krate,
src_name,
src,
annotation.pp_ann(),
false,
parse.edition,
)
})
} else {
unreachable!()
};

write_or_print(&out, ofile);
}

pub fn print_after_hir_lowering<'tcx>(
tcx: TyCtxt<'tcx>,
input: &Input,
krate: &ast::Crate,
ppm: PpMode,
ppms: &Vec<PpMode>,
ofile: Option<&Path>,
) {
if ppm.needs_analysis() {
abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
return;
}

let (src, src_name) = get_source(input, tcx.sess);
ppms.iter().for_each(|ppm| {
let (src, src_name) = get_source(input, sess);

let out = match ppm {
Source(s) => {
let out = if let Source(s) = ppm {
// Silently ignores an identified node.
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
call_with_pp_support(&s, sess, None, move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
let parse = &sess.parse_sess;
Expand All @@ -441,28 +404,71 @@ pub fn print_after_hir_lowering<'tcx>(
src_name,
src,
annotation.pp_ann(),
true,
false,
parse.edition,
)
})
} else {
unreachable!()
};

write_or_print(&out, ofile);
});
}

pub fn print_after_hir_lowering<'tcx>(
tcx: TyCtxt<'tcx>,
input: &Input,
krate: &ast::Crate,
ppms: &Vec<PpMode>,
ofile: Option<&Path>,
) {
ppms.iter().for_each(|ppm| {
if ppm.needs_analysis() {
abort_on_err(print_with_analysis(tcx, *ppm, ofile), tcx.sess);
return;
}

Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
debug!("pretty printing HIR {:?}", s);
let sess = annotation.sess();
let sm = sess.source_map();
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
}),
let (src, src_name) = get_source(input, tcx.sess);

let out = match ppm {
Source(s) => {
// Silently ignores an identified node.
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
let parse = &sess.parse_sess;
pprust::print_crate(
sess.source_map(),
krate,
src_name,
src,
annotation.pp_ann(),
true,
parse.edition,
)
})
}

HirTree => call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
debug!("pretty printing HIR tree");
format!("{:#?}", krate)
}),
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
debug!("pretty printing HIR {:?}", s);
let sess = annotation.sess();
let sm = sess.source_map();
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
}),

HirTree => {
call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
debug!("pretty printing HIR tree");
format!("{:#?}", krate)
})
}

_ => unreachable!(),
};
_ => unreachable!(),
};

write_or_print(&out, ofile);
write_or_print(&out, ofile);
});
}

// In an ideal world, this would be a public function called by the driver after
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ fn configure_and_expand_inner<'a>(
rustc_builtin_macros::test_harness::inject(&sess, &mut resolver, &mut krate)
});

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

let has_proc_macro_decls = sess.time("AST_validation", || {
Expand Down
94 changes: 48 additions & 46 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
"Pretty-print the input instead of compiling;
valid types are: `normal` (un-annotated source),
`expanded` (crates expanded), or
`expanded,identified` (fully parenthesized, AST nodes with IDs).",
`expanded+identified` (fully parenthesized, AST nodes with IDs).",
"TYPE",
),
opt::multi_s(
Expand Down Expand Up @@ -2056,49 +2056,51 @@ fn parse_pretty(
matches: &getopts::Matches,
debugging_opts: &DebuggingOptions,
efmt: ErrorOutputType,
) -> Option<PpMode> {
fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMode {
) -> Option<Vec<PpMode>> {
fn parse_pretty_inner(efmt: ErrorOutputType, list: &str, extended: bool) -> Vec<PpMode> {
use PpMode::*;
let first = match (name, extended) {
("normal", _) => Source(PpSourceMode::Normal),
("identified", _) => Source(PpSourceMode::Identified),
("everybody_loops", true) => Source(PpSourceMode::EveryBodyLoops),
("expanded", _) => Source(PpSourceMode::Expanded),
("expanded,identified", _) => Source(PpSourceMode::ExpandedIdentified),
("expanded,hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
("hir", true) => Hir(PpHirMode::Normal),
("hir,identified", true) => Hir(PpHirMode::Identified),
("hir,typed", true) => Hir(PpHirMode::Typed),
("hir-tree", true) => HirTree,
("mir", true) => Mir,
("mir-cfg", true) => MirCFG,
_ => {
if extended {
early_error(
efmt,
&format!(
"argument to `unpretty` must be one of `normal`, \
`expanded`, `identified`, `expanded,identified`, \
`expanded,hygiene`, `everybody_loops`, \
`hir`, `hir,identified`, `hir,typed`, `hir-tree`, \
`mir` or `mir-cfg`; got {}",
name
),
);
} else {
early_error(
efmt,
&format!(
"argument to `pretty` must be one of `normal`, \
`expanded`, `identified`, or `expanded,identified`; got {}",
name
),
);
list.split(",").map(|ppmode| {
let first = match (ppmode, extended) {
("normal", _) => Source(PpSourceMode::Normal),
("identified", _) => Source(PpSourceMode::Identified),
("everybody_loops", true) => Source(PpSourceMode::EveryBodyLoops),
("expanded", _) => Source(PpSourceMode::Expanded),
("expanded+identified", _) => Source(PpSourceMode::ExpandedIdentified),
("expanded+hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
("hir", true) => Hir(PpHirMode::Normal),
("hir+identified", true) => Hir(PpHirMode::Identified),
("hir+typed", true) => Hir(PpHirMode::Typed),
("hir-tree", true) => HirTree,
("mir", true) => Mir,
("mir-cfg", true) => MirCFG,
_ => {
if extended {
early_error(
efmt,
&format!(
"argument to `unpretty` must be one of `normal`, \
`expanded`, `identified`, `expanded+identified`, \
`expanded+hygiene`, `everybody_loops`, \
`hir`, `hir+identified`, `hir+typed`, `hir-tree`, \
`mir` or `mir-cfg`; got {}",
ppmode
),
);
} else {
early_error(
efmt,
&format!(
"argument to `pretty` must be a comma-separated list or one of `normal`, \
`expanded`, `identified`, or `expanded+identified`; got {}",
ppmode
),
);
}
}
}
};
tracing::debug!("got unpretty option: {:?}", first);
first
};
tracing::debug!("got unpretty option: {:?}", first);
first
}).collect()
}

if debugging_opts.unstable_options {
Expand Down Expand Up @@ -2227,19 +2229,19 @@ pub enum PpSourceMode {
Expanded,
/// `--pretty=identified`
Identified,
/// `--pretty=expanded,identified`
/// `--pretty=expanded+identified`
ExpandedIdentified,
/// `--pretty=expanded,hygiene`
/// `--pretty=expanded+hygiene`
ExpandedHygiene,
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum PpHirMode {
/// `-Zunpretty=hir`
Normal,
/// `-Zunpretty=hir,identified`
/// `-Zunpretty=hir+identified`
Identified,
/// `-Zunpretty=hir,typed`
/// `-Zunpretty=hir+typed`
Typed,
}

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ top_level_options!(
// by the compiler.
json_artifact_notifications: bool [TRACKED],

pretty: Option<PpMode> [UNTRACKED],
pretty: Option<Vec<PpMode>> [UNTRACKED],
}
);

Expand Down Expand Up @@ -1153,11 +1153,11 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
unpretty: Option<String> = (None, parse_unpretty, [UNTRACKED],
"present the input source, unstable (and less-pretty) variants;
valid types are any of the types for `--pretty`, as well as:
`expanded`, `expanded,identified`,
`expanded,hygiene` (with internal representations),
`expanded`, `expanded+identified`,
`expanded+hygiene` (with internal representations),
`everybody_loops` (all function bodies replaced with `loop {}`),
`hir` (the HIR), `hir,identified`,
`hir,typed` (HIR with types for each node),
`hir` (the HIR), `hir+identified`,
`hir+typed` (HIR with types for each node),
`hir-tree` (dump the raw HIR),
`mir` (the MIR), or `mir-cfg` (graphviz formatted MIR)"),
unsound_mir_opts: bool = (false, parse_bool, [TRACKED],
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/issue-4264.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[macro_use]
extern crate std;
// pretty-compare-only
// pretty-mode:hir,typed
// pretty-mode:hir+typed
// pp-exact:issue-4264.pp

// #4264 fixed-length vector types
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/issue-4264.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// pretty-compare-only
// pretty-mode:hir,typed
// pretty-mode:hir+typed
// pp-exact:issue-4264.pp

// #4264 fixed-length vector types
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/unpretty-debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-pass
// compile-flags: -Zunpretty=expanded,hygiene
// compile-flags: -Zunpretty=expanded+hygiene

// Don't break whenever Symbol numbering changes
// normalize-stdout-test "\d+#" -> "0#"
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/unpretty-debug.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// check-pass
// compile-flags: -Zunpretty=expanded,hygiene
// compile-flags: -Zunpretty=expanded+hygiene

// Don't break whenever Symbol numbering changes
// normalize-stdout-test "\d+#" -> "0#"
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/meta-macro-hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// aux-build:make-macro.rs
// aux-build:meta-macro.rs
// edition:2018
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene -Z trim-diagnostic-paths=no
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded+hygiene -Z trim-diagnostic-paths=no
// check-pass
// normalize-stdout-test "\d+#" -> "0#"
//
Expand Down
Loading