2018-11-26 20:59:49 -06:00
|
|
|
//! The various pretty-printing routines.
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::cfg;
|
|
|
|
use rustc::cfg::graphviz::LabelledCFG;
|
2018-11-26 20:59:49 -06:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::map as hir_map;
|
|
|
|
use rustc::hir::map::blocks;
|
|
|
|
use rustc::hir::print as pprust_hir;
|
2018-12-08 13:30:23 -06:00
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc::session::Session;
|
2018-12-08 13:30:23 -06:00
|
|
|
use rustc::session::config::Input;
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
|
|
|
use rustc::util::common::ErrorReported;
|
|
|
|
use rustc_interface::util::ReplaceBodyWithLoop;
|
2014-12-05 13:17:35 -06:00
|
|
|
use rustc_borrowck as borrowck;
|
|
|
|
use rustc_borrowck::graphviz as borrowck_dot;
|
2017-03-09 12:10:05 -06:00
|
|
|
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
|
2016-02-14 12:23:05 -06:00
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::mut_visit::MutVisitor;
|
2017-06-24 22:22:42 -05:00
|
|
|
use syntax::print::{pprust};
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::print::pprust::PrintState;
|
2018-12-08 13:30:23 -06:00
|
|
|
use syntax_pos::FileName;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
use graphviz as dot;
|
|
|
|
|
2017-01-06 13:54:24 -06:00
|
|
|
use std::cell::Cell;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Write};
|
2014-08-11 05:59:35 -05:00
|
|
|
use std::option;
|
2016-04-19 23:24:14 -05:00
|
|
|
use std::path::Path;
|
2014-11-14 22:52:00 -06:00
|
|
|
use std::str::FromStr;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
pub use self::UserIdentifiedItem::*;
|
|
|
|
pub use self::PpSourceMode::*;
|
|
|
|
pub use self::PpMode::*;
|
|
|
|
use self::NodesMatchingUII::*;
|
2019-03-26 15:35:18 -05:00
|
|
|
use crate::abort_on_err;
|
2018-12-08 13:30:23 -06:00
|
|
|
|
2019-03-26 15:35:18 -05:00
|
|
|
use crate::source_name;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2014-08-11 05:59:35 -05:00
|
|
|
pub enum PpSourceMode {
|
|
|
|
PpmNormal,
|
2014-12-17 06:37:26 -06:00
|
|
|
PpmEveryBodyLoops,
|
2014-08-11 05:59:35 -05:00
|
|
|
PpmExpanded,
|
|
|
|
PpmIdentified,
|
|
|
|
PpmExpandedIdentified,
|
2014-08-11 07:01:37 -05:00
|
|
|
PpmExpandedHygiene,
|
2015-07-31 02:04:06 -05:00
|
|
|
PpmTyped,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2015-01-12 15:42:12 -06:00
|
|
|
pub enum PpFlowGraphMode {
|
|
|
|
Default,
|
|
|
|
/// Drops the labels from the edges in the flowgraph output. This
|
2018-01-14 21:11:40 -06:00
|
|
|
/// is mostly for use in the -Z unpretty flowgraph run-make tests,
|
2015-01-12 15:42:12 -06:00
|
|
|
/// since the labels are largely uninteresting in those cases and
|
|
|
|
/// have become a pain to maintain.
|
|
|
|
UnlabelledEdges,
|
|
|
|
}
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2014-08-11 05:59:35 -05:00
|
|
|
pub enum PpMode {
|
|
|
|
PpmSource(PpSourceMode),
|
2015-07-31 02:04:06 -05:00
|
|
|
PpmHir(PpSourceMode),
|
2017-11-02 07:58:13 -05:00
|
|
|
PpmHirTree(PpSourceMode),
|
2015-01-12 15:42:12 -06:00
|
|
|
PpmFlowGraph(PpFlowGraphMode),
|
2016-02-14 12:23:05 -06:00
|
|
|
PpmMir,
|
2016-03-12 11:07:00 -06:00
|
|
|
PpmMirCFG,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
impl PpMode {
|
|
|
|
pub fn needs_ast_map(&self, opt_uii: &Option<UserIdentifiedItem>) -> bool {
|
|
|
|
match *self {
|
|
|
|
PpmSource(PpmNormal) |
|
|
|
|
PpmSource(PpmEveryBodyLoops) |
|
|
|
|
PpmSource(PpmIdentified) => opt_uii.is_some(),
|
|
|
|
|
|
|
|
PpmSource(PpmExpanded) |
|
|
|
|
PpmSource(PpmExpandedIdentified) |
|
|
|
|
PpmSource(PpmExpandedHygiene) |
|
|
|
|
PpmHir(_) |
|
2017-11-02 07:58:13 -05:00
|
|
|
PpmHirTree(_) |
|
2016-04-20 17:29:49 -05:00
|
|
|
PpmMir |
|
|
|
|
PpmMirCFG |
|
|
|
|
PpmFlowGraph(_) => true,
|
|
|
|
PpmSource(PpmTyped) => panic!("invalid state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_analysis(&self) -> bool {
|
|
|
|
match *self {
|
2016-10-19 20:38:43 -05:00
|
|
|
PpmMir | PpmMirCFG | PpmFlowGraph(_) => true,
|
|
|
|
_ => false,
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 08:13:38 -06:00
|
|
|
pub fn parse_pretty(sess: &Session,
|
|
|
|
name: &str,
|
2015-11-10 14:48:44 -06:00
|
|
|
extended: bool)
|
|
|
|
-> (PpMode, Option<UserIdentifiedItem>) {
|
2015-04-01 13:28:34 -05:00
|
|
|
let mut split = name.splitn(2, '=');
|
2014-08-11 05:59:35 -05:00
|
|
|
let first = split.next().unwrap();
|
|
|
|
let opt_second = split.next();
|
2014-12-17 08:13:38 -06:00
|
|
|
let first = match (first, extended) {
|
2015-11-10 14:48:44 -06:00
|
|
|
("normal", _) => PpmSource(PpmNormal),
|
|
|
|
("identified", _) => PpmSource(PpmIdentified),
|
2014-12-17 08:13:38 -06:00
|
|
|
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
|
2015-11-10 14:48:44 -06:00
|
|
|
("expanded", _) => PpmSource(PpmExpanded),
|
2014-12-17 08:13:38 -06:00
|
|
|
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
|
|
|
|
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
|
2015-11-10 14:48:44 -06:00
|
|
|
("hir", true) => PpmHir(PpmNormal),
|
2015-10-22 15:05:51 -05:00
|
|
|
("hir,identified", true) => PpmHir(PpmIdentified),
|
2015-11-10 14:48:44 -06:00
|
|
|
("hir,typed", true) => PpmHir(PpmTyped),
|
2017-11-02 07:58:13 -05:00
|
|
|
("hir-tree", true) => PpmHirTree(PpmNormal),
|
2016-02-14 12:23:05 -06:00
|
|
|
("mir", true) => PpmMir,
|
2016-03-12 11:07:00 -06:00
|
|
|
("mir-cfg", true) => PpmMirCFG,
|
2015-11-10 14:48:44 -06:00
|
|
|
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
|
|
|
|
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
|
2014-08-11 05:59:35 -05:00
|
|
|
_ => {
|
2014-12-17 08:13:38 -06:00
|
|
|
if extended {
|
2015-11-10 14:48:44 -06:00
|
|
|
sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \
|
|
|
|
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, \
|
|
|
|
`identified`, `expanded,identified`, `everybody_loops`, \
|
2019-01-14 09:09:21 -06:00
|
|
|
`hir`, `hir,identified`, `hir,typed`, `hir-tree`, \
|
|
|
|
`mir` or `mir-cfg`; got {}",
|
2015-11-10 14:48:44 -06:00
|
|
|
name));
|
2014-12-17 08:13:38 -06:00
|
|
|
} else {
|
2015-11-10 14:48:44 -06:00
|
|
|
sess.fatal(&format!("argument to `pretty` must be one of `normal`, `expanded`, \
|
|
|
|
`identified`, or `expanded,identified`; got {}",
|
|
|
|
name));
|
2014-12-17 08:13:38 -06:00
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
};
|
2015-01-28 00:52:32 -06:00
|
|
|
let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
|
2014-08-11 05:59:35 -05:00
|
|
|
(first, opt_second)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This slightly awkward construction is to allow for each PpMode to
|
|
|
|
// choose whether it needs to do analyses (which can consume the
|
|
|
|
// Session) and then pass through the session (now attached to the
|
|
|
|
// analysis results) on to the chosen pretty-printer, along with the
|
|
|
|
// `&PpAnn` object.
|
|
|
|
//
|
|
|
|
// Note that since the `&PrinterSupport` is freshly constructed on each
|
|
|
|
// call, it would not make sense to try to attach the lifetime of `self`
|
|
|
|
// to the lifetime of the `&PrinterObject`.
|
|
|
|
//
|
|
|
|
// (The `use_once_payload` is working around the current lack of once
|
|
|
|
// functions in the compiler.)
|
|
|
|
|
2014-08-11 06:12:23 -05:00
|
|
|
impl PpSourceMode {
|
2014-08-11 05:59:35 -05:00
|
|
|
/// Constructs a `PrinterSupport` object and passes it to `f`.
|
2019-06-11 16:11:55 -05:00
|
|
|
fn call_with_pp_support<'tcx, A, F>(
|
|
|
|
&self,
|
|
|
|
sess: &'tcx Session,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: Option<TyCtxt<'tcx>>,
|
2019-06-11 16:11:55 -05:00
|
|
|
f: F,
|
|
|
|
) -> A
|
|
|
|
where
|
|
|
|
F: FnOnce(&dyn PrinterSupport) -> A,
|
2014-12-09 15:32:45 -06:00
|
|
|
{
|
2014-08-11 06:12:23 -05:00
|
|
|
match *self {
|
2014-12-17 06:37:26 -06:00
|
|
|
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
|
2015-11-10 14:48:44 -06:00
|
|
|
let annotation = NoAnn {
|
2017-08-07 00:54:09 -05:00
|
|
|
sess,
|
2018-12-08 13:30:23 -06:00
|
|
|
tcx,
|
2015-11-10 14:48:44 -06:00
|
|
|
};
|
2017-06-25 10:54:09 -05:00
|
|
|
f(&annotation)
|
2014-08-11 06:12:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified | PpmExpandedIdentified => {
|
2015-11-10 14:48:44 -06:00
|
|
|
let annotation = IdentifiedAnnotation {
|
2017-08-07 00:54:09 -05:00
|
|
|
sess,
|
2018-12-08 13:30:23 -06:00
|
|
|
tcx,
|
2015-11-10 14:48:44 -06:00
|
|
|
};
|
2017-06-25 10:54:09 -05:00
|
|
|
f(&annotation)
|
2014-08-11 06:12:23 -05:00
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
PpmExpandedHygiene => {
|
2015-11-10 14:48:44 -06:00
|
|
|
let annotation = HygieneAnnotation {
|
2017-08-07 00:54:09 -05:00
|
|
|
sess,
|
2015-11-10 14:48:44 -06:00
|
|
|
};
|
2017-06-25 10:54:09 -05:00
|
|
|
f(&annotation)
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
_ => panic!("Should use call_with_pp_support_hir"),
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 13:27:44 -05:00
|
|
|
fn call_with_pp_support_hir<A, F>(&self, tcx: TyCtxt<'_>, f: F) -> A
|
2019-06-11 16:11:55 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate) -> A,
|
2015-07-31 02:04:06 -05:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
PpmNormal => {
|
2015-11-10 14:48:44 -06:00
|
|
|
let annotation = NoAnn {
|
2018-12-08 13:30:23 -06:00
|
|
|
sess: tcx.sess,
|
|
|
|
tcx: Some(tcx),
|
2015-11-10 14:48:44 -06:00
|
|
|
};
|
2018-12-08 13:30:23 -06:00
|
|
|
f(&annotation, tcx.hir().forest.krate())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified => {
|
|
|
|
let annotation = IdentifiedAnnotation {
|
2018-12-08 13:30:23 -06:00
|
|
|
sess: tcx.sess,
|
|
|
|
tcx: Some(tcx),
|
2015-07-31 02:04:06 -05:00
|
|
|
};
|
2018-12-08 13:30:23 -06:00
|
|
|
f(&annotation, tcx.hir().forest.krate())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2014-08-11 06:12:23 -05:00
|
|
|
PpmTyped => {
|
2018-12-08 13:30:23 -06:00
|
|
|
abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
|
|
|
|
|
|
|
|
let empty_tables = ty::TypeckTables::empty(None);
|
|
|
|
let annotation = TypedAnnotation {
|
|
|
|
tcx,
|
|
|
|
tables: Cell::new(&empty_tables)
|
|
|
|
};
|
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
f(&annotation, tcx.hir().forest.krate())
|
2018-12-08 13:30:23 -06:00
|
|
|
})
|
2014-08-11 06:12:23 -05:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
_ => panic!("Should use call_with_pp_support"),
|
2014-08-11 06:12:23 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
trait PrinterSupport: pprust::PpAnn {
|
2014-08-11 05:59:35 -05:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
/// Produces the pretty-print annotation object.
|
|
|
|
///
|
|
|
|
/// (Rust does not yet support upcasting from a trait object to
|
|
|
|
/// an object for one of its super-traits.)
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn;
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
|
2015-07-31 02:04:06 -05:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to an
|
|
|
|
/// `hir_map::Map` from a value that now owns it.
|
2017-01-25 19:21:50 -06:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>>;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
/// Produces the pretty-print annotation object.
|
|
|
|
///
|
|
|
|
/// (Rust does not yet support upcasting from a trait object to
|
|
|
|
/// an object for one of its super-traits.)
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn;
|
2016-04-06 05:51:55 -05:00
|
|
|
|
|
|
|
/// Computes an user-readable representation of a path, if possible.
|
2019-06-24 03:03:37 -05:00
|
|
|
fn node_path(&self, id: hir::HirId) -> Option<String> {
|
2019-04-27 15:39:17 -05:00
|
|
|
self.hir_map().and_then(|map| {
|
2019-06-24 03:03:37 -05:00
|
|
|
map.def_path_from_hir_id(id)
|
2019-04-27 15:39:17 -05:00
|
|
|
}).map(|path| {
|
2016-10-19 20:38:43 -05:00
|
|
|
path.data
|
|
|
|
.into_iter()
|
|
|
|
.map(|elem| elem.data.to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("::")
|
2016-04-06 05:51:55 -05:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct NoAnn<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> PrinterSupport for NoAnn<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
|
2018-12-08 13:30:23 -06:00
|
|
|
self.tcx.map(|tcx| tcx.hir())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
|
|
|
|
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 13:30:23 -06:00
|
|
|
if let Some(tcx) = self.tcx {
|
|
|
|
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct IdentifiedAnnotation<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Expr(_) => s.popen(),
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Ident(_) |
|
2019-06-24 13:15:11 -05:00
|
|
|
pprust::AnnNode::Name(_) => {},
|
2014-08-11 07:01:37 -05:00
|
|
|
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Item(item) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2014-08-11 05:59:35 -05:00
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::SubItem(id) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2015-03-23 20:52:55 -05:00
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Block(blk) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2014-08-11 05:59:35 -05:00
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Expr(expr) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(expr.id.to_string());
|
2014-08-11 05:59:35 -05:00
|
|
|
s.pclose()
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Pat(pat) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(format!("pat {}", pat.id));
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
|
2018-12-08 13:30:23 -06:00
|
|
|
self.tcx.map(|tcx| tcx.hir())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 13:30:23 -06:00
|
|
|
if let Some(ref tcx) = self.tcx {
|
|
|
|
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2015-07-31 02:04:06 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2015-07-31 02:04:06 -05:00
|
|
|
match node {
|
2019-06-24 13:15:11 -05:00
|
|
|
pprust_hir::AnnNode::Name(_) => {},
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Item(item) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("hir_id: {}", item.hir_id));
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::SubItem(id) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(id.to_string());
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Block(blk) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("block hir_id: {}", blk.hir_id));
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("expr hir_id: {}", expr.hir_id));
|
|
|
|
s.pclose();
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Pat(pat) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("pat hir_id: {}", pat.hir_id));
|
|
|
|
}
|
|
|
|
pprust_hir::AnnNode::Arm(arm) => {
|
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(format!("arm hir_id: {}", arm.hir_id));
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct HygieneAnnotation<'a> {
|
|
|
|
sess: &'a Session
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'a> PrinterSupport for HygieneAnnotation<'a> {
|
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2014-08-11 07:01:37 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2014-08-11 07:01:37 -05:00
|
|
|
// FIXME #16420: this doesn't display the connections
|
|
|
|
// between syntax contexts
|
2018-03-17 18:57:23 -05:00
|
|
|
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Name(&name) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2016-11-16 02:21:52 -06:00
|
|
|
s.synth_comment(name.as_u32().to_string())
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {}
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
struct TypedAnnotation<'a, 'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-01-25 15:24:00 -06:00
|
|
|
tables: Cell<&'a ty::TypeckTables<'tcx>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
&self.tcx.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'tcx>> {
|
2018-12-04 06:45:36 -06:00
|
|
|
Some(&self.tcx.hir())
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2016-04-06 05:51:55 -05:00
|
|
|
|
2019-06-24 03:03:37 -05:00
|
|
|
fn node_path(&self, id: hir::HirId) -> Option<String> {
|
2019-06-27 04:28:14 -05:00
|
|
|
Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id)))
|
2016-04-06 05:51:55 -05:00
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2017-01-06 13:54:24 -06:00
|
|
|
let old_tables = self.tables.get();
|
|
|
|
if let pprust_hir::Nested::Body(id) = nested {
|
|
|
|
self.tables.set(self.tcx.body_tables(id));
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested);
|
2017-01-06 13:54:24 -06:00
|
|
|
self.tables.set(old_tables);
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
|
|
|
s.s.word("as");
|
|
|
|
s.s.space();
|
|
|
|
s.s.word(self.tables.get().expr_ty(expr).to_string());
|
|
|
|
s.pclose();
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {},
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gather_flowgraph_variants(sess: &Session) -> Vec<borrowck_dot::Variant> {
|
2014-12-09 03:55:49 -06:00
|
|
|
let print_loans = sess.opts.debugging_opts.flowgraph_print_loans;
|
|
|
|
let print_moves = sess.opts.debugging_opts.flowgraph_print_moves;
|
|
|
|
let print_assigns = sess.opts.debugging_opts.flowgraph_print_assigns;
|
|
|
|
let print_all = sess.opts.debugging_opts.flowgraph_print_all;
|
2014-08-11 05:59:35 -05:00
|
|
|
let mut variants = Vec::new();
|
2014-12-09 03:55:49 -06:00
|
|
|
if print_all || print_loans {
|
2014-08-11 05:59:35 -05:00
|
|
|
variants.push(borrowck_dot::Loans);
|
|
|
|
}
|
2014-12-09 03:55:49 -06:00
|
|
|
if print_all || print_moves {
|
2014-08-11 05:59:35 -05:00
|
|
|
variants.push(borrowck_dot::Moves);
|
|
|
|
}
|
2014-12-09 03:55:49 -06:00
|
|
|
if print_all || print_assigns {
|
2014-08-11 05:59:35 -05:00
|
|
|
variants.push(borrowck_dot::Assigns);
|
|
|
|
}
|
|
|
|
variants
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2014-08-11 05:59:35 -05:00
|
|
|
pub enum UserIdentifiedItem {
|
|
|
|
ItemViaNode(ast::NodeId),
|
|
|
|
ItemViaPath(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for UserIdentifiedItem {
|
2015-01-28 00:52:32 -06:00
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
|
2015-11-10 14:48:44 -06:00
|
|
|
Ok(s.parse()
|
2018-11-07 03:08:41 -06:00
|
|
|
.map(ast::NodeId::from_u32)
|
2015-11-10 14:48:44 -06:00
|
|
|
.map(ItemViaNode)
|
|
|
|
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 14:02:17 -05:00
|
|
|
enum NodesMatchingUII<'a> {
|
2014-12-14 12:34:23 -06:00
|
|
|
NodesMatchingDirect(option::IntoIter<ast::NodeId>),
|
2019-04-24 14:02:17 -05:00
|
|
|
NodesMatchingSuffix(Box<dyn Iterator<Item = ast::NodeId> + 'a>),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2019-04-24 14:02:17 -05:00
|
|
|
impl<'a> Iterator for NodesMatchingUII<'a> {
|
2015-01-02 07:15:20 -06:00
|
|
|
type Item = ast::NodeId;
|
|
|
|
|
2014-08-11 05:59:35 -05:00
|
|
|
fn next(&mut self) -> Option<ast::NodeId> {
|
|
|
|
match self {
|
2015-01-07 18:26:00 -06:00
|
|
|
&mut NodesMatchingDirect(ref mut iter) => iter.next(),
|
|
|
|
&mut NodesMatchingSuffix(ref mut iter) => iter.next(),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 04:33:59 -05:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
match self {
|
|
|
|
&NodesMatchingDirect(ref iter) => iter.size_hint(),
|
|
|
|
&NodesMatchingSuffix(ref iter) => iter.size_hint(),
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserIdentifiedItem {
|
|
|
|
fn reconstructed_input(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
ItemViaNode(node_id) => node_id.to_string(),
|
2015-07-10 07:19:21 -05:00
|
|
|
ItemViaPath(ref parts) => parts.join("::"),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
fn all_matching_node_ids<'a, 'hir>(&'a self,
|
|
|
|
map: &'a hir_map::Map<'hir>)
|
2019-04-24 14:02:17 -05:00
|
|
|
-> NodesMatchingUII<'a> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match *self {
|
2015-11-10 14:48:44 -06:00
|
|
|
ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()),
|
2019-04-24 14:02:17 -05:00
|
|
|
ItemViaPath(ref parts) => {
|
|
|
|
NodesMatchingSuffix(Box::new(map.nodes_matching_suffix(&parts)))
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:35:18 -05:00
|
|
|
fn to_one_node_id(self,
|
|
|
|
user_option: &str,
|
|
|
|
sess: &Session,
|
|
|
|
map: &hir_map::Map<'_>)
|
|
|
|
-> ast::NodeId {
|
2015-02-01 11:44:15 -06:00
|
|
|
let fail_because = |is_wrong_because| -> ast::NodeId {
|
2015-11-10 14:48:44 -06:00
|
|
|
let message = format!("{} needs NodeId (int) or unique path suffix (b::c::d); got \
|
|
|
|
{}, which {}",
|
|
|
|
user_option,
|
|
|
|
self.reconstructed_input(),
|
|
|
|
is_wrong_because);
|
2017-03-24 03:31:26 -05:00
|
|
|
sess.fatal(&message)
|
2014-08-11 05:59:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut saw_node = ast::DUMMY_NODE_ID;
|
2015-01-24 08:39:32 -06:00
|
|
|
let mut seen = 0;
|
2014-08-11 05:59:35 -05:00
|
|
|
for node in self.all_matching_node_ids(map) {
|
|
|
|
saw_node = node;
|
|
|
|
seen += 1;
|
|
|
|
if seen > 1 {
|
|
|
|
fail_because("does not resolve uniquely");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seen == 0 {
|
|
|
|
fail_because("does not resolve to any item");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(seen == 1);
|
|
|
|
return saw_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:11:55 -05:00
|
|
|
fn print_flowgraph<'tcx, W: Write>(
|
|
|
|
variants: Vec<borrowck_dot::Variant>,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 16:11:55 -05:00
|
|
|
code: blocks::Code<'tcx>,
|
|
|
|
mode: PpFlowGraphMode,
|
|
|
|
mut out: W,
|
|
|
|
) -> io::Result<()> {
|
2017-02-18 05:52:16 -06:00
|
|
|
let body_id = match code {
|
|
|
|
blocks::Code::Expr(expr) => {
|
|
|
|
// Find the function this expression is from.
|
2019-02-24 02:33:17 -06:00
|
|
|
let mut hir_id = expr.hir_id;
|
2017-02-18 05:52:16 -06:00
|
|
|
loop {
|
2019-06-20 03:39:19 -05:00
|
|
|
let node = tcx.hir().get(hir_id);
|
2017-02-18 05:52:16 -06:00
|
|
|
if let Some(n) = hir::map::blocks::FnLikeNode::from_node(node) {
|
|
|
|
break n.body();
|
|
|
|
}
|
2019-06-24 02:46:38 -05:00
|
|
|
let parent = tcx.hir().get_parent_node(hir_id);
|
2019-02-24 02:33:17 -06:00
|
|
|
assert_ne!(hir_id, parent);
|
|
|
|
hir_id = parent;
|
2017-02-18 05:52:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
blocks::Code::FnLike(fn_like) => fn_like.body(),
|
2016-04-20 17:29:49 -05:00
|
|
|
};
|
2018-12-04 06:45:36 -06:00
|
|
|
let body = tcx.hir().body(body_id);
|
2017-02-18 05:52:16 -06:00
|
|
|
let cfg = cfg::CFG::new(tcx, &body);
|
2016-04-20 17:29:49 -05:00
|
|
|
let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
|
2019-03-17 05:36:10 -05:00
|
|
|
let hir_id = code.id();
|
|
|
|
// We have to disassemble the hir_id because name must be ASCII
|
|
|
|
// alphanumeric. This does not appear in the rendered graph, so it does not
|
|
|
|
// have to be user friendly.
|
|
|
|
let name = format!(
|
2019-05-08 14:07:12 -05:00
|
|
|
"hir_id_{}_{}",
|
2019-05-18 06:19:33 -05:00
|
|
|
hir_id.owner.index(),
|
2019-03-17 05:36:10 -05:00
|
|
|
hir_id.local_id.index(),
|
|
|
|
);
|
2016-04-20 17:29:49 -05:00
|
|
|
let lcfg = LabelledCFG {
|
2017-08-29 11:27:30 -05:00
|
|
|
tcx,
|
2016-04-20 17:29:49 -05:00
|
|
|
cfg: &cfg,
|
2019-03-17 05:36:10 -05:00
|
|
|
name,
|
2017-08-07 00:54:09 -05:00
|
|
|
labelled_edges,
|
2014-12-17 06:37:26 -06:00
|
|
|
};
|
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
match code {
|
|
|
|
_ if variants.is_empty() => {
|
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
|
|
|
}
|
2016-10-25 18:27:14 -05:00
|
|
|
blocks::Code::Expr(_) => {
|
2016-04-20 17:29:49 -05:00
|
|
|
tcx.sess.err("--pretty flowgraph with -Z flowgraph-print annotations requires \
|
|
|
|
fn-like node id.");
|
|
|
|
return Ok(());
|
|
|
|
}
|
2016-10-25 18:27:14 -05:00
|
|
|
blocks::Code::FnLike(fn_like) => {
|
2016-04-20 17:29:49 -05:00
|
|
|
let (bccx, analysis_data) =
|
2017-01-06 13:54:24 -06:00
|
|
|
borrowck::build_borrowck_dataflow_data_for_fn(tcx, fn_like.body(), &cfg);
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
let lcfg = borrowck_dot::DataflowLabeller {
|
|
|
|
inner: lcfg,
|
2017-08-07 00:54:09 -05:00
|
|
|
variants,
|
2016-04-20 17:29:49 -05:00
|
|
|
borrowck_ctxt: &bccx,
|
|
|
|
analysis_data: &analysis_data,
|
|
|
|
};
|
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
fn expand_err_details(r: io::Result<()>) -> io::Result<()> {
|
|
|
|
r.map_err(|ioerr| {
|
|
|
|
io::Error::new(io::ErrorKind::Other,
|
2017-03-24 03:31:26 -05:00
|
|
|
format!("graphviz::render failed: {}", ioerr))
|
2016-04-20 17:29:49 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-04 22:20:55 -06:00
|
|
|
pub fn visit_crate(sess: &Session, krate: &mut ast::Crate, ppm: PpMode) {
|
2016-04-20 17:29:49 -05:00
|
|
|
if let PpmSource(PpmEveryBodyLoops) = ppm {
|
2018-12-08 13:30:23 -06:00
|
|
|
ReplaceBodyWithLoop::new(sess).visit_crate(krate);
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2019-07-05 16:12:11 -05:00
|
|
|
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
|
2018-12-08 13:30:23 -06:00
|
|
|
let src_name = source_name(input);
|
2019-07-05 16:12:11 -05:00
|
|
|
let src = String::clone(&sess.source_map()
|
2018-08-18 05:13:56 -05:00
|
|
|
.get_source_file(&src_name)
|
2016-10-19 20:38:43 -05:00
|
|
|
.unwrap()
|
|
|
|
.src
|
|
|
|
.as_ref()
|
2019-07-05 16:12:11 -05:00
|
|
|
.unwrap());
|
2016-04-20 17:29:49 -05:00
|
|
|
(src, src_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
|
|
|
|
match ofile {
|
|
|
|
None => print!("{}", String::from_utf8(out).unwrap()),
|
|
|
|
Some(p) => {
|
|
|
|
match File::create(p) {
|
|
|
|
Ok(mut w) => w.write_all(&out).unwrap(),
|
|
|
|
Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-06-24 11:42:21 -05:00
|
|
|
let mut out = String::new();
|
2016-04-20 17:29:49 -05:00
|
|
|
|
|
|
|
if let PpmSource(s) = ppm {
|
|
|
|
// Silently ignores an identified node.
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2017-06-25 10:54:09 -05:00
|
|
|
s.call_with_pp_support(sess, None, move |annotation| {
|
2018-10-12 07:36:10 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
pprust::print_crate(sess.source_map(),
|
|
|
|
&sess.parse_sess,
|
|
|
|
krate,
|
|
|
|
src_name,
|
2019-07-05 16:12:11 -05:00
|
|
|
src,
|
2019-06-24 11:42:21 -05:00
|
|
|
out,
|
2018-10-12 07:36:10 -05:00
|
|
|
annotation.pp_ann(),
|
|
|
|
false)
|
2019-06-24 13:15:11 -05:00
|
|
|
})
|
2016-04-20 17:29:49 -05:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
2019-06-24 11:42:21 -05:00
|
|
|
write_output(out.into_bytes(), ofile);
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
pub fn print_after_hir_lowering<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-08 13:30:23 -06:00
|
|
|
input: &Input,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
ppm: PpMode,
|
|
|
|
opt_uii: Option<UserIdentifiedItem>,
|
2019-06-11 16:11:55 -05:00
|
|
|
ofile: Option<&Path>,
|
|
|
|
) {
|
2016-04-20 17:29:49 -05:00
|
|
|
if ppm.needs_analysis() {
|
2018-12-08 13:30:23 -06:00
|
|
|
abort_on_err(print_with_analysis(
|
|
|
|
tcx,
|
|
|
|
ppm,
|
|
|
|
opt_uii,
|
|
|
|
ofile
|
|
|
|
), tcx.sess);
|
2016-04-20 17:29:49 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
let (src, src_name) = get_source(input, tcx.sess);
|
2016-04-20 17:29:49 -05:00
|
|
|
|
2019-06-24 11:42:21 -05:00
|
|
|
let mut out = String::new();
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
match (ppm, opt_uii) {
|
2016-10-19 20:38:43 -05:00
|
|
|
(PpmSource(s), _) => {
|
|
|
|
// Silently ignores an identified node.
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2019-07-05 16:12:11 -05:00
|
|
|
let src = src.clone();
|
2018-12-08 13:30:23 -06:00
|
|
|
s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| {
|
2016-10-19 20:38:43 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-08-18 05:14:09 -05:00
|
|
|
pprust::print_crate(sess.source_map(),
|
2017-01-16 19:14:53 -06:00
|
|
|
&sess.parse_sess,
|
2016-10-19 20:38:43 -05:00
|
|
|
krate,
|
2017-12-14 01:09:19 -06:00
|
|
|
src_name,
|
2019-07-05 16:12:11 -05:00
|
|
|
src,
|
2019-06-24 11:42:21 -05:00
|
|
|
out,
|
2016-10-19 20:38:43 -05:00
|
|
|
annotation.pp_ann(),
|
|
|
|
true)
|
|
|
|
})
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2016-10-19 20:38:43 -05:00
|
|
|
(PpmHir(s), None) => {
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2019-07-05 16:12:11 -05:00
|
|
|
let src = src.clone();
|
2018-12-08 13:30:23 -06:00
|
|
|
s.call_with_pp_support_hir(tcx, move |annotation, krate| {
|
2016-10-19 20:38:43 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-08-18 05:14:09 -05:00
|
|
|
pprust_hir::print_crate(sess.source_map(),
|
2017-01-16 19:14:53 -06:00
|
|
|
&sess.parse_sess,
|
2016-10-19 20:38:43 -05:00
|
|
|
krate,
|
2017-12-14 01:09:19 -06:00
|
|
|
src_name,
|
2019-07-05 16:12:11 -05:00
|
|
|
src,
|
2019-06-24 11:42:21 -05:00
|
|
|
out,
|
2019-05-09 11:04:04 -05:00
|
|
|
annotation.pp_ann())
|
2016-10-19 20:38:43 -05:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2017-11-02 07:58:13 -05:00
|
|
|
(PpmHirTree(s), None) => {
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2018-12-08 13:30:23 -06:00
|
|
|
s.call_with_pp_support_hir(tcx, move |_annotation, krate| {
|
2017-11-02 07:58:13 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
2019-06-24 11:42:21 -05:00
|
|
|
*out = format!("{:#?}", krate);
|
|
|
|
});
|
2017-11-02 07:58:13 -05:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:38:43 -05:00
|
|
|
(PpmHir(s), Some(uii)) => {
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2019-07-05 16:12:11 -05:00
|
|
|
let src = src.clone();
|
2018-12-08 13:30:23 -06:00
|
|
|
s.call_with_pp_support_hir(tcx, move |annotation, _| {
|
2016-10-19 20:38:43 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-01-14 21:11:40 -06:00
|
|
|
let hir_map = annotation.hir_map().expect("-Z unpretty missing HIR map");
|
2018-08-18 05:14:09 -05:00
|
|
|
let mut pp_state = pprust_hir::State::new_from_input(sess.source_map(),
|
2017-01-16 19:14:53 -06:00
|
|
|
&sess.parse_sess,
|
2017-12-14 01:09:19 -06:00
|
|
|
src_name,
|
2019-07-05 16:12:11 -05:00
|
|
|
src,
|
2019-06-24 11:42:21 -05:00
|
|
|
out,
|
2019-05-09 11:04:04 -05:00
|
|
|
annotation.pp_ann());
|
2017-01-25 19:21:50 -06:00
|
|
|
for node_id in uii.all_matching_node_ids(hir_map) {
|
2019-06-20 03:34:57 -05:00
|
|
|
let hir_id = tcx.hir().node_to_hir_id(node_id);
|
2019-06-20 03:39:19 -05:00
|
|
|
let node = hir_map.get(hir_id);
|
2019-06-24 13:15:11 -05:00
|
|
|
pp_state.print_node(node);
|
|
|
|
pp_state.s.space();
|
2019-06-24 03:03:37 -05:00
|
|
|
let path = annotation.node_path(hir_id)
|
2018-01-14 21:11:40 -06:00
|
|
|
.expect("-Z unpretty missing node paths");
|
2019-06-24 13:15:11 -05:00
|
|
|
pp_state.synth_comment(path);
|
|
|
|
pp_state.s.hardbreak();
|
2016-10-19 20:38:43 -05:00
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
pp_state.s.eof();
|
2016-10-19 20:38:43 -05:00
|
|
|
})
|
|
|
|
}
|
2017-11-02 07:58:13 -05:00
|
|
|
|
|
|
|
(PpmHirTree(s), Some(uii)) => {
|
2019-06-24 11:42:21 -05:00
|
|
|
let out = &mut out;
|
2018-12-08 13:30:23 -06:00
|
|
|
s.call_with_pp_support_hir(tcx, move |_annotation, _krate| {
|
2017-11-02 07:58:13 -05:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
2018-12-08 13:30:23 -06:00
|
|
|
for node_id in uii.all_matching_node_ids(tcx.hir()) {
|
2019-06-20 03:34:57 -05:00
|
|
|
let hir_id = tcx.hir().node_to_hir_id(node_id);
|
2019-06-20 03:39:19 -05:00
|
|
|
let node = tcx.hir().get(hir_id);
|
2019-06-24 11:42:21 -05:00
|
|
|
out.push_str(&format!("{:#?}", node));
|
2017-11-02 07:58:13 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:38:43 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2016-04-20 17:29:49 -05:00
|
|
|
|
2019-06-24 11:42:21 -05:00
|
|
|
write_output(out.into_bytes(), ofile);
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// In an ideal world, this would be a public function called by the driver after
|
2019-01-07 04:46:44 -06:00
|
|
|
// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
|
2016-04-20 17:29:49 -05:00
|
|
|
// with a different callback than the standard driver, so that isn't easy.
|
|
|
|
// Instead, we call that function ourselves.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn print_with_analysis(
|
|
|
|
tcx: TyCtxt<'_>,
|
2018-12-08 13:30:23 -06:00
|
|
|
ppm: PpMode,
|
|
|
|
uii: Option<UserIdentifiedItem>,
|
2019-06-11 16:11:55 -05:00
|
|
|
ofile: Option<&Path>,
|
2018-12-08 13:30:23 -06:00
|
|
|
) -> Result<(), ErrorReported> {
|
2016-04-20 17:29:49 -05:00
|
|
|
let nodeid = if let Some(uii) = uii {
|
|
|
|
debug!("pretty printing for {:?}", uii);
|
2018-12-08 13:30:23 -06:00
|
|
|
Some(uii.to_one_node_id("-Z unpretty", tcx.sess, tcx.hir()))
|
2016-04-20 17:29:49 -05:00
|
|
|
} else {
|
|
|
|
debug!("pretty printing for whole crate");
|
|
|
|
None
|
|
|
|
};
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
let mut out = Vec::new();
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
tcx.analysis(LOCAL_CRATE)?;
|
|
|
|
|
|
|
|
let mut print = || match ppm {
|
|
|
|
PpmMir | PpmMirCFG => {
|
|
|
|
if let Some(nodeid) = nodeid {
|
2019-06-27 04:16:59 -05:00
|
|
|
let def_id = tcx.hir().local_def_id_from_node_id(nodeid);
|
2018-12-08 13:30:23 -06:00
|
|
|
match ppm {
|
|
|
|
PpmMir => write_mir_pretty(tcx, Some(def_id), &mut out),
|
|
|
|
PpmMirCFG => write_mir_graphviz(tcx, Some(def_id), &mut out),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}?;
|
|
|
|
} else {
|
|
|
|
match ppm {
|
|
|
|
PpmMir => write_mir_pretty(tcx, None, &mut out),
|
|
|
|
PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}?;
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
2018-12-08 13:30:23 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
PpmFlowGraph(mode) => {
|
|
|
|
let nodeid =
|
|
|
|
nodeid.expect("`pretty flowgraph=..` needs NodeId (int) or unique path \
|
|
|
|
suffix (b::c::d)");
|
2019-06-24 02:55:11 -05:00
|
|
|
let hir_id = tcx.hir().node_to_hir_id(nodeid);
|
2019-06-24 02:58:49 -05:00
|
|
|
let node = tcx.hir().find(hir_id).unwrap_or_else(|| {
|
2018-12-08 13:30:23 -06:00
|
|
|
tcx.sess.fatal(&format!("--pretty flowgraph couldn't find id: {}", nodeid))
|
|
|
|
});
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2019-06-20 03:15:07 -05:00
|
|
|
match blocks::Code::from_node(&tcx.hir(), hir_id) {
|
2018-12-08 13:30:23 -06:00
|
|
|
Some(code) => {
|
|
|
|
let variants = gather_flowgraph_variants(tcx.sess);
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
print_flowgraph(variants, tcx, code, mode, out)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let message = format!("--pretty=flowgraph needs block, fn, or method; \
|
|
|
|
got {:?}",
|
|
|
|
node);
|
2016-04-20 17:29:49 -05:00
|
|
|
|
2019-06-14 05:28:47 -05:00
|
|
|
let hir_id = tcx.hir().node_to_hir_id(nodeid);
|
2019-06-14 11:58:55 -05:00
|
|
|
tcx.sess.span_fatal(tcx.hir().span(hir_id), &message)
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 13:30:23 -06:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
print().unwrap();
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2016-04-20 17:29:49 -05:00
|
|
|
write_output(out, ofile);
|
2018-12-08 13:30:23 -06:00
|
|
|
|
|
|
|
Ok(())
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|