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.
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.
11 /// This module provides linkage between rustc::middle::graph and
12 /// libgraphviz traits.
14 // For clarity, rename the graphviz crate locally to dot.
21 pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
22 pub type Edge<'a> = &'a cfg::CFGEdge;
24 pub struct LabelledCFG<'a, 'tcx: 'a> {
25 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
26 pub cfg: &'a cfg::CFG,
28 /// `labelled_edges` controls whether we emit labels on the edges
29 pub labelled_edges: bool,
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,
39 let s = self.tcx.hir.node_to_string(node_id);
41 // Replacing newlines with \\l causes each line to be left-aligned,
42 // improving presentation of (long) pretty-printed expressions.
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
49 let mut last_two: Vec<_> =
50 s.chars().rev().take(2).collect();
52 if last_two != ['\\', 'l'] {
62 impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
65 fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
67 fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
68 dot::Id::new(format!("N{}", i.node_id())).unwrap()
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())
79 let s = self.local_id_to_string(n.data.id());
80 dot::LabelText::EscStr(s.into())
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());
89 let mut put_one = false;
90 for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
92 label.push_str(",\\l");
96 let s = self.local_id_to_string(id);
97 label.push_str(&format!("exiting scope_{} {}",
101 dot::LabelText::EscStr(label.into())
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 });
113 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
114 self.graph.all_edges().iter().collect()
116 fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
117 let i = edge.source();
118 (i, self.graph.node(i))
120 fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
121 let i = edge.target();
122 (i, self.graph.node(i))
126 impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
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) }