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 rustc_trans::back::link;
20 use {driver, abort_on_err};
22 use rustc::dep_graph::DepGraph;
23 use rustc::middle::ty;
24 use rustc::middle::cfg;
25 use rustc::middle::cfg::graphviz::LabelledCFG;
26 use rustc::session::Session;
27 use rustc::session::config::Input;
28 use rustc_borrowck as borrowck;
29 use rustc_borrowck::graphviz as borrowck_dot;
30 use rustc_resolve as resolve;
31 use rustc_metadata::cstore::CStore;
33 use syntax::ast::{self, BlockCheckMode};
35 use syntax::fold::{self, Folder};
36 use syntax::print::{pp, pprust};
37 use syntax::print::pprust::PrintState;
39 use syntax::util::small_vector::SmallVector;
44 use std::io::{self, Write};
46 use std::path::PathBuf;
47 use std::str::FromStr;
49 use rustc::front::map as hir_map;
50 use rustc::front::map::{blocks, NodePrinter};
52 use rustc_front::lowering::{lower_crate, LoweringContext};
53 use rustc_front::print::pprust as pprust_hir;
55 #[derive(Copy, Clone, PartialEq, Debug)]
56 pub enum PpSourceMode {
61 PpmExpandedIdentified,
66 #[derive(Copy, Clone, PartialEq, Debug)]
67 pub enum PpFlowGraphMode {
69 /// Drops the labels from the edges in the flowgraph output. This
70 /// is mostly for use in the --unpretty flowgraph run-make tests,
71 /// since the labels are largely uninteresting in those cases and
72 /// have become a pain to maintain.
75 #[derive(Copy, Clone, PartialEq, Debug)]
77 PpmSource(PpSourceMode),
79 PpmFlowGraph(PpFlowGraphMode),
82 pub fn parse_pretty(sess: &Session,
85 -> (PpMode, Option<UserIdentifiedItem>) {
86 let mut split = name.splitn(2, '=');
87 let first = split.next().unwrap();
88 let opt_second = split.next();
89 let first = match (first, extended) {
90 ("normal", _) => PpmSource(PpmNormal),
91 ("identified", _) => PpmSource(PpmIdentified),
92 ("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
93 ("expanded", _) => PpmSource(PpmExpanded),
94 ("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
95 ("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
96 ("hir", true) => PpmHir(PpmNormal),
97 ("hir,identified", true) => PpmHir(PpmIdentified),
98 ("hir,typed", true) => PpmHir(PpmTyped),
99 ("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
100 ("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
103 sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \
104 `expanded`, `flowgraph[,unlabelled]=<nodeid>`, \
105 `identified`, `expanded,identified`, `everybody_loops`, \
106 `hir`, `hir,identified`, or `hir,typed`; got {}",
109 sess.fatal(&format!("argument to `pretty` must be one of `normal`, `expanded`, \
110 `identified`, or `expanded,identified`; got {}",
115 let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
121 // This slightly awkward construction is to allow for each PpMode to
122 // choose whether it needs to do analyses (which can consume the
123 // Session) and then pass through the session (now attached to the
124 // analysis results) on to the chosen pretty-printer, along with the
127 // Note that since the `&PrinterSupport` is freshly constructed on each
128 // call, it would not make sense to try to attach the lifetime of `self`
129 // to the lifetime of the `&PrinterObject`.
131 // (The `use_once_payload` is working around the current lack of once
132 // functions in the compiler.)
135 /// Constructs a `PrinterSupport` object and passes it to `f`.
136 fn call_with_pp_support<'tcx, A, B, F>(&self,
138 ast_map: Option<hir_map::Map<'tcx>>,
142 where F: FnOnce(&PrinterSupport, B) -> A
145 PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
146 let annotation = NoAnn {
150 f(&annotation, payload)
153 PpmIdentified | PpmExpandedIdentified => {
154 let annotation = IdentifiedAnnotation {
158 f(&annotation, payload)
160 PpmExpandedHygiene => {
161 let annotation = HygieneAnnotation {
165 f(&annotation, payload)
167 _ => panic!("Should use call_with_pp_support_hir"),
170 fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
173 ast_map: &hir_map::Map<'tcx>,
174 arenas: &'tcx ty::CtxtArenas<'tcx>,
179 where F: FnOnce(&HirPrinterSupport, B, &hir::Crate) -> A
183 let annotation = NoAnn {
185 ast_map: Some(ast_map.clone()),
187 f(&annotation, payload, ast_map.forest.krate())
191 let annotation = IdentifiedAnnotation {
193 ast_map: Some(ast_map.clone()),
195 f(&annotation, payload, ast_map.forest.krate())
198 abort_on_err(driver::phase_3_run_analysis_passes(sess,
203 resolve::MakeGlobMap::No,
205 let annotation = TypedAnnotation {
208 let _ignore = tcx.dep_graph.in_ignore();
211 ast_map.forest.krate())
214 _ => panic!("Should use call_with_pp_support"),
219 trait PrinterSupport<'ast>: pprust::PpAnn {
220 /// Provides a uniform interface for re-extracting a reference to a
221 /// `Session` from a value that now owns it.
222 fn sess<'a>(&'a self) -> &'a Session;
224 /// Provides a uniform interface for re-extracting a reference to an
225 /// `hir_map::Map` from a value that now owns it.
226 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>>;
228 /// Produces the pretty-print annotation object.
230 /// (Rust does not yet support upcasting from a trait object to
231 /// an object for one of its super-traits.)
232 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn;
235 trait HirPrinterSupport<'ast>: pprust_hir::PpAnn {
236 /// Provides a uniform interface for re-extracting a reference to a
237 /// `Session` from a value that now owns it.
238 fn sess<'a>(&'a self) -> &'a Session;
240 /// Provides a uniform interface for re-extracting a reference to an
241 /// `hir_map::Map` from a value that now owns it.
242 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>>;
244 /// Produces the pretty-print annotation object.
246 /// (Rust does not yet support upcasting from a trait object to
247 /// an object for one of its super-traits.)
248 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn;
253 ast_map: Option<hir_map::Map<'ast>>,
256 impl<'ast> PrinterSupport<'ast> for NoAnn<'ast> {
257 fn sess<'a>(&'a self) -> &'a Session {
261 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
262 self.ast_map.as_ref()
265 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
270 impl<'ast> HirPrinterSupport<'ast> for NoAnn<'ast> {
271 fn sess<'a>(&'a self) -> &'a Session {
275 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
276 self.ast_map.as_ref()
279 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
284 impl<'ast> pprust::PpAnn for NoAnn<'ast> {}
285 impl<'ast> pprust_hir::PpAnn for NoAnn<'ast> {}
287 struct IdentifiedAnnotation<'ast> {
289 ast_map: Option<hir_map::Map<'ast>>,
292 impl<'ast> PrinterSupport<'ast> for IdentifiedAnnotation<'ast> {
293 fn sess<'a>(&'a self) -> &'a Session {
297 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
298 self.ast_map.as_ref()
301 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
306 impl<'ast> pprust::PpAnn for IdentifiedAnnotation<'ast> {
307 fn pre(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
309 pprust::NodeExpr(_) => s.popen(),
313 fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
315 pprust::NodeIdent(_) | pprust::NodeName(_) => Ok(()),
317 pprust::NodeItem(item) => {
318 try!(pp::space(&mut s.s));
319 s.synth_comment(item.id.to_string())
321 pprust::NodeSubItem(id) => {
322 try!(pp::space(&mut s.s));
323 s.synth_comment(id.to_string())
325 pprust::NodeBlock(blk) => {
326 try!(pp::space(&mut s.s));
327 s.synth_comment(format!("block {}", blk.id))
329 pprust::NodeExpr(expr) => {
330 try!(pp::space(&mut s.s));
331 try!(s.synth_comment(expr.id.to_string()));
334 pprust::NodePat(pat) => {
335 try!(pp::space(&mut s.s));
336 s.synth_comment(format!("pat {}", pat.id))
342 impl<'ast> HirPrinterSupport<'ast> for IdentifiedAnnotation<'ast> {
343 fn sess<'a>(&'a self) -> &'a Session {
347 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
348 self.ast_map.as_ref()
351 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
356 impl<'ast> pprust_hir::PpAnn for IdentifiedAnnotation<'ast> {
357 fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
359 pprust_hir::NodeExpr(_) => s.popen(),
363 fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
365 pprust_hir::NodeName(_) => Ok(()),
366 pprust_hir::NodeItem(item) => {
367 try!(pp::space(&mut s.s));
368 s.synth_comment(item.id.to_string())
370 pprust_hir::NodeSubItem(id) => {
371 try!(pp::space(&mut s.s));
372 s.synth_comment(id.to_string())
374 pprust_hir::NodeBlock(blk) => {
375 try!(pp::space(&mut s.s));
376 s.synth_comment(format!("block {}", blk.id))
378 pprust_hir::NodeExpr(expr) => {
379 try!(pp::space(&mut s.s));
380 try!(s.synth_comment(expr.id.to_string()));
383 pprust_hir::NodePat(pat) => {
384 try!(pp::space(&mut s.s));
385 s.synth_comment(format!("pat {}", pat.id))
391 struct HygieneAnnotation<'ast> {
393 ast_map: Option<hir_map::Map<'ast>>,
396 impl<'ast> PrinterSupport<'ast> for HygieneAnnotation<'ast> {
397 fn sess<'a>(&'a self) -> &'a Session {
401 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'ast>> {
402 self.ast_map.as_ref()
405 fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
410 impl<'ast> pprust::PpAnn for HygieneAnnotation<'ast> {
411 fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
413 pprust::NodeIdent(&ast::Ident { name: ast::Name(nm), ctxt }) => {
414 try!(pp::space(&mut s.s));
415 // FIXME #16420: this doesn't display the connections
416 // between syntax contexts
417 s.synth_comment(format!("{}#{}", nm, ctxt.0))
419 pprust::NodeName(&ast::Name(nm)) => {
420 try!(pp::space(&mut s.s));
421 s.synth_comment(nm.to_string())
429 struct TypedAnnotation<'a, 'tcx: 'a> {
430 tcx: &'a ty::ctxt<'tcx>,
433 impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
434 fn sess<'a>(&'a self) -> &'a Session {
438 fn ast_map<'a>(&'a self) -> Option<&'a hir_map::Map<'tcx>> {
442 fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
447 impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
448 fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
450 pprust_hir::NodeExpr(_) => s.popen(),
454 fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
456 pprust_hir::NodeExpr(expr) => {
457 try!(pp::space(&mut s.s));
458 try!(pp::word(&mut s.s, "as"));
459 try!(pp::space(&mut s.s));
460 try!(pp::word(&mut s.s, &self.tcx.expr_ty(expr).to_string()));
468 fn gather_flowgraph_variants(sess: &Session) -> Vec<borrowck_dot::Variant> {
469 let print_loans = sess.opts.debugging_opts.flowgraph_print_loans;
470 let print_moves = sess.opts.debugging_opts.flowgraph_print_moves;
471 let print_assigns = sess.opts.debugging_opts.flowgraph_print_assigns;
472 let print_all = sess.opts.debugging_opts.flowgraph_print_all;
473 let mut variants = Vec::new();
474 if print_all || print_loans {
475 variants.push(borrowck_dot::Loans);
477 if print_all || print_moves {
478 variants.push(borrowck_dot::Moves);
480 if print_all || print_assigns {
481 variants.push(borrowck_dot::Assigns);
486 #[derive(Clone, Debug)]
487 pub enum UserIdentifiedItem {
488 ItemViaNode(ast::NodeId),
489 ItemViaPath(Vec<String>),
492 impl FromStr for UserIdentifiedItem {
494 fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
497 .unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
501 enum NodesMatchingUII<'a, 'ast: 'a> {
502 NodesMatchingDirect(option::IntoIter<ast::NodeId>),
503 NodesMatchingSuffix(hir_map::NodesMatchingSuffix<'a, 'ast>),
506 impl<'a, 'ast> Iterator for NodesMatchingUII<'a, 'ast> {
507 type Item = ast::NodeId;
509 fn next(&mut self) -> Option<ast::NodeId> {
511 &mut NodesMatchingDirect(ref mut iter) => iter.next(),
512 &mut NodesMatchingSuffix(ref mut iter) => iter.next(),
517 impl UserIdentifiedItem {
518 fn reconstructed_input(&self) -> String {
520 ItemViaNode(node_id) => node_id.to_string(),
521 ItemViaPath(ref parts) => parts.join("::"),
525 fn all_matching_node_ids<'a, 'ast>(&'a self,
526 map: &'a hir_map::Map<'ast>)
527 -> NodesMatchingUII<'a, 'ast> {
529 ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()),
530 ItemViaPath(ref parts) => NodesMatchingSuffix(map.nodes_matching_suffix(&parts[..])),
534 fn to_one_node_id(self, user_option: &str, sess: &Session, map: &hir_map::Map) -> ast::NodeId {
535 let fail_because = |is_wrong_because| -> ast::NodeId {
536 let message = format!("{} needs NodeId (int) or unique path suffix (b::c::d); got \
539 self.reconstructed_input(),
541 sess.fatal(&message[..])
544 let mut saw_node = ast::DUMMY_NODE_ID;
546 for node in self.all_matching_node_ids(map) {
550 fail_because("does not resolve uniquely");
554 fail_because("does not resolve to any item");
562 fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
564 PpmSource(PpmNormal) |
565 PpmSource(PpmEveryBodyLoops) |
566 PpmSource(PpmIdentified) => opt_uii.is_some(),
568 PpmSource(PpmExpanded) |
569 PpmSource(PpmExpandedIdentified) |
570 PpmSource(PpmExpandedHygiene) |
572 PpmFlowGraph(_) => true,
573 PpmSource(PpmTyped) => panic!("invalid state"),
577 fn needs_expansion(ppm: &PpMode) -> bool {
579 PpmSource(PpmNormal) |
580 PpmSource(PpmEveryBodyLoops) |
581 PpmSource(PpmIdentified) => false,
583 PpmSource(PpmExpanded) |
584 PpmSource(PpmExpandedIdentified) |
585 PpmSource(PpmExpandedHygiene) |
587 PpmFlowGraph(_) => true,
588 PpmSource(PpmTyped) => panic!("invalid state"),
592 struct ReplaceBodyWithLoop {
593 within_static_or_const: bool,
596 impl ReplaceBodyWithLoop {
597 fn new() -> ReplaceBodyWithLoop {
598 ReplaceBodyWithLoop { within_static_or_const: false }
602 impl fold::Folder for ReplaceBodyWithLoop {
603 fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
605 ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
606 self.within_static_or_const = true;
607 let ret = fold::noop_fold_item_kind(i, self);
608 self.within_static_or_const = false;
612 fold::noop_fold_item_kind(i, self)
617 fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector<ast::TraitItem> {
619 ast::TraitItemKind::Const(..) => {
620 self.within_static_or_const = true;
621 let ret = fold::noop_fold_trait_item(i, self);
622 self.within_static_or_const = false;
625 _ => fold::noop_fold_trait_item(i, self),
629 fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector<ast::ImplItem> {
631 ast::ImplItemKind::Const(..) => {
632 self.within_static_or_const = true;
633 let ret = fold::noop_fold_impl_item(i, self);
634 self.within_static_or_const = false;
637 _ => fold::noop_fold_impl_item(i, self),
641 fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
642 fn expr_to_block(rules: ast::BlockCheckMode, e: Option<P<ast::Expr>>) -> P<ast::Block> {
647 id: ast::DUMMY_NODE_ID,
648 span: codemap::DUMMY_SP,
652 if !self.within_static_or_const {
654 let empty_block = expr_to_block(BlockCheckMode::Default, None);
655 let loop_expr = P(ast::Expr {
656 node: ast::ExprKind::Loop(empty_block, None),
657 id: ast::DUMMY_NODE_ID,
658 span: codemap::DUMMY_SP,
662 expr_to_block(b.rules, Some(loop_expr))
665 fold::noop_fold_block(b, self)
669 // in general the pretty printer processes unexpanded code, so
670 // we override the default `fold_mac` method which panics.
671 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
672 fold::noop_fold_mac(mac, self)
676 pub fn pretty_print_input(sess: Session,
678 cfg: ast::CrateConfig,
681 opt_uii: Option<UserIdentifiedItem>,
682 ofile: Option<PathBuf>) {
683 let krate = driver::phase_1_parse_input(&sess, cfg, input);
685 let krate = if let PpmSource(PpmEveryBodyLoops) = ppm {
686 let mut fold = ReplaceBodyWithLoop::new();
687 fold.fold_crate(krate)
692 let id = link::find_crate_name(Some(&sess), &krate.attrs, input);
694 let is_expanded = needs_expansion(&ppm);
695 let compute_ast_map = needs_ast_map(&ppm, &opt_uii);
696 let krate = if compute_ast_map {
697 match driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id[..], None) {
699 Ok(k) => driver::assign_node_ids(&sess, k),
705 // There is some twisted, god-forsaken tangle of lifetimes here which makes
706 // the ordering of stuff super-finicky.
708 let lcx = LoweringContext::new(&sess, Some(&krate));
709 let arenas = ty::CtxtArenas::new();
710 let dep_graph = DepGraph::new(false);
711 let _ignore = dep_graph.in_ignore();
712 let ast_map = if compute_ast_map {
713 hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph.clone());
714 let map = driver::make_map(&sess, &mut hir_forest);
720 let src_name = driver::source_name(input);
721 let src = sess.codemap()
722 .get_filemap(&src_name[..])
728 let mut rdr = &src[..];
730 let mut out = Vec::new();
732 match (ppm, opt_uii) {
733 (PpmSource(s), _) => {
734 // Silently ignores an identified node.
735 let out: &mut Write = &mut out;
736 s.call_with_pp_support(&sess, ast_map, box out, |annotation, out| {
737 debug!("pretty printing source code {:?}", s);
738 let sess = annotation.sess();
739 pprust::print_crate(sess.codemap(),
742 src_name.to_string(),
750 (PpmHir(s), None) => {
751 let out: &mut Write = &mut out;
752 s.call_with_pp_support_hir(&sess,
758 |annotation, out, krate| {
759 debug!("pretty printing source code {:?}", s);
760 let sess = annotation.sess();
761 pprust_hir::print_crate(sess.codemap(),
764 src_name.to_string(),
772 (PpmHir(s), Some(uii)) => {
773 let out: &mut Write = &mut out;
774 s.call_with_pp_support_hir(&sess,
780 |annotation, (out,uii), _| {
781 debug!("pretty printing source code {:?}", s);
782 let sess = annotation.sess();
783 let ast_map = annotation.ast_map().expect("--pretty missing ast_map");
785 pprust_hir::State::new_from_input(sess.codemap(),
787 src_name.to_string(),
792 Some(ast_map.krate()));
793 for node_id in uii.all_matching_node_ids(ast_map) {
794 let node = ast_map.get(node_id);
795 try!(pp_state.print_node(&node));
796 try!(pp::space(&mut pp_state.s));
797 try!(pp_state.synth_comment(ast_map.path_to_string(node_id)));
798 try!(pp::hardbreak(&mut pp_state.s));
800 pp::eof(&mut pp_state.s)
804 (PpmFlowGraph(mode), opt_uii) => {
805 debug!("pretty printing flow graph for {:?}", opt_uii);
806 let uii = opt_uii.unwrap_or_else(|| {
807 sess.fatal(&format!("`pretty flowgraph=..` needs NodeId (int) or
809 unique path suffix (b::c::d)"))
812 let ast_map = ast_map.expect("--pretty flowgraph missing ast_map");
813 let nodeid = uii.to_one_node_id("--pretty", &sess, &ast_map);
815 let node = ast_map.find(nodeid).unwrap_or_else(|| {
816 sess.fatal(&format!("--pretty flowgraph couldn't find id: {}", nodeid))
819 let code = blocks::Code::from_node(node);
820 let out: &mut Write = &mut out;
823 let variants = gather_flowgraph_variants(&sess);
824 abort_on_err(driver::phase_3_run_analysis_passes(&sess,
829 resolve::MakeGlobMap::No,
831 print_flowgraph(variants,
839 let message = format!("--pretty=flowgraph needs block, fn, or method; got \
843 // point to what was found, if there's an
845 match ast_map.opt_span(nodeid) {
846 Some(sp) => sess.span_fatal(sp, &message[..]),
847 None => sess.fatal(&message[..]),
856 None => print!("{}", String::from_utf8(out).unwrap()),
858 match File::create(&p) {
859 Ok(mut w) => w.write_all(&out).unwrap(),
860 Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
866 fn print_flowgraph<W: Write>(variants: Vec<borrowck_dot::Variant>,
869 mode: PpFlowGraphMode,
872 let cfg = match code {
873 blocks::BlockCode(block) => cfg::CFG::new(tcx, &block),
874 blocks::FnLikeCode(fn_like) => cfg::CFG::new(tcx, &fn_like.body()),
876 let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
877 let lcfg = LabelledCFG {
880 name: format!("node_{}", code.id()),
881 labelled_edges: labelled_edges,
885 _ if variants.is_empty() => {
886 let r = dot::render(&lcfg, &mut out);
887 return expand_err_details(r);
889 blocks::BlockCode(_) => {
890 tcx.sess.err("--pretty flowgraph with -Z flowgraph-print annotations requires \
894 blocks::FnLikeCode(fn_like) => {
895 let (bccx, analysis_data) =
896 borrowck::build_borrowck_dataflow_data_for_fn(tcx,
897 fn_like.to_fn_parts(),
900 let lcfg = borrowck_dot::DataflowLabeller {
903 borrowck_ctxt: &bccx,
904 analysis_data: &analysis_data,
906 let r = dot::render(&lcfg, &mut out);
907 return expand_err_details(r);
911 fn expand_err_details(r: io::Result<()>) -> io::Result<()> {
913 io::Error::new(io::ErrorKind::Other,
914 &format!("graphviz::render failed: {}", ioerr)[..])