1 /// This module provides linkage between rustc::middle::graph and
2 /// libgraphviz traits.
8 pub(crate) type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
9 pub(crate) type Edge<'a> = &'a cfg::CFGEdge;
11 pub struct LabelledCFG<'a, 'tcx> {
12 pub tcx: TyCtxt<'tcx>,
13 pub cfg: &'a cfg::CFG,
15 /// `labelled_edges` controls whether we emit labels on the edges
16 pub labelled_edges: bool,
19 impl<'a, 'tcx> LabelledCFG<'a, 'tcx> {
20 fn local_id_to_string(&self, local_id: hir::ItemLocalId) -> String {
21 assert!(self.cfg.owner_def_id.is_local());
22 let hir_id = hir::HirId {
23 owner: self.tcx.hir().def_index_to_hir_id(self.cfg.owner_def_id.index).owner,
26 let s = self.tcx.hir().node_to_string(hir_id);
28 // Replacing newlines with \\l causes each line to be left-aligned,
29 // improving presentation of (long) pretty-printed expressions.
31 let mut s = s.replace("\n", "\\l");
32 // Apparently left-alignment applies to the line that precedes
33 // \l, not the line that follows; so, add \l at end of string
34 // if not already present, ensuring last line gets left-aligned
36 let mut last_two: Vec<_> =
37 s.chars().rev().take(2).collect();
39 if last_two != ['\\', 'l'] {
49 impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
52 fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
54 fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
55 dot::Id::new(format!("N{}", i.node_id())).unwrap()
58 fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {
59 if i == self.cfg.entry {
60 dot::LabelText::LabelStr("entry".into())
61 } else if i == self.cfg.exit {
62 dot::LabelText::LabelStr("exit".into())
63 } else if n.data.id() == hir::DUMMY_ITEM_LOCAL_ID {
64 dot::LabelText::LabelStr("(dummy_node)".into())
66 let s = self.local_id_to_string(n.data.id());
67 dot::LabelText::EscStr(s.into())
71 fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
72 let mut label = String::new();
73 if !self.labelled_edges {
74 return dot::LabelText::EscStr(label.into());
76 let mut put_one = false;
77 for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
79 label.push_str(",\\l");
83 let s = self.local_id_to_string(id);
84 label.push_str(&format!("exiting scope_{} {}",
88 dot::LabelText::EscStr(label.into())
92 impl<'a> dot::GraphWalk<'a> for &'a cfg::CFG {
95 fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> {
96 let v: Vec<_> = self.graph.enumerated_nodes().collect();
99 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> {
100 self.graph.all_edges().iter().collect()
102 fn source(&'a self, edge: &Edge<'a>) -> Node<'a> {
103 let i = edge.source();
104 (i, self.graph.node(i))
106 fn target(&'a self, edge: &Edge<'a>) -> Node<'a> {
107 let i = edge.target();
108 (i, self.graph.node(i))
112 impl<'a, 'hir> dot::GraphWalk<'a> for LabelledCFG<'a, 'hir>
114 type Node = Node<'a>;
115 type Edge = Edge<'a>;
116 fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.cfg.nodes() }
117 fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.cfg.edges() }
118 fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.source(edge) }
119 fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.cfg.target(edge) }