]> git.lizzy.rs Git - rust.git/blob - src/librustc/cfg/graphviz.rs
StmtKind
[rust.git] / src / librustc / cfg / graphviz.rs
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.
4 //
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.
10
11 /// This module provides linkage between rustc::middle::graph and
12 /// libgraphviz traits.
13
14 // For clarity, rename the graphviz crate locally to dot.
15 use graphviz as dot;
16 use graphviz::IntoCow;
17
18 use cfg;
19 use hir;
20 use ty::TyCtxt;
21
22 pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
23 pub type Edge<'a> = &'a cfg::CFGEdge;
24
25 pub struct LabelledCFG<'a, 'tcx: 'a> {
26     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
27     pub cfg: &'a cfg::CFG,
28     pub name: String,
29     /// `labelled_edges` controls whether we emit labels on the edges
30     pub labelled_edges: bool,
31 }
32
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,
38             local_id
39         });
40         let s = self.tcx.hir.node_to_string(node_id);
41
42         // Replacing newlines with \\l causes each line to be left-aligned,
43         // improving presentation of (long) pretty-printed expressions.
44         if s.contains("\n") {
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
49             // as well.
50             let mut last_two: Vec<_> =
51                 s.chars().rev().take(2).collect();
52             last_two.reverse();
53             if last_two != ['\\', 'l'] {
54                 s.push_str("\\l");
55             }
56             s
57         } else {
58             s
59         }
60     }
61 }
62
63 impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
64     type Node = Node<'a>;
65     type Edge = Edge<'a>;
66     fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
67
68     fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
69         dot::Id::new(format!("N{}", i.node_id())).unwrap()
70     }
71
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())
79         } else {
80             let s = self.local_id_to_string(n.data.id());
81             dot::LabelText::EscStr(s.into_cow())
82         }
83     }
84
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());
89         }
90         let mut put_one = false;
91         for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
92             if put_one {
93                 label.push_str(",\\l");
94             } else {
95                 put_one = true;
96             }
97             let s = self.local_id_to_string(id);
98             label.push_str(&format!("exiting scope_{} {}",
99                                    i,
100                                    &s[..]));
101         }
102         dot::LabelText::EscStr(label.into_cow())
103     }
104 }
105
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 });
112         v.into_cow()
113     }
114     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
115         self.graph.all_edges().iter().collect()
116     }
117     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
118         let i = edge.source();
119         (i, self.graph.node(i))
120     }
121     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
122         let i = edge.target();
123         (i, self.graph.node(i))
124     }
125 }
126
127 impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
128 {
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) }
135 }