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