1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The various pretty print routines.
13 pub use self::UserIdentifiedItem::*;
14 pub use self::PpSourceMode::*;
15 pub use self::PpMode::*;
16 use self::NodesMatchingUII::*;
18 use {abort_on_err, driver};
20 use rustc::ty::{self, TyCtxt, GlobalArenas, Resolutions};
22 use rustc::cfg::graphviz::LabelledCFG;
23 use rustc::dep_graph::DepGraph;
24 use rustc::session::Session;
25 use rustc::session::config::Input;
26 use rustc_borrowck as borrowck;
27 use rustc_borrowck::graphviz as borrowck_dot;
29 use rustc_mir::pretty::write_mir_pretty;
30 use rustc_mir::graphviz::write_mir_graphviz;
32 use syntax::ast::{self, BlockCheckMode};
33 use syntax::fold::{self, Folder};
34 use syntax::print::{pp, pprust};
35 use syntax::print::pprust::PrintState;
37 use syntax::util::small_vector::SmallVector;
44 use std::io::{self, Write};
48 use std::str::FromStr;
50 use rustc::hir::map as hir_map;
51 use rustc::hir::map::blocks;
53 use rustc::hir::print as pprust_hir;
55 use arena::DroplessArena;
57 #[derive(Copy, Clone, PartialEq, Debug)]
58 pub enum PpSourceMode {
63 PpmExpandedIdentified,
68 #[derive(Copy, Clone, PartialEq, Debug)]
69 pub enum PpFlowGraphMode {
71 /// Drops the labels from the edges in the flowgraph output. This
72 /// is mostly for use in the --unpretty flowgraph run-make tests,
73 /// since the labels are largely uninteresting in those cases and
74 /// have become a pain to maintain.
77 #[derive(Copy, Clone, PartialEq, Debug)]
79 PpmSource(PpSourceMode),
81 PpmFlowGraph(PpFlowGraphMode),
87 pub fn needs_ast_map(&self, opt_uii: &Option<UserIdentifiedItem>) -> bool {
89 PpmSource(PpmNormal) |
90 PpmSource(PpmEveryBodyLoops) |
91 PpmSource(PpmIdentified) => opt_uii.is_some(),
93 PpmSource(PpmExpanded) |
94 PpmSource(PpmExpandedIdentified) |
95 PpmSource(PpmExpandedHygiene) |
99 PpmFlowGraph(_) => true,
100 PpmSource(PpmTyped) => panic!("invalid state"),
104 pub fn needs_analysis(&self) -> bool {
106 PpmMir | PpmMirCFG | PpmFlowGraph(_) => true,
112 pub fn parse_pretty(sess: &Session,
115 -> (PpMode, Option<UserIdentifiedItem>) {
116 let mut split = name.splitn(2, '=');
117 let first = split.next().unwrap();
118 let opt_second = split.next();
119 let first = match (first, extended) {
120 ("normal", _) => PpmSource(PpmNormal),
121 ("identified", _) => PpmSource(PpmIdentified),
122 ("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
123 ("expanded", _) => PpmSource(PpmExpanded),
124 ("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
125 ("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
126 ("hir", true) => PpmHir(PpmNormal),
127 ("hir,identified", true) => PpmHir(PpmIdentified),
128 ("hir,typed", true) => PpmHir(PpmTyped),
129 ("mir", true) => PpmMir,
130 ("mir-cfg", true) => PpmMirCFG,
131 ("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
132 ("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
135 sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \
136 `expanded`, `flowgraph[,unlabelled]=<nodeid>`, \
137 `identified`, `expanded,identified`, `everybody_loops`, \
138 `hir`, `hir,identified`, `hir,typed`, or `mir`; got {}",
141 sess.fatal(&format!("argument to `pretty` must be one of `normal`, `expanded`, \
142 `identified`, or `expanded,identified`; got {}",
147 let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
153 // This slightly awkward construction is to allow for each PpMode to
154 // choose whether it needs to do analyses (which can consume the
155 // Session) and then pass through the session (now attached to the
156 // analysis results) on to the chosen pretty-printer, along with the
159 // Note that since the `&PrinterSupport` is freshly constructed on each
160 // call, it would not make sense to try to attach the lifetime of `self`
161 // to the lifetime of the `&PrinterObject`.
163 // (The `use_once_payload` is working around the current lack of once
164 // functions in the compiler.)
167 /// Constructs a `PrinterSupport` object and passes it to `f`.
168 fn call_with_pp_support<'tcx, A, B, F>(&self,
170 hir_map: Option<&hir_map::Map<'tcx>>,
174 where F: FnOnce(&PrinterSupport, B) -> A
177 PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
178 let annotation = NoAnn {
180 hir_map: hir_map.map(|m| m.clone()),
182 f(&annotation, payload)
185 PpmIdentified | PpmExpandedIdentified => {
186 let annotation = IdentifiedAnnotation {
188 hir_map: hir_map.map(|m| m.clone()),
190 f(&annotation, payload)
192 PpmExpandedHygiene => {
193 let annotation = HygieneAnnotation {
196 f(&annotation, payload)
198 _ => panic!("Should use call_with_pp_support_hir"),
201 fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
203 hir_map: &hir_map::Map<'tcx>,
204 analysis: &ty::CrateAnalysis,
205 resolutions: &Resolutions,
206 arena: &'tcx DroplessArena,
207 arenas: &'tcx GlobalArenas<'tcx>,
212 where F: FnOnce(&HirPrinterSupport, B, &hir::Crate) -> A
216 let annotation = NoAnn {
218 hir_map: Some(hir_map.clone()),
220 f(&annotation, payload, hir_map.forest.krate())
224 let annotation = IdentifiedAnnotation {
226 hir_map: Some(hir_map.clone()),
228 f(&annotation, payload, hir_map.forest.krate())
231 abort_on_err(driver::phase_3_run_analysis_passes(sess,
239 let empty_tables = ty::TypeckTables::empty();
240 let annotation = TypedAnnotation {
242 tables: Cell::new(&empty_tables)
244 let _ignore = tcx.dep_graph.in_ignore();
245 f(&annotation, payload, hir_map.forest.krate())
249 _ => panic!("Should use call_with_pp_support"),
254 trait PrinterSupport: pprust::PpAnn {
255 /// Provides a uniform interface for re-extracting a reference to a
256 /// `Session` from a value that now owns it.
257 fn sess<'a>(&'a self) -> &'a Session;
259 /// Produces the pretty-print annotation object.
261 /// (Rust does not yet support upcasting from a trait object to
262 /// an object for one of its super-traits.)
263 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn;
266 trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
267 /// Provides a uniform interface for re-extracting a reference to a
268 /// `Session` from a value that now owns it.
269 fn sess<'a>(&'a self) -> &'a Session;
271 /// Provides a uniform interface for re-extracting a reference to an
272 /// `hir_map::Map` from a value that now owns it.
273 fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>>;
275 /// Produces the pretty-print annotation object.
277 /// (Rust does not yet support upcasting from a trait object to
278 /// an object for one of its super-traits.)
279 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn;
281 /// Computes an user-readable representation of a path, if possible.
282 fn node_path(&self, id: ast::NodeId) -> Option<String> {
283 self.hir_map().and_then(|map| map.def_path_from_id(id)).map(|path| {
286 .map(|elem| elem.data.to_string())
295 hir_map: Option<hir_map::Map<'hir>>,
298 impl<'hir> PrinterSupport for NoAnn<'hir> {
299 fn sess<'a>(&'a self) -> &'a Session {
303 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
308 impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
309 fn sess<'a>(&'a self) -> &'a Session {
313 fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
314 self.hir_map.as_ref()
317 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
322 impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
323 impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
324 fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
326 if let Some(ref map) = self.hir_map {
327 pprust_hir::PpAnn::nested(map, state, nested)
334 struct IdentifiedAnnotation<'hir> {
336 hir_map: Option<hir_map::Map<'hir>>,
339 impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
340 fn sess<'a>(&'a self) -> &'a Session {
344 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
349 impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
350 fn pre(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
352 pprust::NodeExpr(_) => s.popen(),
356 fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
358 pprust::NodeIdent(_) |
359 pprust::NodeName(_) => Ok(()),
361 pprust::NodeItem(item) => {
362 pp::space(&mut s.s)?;
363 s.synth_comment(item.id.to_string())
365 pprust::NodeSubItem(id) => {
366 pp::space(&mut s.s)?;
367 s.synth_comment(id.to_string())
369 pprust::NodeBlock(blk) => {
370 pp::space(&mut s.s)?;
371 s.synth_comment(format!("block {}", blk.id))
373 pprust::NodeExpr(expr) => {
374 pp::space(&mut s.s)?;
375 s.synth_comment(expr.id.to_string())?;
378 pprust::NodePat(pat) => {
379 pp::space(&mut s.s)?;
380 s.synth_comment(format!("pat {}", pat.id))
386 impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
387 fn sess<'a>(&'a self) -> &'a Session {
391 fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
392 self.hir_map.as_ref()
395 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
400 impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
401 fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
403 if let Some(ref map) = self.hir_map {
404 pprust_hir::PpAnn::nested(map, state, nested)
409 fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
411 pprust_hir::NodeExpr(_) => s.popen(),
415 fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
417 pprust_hir::NodeName(_) => Ok(()),
418 pprust_hir::NodeItem(item) => {
419 pp::space(&mut s.s)?;
420 s.synth_comment(item.id.to_string())
422 pprust_hir::NodeSubItem(id) => {
423 pp::space(&mut s.s)?;
424 s.synth_comment(id.to_string())
426 pprust_hir::NodeBlock(blk) => {
427 pp::space(&mut s.s)?;
428 s.synth_comment(format!("block {}", blk.id))
430 pprust_hir::NodeExpr(expr) => {
431 pp::space(&mut s.s)?;
432 s.synth_comment(expr.id.to_string())?;
435 pprust_hir::NodePat(pat) => {
436 pp::space(&mut s.s)?;
437 s.synth_comment(format!("pat {}", pat.id))
443 struct HygieneAnnotation<'a> {
447 impl<'a> PrinterSupport for HygieneAnnotation<'a> {
448 fn sess(&self) -> &Session {
452 fn pp_ann(&self) -> &pprust::PpAnn {
457 impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
458 fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
460 pprust::NodeIdent(&ast::Ident { name, ctxt }) => {
461 pp::space(&mut s.s)?;
462 // FIXME #16420: this doesn't display the connections
463 // between syntax contexts
464 s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt))
466 pprust::NodeName(&name) => {
467 pp::space(&mut s.s)?;
468 s.synth_comment(name.as_u32().to_string())
476 struct TypedAnnotation<'a, 'tcx: 'a> {
477 tcx: TyCtxt<'a, 'tcx, 'tcx>,
478 tables: Cell<&'a ty::TypeckTables<'tcx>>,
481 impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
482 fn sess<'a>(&'a self) -> &'a Session {
486 fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'tcx>> {
490 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
494 fn node_path(&self, id: ast::NodeId) -> Option<String> {
495 Some(self.tcx.node_path_str(id))
499 impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
500 fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
502 let old_tables = self.tables.get();
503 if let pprust_hir::Nested::Body(id) = nested {
504 self.tables.set(self.tcx.body_tables(id));
506 pprust_hir::PpAnn::nested(&self.tcx.hir, state, nested)?;
507 self.tables.set(old_tables);
510 fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
512 pprust_hir::NodeExpr(_) => s.popen(),
516 fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
518 pprust_hir::NodeExpr(expr) => {
519 pp::space(&mut s.s)?;
520 pp::word(&mut s.s, "as")?;
521 pp::space(&mut s.s)?;
522 pp::word(&mut s.s, &self.tables.get().expr_ty(expr).to_string())?;
530 fn gather_flowgraph_variants(sess: &Session) -> Vec<borrowck_dot::Variant> {
531 let print_loans = sess.opts.debugging_opts.flowgraph_print_loans;
532 let print_moves = sess.opts.debugging_opts.flowgraph_print_moves;
533 let print_assigns = sess.opts.debugging_opts.flowgraph_print_assigns;
534 let print_all = sess.opts.debugging_opts.flowgraph_print_all;
535 let mut variants = Vec::new();
536 if print_all || print_loans {
537 variants.push(borrowck_dot::Loans);
539 if print_all || print_moves {
540 variants.push(borrowck_dot::Moves);
542 if print_all || print_assigns {
543 variants.push(borrowck_dot::Assigns);
548 #[derive(Clone, Debug)]
549 pub enum UserIdentifiedItem {
550 ItemViaNode(ast::NodeId),
551 ItemViaPath(Vec<String>),
554 impl FromStr for UserIdentifiedItem {
556 fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
558 .map(ast::NodeId::new)
560 .unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
564 enum NodesMatchingUII<'a, 'hir: 'a> {
565 NodesMatchingDirect(option::IntoIter<ast::NodeId>),
566 NodesMatchingSuffix(hir_map::NodesMatchingSuffix<'a, 'hir>),
569 impl<'a, 'hir> Iterator for NodesMatchingUII<'a, 'hir> {
570 type Item = ast::NodeId;
572 fn next(&mut self) -> Option<ast::NodeId> {
574 &mut NodesMatchingDirect(ref mut iter) => iter.next(),
575 &mut NodesMatchingSuffix(ref mut iter) => iter.next(),
580 impl UserIdentifiedItem {
581 fn reconstructed_input(&self) -> String {
583 ItemViaNode(node_id) => node_id.to_string(),
584 ItemViaPath(ref parts) => parts.join("::"),
588 fn all_matching_node_ids<'a, 'hir>(&'a self,
589 map: &'a hir_map::Map<'hir>)
590 -> NodesMatchingUII<'a, 'hir> {
592 ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()),
593 ItemViaPath(ref parts) => NodesMatchingSuffix(map.nodes_matching_suffix(&parts[..])),
597 fn to_one_node_id(self, user_option: &str, sess: &Session, map: &hir_map::Map) -> ast::NodeId {
598 let fail_because = |is_wrong_because| -> ast::NodeId {
599 let message = format!("{} needs NodeId (int) or unique path suffix (b::c::d); got \
602 self.reconstructed_input(),
604 sess.fatal(&message[..])
607 let mut saw_node = ast::DUMMY_NODE_ID;
609 for node in self.all_matching_node_ids(map) {
613 fail_because("does not resolve uniquely");
617 fail_because("does not resolve to any item");
625 struct ReplaceBodyWithLoop {
626 within_static_or_const: bool,
629 impl ReplaceBodyWithLoop {
630 fn new() -> ReplaceBodyWithLoop {
631 ReplaceBodyWithLoop { within_static_or_const: false }
635 impl fold::Folder for ReplaceBodyWithLoop {
636 fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
638 ast::ItemKind::Static(..) |
639 ast::ItemKind::Const(..) => {
640 self.within_static_or_const = true;
641 let ret = fold::noop_fold_item_kind(i, self);
642 self.within_static_or_const = false;
645 _ => fold::noop_fold_item_kind(i, self),
649 fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector<ast::TraitItem> {
651 ast::TraitItemKind::Const(..) => {
652 self.within_static_or_const = true;
653 let ret = fold::noop_fold_trait_item(i, self);
654 self.within_static_or_const = false;
657 _ => fold::noop_fold_trait_item(i, self),
661 fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector<ast::ImplItem> {
663 ast::ImplItemKind::Const(..) => {
664 self.within_static_or_const = true;
665 let ret = fold::noop_fold_impl_item(i, self);
666 self.within_static_or_const = false;
669 _ => fold::noop_fold_impl_item(i, self),
673 fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
674 fn expr_to_block(rules: ast::BlockCheckMode, e: Option<P<ast::Expr>>) -> P<ast::Block> {
678 id: ast::DUMMY_NODE_ID,
680 node: ast::StmtKind::Expr(e),
686 id: ast::DUMMY_NODE_ID,
687 span: syntax_pos::DUMMY_SP,
691 if !self.within_static_or_const {
693 let empty_block = expr_to_block(BlockCheckMode::Default, None);
694 let loop_expr = P(ast::Expr {
695 node: ast::ExprKind::Loop(empty_block, None),
696 id: ast::DUMMY_NODE_ID,
697 span: syntax_pos::DUMMY_SP,
698 attrs: ast::ThinVec::new(),
701 expr_to_block(b.rules, Some(loop_expr))
704 fold::noop_fold_block(b, self)
708 // in general the pretty printer processes unexpanded code, so
709 // we override the default `fold_mac` method which panics.
710 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
711 fold::noop_fold_mac(mac, self)
715 fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
716 tcx: TyCtxt<'a, 'tcx, 'tcx>,
717 code: blocks::Code<'tcx>,
718 mode: PpFlowGraphMode,
721 let body_id = match code {
722 blocks::Code::Expr(expr) => {
723 // Find the function this expression is from.
724 let mut node_id = expr.id;
726 let node = tcx.hir.get(node_id);
727 if let Some(n) = hir::map::blocks::FnLikeNode::from_node(node) {
730 let parent = tcx.hir.get_parent_node(node_id);
731 assert!(node_id != parent);
735 blocks::Code::FnLike(fn_like) => fn_like.body(),
737 let body = tcx.hir.body(body_id);
738 let cfg = cfg::CFG::new(tcx, &body);
739 let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
740 let lcfg = LabelledCFG {
743 name: format!("node_{}", code.id()),
744 labelled_edges: labelled_edges,
748 _ if variants.is_empty() => {
749 let r = dot::render(&lcfg, &mut out);
750 return expand_err_details(r);
752 blocks::Code::Expr(_) => {
753 tcx.sess.err("--pretty flowgraph with -Z flowgraph-print annotations requires \
757 blocks::Code::FnLike(fn_like) => {
758 let (bccx, analysis_data) =
759 borrowck::build_borrowck_dataflow_data_for_fn(tcx, fn_like.body(), &cfg);
761 let lcfg = borrowck_dot::DataflowLabeller {
764 borrowck_ctxt: &bccx,
765 analysis_data: &analysis_data,
767 let r = dot::render(&lcfg, &mut out);
768 return expand_err_details(r);
772 fn expand_err_details(r: io::Result<()>) -> io::Result<()> {
774 io::Error::new(io::ErrorKind::Other,
775 &format!("graphviz::render failed: {}", ioerr)[..])
780 pub fn fold_crate(krate: ast::Crate, ppm: PpMode) -> ast::Crate {
781 if let PpmSource(PpmEveryBodyLoops) = ppm {
782 let mut fold = ReplaceBodyWithLoop::new();
783 fold.fold_crate(krate)
789 fn get_source(input: &Input, sess: &Session) -> (Vec<u8>, String) {
790 let src_name = driver::source_name(input);
791 let src = sess.codemap()
792 .get_filemap(&src_name)
802 fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
804 None => print!("{}", String::from_utf8(out).unwrap()),
806 match File::create(p) {
807 Ok(mut w) => w.write_all(&out).unwrap(),
808 Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
814 pub fn print_after_parsing(sess: &Session,
818 ofile: Option<&Path>) {
819 let dep_graph = DepGraph::new(false);
820 let _ignore = dep_graph.in_ignore();
822 let (src, src_name) = get_source(input, sess);
825 let mut out = Vec::new();
827 if let PpmSource(s) = ppm {
828 // Silently ignores an identified node.
829 let out: &mut Write = &mut out;
830 s.call_with_pp_support(sess, None, box out, |annotation, out| {
831 debug!("pretty printing source code {:?}", s);
832 let sess = annotation.sess();
833 pprust::print_crate(sess.codemap(),
836 src_name.to_string(),
847 write_output(out, ofile);
850 pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
851 hir_map: &hir_map::Map<'tcx>,
852 analysis: &ty::CrateAnalysis,
853 resolutions: &Resolutions,
858 arena: &'tcx DroplessArena,
859 arenas: &'tcx GlobalArenas<'tcx>,
860 opt_uii: Option<UserIdentifiedItem>,
861 ofile: Option<&Path>) {
862 let dep_graph = DepGraph::new(false);
863 let _ignore = dep_graph.in_ignore();
865 if ppm.needs_analysis() {
866 print_with_analysis(sess,
879 let (src, src_name) = get_source(input, sess);
881 let mut rdr = &src[..];
882 let mut out = Vec::new();
884 match (ppm, opt_uii) {
885 (PpmSource(s), _) => {
886 // Silently ignores an identified node.
887 let out: &mut Write = &mut out;
888 s.call_with_pp_support(sess, Some(hir_map), box out, |annotation, out| {
889 debug!("pretty printing source code {:?}", s);
890 let sess = annotation.sess();
891 pprust::print_crate(sess.codemap(),
894 src_name.to_string(),
902 (PpmHir(s), None) => {
903 let out: &mut Write = &mut out;
904 s.call_with_pp_support_hir(sess,
912 |annotation, out, krate| {
913 debug!("pretty printing source code {:?}", s);
914 let sess = annotation.sess();
915 pprust_hir::print_crate(sess.codemap(),
918 src_name.to_string(),
926 (PpmHir(s), Some(uii)) => {
927 let out: &mut Write = &mut out;
928 s.call_with_pp_support_hir(sess,
936 |annotation, (out, uii), _| {
937 debug!("pretty printing source code {:?}", s);
938 let sess = annotation.sess();
939 let hir_map = annotation.hir_map().expect("--unpretty missing HIR map");
940 let mut pp_state = pprust_hir::State::new_from_input(sess.codemap(),
942 src_name.to_string(),
947 for node_id in uii.all_matching_node_ids(hir_map) {
948 let node = hir_map.get(node_id);
949 pp_state.print_node(node)?;
950 pp::space(&mut pp_state.s)?;
951 let path = annotation.node_path(node_id)
952 .expect("--unpretty missing node paths");
953 pp_state.synth_comment(path)?;
954 pp::hardbreak(&mut pp_state.s)?;
956 pp::eof(&mut pp_state.s)
963 write_output(out, ofile);
966 // In an ideal world, this would be a public function called by the driver after
967 // analsysis is performed. However, we want to call `phase_3_run_analysis_passes`
968 // with a different callback than the standard driver, so that isn't easy.
969 // Instead, we call that function ourselves.
970 fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
971 hir_map: &hir_map::Map<'tcx>,
972 analysis: &ty::CrateAnalysis,
973 resolutions: &Resolutions,
975 arena: &'tcx DroplessArena,
976 arenas: &'tcx GlobalArenas<'tcx>,
978 uii: Option<UserIdentifiedItem>,
979 ofile: Option<&Path>) {
980 let nodeid = if let Some(uii) = uii {
981 debug!("pretty printing for {:?}", uii);
982 Some(uii.to_one_node_id("--unpretty", sess, &hir_map))
984 debug!("pretty printing for whole crate");
988 let mut out = Vec::new();
990 abort_on_err(driver::phase_3_run_analysis_passes(sess,
999 PpmMir | PpmMirCFG => {
1000 if let Some(nodeid) = nodeid {
1001 let def_id = tcx.hir.local_def_id(nodeid);
1003 PpmMir => write_mir_pretty(tcx, iter::once(def_id), &mut out),
1004 PpmMirCFG => write_mir_graphviz(tcx, iter::once(def_id), &mut out),
1005 _ => unreachable!(),
1010 write_mir_pretty(tcx,
1011 tcx.maps.mir.borrow().keys().into_iter(),
1015 write_mir_graphviz(tcx,
1016 tcx.maps.mir.borrow().keys().into_iter(),
1019 _ => unreachable!(),
1024 PpmFlowGraph(mode) => {
1026 nodeid.expect("`pretty flowgraph=..` needs NodeId (int) or unique path \
1028 let node = tcx.hir.find(nodeid).unwrap_or_else(|| {
1029 tcx.sess.fatal(&format!("--pretty flowgraph couldn't find id: {}", nodeid))
1032 match blocks::Code::from_node(&tcx.hir, nodeid) {
1034 let variants = gather_flowgraph_variants(tcx.sess);
1036 let out: &mut Write = &mut out;
1038 print_flowgraph(variants, tcx, code, mode, out)
1041 let message = format!("--pretty=flowgraph needs block, fn, or method; \
1045 tcx.sess.span_fatal(tcx.hir.span(nodeid), &message)
1049 _ => unreachable!(),
1055 write_output(out, ofile);