Skip to content

Commit

Permalink
Improves forc build --dca-graph. (FuelLabs#4239)
Browse files Browse the repository at this point in the history
## Description

This commit changes graph.visualize to print nodes using
DisplayWithEngines.

Before the changes using `forc build --dca-graph` in a simple example
gives:
```
digraph {
    0 [ label = "Program exit" ]
    1 [ label = "TyAstNode { content: Declaration(StructDeclaration(DeclRef { name: BaseIdent { name_override_opt: None, span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 16, end: 17, as_str(): \"S\" }, is_raw_ident: false }, id: DeclId(0, PhantomData<sway_core::language::ty::declaration::struct::TyStructDeclaration>), decl_span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 9, end: 20, as_str(): \"struct S {}\" } })), span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 9, end: 20, as_str(): \"struct S {}\" } }" ]
    2 [ label = "TyAstNode { content: Declaration(FunctionDeclaration { name: BaseIdent { name_override_opt: None, span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 25, end: 29, as_str(): \"main\" }, is_raw_ident: false }, decl_id: DeclId(0, PhantomData<sway_core::language::ty::declaration::function::TyFunctionDeclaration>), decl_span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 22, end: 34, as_str(): \"fn main() {}\" } }), span: Span { src (ptr): 0x562355d3d3d0, path: Some(\"/home/marcos/Documents/dev/sway/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_struct/src/main.sw\"), start: 22, end: 34, as_str(): \"fn main() {}\" } }" ]
    3 [ label = "\"main\" fn exit" ]
    3 -> 0 [ ]
}
```

After the changes we get:
```
digraph {
    0 [ label = "\"Program exit\"" ]
    1 [ label = "\"struct declaration (S)\"" ]
    2 [ label = "\"function declaration (main)\"" ]
    3 [ label = "\"\\\"main\\\" fn exit\"" ]
    3 -> 0 [ ]
}
```




## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

Co-authored-by: João Matos <[email protected]>
  • Loading branch information
esdrubal and tritao authored Mar 8, 2023
1 parent b243c3d commit 89f29e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions sway-core/src/control_flow_analysis/flow_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::HashMap;

use crate::{
decl_engine::*,
engine_threading::DisplayWithEngines,
language::ty::{self, GetDeclIdent},
transform, Engines, Ident,
};
Expand Down Expand Up @@ -117,11 +118,11 @@ impl<'cfg> std::convert::From<&str> for ControlFlowGraphNode<'cfg> {
}
}

impl<'cfg> std::fmt::Debug for ControlFlowGraphNode<'cfg> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<'cfg> DisplayWithEngines for ControlFlowGraphNode<'cfg> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: Engines<'_>) -> std::fmt::Result {
let text = match self {
ControlFlowGraphNode::OrganizationalDominator(s) => s.to_string(),
ControlFlowGraphNode::ProgramNode { node, .. } => format!("{node:?}"),
ControlFlowGraphNode::ProgramNode { node, .. } => engines.help_out(node).to_string(),
ControlFlowGraphNode::EnumVariant { variant_name, .. } => {
format!("Enum variant {variant_name}")
}
Expand Down Expand Up @@ -195,11 +196,16 @@ impl<'cfg> ControlFlowGraph<'cfg> {
}

/// Prints out GraphViz DOT format for this graph.
pub(crate) fn visualize(&self) {
pub(crate) fn visualize(&self, engines: Engines<'_>) {
use petgraph::dot::{Config, Dot};
let string_graph = self.graph.filter_map(
|_idx, node| Some(engines.help_out(node).to_string()),
|_idx, edge| Some(edge.0.clone()),
);

tracing::info!(
"{:?}",
Dot::with_config(&self.graph, &[Config::EdgeNoLabel])
Dot::with_config(&string_graph, &[Config::EdgeNoLabel])
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ fn perform_control_flow_analysis(
};
if let Some(graph) = dca_res.clone().value {
if print_graph {
graph.visualize();
graph.visualize(engines);
}
}
dca_res.flat_map(|_| rpa_res)
Expand Down

0 comments on commit 89f29e7

Please sign in to comment.