From 5c29332ace48d8c34ad415ffcdce2641526938b8 Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Wed, 16 Sep 2020 08:10:06 -0700 Subject: [PATCH 1/2] Make graphviz font configurable Alternative to PR ##76776. To change the graphviz output to use an alternative `fontname` value, add a command line option like: `rustc --graphviz-font=monospace`. --- compiler/rustc_graphviz/src/lib.rs | 17 ++++++++++------- .../rustc_mir/src/dataflow/framework/engine.rs | 3 ++- compiler/rustc_mir/src/util/graphviz.rs | 6 +++--- compiler/rustc_session/src/options.rs | 2 ++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_graphviz/src/lib.rs b/compiler/rustc_graphviz/src/lib.rs index 29ec3572016..58db81bc1dc 100644 --- a/compiler/rustc_graphviz/src/lib.rs +++ b/compiler/rustc_graphviz/src/lib.rs @@ -591,14 +591,14 @@ pub trait GraphWalk<'a> { fn target(&'a self, edge: &Self::Edge) -> Self::Node; } -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug)] pub enum RenderOption { NoEdgeLabels, NoNodeLabels, NoEdgeStyles, NoNodeStyles, - Monospace, + Fontname(String), DarkTheme, } @@ -633,11 +633,14 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G, w: &mut W, options: &[RenderOption] // Global graph properties let mut graph_attrs = Vec::new(); let mut content_attrs = Vec::new(); - if options.contains(&RenderOption::Monospace) { - let font = r#"fontname="Courier, monospace""#; - graph_attrs.push(font); - content_attrs.push(font); - }; + let font; + if let Some(fontname) = options.iter().find_map(|option| { + if let RenderOption::Fontname(fontname) = option { Some(fontname) } else { None } + }) { + font = format!(r#"fontname="{}""#, fontname); + 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""#); diff --git a/compiler/rustc_mir/src/dataflow/framework/engine.rs b/compiler/rustc_mir/src/dataflow/framework/engine.rs index 0b5b437d186..4084083bd99 100644 --- a/compiler/rustc_mir/src/dataflow/framework/engine.rs +++ b/compiler/rustc_mir/src/dataflow/framework/engine.rs @@ -306,7 +306,8 @@ fn write_graphviz_results( let mut buf = Vec::new(); let graphviz = graphviz::Formatter::new(body, def_id, results, style); - let mut render_opts = vec![dot::RenderOption::Monospace]; + let mut render_opts = + vec![dot::RenderOption::Fontname(tcx.sess.opts.debugging_opts.graphviz_font.clone())]; if tcx.sess.opts.debugging_opts.graphviz_dark_mode { render_opts.push(dot::RenderOption::DarkTheme); } diff --git a/compiler/rustc_mir/src/util/graphviz.rs b/compiler/rustc_mir/src/util/graphviz.rs index e89c9437706..4511962d68f 100644 --- a/compiler/rustc_mir/src/util/graphviz.rs +++ b/compiler/rustc_mir/src/util/graphviz.rs @@ -55,9 +55,9 @@ pub fn write_mir_fn_graphviz<'tcx, W>( writeln!(w, "{} {}Mir_{} {{", kind, cluster, def_name)?; // Global graph properties - let font = r#"fontname="Courier, monospace""#; - let mut graph_attrs = vec![font]; - let mut content_attrs = vec![font]; + let font = format!(r#"fontname="{}""#, tcx.sess.opts.debugging_opts.graphviz_font); + 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 { diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index f31e0431d0d..60bd557b880 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -909,6 +909,8 @@ fn parse_target_feature(slot: &mut String, v: Option<&str>) -> bool { "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)"), + graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED], + "use the given `fontname` in graphviz output (default: `Courier, monospace`)"), 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], From 3875abe32f6b98449bf2ec99ed210b7b77953703 Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Wed, 16 Sep 2020 10:47:56 -0700 Subject: [PATCH 2/2] Added RUSTC_GRAPHVIZ_FONT environment variable Overrides the debugging_opts.graphviz_font setting. --- compiler/rustc_session/src/config.rs | 4 ++++ compiler/rustc_session/src/options.rs | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 19cd2385992..b0d39b95313 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1762,6 +1762,10 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { debugging_opts.symbol_mangling_version = SymbolManglingVersion::V0; } + if let Ok(graphviz_font) = std::env::var("RUSTC_GRAPHVIZ_FONT") { + debugging_opts.graphviz_font = graphviz_font; + } + if !cg.embed_bitcode { match cg.lto { LtoCli::No | LtoCli::Unspecified => {} diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 60bd557b880..4d35ec2e5d9 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -910,7 +910,8 @@ fn parse_target_feature(slot: &mut String, v: Option<&str>) -> bool { graphviz_dark_mode: bool = (false, parse_bool, [UNTRACKED], "use dark-themed colors in graphviz output (default: no)"), graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED], - "use the given `fontname` in graphviz output (default: `Courier, monospace`)"), + "use the given `fontname` in graphviz output; can be overridden by setting \ + environment variable `RUSTC_GRAPHVIZ_FONT` (default: `Courier, monospace`)"), 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],