]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/cfg/graphviz.rs
prefer "FIXME" to "TODO".
[rust.git] / src / librustc / middle / 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 syntax::ast;
18 use syntax::ast_map;
19
20 use middle::cfg;
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, 'ast: 'a> {
26     pub ast_map: &'a ast_map::Map<'ast>,
27     pub cfg: &'a cfg::CFG,
28     pub name: String,
29 }
30
31 fn replace_newline_with_backslash_l(s: String) -> String {
32     // Replacing newlines with \\l causes each line to be left-aligned,
33     // improving presentation of (long) pretty-printed expressions.
34     if s.as_slice().contains("\n") {
35         let mut s = s.replace("\n", "\\l");
36         // Apparently left-alignment applies to the line that precedes
37         // \l, not the line that follows; so, add \l at end of string
38         // if not already present, ensuring last line gets left-aligned
39         // as well.
40         let mut last_two: Vec<_> =
41             s.as_slice().chars().rev().take(2).collect();
42         last_two.reverse();
43         if last_two != ['\\', 'l'] {
44             s.push_str("\\l");
45         }
46         s
47     } else {
48         s
49     }
50 }
51
52 impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
53     fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()).unwrap() }
54
55     fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
56         dot::Id::new(format!("N{}", i.node_id())).unwrap()
57     }
58
59     fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
60         if i == self.cfg.entry {
61             dot::LabelStr("entry".into_cow())
62         } else if i == self.cfg.exit {
63             dot::LabelStr("exit".into_cow())
64         } else if n.data.id == ast::DUMMY_NODE_ID {
65             dot::LabelStr("(dummy_node)".into_cow())
66         } else {
67             let s = self.ast_map.node_to_string(n.data.id);
68             // left-aligns the lines
69             let s = replace_newline_with_backslash_l(s);
70             dot::EscStr(s.into_cow())
71         }
72     }
73
74     fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
75         let mut label = String::new();
76         let mut put_one = false;
77         for (i, &node_id) in e.data.exiting_scopes.iter().enumerate() {
78             if put_one {
79                 label.push_str(",\\l");
80             } else {
81                 put_one = true;
82             }
83             let s = self.ast_map.node_to_string(node_id);
84             // left-aligns the lines
85             let s = replace_newline_with_backslash_l(s);
86             label.push_str(format!("exiting scope_{} {}", i,
87                                    s.as_slice()).as_slice());
88         }
89         dot::EscStr(label.into_cow())
90     }
91 }
92
93 impl<'a> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for &'a cfg::CFG {
94     fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
95         let mut v = Vec::new();
96         self.graph.each_node(|i, nd| { v.push((i, nd)); true });
97         v.into_cow()
98     }
99     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
100         self.graph.all_edges().iter().collect()
101     }
102     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
103         let i = edge.source();
104         (i, self.graph.node(i))
105     }
106     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
107         let i = edge.target();
108         (i, self.graph.node(i))
109     }
110 }
111
112 impl<'a, 'ast> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast>
113 {
114     fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
115     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
116     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }
117     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.target(edge) }
118 }
119