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