Rollup merge of #76500 - richkadel:mir-graphviz-dark, r=tmandry
Add -Zgraphviz_dark_mode and monospace font fix Many developers use a dark theme with editors and IDEs, but this typically doesn't extend to graphviz output. When I bring up a MIR graphviz document, the white background is strikingly bright. This new option changes the colors used for graphviz output to work better in dark-themed UIs. <img width="1305" alt="Screen Shot 2020-09-09 at 3 00 31 PM" src="https://user-images.githubusercontent.com/3827298/92659478-4b9bff00-f2ad-11ea-8894-b40d3a873cb9.png"> Also fixed the monospace font for common graphviz renders (e.g., VS Code extensions), as described in https://github.com/rust-lang/rust/pull/76500#issuecomment-689837948 **Before:** <img width="943" alt="Screen Shot 2020-09-09 at 2 48 44 PM" src="https://user-images.githubusercontent.com/3827298/92658939-47231680-f2ac-11ea-97ac-96727e4dd622.png"> **Now with fix:** <img width="943" alt="Screen Shot 2020-09-09 at 2 49 02 PM" src="https://user-images.githubusercontent.com/3827298/92658959-51451500-f2ac-11ea-9aae-de982d466d6a.png">
This commit is contained in:
commit
ba6e2b3a31
@ -599,6 +599,7 @@ pub enum RenderOption {
|
||||
NoNodeStyles,
|
||||
|
||||
Monospace,
|
||||
DarkTheme,
|
||||
}
|
||||
|
||||
/// Returns vec holding all the default render options.
|
||||
@ -630,10 +631,23 @@ where
|
||||
writeln!(w, "digraph {} {{", g.graph_id().as_slice())?;
|
||||
|
||||
// Global graph properties
|
||||
let mut graph_attrs = Vec::new();
|
||||
let mut content_attrs = Vec::new();
|
||||
if options.contains(&RenderOption::Monospace) {
|
||||
writeln!(w, r#" graph[fontname="monospace"];"#)?;
|
||||
writeln!(w, r#" node[fontname="monospace"];"#)?;
|
||||
writeln!(w, r#" edge[fontname="monospace"];"#)?;
|
||||
let font = r#"fontname="Courier, monospace""#;
|
||||
graph_attrs.push(font);
|
||||
content_attrs.push(font);
|
||||
};
|
||||
if options.contains(&RenderOption::DarkTheme) {
|
||||
graph_attrs.push(r#"bgcolor="black""#);
|
||||
content_attrs.push(r#"color="white""#);
|
||||
content_attrs.push(r#"fontcolor="white""#);
|
||||
}
|
||||
if !(graph_attrs.is_empty() && content_attrs.is_empty()) {
|
||||
writeln!(w, r#" graph[{}];"#, graph_attrs.join(" "))?;
|
||||
let content_attrs_str = content_attrs.join(" ");
|
||||
writeln!(w, r#" node[{}];"#, content_attrs_str)?;
|
||||
writeln!(w, r#" edge[{}];"#, content_attrs_str)?;
|
||||
}
|
||||
|
||||
for n in g.nodes().iter() {
|
||||
|
@ -306,7 +306,11 @@ where
|
||||
let mut buf = Vec::new();
|
||||
|
||||
let graphviz = graphviz::Formatter::new(body, def_id, results, style);
|
||||
dot::render_opts(&graphviz, &mut buf, &[dot::RenderOption::Monospace])?;
|
||||
let mut render_opts = vec![dot::RenderOption::Monospace];
|
||||
if tcx.sess.opts.debugging_opts.graphviz_dark_mode {
|
||||
render_opts.push(dot::RenderOption::DarkTheme);
|
||||
}
|
||||
dot::render_opts(&graphviz, &mut buf, &render_opts)?;
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
|
@ -55,16 +55,28 @@ where
|
||||
writeln!(w, "{} {}Mir_{} {{", kind, cluster, def_name)?;
|
||||
|
||||
// Global graph properties
|
||||
writeln!(w, r#" graph [fontname="monospace"];"#)?;
|
||||
writeln!(w, r#" node [fontname="monospace"];"#)?;
|
||||
writeln!(w, r#" edge [fontname="monospace"];"#)?;
|
||||
let font = r#"fontname="Courier, monospace""#;
|
||||
let mut graph_attrs = vec![font];
|
||||
let mut content_attrs = vec![font];
|
||||
|
||||
let dark_mode = tcx.sess.opts.debugging_opts.graphviz_dark_mode;
|
||||
if dark_mode {
|
||||
graph_attrs.push(r#"bgcolor="black""#);
|
||||
content_attrs.push(r#"color="white""#);
|
||||
content_attrs.push(r#"fontcolor="white""#);
|
||||
}
|
||||
|
||||
writeln!(w, r#" graph [{}];"#, graph_attrs.join(" "))?;
|
||||
let content_attrs_str = content_attrs.join(" ");
|
||||
writeln!(w, r#" node [{}];"#, content_attrs_str)?;
|
||||
writeln!(w, r#" edge [{}];"#, content_attrs_str)?;
|
||||
|
||||
// Graph label
|
||||
write_graph_label(tcx, def_id, body, w)?;
|
||||
|
||||
// Nodes
|
||||
for (block, _) in body.basic_blocks().iter_enumerated() {
|
||||
write_node(def_id, block, body, w)?;
|
||||
write_node(def_id, block, body, dark_mode, w)?;
|
||||
}
|
||||
|
||||
// Edges
|
||||
@ -84,6 +96,7 @@ where
|
||||
pub fn write_node_label<W: Write, INIT, FINI>(
|
||||
block: BasicBlock,
|
||||
body: &Body<'_>,
|
||||
dark_mode: bool,
|
||||
w: &mut W,
|
||||
num_cols: u32,
|
||||
init: INIT,
|
||||
@ -100,8 +113,9 @@ where
|
||||
// Basic block number at the top.
|
||||
write!(
|
||||
w,
|
||||
r#"<tr><td {attrs} colspan="{colspan}">{blk}</td></tr>"#,
|
||||
attrs = r#"bgcolor="gray" align="center""#,
|
||||
r#"<tr><td bgcolor="{bgcolor}" {attrs} colspan="{colspan}">{blk}</td></tr>"#,
|
||||
bgcolor = if dark_mode { "dimgray" } else { "gray" },
|
||||
attrs = r#"align="center""#,
|
||||
colspan = num_cols,
|
||||
blk = block.index()
|
||||
)?;
|
||||
@ -134,11 +148,12 @@ fn write_node<W: Write>(
|
||||
def_id: DefId,
|
||||
block: BasicBlock,
|
||||
body: &Body<'_>,
|
||||
dark_mode: bool,
|
||||
w: &mut W,
|
||||
) -> io::Result<()> {
|
||||
// Start a new node with the label to follow, in one of DOT's pseudo-HTML tables.
|
||||
write!(w, r#" {} [shape="none", label=<"#, node(def_id, block))?;
|
||||
write_node_label(block, body, w, 1, |_| Ok(()), |_| Ok(()))?;
|
||||
write_node_label(block, body, dark_mode, w, 1, |_| Ok(()), |_| Ok(()))?;
|
||||
// Close the node label and the node itself.
|
||||
writeln!(w, ">];")
|
||||
}
|
||||
|
@ -909,6 +909,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
|
||||
"force all crates to be `rustc_private` unstable (default: no)"),
|
||||
fuel: Option<(String, u64)> = (None, parse_optimization_fuel, [TRACKED],
|
||||
"set the optimization fuel quota for a crate"),
|
||||
graphviz_dark_mode: bool = (false, parse_bool, [UNTRACKED],
|
||||
"use dark-themed colors in graphviz output (default: no)"),
|
||||
hir_stats: bool = (false, parse_bool, [UNTRACKED],
|
||||
"print some statistics about AST and HIR (default: no)"),
|
||||
human_readable_cgu_names: bool = (false, parse_bool, [TRACKED],
|
||||
|
@ -1,7 +1,7 @@
|
||||
digraph Mir_0_3 {
|
||||
graph [fontname="monospace"];
|
||||
node [fontname="monospace"];
|
||||
edge [fontname="monospace"];
|
||||
graph [fontname="Courier, monospace"];
|
||||
node [fontname="Courier, monospace"];
|
||||
edge [fontname="Courier, monospace"];
|
||||
label=<fn main() -> ()<br align="left"/>>;
|
||||
bb0__0_3 [shape="none", label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">0</td></tr><tr><td align="left" balign="left">_0 = const ()<br/></td></tr><tr><td align="left">goto</td></tr></table>>];
|
||||
bb1__0_3 [shape="none", label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">1</td></tr><tr><td align="left">resume</td></tr></table>>];
|
||||
|
Loading…
x
Reference in New Issue
Block a user