forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#78399 - vn-ki:gsgdt-graphviz, r=oli-obk
make MIR graphviz generation use gsgdt gsgdt [https://crates.io/crates/gsgdt] is a crate which provides an interface for stringly typed graphs. It also provides generation of graphviz dot format from said graph. This is the first in a series of PRs on moving graphviz code out of rustc into normal crates and then implementating graph diffing on top of these crates. r? ``@oli-obk``
- Loading branch information
Showing
7 changed files
with
103 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use gsgdt::{Edge, Graph, Node, NodeStyle}; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_index::vec::Idx; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
|
||
/// Convert an MIR function into a gsgdt Graph | ||
pub fn mir_fn_to_generic_graph<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Graph { | ||
let def_id = body.source.def_id(); | ||
let def_name = graphviz_safe_def_name(def_id); | ||
let graph_name = format!("Mir_{}", def_name); | ||
let dark_mode = tcx.sess.opts.debugging_opts.graphviz_dark_mode; | ||
|
||
// Nodes | ||
let nodes: Vec<Node> = body | ||
.basic_blocks() | ||
.iter_enumerated() | ||
.map(|(block, _)| bb_to_graph_node(block, body, dark_mode)) | ||
.collect(); | ||
|
||
// Edges | ||
let mut edges = Vec::new(); | ||
for (source, _) in body.basic_blocks().iter_enumerated() { | ||
let def_id = body.source.def_id(); | ||
let terminator = body[source].terminator(); | ||
let labels = terminator.kind.fmt_successor_labels(); | ||
|
||
for (&target, label) in terminator.successors().zip(labels) { | ||
let src = node(def_id, source); | ||
let trg = node(def_id, target); | ||
edges.push(Edge::new(src, trg, label.to_string())); | ||
} | ||
} | ||
|
||
Graph::new(graph_name, nodes, edges) | ||
} | ||
|
||
fn bb_to_graph_node(block: BasicBlock, body: &Body<'_>, dark_mode: bool) -> Node { | ||
let def_id = body.source.def_id(); | ||
let data = &body[block]; | ||
let label = node(def_id, block); | ||
|
||
let (title, bgcolor) = if data.is_cleanup { | ||
let color = if dark_mode { "royalblue" } else { "lightblue" }; | ||
(format!("{} (cleanup)", block.index()), color) | ||
} else { | ||
let color = if dark_mode { "dimgray" } else { "gray" }; | ||
(format!("{}", block.index()), color) | ||
}; | ||
|
||
let style = NodeStyle { title_bg: Some(bgcolor.to_owned()), ..Default::default() }; | ||
let mut stmts: Vec<String> = data.statements.iter().map(|x| format!("{:?}", x)).collect(); | ||
|
||
// add the terminator to the stmts, gsgdt can print it out seperately | ||
let mut terminator_head = String::new(); | ||
data.terminator().kind.fmt_head(&mut terminator_head).unwrap(); | ||
stmts.push(terminator_head); | ||
|
||
Node::new(stmts, label, title, style) | ||
} | ||
|
||
// Must match `[0-9A-Za-z_]*`. This does not appear in the rendered graph, so | ||
// it does not have to be user friendly. | ||
pub fn graphviz_safe_def_name(def_id: DefId) -> String { | ||
format!("{}_{}", def_id.krate.index(), def_id.index.index(),) | ||
} | ||
|
||
fn node(def_id: DefId, block: BasicBlock) -> String { | ||
format!("bb{}__{}", block.index(), graphviz_safe_def_name(def_id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
src/tools/clippy/tests/ui/crashes/used_underscore_binding_macro.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters