Cleanup PpMode
and friends
This commit is contained in:
parent
cb2effd44e
commit
3ed189e8af
3 changed files with 83 additions and 74 deletions
|
@ -9,7 +9,7 @@ use rustc_hir_pretty as pprust_hir;
|
||||||
use rustc_middle::hir::map as hir_map;
|
use rustc_middle::hir::map as hir_map;
|
||||||
use rustc_middle::ty::{self, TyCtxt};
|
use rustc_middle::ty::{self, TyCtxt};
|
||||||
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
|
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
|
||||||
use rustc_session::config::{Input, PpMode, PpSourceMode};
|
use rustc_session::config::{Input, PpHirMode, PpMode, PpSourceMode};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::symbol::Ident;
|
use rustc_span::symbol::Ident;
|
||||||
use rustc_span::FileName;
|
use rustc_span::FileName;
|
||||||
|
@ -42,43 +42,41 @@ where
|
||||||
F: FnOnce(&dyn PrinterSupport) -> A,
|
F: FnOnce(&dyn PrinterSupport) -> A,
|
||||||
{
|
{
|
||||||
match *ppmode {
|
match *ppmode {
|
||||||
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
|
Normal | EveryBodyLoops | Expanded => {
|
||||||
let annotation = NoAnn { sess, tcx };
|
let annotation = NoAnn { sess, tcx };
|
||||||
f(&annotation)
|
f(&annotation)
|
||||||
}
|
}
|
||||||
|
|
||||||
PpmIdentified | PpmExpandedIdentified => {
|
Identified | ExpandedIdentified => {
|
||||||
let annotation = IdentifiedAnnotation { sess, tcx };
|
let annotation = IdentifiedAnnotation { sess, tcx };
|
||||||
f(&annotation)
|
f(&annotation)
|
||||||
}
|
}
|
||||||
PpmExpandedHygiene => {
|
ExpandedHygiene => {
|
||||||
let annotation = HygieneAnnotation { sess };
|
let annotation = HygieneAnnotation { sess };
|
||||||
f(&annotation)
|
f(&annotation)
|
||||||
}
|
}
|
||||||
_ => panic!("Should use call_with_pp_support_hir"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn call_with_pp_support_hir<A, F>(ppmode: &PpSourceMode, tcx: TyCtxt<'_>, f: F) -> A
|
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
|
||||||
where
|
where
|
||||||
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
|
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
|
||||||
{
|
{
|
||||||
match *ppmode {
|
match *ppmode {
|
||||||
PpmNormal => {
|
PpHirMode::Normal => {
|
||||||
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
|
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
|
||||||
f(&annotation, tcx.hir().krate())
|
f(&annotation, tcx.hir().krate())
|
||||||
}
|
}
|
||||||
|
|
||||||
PpmIdentified => {
|
PpHirMode::Identified => {
|
||||||
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
|
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
|
||||||
f(&annotation, tcx.hir().krate())
|
f(&annotation, tcx.hir().krate())
|
||||||
}
|
}
|
||||||
PpmTyped => {
|
PpHirMode::Typed => {
|
||||||
abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
|
abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
|
||||||
|
|
||||||
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
|
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
|
||||||
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
|
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
|
||||||
}
|
}
|
||||||
_ => panic!("Should use call_with_pp_support"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -395,7 +393,7 @@ pub fn print_after_parsing(
|
||||||
|
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
|
|
||||||
if let PpmSource(s) = ppm {
|
if let Source(s) = ppm {
|
||||||
// Silently ignores an identified node.
|
// Silently ignores an identified node.
|
||||||
let out = &mut out;
|
let out = &mut out;
|
||||||
call_with_pp_support(&s, sess, None, move |annotation| {
|
call_with_pp_support(&s, sess, None, move |annotation| {
|
||||||
|
@ -436,7 +434,7 @@ pub fn print_after_hir_lowering<'tcx>(
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
|
|
||||||
match ppm {
|
match ppm {
|
||||||
PpmSource(s) => {
|
Source(s) => {
|
||||||
// Silently ignores an identified node.
|
// Silently ignores an identified node.
|
||||||
let out = &mut out;
|
let out = &mut out;
|
||||||
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
|
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
|
||||||
|
@ -455,20 +453,20 @@ pub fn print_after_hir_lowering<'tcx>(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
PpmHir(s) => {
|
Hir(s) => {
|
||||||
let out = &mut out;
|
let out = &mut out;
|
||||||
call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
|
call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
|
||||||
debug!("pretty printing source code {:?}", s);
|
debug!("pretty printing HIR {:?}", s);
|
||||||
let sess = annotation.sess();
|
let sess = annotation.sess();
|
||||||
let sm = sess.source_map();
|
let sm = sess.source_map();
|
||||||
*out = pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
|
*out = pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
PpmHirTree(s) => {
|
HirTree => {
|
||||||
let out = &mut out;
|
let out = &mut out;
|
||||||
call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
|
call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
|
||||||
debug!("pretty printing source code {:?}", s);
|
debug!("pretty printing HIR tree");
|
||||||
*out = format!("{:#?}", krate);
|
*out = format!("{:#?}", krate);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -493,9 +491,9 @@ fn print_with_analysis(
|
||||||
tcx.analysis(LOCAL_CRATE)?;
|
tcx.analysis(LOCAL_CRATE)?;
|
||||||
|
|
||||||
match ppm {
|
match ppm {
|
||||||
PpmMir | PpmMirCFG => match ppm {
|
Mir | MirCFG => match ppm {
|
||||||
PpmMir => write_mir_pretty(tcx, None, &mut out),
|
Mir => write_mir_pretty(tcx, None, &mut out),
|
||||||
PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
|
MirCFG => write_mir_graphviz(tcx, None, &mut out),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
|
|
@ -348,7 +348,7 @@ fn configure_and_expand_inner<'a>(
|
||||||
rustc_builtin_macros::test_harness::inject(&sess, &mut resolver, &mut krate)
|
rustc_builtin_macros::test_harness::inject(&sess, &mut resolver, &mut krate)
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(PpMode::PpmSource(PpSourceMode::PpmEveryBodyLoops)) = sess.opts.pretty {
|
if let Some(PpMode::Source(PpSourceMode::EveryBodyLoops)) = sess.opts.pretty {
|
||||||
tracing::debug!("replacing bodies with loop {{}}");
|
tracing::debug!("replacing bodies with loop {{}}");
|
||||||
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
|
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2068,40 +2068,21 @@ fn parse_pretty(
|
||||||
debugging_opts: &DebuggingOptions,
|
debugging_opts: &DebuggingOptions,
|
||||||
efmt: ErrorOutputType,
|
efmt: ErrorOutputType,
|
||||||
) -> Option<PpMode> {
|
) -> Option<PpMode> {
|
||||||
let pretty = if debugging_opts.unstable_options {
|
|
||||||
matches.opt_default("pretty", "normal").map(|a| {
|
|
||||||
// stable pretty-print variants only
|
|
||||||
parse_pretty_inner(efmt, &a, false)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
return if pretty.is_none() {
|
|
||||||
debugging_opts.unpretty.as_ref().map(|a| {
|
|
||||||
// extended with unstable pretty-print variants
|
|
||||||
parse_pretty_inner(efmt, &a, true)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
pretty
|
|
||||||
};
|
|
||||||
|
|
||||||
fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMode {
|
fn parse_pretty_inner(efmt: ErrorOutputType, name: &str, extended: bool) -> PpMode {
|
||||||
use PpMode::*;
|
use PpMode::*;
|
||||||
use PpSourceMode::*;
|
|
||||||
let first = match (name, extended) {
|
let first = match (name, extended) {
|
||||||
("normal", _) => PpmSource(PpmNormal),
|
("normal", _) => Source(PpSourceMode::Normal),
|
||||||
("identified", _) => PpmSource(PpmIdentified),
|
("identified", _) => Source(PpSourceMode::Identified),
|
||||||
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
|
("everybody_loops", true) => Source(PpSourceMode::EveryBodyLoops),
|
||||||
("expanded", _) => PpmSource(PpmExpanded),
|
("expanded", _) => Source(PpSourceMode::Expanded),
|
||||||
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
|
("expanded,identified", _) => Source(PpSourceMode::ExpandedIdentified),
|
||||||
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
|
("expanded,hygiene", _) => Source(PpSourceMode::ExpandedHygiene),
|
||||||
("hir", true) => PpmHir(PpmNormal),
|
("hir", true) => Hir(PpHirMode::Normal),
|
||||||
("hir,identified", true) => PpmHir(PpmIdentified),
|
("hir,identified", true) => Hir(PpHirMode::Identified),
|
||||||
("hir,typed", true) => PpmHir(PpmTyped),
|
("hir,typed", true) => Hir(PpHirMode::Typed),
|
||||||
("hir-tree", true) => PpmHirTree(PpmNormal),
|
("hir-tree", true) => HirTree,
|
||||||
("mir", true) => PpmMir,
|
("mir", true) => Mir,
|
||||||
("mir-cfg", true) => PpmMirCFG,
|
("mir-cfg", true) => MirCFG,
|
||||||
_ => {
|
_ => {
|
||||||
if extended {
|
if extended {
|
||||||
early_error(
|
early_error(
|
||||||
|
@ -2130,6 +2111,18 @@ fn parse_pretty(
|
||||||
tracing::debug!("got unpretty option: {:?}", first);
|
tracing::debug!("got unpretty option: {:?}", first);
|
||||||
first
|
first
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if debugging_opts.unstable_options {
|
||||||
|
if let Some(a) = matches.opt_default("pretty", "normal") {
|
||||||
|
// stable pretty-print variants only
|
||||||
|
return Some(parse_pretty_inner(efmt, &a, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debugging_opts.unpretty.as_ref().map(|a| {
|
||||||
|
// extended with unstable pretty-print variants
|
||||||
|
parse_pretty_inner(efmt, &a, true)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_crate_type_option() -> RustcOptGroup {
|
pub fn make_crate_type_option() -> RustcOptGroup {
|
||||||
|
@ -2237,22 +2230,43 @@ impl fmt::Display for CrateType {
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum PpSourceMode {
|
pub enum PpSourceMode {
|
||||||
PpmNormal,
|
/// `--pretty=normal`
|
||||||
PpmEveryBodyLoops,
|
Normal,
|
||||||
PpmExpanded,
|
/// `-Zunpretty=everybody_loops`
|
||||||
PpmIdentified,
|
EveryBodyLoops,
|
||||||
PpmExpandedIdentified,
|
/// `--pretty=expanded`
|
||||||
PpmExpandedHygiene,
|
Expanded,
|
||||||
PpmTyped,
|
/// `--pretty=identified`
|
||||||
|
Identified,
|
||||||
|
/// `--pretty=expanded,identified`
|
||||||
|
ExpandedIdentified,
|
||||||
|
/// `--pretty=expanded,hygiene`
|
||||||
|
ExpandedHygiene,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
|
pub enum PpHirMode {
|
||||||
|
/// `-Zunpretty=hir`
|
||||||
|
Normal,
|
||||||
|
/// `-Zunpretty=hir,identified`
|
||||||
|
Identified,
|
||||||
|
/// `-Zunpretty=hir,typed`
|
||||||
|
Typed,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum PpMode {
|
pub enum PpMode {
|
||||||
PpmSource(PpSourceMode),
|
/// Options that print the source code, i.e.
|
||||||
PpmHir(PpSourceMode),
|
/// `--pretty` and `-Zunpretty=everybody_loops`
|
||||||
PpmHirTree(PpSourceMode),
|
Source(PpSourceMode),
|
||||||
PpmMir,
|
/// Options that print the HIR, i.e. `-Zunpretty=hir`
|
||||||
PpmMirCFG,
|
Hir(PpHirMode),
|
||||||
|
/// `-Zunpretty=hir-tree`
|
||||||
|
HirTree,
|
||||||
|
/// `-Zunpretty=mir`
|
||||||
|
Mir,
|
||||||
|
/// `-Zunpretty=mir-cfg`
|
||||||
|
MirCFG,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PpMode {
|
impl PpMode {
|
||||||
|
@ -2260,22 +2274,19 @@ impl PpMode {
|
||||||
use PpMode::*;
|
use PpMode::*;
|
||||||
use PpSourceMode::*;
|
use PpSourceMode::*;
|
||||||
match *self {
|
match *self {
|
||||||
PpmSource(PpmNormal | PpmIdentified) => false,
|
Source(Normal | Identified) => false,
|
||||||
|
|
||||||
PpmSource(
|
Source(Expanded | EveryBodyLoops | ExpandedIdentified | ExpandedHygiene)
|
||||||
PpmExpanded | PpmEveryBodyLoops | PpmExpandedIdentified | PpmExpandedHygiene,
|
| Hir(_)
|
||||||
)
|
| HirTree
|
||||||
| PpmHir(_)
|
| Mir
|
||||||
| PpmHirTree(_)
|
| MirCFG => true,
|
||||||
| PpmMir
|
|
||||||
| PpmMirCFG => true,
|
|
||||||
PpmSource(PpmTyped) => panic!("invalid state"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn needs_analysis(&self) -> bool {
|
pub fn needs_analysis(&self) -> bool {
|
||||||
use PpMode::*;
|
use PpMode::*;
|
||||||
matches!(*self, PpmMir | PpmMirCFG)
|
matches!(*self, Mir | MirCFG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue