]> git.lizzy.rs Git - rust.git/blob - src/librustc/cfg/graphviz.rs
918120057d4d35a99b256e7ec61aba594780d1af
[rust.git] / src / librustc / cfg / graphviz.rs
1 /// This module provides linkage between rustc::middle::graph and
2 /// libgraphviz traits.
3
4 // For clarity, rename the graphviz crate locally to dot.
5 use graphviz as dot;
6
7 use crate::cfg;
8 use crate::hir;
9 use crate::ty::TyCtxt;
10
11 pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
12 pub type Edge<'a> = &'a cfg::CFGEdge;
13
14 pub struct LabelledCFG<'a, 'tcx> {
15     pub tcx: TyCtxt<'tcx>,
16     pub cfg: &'a cfg::CFG,
17     pub name: String,
18     /// `labelled_edges` controls whether we emit labels on the edges
19     pub labelled_edges: bool,
20 }
21
22 impl<'a, 'tcx> LabelledCFG<'a, 'tcx> {
23     fn local_id_to_string(&self, local_id: hir::ItemLocalId) -> String {
24         assert!(self.cfg.owner_def_id.is_local());
25         let hir_id = hir::HirId {
26             owner: self.tcx.hir().def_index_to_hir_id(self.cfg.owner_def_id.index).owner,
27             local_id
28         };
29         let s = self.tcx.hir().node_to_string(hir_id);
30
31         // Replacing newlines with \\l causes each line to be left-aligned,
32         // improving presentation of (long) pretty-printed expressions.
33         if s.contains("\n") {
34             let mut s = s.replace("\n", "\\l");
35             // Apparently left-alignment applies to the line that precedes
36             // \l, not the line that follows; so, add \l at end of string
37             // if not already present, ensuring last line gets left-aligned
38             // as well.
39             let mut last_two: Vec<_> =
40                 s.chars().rev().take(2).collect();
41             last_two.reverse();
42             if last_two != ['\\', 'l'] {
43                 s.push_str("\\l");
44             }
45             s
46         } else {
47             s
48         }
49     }
50 }
51
52 impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
53     type Node = Node<'a>;
54     type Edge = Edge<'a>;
55     fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
56
57     fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
58         dot::Id::new(format!("N{}", i.node_id())).unwrap()
59     }
60
61     fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
62         if i == self.cfg.entry {
63             dot::LabelText::LabelStr("entry".into())
64         } else if i == self.cfg.exit {
65             dot::LabelText::LabelStr("exit".into())
66         } else if n.data.id() == hir::DUMMY_ITEM_LOCAL_ID {
67             dot::LabelText::LabelStr("(dummy_node)".into())
68         } else {
69             let s = self.local_id_to_string(n.data.id());
70             dot::LabelText::EscStr(s.into())
71         }
72     }
73
74     fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
75         let mut label = String::new();
76         if !self.labelled_edges {
77             return dot::LabelText::EscStr(label.into());
78         }
79         let mut put_one = false;
80         for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
81             if put_one {
82                 label.push_str(",\\l");
83             } else {
84                 put_one = true;
85             }
86             let s = self.local_id_to_string(id);
87             label.push_str(&format!("exiting scope_{} {}",
88                                    i,
89                                    &s[..]));
90         }
91         dot::LabelText::EscStr(label.into())
92     }
93 }
94
95 impl<'a> dot::GraphWalk<'a> for &'a cfg::CFG {
96     type Node = Node<'a>;
97     type Edge = Edge<'a>;
98     fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
99         let v: Vec<_> = self.graph.enumerated_nodes().collect();
100         v.into()
101     }
102     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
103         self.graph.all_edges().iter().collect()
104     }
105     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
106         let i = edge.source();
107         (i, self.graph.node(i))
108     }
109     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
110         let i = edge.target();
111         (i, self.graph.node(i))
112     }
113 }
114
115 impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
116 {
117     type Node = Node<'a>;
118     type Edge = Edge<'a>;
119     fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
120     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
121     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }
122     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.target(edge) }
123 }