Skip to content

Commit

Permalink
add --xpretty flowgraph,unlabelled variant.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnkfelix committed Jan 12, 2015
1 parent b21a6da commit 82eefe3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/librustc/middle/cfg/graphviz.rs
Expand Up @@ -28,6 +28,8 @@ pub struct LabelledCFG<'a, 'ast: 'a> {
pub ast_map: &'a ast_map::Map<'ast>,
pub cfg: &'a cfg::CFG,
pub name: String,
/// `labelled_edges` controls whether we emit labels on the edges
pub labelled_edges: bool,
}

fn replace_newline_with_backslash_l(s: String) -> String {
Expand Down Expand Up @@ -75,6 +77,9 @@ impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {

fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> {
let mut label = String::new();
if !self.labelled_edges {
return dot::LabelText::EscStr(label.into_cow());
}
let mut put_one = false;
for (i, &node_id) in e.data.exiting_scopes.iter().enumerate() {
if put_one {
Expand Down
43 changes: 26 additions & 17 deletions src/librustc_driver/pretty.rs
Expand Up @@ -53,10 +53,20 @@ pub enum PpSourceMode {
PpmExpandedHygiene,
}


#[derive(Copy, PartialEq, Show)]
pub enum PpFlowGraphMode {
Default,
/// Drops the labels from the edges in the flowgraph output. This
/// is mostly for use in the --xpretty flowgraph run-make tests,
/// since the labels are largely uninteresting in those cases and
/// have become a pain to maintain.
UnlabelledEdges,
}
#[derive(Copy, PartialEq, Show)]
pub enum PpMode {
PpmSource(PpSourceMode),
PpmFlowGraph,
PpmFlowGraph(PpFlowGraphMode),
}

pub fn parse_pretty(sess: &Session,
Expand All @@ -73,12 +83,13 @@ pub fn parse_pretty(sess: &Session,
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
("identified", _) => PpmSource(PpmIdentified),
("flowgraph", true) => PpmFlowGraph,
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
_ => {
if extended {
sess.fatal(format!(
"argument to `xpretty` must be one of `normal`, \
`expanded`, `flowgraph=<nodeid>`, `typed`, `identified`, \
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, `typed`, `identified`, \
`expanded,identified`, or `everybody_loops`; got {}", name).as_slice());
} else {
sess.fatal(format!(
Expand Down Expand Up @@ -417,7 +428,7 @@ fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
PpmSource(PpmExpandedIdentified) |
PpmSource(PpmExpandedHygiene) |
PpmSource(PpmTyped) |
PpmFlowGraph => true
PpmFlowGraph(_) => true
}
}

Expand All @@ -431,7 +442,7 @@ fn needs_expansion(ppm: &PpMode) -> bool {
PpmSource(PpmExpandedIdentified) |
PpmSource(PpmExpandedHygiene) |
PpmSource(PpmTyped) |
PpmFlowGraph => true
PpmFlowGraph(_) => true
}
}

Expand Down Expand Up @@ -589,7 +600,7 @@ pub fn pretty_print_input(sess: Session,
pp::eof(&mut pp_state.s)
}),

(PpmFlowGraph, opt_uii) => {
(PpmFlowGraph(mode), opt_uii) => {
debug!("pretty printing flow graph for {:?}", opt_uii);
let uii = opt_uii.unwrap_or_else(|| {
sess.fatal(&format!("`pretty flowgraph=..` needs NodeId (int) or
Expand All @@ -613,7 +624,7 @@ pub fn pretty_print_input(sess: Session,
&arenas,
id,
resolve::MakeGlobMap::No);
print_flowgraph(variants, analysis, code, out)
print_flowgraph(variants, analysis, code, mode, out)
}
None => {
let message = format!("--pretty=flowgraph needs \
Expand All @@ -635,20 +646,23 @@ pub fn pretty_print_input(sess: Session,
fn print_flowgraph<W:io::Writer>(variants: Vec<borrowck_dot::Variant>,
analysis: ty::CrateAnalysis,
code: blocks::Code,
mode: PpFlowGraphMode,
mut out: W) -> io::IoResult<()> {
let ty_cx = &analysis.ty_cx;
let cfg = match code {
blocks::BlockCode(block) => cfg::CFG::new(ty_cx, &*block),
blocks::FnLikeCode(fn_like) => cfg::CFG::new(ty_cx, &*fn_like.body()),
};
let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
let lcfg = LabelledCFG {
ast_map: &ty_cx.map,
cfg: &cfg,
name: format!("node_{}", code.id()),
labelled_edges: labelled_edges,
};

match code {
_ if variants.len() == 0 => {
let lcfg = LabelledCFG {
ast_map: &ty_cx.map,
cfg: &cfg,
name: format!("node_{}", code.id()),
};
let r = dot::render(&lcfg, &mut out);
return expand_err_details(r);
}
Expand All @@ -662,11 +676,6 @@ fn print_flowgraph<W:io::Writer>(variants: Vec<borrowck_dot::Variant>,
let (bccx, analysis_data) =
borrowck::build_borrowck_dataflow_data_for_fn(ty_cx, fn_parts);

let lcfg = LabelledCFG {
ast_map: &ty_cx.map,
cfg: &cfg,
name: format!("node_{}", code.id()),
};
let lcfg = borrowck_dot::DataflowLabeller {
inner: lcfg,
variants: variants,
Expand Down

0 comments on commit 82eefe3

Please sign in to comment.