|
| 1 | +// Copyright 2018 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 | +use graphviz as dot; |
| 12 | +use obligation_forest::{ForestObligation, ObligationForest}; |
| 13 | +use std::env::var_os; |
| 14 | +use std::fs::File; |
| 15 | +use std::path::Path; |
| 16 | +use std::sync::atomic::AtomicUsize; |
| 17 | +use std::sync::atomic::Ordering; |
| 18 | + |
| 19 | +impl<O: ForestObligation> ObligationForest<O> { |
| 20 | + /// Create a graphviz representation of the obligation forest. Given a directory this will |
| 21 | + /// create files with name of the format `<counter>_<description>.gv`. The counter is |
| 22 | + /// global and is maintained internally. |
| 23 | + /// |
| 24 | + /// Calling this will do nothing unless the environment variable |
| 25 | + /// `DUMP_OBLIGATION_FOREST_GRAPHVIZ` is defined. |
| 26 | + /// |
| 27 | + /// A few post-processing that you might want to do make the forest easier to visualize: |
| 28 | + /// |
| 29 | + /// * `sed 's,std::[a-z]*::,,g'` — Deletes the `std::<package>::` prefix of paths. |
| 30 | + /// * `sed 's,"Binder(TraitPredicate(<\(.*\)>)) (\([^)]*\))","\1 (\2)",'` — Transforms |
| 31 | + /// `Binder(TraitPredicate(<predicate>))` into just `<predicate>`. |
| 32 | + #[allow(dead_code)] |
| 33 | + pub fn dump_graphviz<P: AsRef<Path>>(&self, dir: P, description: &str) { |
| 34 | + static COUNTER: AtomicUsize = AtomicUsize::new(0); |
| 35 | + |
| 36 | + if var_os("DUMP_OBLIGATION_FOREST_GRAPHVIZ").is_none() { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + let counter = COUNTER.fetch_add(1, Ordering::AcqRel); |
| 41 | + |
| 42 | + let file_path = dir.as_ref().join(format!("{:010}_{}.gv", counter, description)); |
| 43 | + |
| 44 | + let mut gv_file = File::create(file_path).unwrap(); |
| 45 | + |
| 46 | + dot::render(&self, &mut gv_file).unwrap(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'a, O: ForestObligation + 'a> dot::Labeller<'a> for &'a ObligationForest<O> { |
| 51 | + type Node = usize; |
| 52 | + type Edge = (usize, usize); |
| 53 | + |
| 54 | + fn graph_id(&self) -> dot::Id { |
| 55 | + dot::Id::new("trait_obligation_forest").unwrap() |
| 56 | + } |
| 57 | + |
| 58 | + fn node_id(&self, index: &Self::Node) -> dot::Id { |
| 59 | + dot::Id::new(format!("obligation_{}", index)).unwrap() |
| 60 | + } |
| 61 | + |
| 62 | + fn node_label(&self, index: &Self::Node) -> dot::LabelText { |
| 63 | + let node = &self.nodes[*index]; |
| 64 | + let label = format!("{:?} ({:?})", node.obligation.as_predicate(), node.state.get()); |
| 65 | + |
| 66 | + dot::LabelText::LabelStr(label.into()) |
| 67 | + } |
| 68 | + |
| 69 | + fn edge_label(&self, (_index_source, _index_target): &Self::Edge) -> dot::LabelText { |
| 70 | + dot::LabelText::LabelStr("".into()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a, O: ForestObligation + 'a> dot::GraphWalk<'a> for &'a ObligationForest<O> { |
| 75 | + type Node = usize; |
| 76 | + type Edge = (usize, usize); |
| 77 | + |
| 78 | + fn nodes(&self) -> dot::Nodes<Self::Node> { |
| 79 | + (0..self.nodes.len()).collect() |
| 80 | + } |
| 81 | + |
| 82 | + fn edges(&self) -> dot::Edges<Self::Edge> { |
| 83 | + (0..self.nodes.len()) |
| 84 | + .flat_map(|i| { |
| 85 | + let node = &self.nodes[i]; |
| 86 | + |
| 87 | + node.parent.iter().map(|p| p.get()) |
| 88 | + .chain(node.dependents.iter().map(|p| p.get())) |
| 89 | + .map(move |p| (p, i)) |
| 90 | + }) |
| 91 | + .collect() |
| 92 | + } |
| 93 | + |
| 94 | + fn source(&self, (s, _): &Self::Edge) -> Self::Node { |
| 95 | + *s |
| 96 | + } |
| 97 | + |
| 98 | + fn target(&self, (_, t): &Self::Edge) -> Self::Node { |
| 99 | + *t |
| 100 | + } |
| 101 | +} |
0 commit comments