Skip to content

Commit

Permalink
feat(oxc_cfg): better control flow graph dot dot repr (#5731)
Browse files Browse the repository at this point in the history
1. add basic block index for each basicblock, so there should no basic block display empty string
2. use `shape` box, since it is basic **block**
## Before
![image](https://github.com/user-attachments/assets/9b556cca-8401-40bd-b3b6-d31c5e7cf8ba)

## After
![image](https://github.com/user-attachments/assets/b8e4c52d-ecdf-4e92-85be-ea37f0cb2330)
  • Loading branch information
IWANABETHATGUY committed Sep 12, 2024
1 parent d8b612c commit 805fbac
Show file tree
Hide file tree
Showing 43 changed files with 540 additions and 336 deletions.
16 changes: 12 additions & 4 deletions crates/oxc_semantic/src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ impl DebugDot for ControlFlowGraph {
label
}
},
&|_graph, node| format!(
"label = {:?} ",
self.basic_blocks[*node.1].debug_dot(ctx).trim()
),
&|_graph, node| {
let basic_block_index = *node.1;
let basic_block_debug_str = self.basic_blocks[*node.1].debug_dot(ctx);
let trimmed_debug_str = basic_block_debug_str.trim();
if trimmed_debug_str.is_empty() {
format!("label = \"bb{basic_block_index}\" shape = box",)
} else {
format!(
"label = \"bb{basic_block_index}\n{trimmed_debug_str}\" shape = box",
)
}
},
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ bb4: {
}

digraph {
0 [ label = "" ]
1 [ label = "" ]
2 [ label = "" ]
3 [ label = "ExpressionStatement" ]
4 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
4 [ label = "bb4" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ bb5: {
}

digraph {
0 [ label = "" ]
1 [ label = "ExpressionStatement\nVariableDeclaration" ]
2 [ label = "" ]
3 [ label = "ExpressionStatement" ]
4 [ label = "" ]
5 [ label = "ExpressionStatement" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
ExpressionStatement
VariableDeclaration" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
4 [ label = "bb4" shape = box]
5 [ label = "bb5
ExpressionStatement" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ bb7: {
}

digraph {
0 [ label = "" ]
1 [ label = "ExpressionStatement" ]
2 [ label = "" ]
3 [ label = "ExpressionStatement" ]
4 [ label = "" ]
5 [ label = "ExpressionStatement" ]
6 [ label = "" ]
7 [ label = "ExpressionStatement\nExpressionStatement" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
ExpressionStatement" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
4 [ label = "bb4" shape = box]
5 [ label = "bb5
ExpressionStatement" shape = box]
6 [ label = "bb6" shape = box]
7 [ label = "bb7
ExpressionStatement
ExpressionStatement" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
2 -> 0 [ label = "Error(Implicit)" ]
3 -> 0 [ label = "Error(Implicit)" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ bb3: {
}

digraph {
0 [ label = "" ]
1 [ label = "LabeledStatement(A)\nbreak <A>" ]
2 [ label = "unreachable" ]
3 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
LabeledStatement(A)
break <A>" shape = box]
2 [ label = "bb2
unreachable" shape = box]
3 [ label = "bb3" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
2 -> 0 [ label = "Error(Implicit)" , style = "dotted" ]
1 -> 2 [ label = "Unreachable" , style = "dotted" ]
Expand Down
11 changes: 6 additions & 5 deletions crates/oxc_semantic/tests/integration/snapshots/class.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ bb4: {
}

digraph {
0 [ label = "" ]
1 [ label = "" ]
2 [ label = "" ]
3 [ label = "ExpressionStatement" ]
4 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
4 [ label = "bb4" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ bb5: {
}

digraph {
0 [ label = "" ]
1 [ label = "" ]
2 [ label = "" ]
3 [ label = "return <value>" ]
4 [ label = "unreachable" ]
5 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
return <value>" shape = box]
4 [ label = "bb4
unreachable" shape = box]
5 [ label = "bb5" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ bb7: {
}

digraph {
0 [ label = "" ]
1 [ label = "VariableDeclaration" ]
2 [ label = "" ]
3 [ label = "ExpressionStatement" ]
4 [ label = "Condition(CallExpression(a))" ]
5 [ label = "" ]
6 [ label = "" ]
7 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
VariableDeclaration" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
4 [ label = "bb4
Condition(CallExpression(a))" shape = box]
5 [ label = "bb5" shape = box]
6 [ label = "bb6" shape = box]
7 [ label = "bb7" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ bb5: {
}

digraph {
0 [ label = "" ]
1 [ label = "VariableDeclaration" ]
2 [ label = "Condition(CallExpression(a))" ]
3 [ label = "" ]
4 [ label = "" ]
5 [ label = "VariableDeclaration" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
VariableDeclaration" shape = box]
2 [ label = "bb2
Condition(CallExpression(a))" shape = box]
3 [ label = "bb3" shape = box]
4 [ label = "bb4" shape = box]
5 [ label = "bb5
VariableDeclaration" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
2 -> 0 [ label = "Error(Implicit)" ]
3 -> 0 [ label = "Error(Implicit)" ]
Expand Down
34 changes: 21 additions & 13 deletions crates/oxc_semantic/tests/integration/snapshots/do_while_break.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,27 @@ bb12: {
}

digraph {
0 [ label = "" ]
1 [ label = "VariableDeclaration" ]
2 [ label = "" ]
3 [ label = "TryStatement" ]
4 [ label = "" ]
5 [ label = "BlockStatement" ]
6 [ label = "DoWhileStatement" ]
7 [ label = "BlockStatement\nbreak" ]
8 [ label = "unreachable" ]
9 [ label = "Condition(true)" ]
10 [ label = "" ]
11 [ label = "" ]
12 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
VariableDeclaration" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
TryStatement" shape = box]
4 [ label = "bb4" shape = box]
5 [ label = "bb5
BlockStatement" shape = box]
6 [ label = "bb6
DoWhileStatement" shape = box]
7 [ label = "bb7
BlockStatement
break" shape = box]
8 [ label = "bb8
unreachable" shape = box]
9 [ label = "bb9
Condition(true)" shape = box]
10 [ label = "bb10" shape = box]
11 [ label = "bb11" shape = box]
12 [ label = "bb12" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ bb1: {
}

digraph {
0 [ label = "" ]
1 [ label = "VariableDeclaration" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
VariableDeclaration" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ bb5: {
}

digraph {
0 [ label = "" ]
1 [ label = "" ]
2 [ label = "" ]
3 [ label = "return <value>" ]
4 [ label = "unreachable" ]
5 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
return <value>" shape = box]
4 [ label = "bb4
unreachable" shape = box]
5 [ label = "bb5" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
23 changes: 14 additions & 9 deletions crates/oxc_semantic/tests/integration/snapshots/for_in.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ bb8: {
}

digraph {
0 [ label = "" ]
1 [ label = "" ]
2 [ label = "" ]
3 [ label = "ForStatement\nVariableDeclaration" ]
4 [ label = "Condition(test)" ]
5 [ label = "" ]
6 [ label = "ExpressionStatement" ]
7 [ label = "ExpressionStatement" ]
8 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ForStatement
VariableDeclaration" shape = box]
4 [ label = "bb4
Condition(test)" shape = box]
5 [ label = "bb5" shape = box]
6 [ label = "bb6
ExpressionStatement" shape = box]
7 [ label = "bb7
ExpressionStatement" shape = box]
8 [ label = "bb8" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,45 +160,55 @@ bb38: {
}

digraph {
0 [ label = "" ]
1 [ label = "ExpressionStatement" ]
2 [ label = "" ]
3 [ label = "" ]
4 [ label = "ExpressionStatement" ]
5 [ label = "" ]
6 [ label = "" ]
7 [ label = "" ]
8 [ label = "" ]
9 [ label = "" ]
10 [ label = "ExpressionStatement" ]
11 [ label = "" ]
12 [ label = "" ]
13 [ label = "ExpressionStatement" ]
14 [ label = "" ]
15 [ label = "" ]
16 [ label = "ExpressionStatement" ]
17 [ label = "" ]
18 [ label = "" ]
19 [ label = "ExpressionStatement" ]
20 [ label = "" ]
21 [ label = "" ]
22 [ label = "ExpressionStatement" ]
23 [ label = "" ]
24 [ label = "" ]
25 [ label = "" ]
26 [ label = "" ]
27 [ label = "ExpressionStatement" ]
28 [ label = "" ]
29 [ label = "" ]
30 [ label = "" ]
31 [ label = "" ]
32 [ label = "ExpressionStatement" ]
33 [ label = "" ]
34 [ label = "" ]
35 [ label = "ExpressionStatement" ]
36 [ label = "" ]
37 [ label = "" ]
38 [ label = "" ]
0 [ label = "bb0" shape = box]
1 [ label = "bb1
ExpressionStatement" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3" shape = box]
4 [ label = "bb4
ExpressionStatement" shape = box]
5 [ label = "bb5" shape = box]
6 [ label = "bb6" shape = box]
7 [ label = "bb7" shape = box]
8 [ label = "bb8" shape = box]
9 [ label = "bb9" shape = box]
10 [ label = "bb10
ExpressionStatement" shape = box]
11 [ label = "bb11" shape = box]
12 [ label = "bb12" shape = box]
13 [ label = "bb13
ExpressionStatement" shape = box]
14 [ label = "bb14" shape = box]
15 [ label = "bb15" shape = box]
16 [ label = "bb16
ExpressionStatement" shape = box]
17 [ label = "bb17" shape = box]
18 [ label = "bb18" shape = box]
19 [ label = "bb19
ExpressionStatement" shape = box]
20 [ label = "bb20" shape = box]
21 [ label = "bb21" shape = box]
22 [ label = "bb22
ExpressionStatement" shape = box]
23 [ label = "bb23" shape = box]
24 [ label = "bb24" shape = box]
25 [ label = "bb25" shape = box]
26 [ label = "bb26" shape = box]
27 [ label = "bb27
ExpressionStatement" shape = box]
28 [ label = "bb28" shape = box]
29 [ label = "bb29" shape = box]
30 [ label = "bb30" shape = box]
31 [ label = "bb31" shape = box]
32 [ label = "bb32
ExpressionStatement" shape = box]
33 [ label = "bb33" shape = box]
34 [ label = "bb34" shape = box]
35 [ label = "bb35
ExpressionStatement" shape = box]
36 [ label = "bb36" shape = box]
37 [ label = "bb37" shape = box]
38 [ label = "bb38" shape = box]
1 -> 0 [ label = "Error(Implicit)" ]
3 -> 2 [ label = "Error(Implicit)" ]
1 -> 3 [ label = "NewFunction" ]
Expand Down
Loading

0 comments on commit 805fbac

Please sign in to comment.