2014-08-11 05:59:35 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! The various pretty print routines.
|
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::UserIdentifiedItem::*;
|
|
|
|
pub use self::PpSourceMode::*;
|
|
|
|
pub use self::PpMode::*;
|
|
|
|
use self::NodesMatchingUII::*;
|
|
|
|
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc_trans::back::link;
|
|
|
|
|
|
|
|
use driver;
|
|
|
|
|
2014-12-04 18:25:29 -06:00
|
|
|
use rustc::middle::ty;
|
2014-11-27 08:57:47 -06:00
|
|
|
use rustc::middle::cfg;
|
|
|
|
use rustc::middle::cfg::graphviz::LabelledCFG;
|
|
|
|
use rustc::session::Session;
|
2014-12-09 03:55:49 -06:00
|
|
|
use rustc::session::config::Input;
|
2014-12-05 13:17:35 -06:00
|
|
|
use rustc_borrowck as borrowck;
|
|
|
|
use rustc_borrowck::graphviz as borrowck_dot;
|
2015-01-10 20:03:34 -06:00
|
|
|
use rustc_resolve as resolve;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
use syntax::ast;
|
2014-12-17 06:37:26 -06:00
|
|
|
use syntax::codemap;
|
2015-01-03 21:42:21 -06:00
|
|
|
use syntax::fold::{self, Folder};
|
2014-08-11 05:59:35 -05:00
|
|
|
use syntax::print::{pp, pprust};
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::print::pprust::PrintState;
|
2014-12-17 06:37:26 -06:00
|
|
|
use syntax::ptr::P;
|
2015-03-15 20:35:25 -05:00
|
|
|
use syntax::util::small_vector::SmallVector;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
use graphviz as dot;
|
|
|
|
|
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;
|
2015-02-26 23:00:43 -06:00
|
|
|
use std::path::PathBuf;
|
2014-11-14 22:52:00 -06:00
|
|
|
use std::str::FromStr;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
use rustc::front::map as hir_map;
|
|
|
|
use rustc::front::map::{blocks, NodePrinter};
|
|
|
|
use rustc_front::hir;
|
2015-09-24 23:03:28 -05:00
|
|
|
use rustc_front::lowering::{lower_crate, LoweringContext};
|
2015-07-31 02:04:06 -05:00
|
|
|
use rustc_front::print::pprust as pprust_hir;
|
|
|
|
|
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
|
2015-07-29 18:33:38 -05:00
|
|
|
/// is mostly for use in the --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),
|
2015-01-12 15:42:12 -06:00
|
|
|
PpmFlowGraph(PpFlowGraphMode),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-17 08:13:38 -06:00
|
|
|
pub fn parse_pretty(sess: &Session,
|
|
|
|
name: &str,
|
|
|
|
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) {
|
|
|
|
("normal", _) => PpmSource(PpmNormal),
|
2015-07-31 02:04:06 -05:00
|
|
|
("identified", _) => PpmSource(PpmIdentified),
|
2014-12-17 08:13:38 -06:00
|
|
|
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
|
|
|
|
("expanded", _) => PpmSource(PpmExpanded),
|
|
|
|
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
|
|
|
|
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
|
2015-07-31 02:04:06 -05:00
|
|
|
("hir", true) => PpmHir(PpmNormal),
|
2015-10-22 15:05:51 -05:00
|
|
|
("hir,identified", true) => PpmHir(PpmIdentified),
|
2015-07-31 02:04:06 -05:00
|
|
|
("hir,typed", true) => PpmHir(PpmTyped),
|
2015-01-12 15:42:12 -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-02-01 20:53:25 -06:00
|
|
|
sess.fatal(&format!(
|
2015-07-29 18:33:38 -05:00
|
|
|
"argument to `unpretty` must be one of `normal`, \
|
2015-07-31 02:04:06 -05:00
|
|
|
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, `identified`, \
|
|
|
|
`expanded,identified`, `everybody_loops`, `hir`, \
|
|
|
|
`hir,identified`, or `hir,typed`; got {}", name));
|
2014-12-17 08:13:38 -06:00
|
|
|
} else {
|
2015-02-01 20:53:25 -06:00
|
|
|
sess.fatal(&format!(
|
2015-07-31 02:04:06 -05:00
|
|
|
"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`.
|
2014-12-09 15:32:45 -06:00
|
|
|
fn call_with_pp_support<'tcx, A, B, F>(&self,
|
2015-09-27 21:00:15 -05:00
|
|
|
sess: &'tcx Session,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map: Option<hir_map::Map<'tcx>>,
|
2014-12-09 15:32:45 -06:00
|
|
|
payload: B,
|
|
|
|
f: F) -> A where
|
|
|
|
F: FnOnce(&PrinterSupport, B) -> A,
|
|
|
|
{
|
2014-08-11 06:12:23 -05:00
|
|
|
match *self {
|
2014-12-17 06:37:26 -06:00
|
|
|
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
|
2014-08-11 06:12:23 -05:00
|
|
|
let annotation = NoAnn { sess: sess, ast_map: ast_map };
|
|
|
|
f(&annotation, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified | PpmExpandedIdentified => {
|
|
|
|
let annotation = IdentifiedAnnotation { sess: sess, ast_map: ast_map };
|
|
|
|
f(&annotation, payload)
|
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
PpmExpandedHygiene => {
|
|
|
|
let annotation = HygieneAnnotation { sess: sess, ast_map: ast_map };
|
|
|
|
f(&annotation, payload)
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
_ => panic!("Should use call_with_pp_support_hir"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
|
2015-09-27 21:00:15 -05:00
|
|
|
sess: &'tcx Session,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map: &hir_map::Map<'tcx>,
|
|
|
|
arenas: &'tcx ty::CtxtArenas<'tcx>,
|
2015-10-19 14:54:19 -05:00
|
|
|
id: &str,
|
2015-07-31 02:04:06 -05:00
|
|
|
payload: B,
|
|
|
|
f: F) -> A where
|
|
|
|
F: FnOnce(&HirPrinterSupport, B, &hir::Crate) -> A,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
PpmNormal => {
|
|
|
|
let annotation = NoAnn { sess: sess, ast_map: Some(ast_map.clone()) };
|
|
|
|
f(&annotation, payload, &ast_map.forest.krate)
|
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified => {
|
|
|
|
let annotation = IdentifiedAnnotation {
|
|
|
|
sess: sess,
|
|
|
|
ast_map: Some(ast_map.clone())
|
|
|
|
};
|
|
|
|
f(&annotation, payload, &ast_map.forest.krate)
|
|
|
|
}
|
2014-08-11 06:12:23 -05:00
|
|
|
PpmTyped => {
|
2015-06-13 20:50:23 -05:00
|
|
|
driver::phase_3_run_analysis_passes(sess,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map.clone(),
|
2015-06-13 20:50:23 -05:00
|
|
|
arenas,
|
|
|
|
id,
|
|
|
|
resolve::MakeGlobMap::No,
|
2015-10-21 16:20:00 -05:00
|
|
|
|tcx, _, _| {
|
2015-06-13 20:50:23 -05:00
|
|
|
let annotation = TypedAnnotation { tcx: tcx };
|
2015-07-31 02:04:06 -05:00
|
|
|
f(&annotation, payload, &ast_map.forest.krate)
|
2015-09-27 21:00:15 -05: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
|
|
|
}
|
|
|
|
|
2014-12-15 20:11:09 -06:00
|
|
|
trait PrinterSupport<'ast>: 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.
|
|
|
|
fn sess<'a>(&'a self) -> &'a Session;
|
|
|
|
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to an
|
2015-07-31 02:04:06 -05:00
|
|
|
/// `hir_map::Map` from a value that now owns it.
|
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>>;
|
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.)
|
2014-12-15 20:11:09 -06:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn;
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
trait HirPrinterSupport<'ast>: pprust_hir::PpAnn {
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
|
|
|
fn sess<'a>(&'a self) -> &'a Session;
|
|
|
|
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to an
|
|
|
|
/// `hir_map::Map` from a value that now owns it.
|
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>>;
|
|
|
|
|
|
|
|
/// 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.)
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn;
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
struct NoAnn<'ast> {
|
|
|
|
sess: &'ast Session,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map: Option<hir_map::Map<'ast>>
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> PrinterSupport<'ast> for NoAnn<'ast> {
|
2015-09-27 21:00:15 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { self.sess }
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
|
2014-08-11 05:59:35 -05:00
|
|
|
self.ast_map.as_ref()
|
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> HirPrinterSupport<'ast> for NoAnn<'ast> {
|
2015-09-27 21:00:15 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { self.sess }
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
|
|
|
|
self.ast_map.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn { self }
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> pprust::PpAnn for NoAnn<'ast> {}
|
|
|
|
impl<'ast> pprust_hir::PpAnn for NoAnn<'ast> {}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
struct IdentifiedAnnotation<'ast> {
|
|
|
|
sess: &'ast Session,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map: Option<hir_map::Map<'ast>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> PrinterSupport<'ast> for IdentifiedAnnotation<'ast> {
|
2015-09-27 21:00:15 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { self.sess }
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
|
2014-08-11 05:59:35 -05:00
|
|
|
self.ast_map.as_ref()
|
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> pprust::PpAnn for IdentifiedAnnotation<'ast> {
|
2014-08-11 05:59:35 -05:00
|
|
|
fn pre(&self,
|
|
|
|
s: &mut pprust::State,
|
2015-02-26 23:00:43 -06:00
|
|
|
node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
|
|
|
pprust::NodeExpr(_) => s.popen(),
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn post(&self,
|
|
|
|
s: &mut pprust::State,
|
2015-02-26 23:00:43 -06:00
|
|
|
node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2014-08-11 07:01:37 -05:00
|
|
|
pprust::NodeIdent(_) | pprust::NodeName(_) => Ok(()),
|
|
|
|
|
2014-08-11 05:59:35 -05:00
|
|
|
pprust::NodeItem(item) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
2015-03-23 20:52:55 -05:00
|
|
|
pprust::NodeSubItem(id) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
pprust::NodeBlock(blk) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
|
|
|
pprust::NodeExpr(expr) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
try!(s.synth_comment(expr.id.to_string()));
|
|
|
|
s.pclose()
|
|
|
|
}
|
|
|
|
pprust::NodePat(pat) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(format!("pat {}", pat.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> HirPrinterSupport<'ast> for IdentifiedAnnotation<'ast> {
|
2015-09-27 21:00:15 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { self.sess }
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
|
|
|
|
self.ast_map.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn { self }
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> pprust_hir::PpAnn for IdentifiedAnnotation<'ast> {
|
2015-07-31 02:04:06 -05:00
|
|
|
fn pre(&self,
|
|
|
|
s: &mut pprust_hir::State,
|
|
|
|
node: pprust_hir::AnnNode) -> io::Result<()> {
|
|
|
|
match node {
|
|
|
|
pprust_hir::NodeExpr(_) => s.popen(),
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn post(&self,
|
|
|
|
s: &mut pprust_hir::State,
|
|
|
|
node: pprust_hir::AnnNode) -> io::Result<()> {
|
|
|
|
match node {
|
2015-09-23 12:04:49 -05:00
|
|
|
pprust_hir::NodeName(_) => Ok(()),
|
2015-07-31 02:04:06 -05:00
|
|
|
pprust_hir::NodeItem(item) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
|
|
|
pprust_hir::NodeSubItem(id) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
|
|
|
pprust_hir::NodeBlock(blk) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
|
|
|
pprust_hir::NodeExpr(expr) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
try!(s.synth_comment(expr.id.to_string()));
|
|
|
|
s.pclose()
|
|
|
|
}
|
|
|
|
pprust_hir::NodePat(pat) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(format!("pat {}", pat.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
struct HygieneAnnotation<'ast> {
|
|
|
|
sess: &'ast Session,
|
2015-07-31 02:04:06 -05:00
|
|
|
ast_map: Option<hir_map::Map<'ast>>,
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> PrinterSupport<'ast> for HygieneAnnotation<'ast> {
|
2015-09-27 21:00:15 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { self.sess }
|
2014-08-11 07:01:37 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
|
2014-08-11 07:01:37 -05:00
|
|
|
self.ast_map.as_ref()
|
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn { self }
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2015-10-06 14:26:22 -05:00
|
|
|
impl<'ast> pprust::PpAnn for HygieneAnnotation<'ast> {
|
2014-08-11 07:01:37 -05:00
|
|
|
fn post(&self,
|
|
|
|
s: &mut pprust::State,
|
2015-02-26 23:00:43 -06:00
|
|
|
node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 07:01:37 -05:00
|
|
|
match node {
|
|
|
|
pprust::NodeIdent(&ast::Ident { name: ast::Name(nm), ctxt }) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
// FIXME #16420: this doesn't display the connections
|
|
|
|
// between syntax contexts
|
2015-09-24 15:05:02 -05:00
|
|
|
s.synth_comment(format!("{}#{}", nm, ctxt.0))
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
pprust::NodeName(&ast::Name(nm)) => {
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
s.synth_comment(nm.to_string())
|
|
|
|
}
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-13 20:50:23 -05:00
|
|
|
struct TypedAnnotation<'a, 'tcx: 'a> {
|
|
|
|
tcx: &'a ty::ctxt<'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> {
|
2015-06-13 17:49:28 -05:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session { &self.tcx.sess }
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'tcx>> {
|
2015-06-13 17:49:28 -05:00
|
|
|
Some(&self.tcx.map)
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn { self }
|
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> {
|
2014-08-11 05:59:35 -05:00
|
|
|
fn pre(&self,
|
2015-07-31 02:04:06 -05:00
|
|
|
s: &mut pprust_hir::State,
|
|
|
|
node: pprust_hir::AnnNode) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2015-07-31 02:04:06 -05:00
|
|
|
pprust_hir::NodeExpr(_) => s.popen(),
|
2014-08-11 05:59:35 -05:00
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn post(&self,
|
2015-07-31 02:04:06 -05:00
|
|
|
s: &mut pprust_hir::State,
|
|
|
|
node: pprust_hir::AnnNode) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2015-07-31 02:04:06 -05:00
|
|
|
pprust_hir::NodeExpr(expr) => {
|
2014-08-11 05:59:35 -05:00
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
try!(pp::word(&mut s.s, "as"));
|
|
|
|
try!(pp::space(&mut s.s));
|
|
|
|
try!(pp::word(&mut s.s,
|
2015-06-25 15:42:17 -05:00
|
|
|
&self.tcx.expr_ty(expr).to_string()));
|
2014-08-11 05:59:35 -05:00
|
|
|
s.pclose()
|
|
|
|
}
|
|
|
|
_ => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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, ()> {
|
|
|
|
Ok(s.parse().map(ItemViaNode).unwrap_or_else(|_| {
|
2015-02-19 07:36:58 -06:00
|
|
|
ItemViaPath(s.split("::").map(|s| s.to_string()).collect())
|
2015-01-28 00:52:32 -06:00
|
|
|
}))
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
enum NodesMatchingUII<'a, 'ast: 'a> {
|
2014-12-14 12:34:23 -06:00
|
|
|
NodesMatchingDirect(option::IntoIter<ast::NodeId>),
|
2015-07-31 02:04:06 -05:00
|
|
|
NodesMatchingSuffix(hir_map::NodesMatchingSuffix<'a, 'ast>),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-01-02 07:15:20 -06:00
|
|
|
impl<'a, 'ast> Iterator for NodesMatchingUII<'a, 'ast> {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
fn all_matching_node_ids<'a, 'ast>(&'a self, map: &'a hir_map::Map<'ast>)
|
2014-09-07 12:09:06 -05:00
|
|
|
-> NodesMatchingUII<'a, 'ast> {
|
2014-08-11 05:59:35 -05:00
|
|
|
match *self {
|
|
|
|
ItemViaNode(node_id) =>
|
2014-09-14 22:27:36 -05:00
|
|
|
NodesMatchingDirect(Some(node_id).into_iter()),
|
2014-08-11 05:59:35 -05:00
|
|
|
ItemViaPath(ref parts) =>
|
2015-02-18 13:48:57 -06:00
|
|
|
NodesMatchingSuffix(map.nodes_matching_suffix(&parts[..])),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -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 {
|
2014-08-11 05:59:35 -05:00
|
|
|
let message =
|
2014-11-17 13:29:38 -06:00
|
|
|
format!("{} needs NodeId (int) or unique \
|
|
|
|
path suffix (b::c::d); got {}, which {}",
|
2014-08-11 05:59:35 -05:00
|
|
|
user_option,
|
|
|
|
self.reconstructed_input(),
|
|
|
|
is_wrong_because);
|
2015-02-18 13:48:57 -06: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
|
|
|
|
match *ppm {
|
|
|
|
PpmSource(PpmNormal) |
|
2014-12-17 06:37:26 -06:00
|
|
|
PpmSource(PpmEveryBodyLoops) |
|
2014-08-11 05:59:35 -05:00
|
|
|
PpmSource(PpmIdentified) => opt_uii.is_some(),
|
|
|
|
|
|
|
|
PpmSource(PpmExpanded) |
|
|
|
|
PpmSource(PpmExpandedIdentified) |
|
2014-08-11 07:01:37 -05:00
|
|
|
PpmSource(PpmExpandedHygiene) |
|
2015-07-31 02:04:06 -05:00
|
|
|
PpmHir(_) |
|
|
|
|
PpmFlowGraph(_) => true,
|
|
|
|
PpmSource(PpmTyped) => panic!("invalid state"),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn needs_expansion(ppm: &PpMode) -> bool {
|
|
|
|
match *ppm {
|
|
|
|
PpmSource(PpmNormal) |
|
2014-12-17 06:37:26 -06:00
|
|
|
PpmSource(PpmEveryBodyLoops) |
|
2014-08-11 05:59:35 -05:00
|
|
|
PpmSource(PpmIdentified) => false,
|
|
|
|
|
|
|
|
PpmSource(PpmExpanded) |
|
|
|
|
PpmSource(PpmExpandedIdentified) |
|
2014-08-11 07:01:37 -05:00
|
|
|
PpmSource(PpmExpandedHygiene) |
|
2015-07-31 02:04:06 -05:00
|
|
|
PpmHir(_) |
|
|
|
|
PpmFlowGraph(_) => true,
|
|
|
|
PpmSource(PpmTyped) => panic!("invalid state"),
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:37:26 -06:00
|
|
|
struct ReplaceBodyWithLoop {
|
|
|
|
within_static_or_const: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReplaceBodyWithLoop {
|
|
|
|
fn new() -> ReplaceBodyWithLoop {
|
|
|
|
ReplaceBodyWithLoop { within_static_or_const: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fold::Folder for ReplaceBodyWithLoop {
|
|
|
|
fn fold_item_underscore(&mut self, i: ast::Item_) -> ast::Item_ {
|
|
|
|
match i {
|
|
|
|
ast::ItemStatic(..) | ast::ItemConst(..) => {
|
|
|
|
self.within_static_or_const = true;
|
|
|
|
let ret = fold::noop_fold_item_underscore(i, self);
|
|
|
|
self.within_static_or_const = false;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
fold::noop_fold_item_underscore(i, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 20:35:25 -05:00
|
|
|
fn fold_trait_item(&mut self, i: P<ast::TraitItem>) -> SmallVector<P<ast::TraitItem>> {
|
|
|
|
match i.node {
|
|
|
|
ast::ConstTraitItem(..) => {
|
|
|
|
self.within_static_or_const = true;
|
|
|
|
let ret = fold::noop_fold_trait_item(i, self);
|
|
|
|
self.within_static_or_const = false;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
_ => fold::noop_fold_trait_item(i, self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl_item(&mut self, i: P<ast::ImplItem>) -> SmallVector<P<ast::ImplItem>> {
|
|
|
|
match i.node {
|
|
|
|
ast::ConstImplItem(..) => {
|
|
|
|
self.within_static_or_const = true;
|
|
|
|
let ret = fold::noop_fold_impl_item(i, self);
|
|
|
|
self.within_static_or_const = false;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
_ => fold::noop_fold_impl_item(i, self),
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 06:37:26 -06:00
|
|
|
|
|
|
|
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
|
|
|
|
fn expr_to_block(rules: ast::BlockCheckMode,
|
|
|
|
e: Option<P<ast::Expr>>) -> P<ast::Block> {
|
|
|
|
P(ast::Block {
|
|
|
|
expr: e,
|
2014-12-23 13:34:26 -06:00
|
|
|
stmts: vec![], rules: rules,
|
2014-12-17 06:37:26 -06:00
|
|
|
id: ast::DUMMY_NODE_ID, span: codemap::DUMMY_SP,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.within_static_or_const {
|
|
|
|
|
|
|
|
let empty_block = expr_to_block(ast::DefaultBlock, None);
|
|
|
|
let loop_expr = P(ast::Expr {
|
|
|
|
node: ast::ExprLoop(empty_block, None),
|
|
|
|
id: ast::DUMMY_NODE_ID, span: codemap::DUMMY_SP
|
|
|
|
});
|
|
|
|
|
|
|
|
expr_to_block(b.rules, Some(loop_expr))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fold::noop_fold_block(b, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in general the pretty printer processes unexpanded code, so
|
|
|
|
// we override the default `fold_mac` method which panics.
|
2015-01-02 15:39:05 -06:00
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
2014-12-17 06:37:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 05:59:35 -05:00
|
|
|
pub fn pretty_print_input(sess: Session,
|
|
|
|
cfg: ast::CrateConfig,
|
2014-11-27 06:21:26 -06:00
|
|
|
input: &Input,
|
2014-08-11 05:59:35 -05:00
|
|
|
ppm: PpMode,
|
|
|
|
opt_uii: Option<UserIdentifiedItem>,
|
2015-02-26 23:00:43 -06:00
|
|
|
ofile: Option<PathBuf>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
let krate = driver::phase_1_parse_input(&sess, cfg, input);
|
2014-12-17 06:37:26 -06:00
|
|
|
|
|
|
|
let krate = if let PpmSource(PpmEveryBodyLoops) = ppm {
|
|
|
|
let mut fold = ReplaceBodyWithLoop::new();
|
|
|
|
fold.fold_crate(krate)
|
|
|
|
} else {
|
|
|
|
krate
|
|
|
|
};
|
|
|
|
|
2015-02-01 20:53:25 -06:00
|
|
|
let id = link::find_crate_name(Some(&sess), &krate.attrs, input);
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
let is_expanded = needs_expansion(&ppm);
|
2014-09-07 12:09:06 -05:00
|
|
|
let compute_ast_map = needs_ast_map(&ppm, &opt_uii);
|
|
|
|
let krate = if compute_ast_map {
|
2015-02-18 13:48:57 -06:00
|
|
|
match driver::phase_2_configure_and_expand(&sess, krate, &id[..], None) {
|
2014-08-11 05:59:35 -05:00
|
|
|
None => return,
|
2015-07-31 02:04:06 -05:00
|
|
|
Some(k) => driver::assign_node_ids(&sess, k)
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
krate
|
|
|
|
};
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
// There is some twisted, god-forsaken tangle of lifetimes here which makes
|
|
|
|
// the ordering of stuff super-finicky.
|
|
|
|
let mut hir_forest;
|
2015-10-05 21:31:43 -05:00
|
|
|
let lcx = LoweringContext::new(&sess, Some(&krate));
|
2014-12-04 18:25:29 -06:00
|
|
|
let arenas = ty::CtxtArenas::new();
|
2015-07-31 02:04:06 -05:00
|
|
|
let ast_map = if compute_ast_map {
|
2015-09-24 23:03:28 -05:00
|
|
|
hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate));
|
2015-07-31 02:04:06 -05:00
|
|
|
let map = driver::make_map(&sess, &mut hir_forest);
|
|
|
|
Some(map)
|
2014-08-11 05:59:35 -05:00
|
|
|
} else {
|
2015-07-31 02:04:06 -05:00
|
|
|
None
|
2014-08-11 05:59:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let src_name = driver::source_name(input);
|
2015-02-18 13:48:57 -06:00
|
|
|
let src = sess.codemap().get_filemap(&src_name[..])
|
2015-02-11 11:29:49 -06:00
|
|
|
.src
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec();
|
2015-02-26 23:00:43 -06:00
|
|
|
let mut rdr = &src[..];
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-02-26 23:00:43 -06:00
|
|
|
let mut out = Vec::new();
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
match (ppm, opt_uii) {
|
2015-07-31 02:04:06 -05:00
|
|
|
(PpmSource(s), _) => {
|
|
|
|
// Silently ignores an identified node.
|
2015-02-26 23:00:43 -06:00
|
|
|
let out: &mut Write = &mut out;
|
2014-08-11 05:59:35 -05:00
|
|
|
s.call_with_pp_support(
|
2015-09-27 21:00:15 -05:00
|
|
|
&sess, ast_map, box out, |annotation, out| {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
2014-08-11 05:59:35 -05:00
|
|
|
let sess = annotation.sess();
|
|
|
|
pprust::print_crate(sess.codemap(),
|
|
|
|
sess.diagnostic(),
|
2015-07-31 02:04:06 -05:00
|
|
|
&krate,
|
2014-08-11 05:59:35 -05:00
|
|
|
src_name.to_string(),
|
|
|
|
&mut rdr,
|
|
|
|
out,
|
|
|
|
annotation.pp_ann(),
|
|
|
|
is_expanded)
|
2015-02-26 23:00:43 -06:00
|
|
|
})
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
(PpmHir(s), None) => {
|
2015-02-26 23:00:43 -06:00
|
|
|
let out: &mut Write = &mut out;
|
2015-07-31 02:04:06 -05:00
|
|
|
s.call_with_pp_support_hir(
|
2015-10-19 14:54:19 -05:00
|
|
|
&sess, &ast_map.unwrap(), &arenas, &id, box out, |annotation, out, krate| {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
2014-08-11 05:59:35 -05:00
|
|
|
let sess = annotation.sess();
|
2015-07-31 02:04:06 -05:00
|
|
|
pprust_hir::print_crate(sess.codemap(),
|
|
|
|
sess.diagnostic(),
|
|
|
|
krate,
|
|
|
|
src_name.to_string(),
|
|
|
|
&mut rdr,
|
|
|
|
out,
|
|
|
|
annotation.pp_ann(),
|
|
|
|
is_expanded)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
(PpmHir(s), Some(uii)) => {
|
|
|
|
let out: &mut Write = &mut out;
|
2015-09-27 21:00:15 -05:00
|
|
|
s.call_with_pp_support_hir(&sess,
|
2015-07-31 02:04:06 -05:00
|
|
|
&ast_map.unwrap(),
|
|
|
|
&arenas,
|
2015-10-19 14:54:19 -05:00
|
|
|
&id,
|
2015-07-31 02:04:06 -05:00
|
|
|
(out,uii),
|
|
|
|
|annotation, (out,uii), _| {
|
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
let ast_map = annotation.ast_map().expect("--pretty missing ast_map");
|
|
|
|
let mut pp_state =
|
|
|
|
pprust_hir::State::new_from_input(sess.codemap(),
|
2014-08-11 05:59:35 -05:00
|
|
|
sess.diagnostic(),
|
|
|
|
src_name.to_string(),
|
|
|
|
&mut rdr,
|
2015-02-26 23:00:43 -06:00
|
|
|
box out,
|
2014-08-11 05:59:35 -05:00
|
|
|
annotation.pp_ann(),
|
2015-07-31 02:04:06 -05:00
|
|
|
true);
|
|
|
|
for node_id in uii.all_matching_node_ids(ast_map) {
|
|
|
|
let node = ast_map.get(node_id);
|
|
|
|
try!(pp_state.print_node(&node));
|
|
|
|
try!(pp::space(&mut pp_state.s));
|
|
|
|
try!(pp_state.synth_comment(ast_map.path_to_string(node_id)));
|
|
|
|
try!(pp::hardbreak(&mut pp_state.s));
|
|
|
|
}
|
|
|
|
pp::eof(&mut pp_state.s)
|
|
|
|
})
|
2015-02-26 23:00:43 -06:00
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2015-01-12 15:42:12 -06:00
|
|
|
(PpmFlowGraph(mode), opt_uii) => {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("pretty printing flow graph for {:?}", opt_uii);
|
2014-08-11 05:59:35 -05:00
|
|
|
let uii = opt_uii.unwrap_or_else(|| {
|
2015-01-07 10:58:31 -06:00
|
|
|
sess.fatal(&format!("`pretty flowgraph=..` needs NodeId (int) or
|
2015-02-20 13:08:14 -06:00
|
|
|
unique path suffix (b::c::d)"))
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
let ast_map = ast_map.expect("--pretty flowgraph missing ast_map");
|
|
|
|
let nodeid = uii.to_one_node_id("--pretty", &sess, &ast_map);
|
|
|
|
|
|
|
|
let node = ast_map.find(nodeid).unwrap_or_else(|| {
|
2015-01-07 10:58:31 -06:00
|
|
|
sess.fatal(&format!("--pretty flowgraph couldn't find id: {}",
|
2015-02-20 13:08:14 -06:00
|
|
|
nodeid))
|
2014-08-11 05:59:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let code = blocks::Code::from_node(node);
|
2015-03-11 17:24:14 -05:00
|
|
|
let out: &mut Write = &mut out;
|
2014-08-11 05:59:35 -05:00
|
|
|
match code {
|
|
|
|
Some(code) => {
|
|
|
|
let variants = gather_flowgraph_variants(&sess);
|
2015-09-27 21:00:15 -05:00
|
|
|
driver::phase_3_run_analysis_passes(&sess,
|
2015-06-13 20:50:23 -05:00
|
|
|
ast_map,
|
|
|
|
&arenas,
|
2015-10-19 14:54:19 -05:00
|
|
|
&id,
|
2015-06-13 20:50:23 -05:00
|
|
|
resolve::MakeGlobMap::No,
|
2015-10-21 16:20:00 -05:00
|
|
|
|tcx, _, _| {
|
2015-06-13 20:50:23 -05:00
|
|
|
print_flowgraph(variants, tcx, code, mode, out)
|
2015-09-27 21:00:15 -05:00
|
|
|
})
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let message = format!("--pretty=flowgraph needs \
|
2014-12-20 02:09:35 -06:00
|
|
|
block, fn, or method; got {:?}",
|
2014-08-11 05:59:35 -05:00
|
|
|
node);
|
|
|
|
|
|
|
|
// point to what was found, if there's an
|
|
|
|
// accessible span.
|
|
|
|
match ast_map.opt_span(nodeid) {
|
2015-02-18 13:48:57 -06:00
|
|
|
Some(sp) => sess.span_fatal(sp, &message[..]),
|
|
|
|
None => sess.fatal(&message[..])
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 23:00:43 -06:00
|
|
|
}.unwrap();
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn print_flowgraph<W: Write>(variants: Vec<borrowck_dot::Variant>,
|
2015-06-13 17:49:28 -05:00
|
|
|
tcx: &ty::ctxt,
|
2015-03-11 17:24:14 -05:00
|
|
|
code: blocks::Code,
|
|
|
|
mode: PpFlowGraphMode,
|
|
|
|
mut out: W) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
let cfg = match code {
|
2015-06-13 17:49:28 -05:00
|
|
|
blocks::BlockCode(block) => cfg::CFG::new(tcx, &*block),
|
|
|
|
blocks::FnLikeCode(fn_like) => cfg::CFG::new(tcx, &*fn_like.body()),
|
2014-08-11 05:59:35 -05:00
|
|
|
};
|
2015-01-12 15:42:12 -06:00
|
|
|
let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
|
|
|
|
let lcfg = LabelledCFG {
|
2015-06-13 17:49:28 -05:00
|
|
|
ast_map: &tcx.map,
|
2015-01-12 15:42:12 -06:00
|
|
|
cfg: &cfg,
|
|
|
|
name: format!("node_{}", code.id()),
|
|
|
|
labelled_edges: labelled_edges,
|
|
|
|
};
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
match code {
|
2015-03-24 18:53:34 -05:00
|
|
|
_ if variants.is_empty() => {
|
2014-08-11 05:59:35 -05:00
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
|
|
|
}
|
|
|
|
blocks::BlockCode(_) => {
|
2015-06-13 17:49:28 -05:00
|
|
|
tcx.sess.err("--pretty flowgraph with -Z flowgraph-print \
|
|
|
|
annotations requires fn-like node id.");
|
2014-08-11 05:59:35 -05:00
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
blocks::FnLikeCode(fn_like) => {
|
2014-12-05 13:17:35 -06:00
|
|
|
let fn_parts = borrowck::FnPartsWithCFG::from_fn_like(&fn_like, &cfg);
|
2014-08-11 05:59:35 -05:00
|
|
|
let (bccx, analysis_data) =
|
2015-06-13 17:49:28 -05:00
|
|
|
borrowck::build_borrowck_dataflow_data_for_fn(tcx, fn_parts);
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
let lcfg = borrowck_dot::DataflowLabeller {
|
|
|
|
inner: lcfg,
|
|
|
|
variants: variants,
|
|
|
|
borrowck_ctxt: &bccx,
|
|
|
|
analysis_data: &analysis_data,
|
|
|
|
};
|
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:24:14 -05:00
|
|
|
fn expand_err_details(r: io::Result<()>) -> io::Result<()> {
|
2014-08-11 05:59:35 -05:00
|
|
|
r.map_err(|ioerr| {
|
2015-03-31 18:01:03 -05:00
|
|
|
io::Error::new(io::ErrorKind::Other,
|
|
|
|
&format!("graphviz::render failed: {}", ioerr)[..])
|
2014-08-11 05:59:35 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|