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 /// This module provides linkage between rustc::middle::graph and
12 /// libgraphviz traits.
14 // For clarity, rename the graphviz crate locally to dot.
16 use graphviz::IntoCow;
22 pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
23 pub type Edge<'a> = &'a cfg::CFGEdge;
25 pub struct LabelledCFG<'a, 'tcx: 'a> {
26 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 pub cfg: &'a cfg::CFG,
29 /// `labelled_edges` controls whether we emit labels on the edges
30 pub labelled_edges: bool,
33 impl<'a, 'tcx> LabelledCFG<'a, 'tcx> {
34 fn local_id_to_string(&self, local_id: hir::ItemLocalId) -> String {
35 assert!(self.cfg.owner_def_id.is_local());
36 let node_id = self.tcx.hir.hir_to_node_id(hir::HirId {
37 owner: self.tcx.hir.def_index_to_hir_id(self.cfg.owner_def_id.index).owner,
40 let s = self.tcx.hir.node_to_string(node_id);
42 // Replacing newlines with \\l causes each line to be left-aligned,
43 // improving presentation of (long) pretty-printed expressions.
45 let mut s = s.replace("\n", "\\l");
46 // Apparently left-alignment applies to the line that precedes
47 // \l, not the line that follows; so, add \l at end of string
48 // if not already present, ensuring last line gets left-aligned
50 let mut last_two: Vec<_> =
51 s.chars().rev().take(2).collect();
53 if last_two != ['\\', 'l'] {
63 impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
66 fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
68 fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
69 dot::Id::new(format!("N{}", i.node_id())).unwrap()
72 fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
73 if i == self.cfg.entry {
74 dot::LabelText::LabelStr("entry".into_cow())
75 } else if i == self.cfg.exit {
76 dot::LabelText::LabelStr("exit".into_cow())
77 } else if n.data.id() == hir::DUMMY_ITEM_LOCAL_ID {
78 dot::LabelText::LabelStr("(dummy_node)".into_cow())
80 let s = self.local_id_to_string(n.data.id());
81 dot::LabelText::EscStr(s.into_cow())
85 fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
86 let mut label = String::new();
87 if !self.labelled_edges {
88 return dot::LabelText::EscStr(label.into_cow());
90 let mut put_one = false;
91 for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
93 label.push_str(",\\l");
97 let s = self.local_id_to_string(id);
98 label.push_str(&format!("exiting scope_{} {}",
102 dot::LabelText::EscStr(label.into_cow())
106 impl<'a> dot::GraphWalk<'a> for &'a cfg::CFG {
107 type Node = Node<'a>;
108 type Edge = Edge<'a>;
109 fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
110 let mut v = Vec::new();
111 self.graph.each_node(|i, nd| { v.push((i, nd)); true });
114 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
115 self.graph.all_edges().iter().collect()
117 fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
118 let i = edge.source();
119 (i, self.graph.node(i))
121 fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
122 let i = edge.target();
123 (i, self.graph.node(i))
127 impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
129 type Node = Node<'a>;
130 type Edge = Edge<'a>;
131 fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
132 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
133 fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }
134 fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.target(edge) }