Skip to content

Commit 91c9995

Browse files
committed
improvement(semantic/cfg): better control flow for DoWhileStatements. (#3452)
similar to #3451
1 parent 0012094 commit 91c9995

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

crates/oxc_semantic/src/builder.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
649649
self.visit_statement(&stmt.body);
650650

651651
/* cfg - condition basic block */
652+
let after_body_graph_ix = self.cfg.current_node_ix;
652653
let start_of_condition_graph_ix = self.cfg.new_basic_block();
653654
/* cfg */
654655

@@ -659,18 +660,14 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
659660

660661
let end_do_while_graph_ix = self.cfg.new_basic_block();
661662

662-
// before do while to start of condition basic block
663-
self.cfg.add_edge(
664-
before_do_while_stmt_graph_ix,
665-
start_of_condition_graph_ix,
666-
EdgeType::Normal,
667-
);
663+
// before do while to start of body basic block
664+
self.cfg.add_edge(before_do_while_stmt_graph_ix, start_body_graph_ix, EdgeType::Normal);
668665
// body of do-while to start of condition
669-
self.cfg.add_edge(start_body_graph_ix, start_of_condition_graph_ix, EdgeType::Backedge);
666+
self.cfg.add_edge(after_body_graph_ix, start_of_condition_graph_ix, EdgeType::Normal);
670667
// end of condition to after do while
671668
self.cfg.add_edge(end_of_condition_graph_ix, end_do_while_graph_ix, EdgeType::Normal);
672669
// end of condition to after start of body
673-
self.cfg.add_edge(end_of_condition_graph_ix, start_body_graph_ix, EdgeType::Normal);
670+
self.cfg.add_edge(end_of_condition_graph_ix, start_body_graph_ix, EdgeType::Backedge);
674671

675672
self.cfg.restore_state(
676673
&statement_state,

crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@do_while_break.js-2.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ digraph {
2626
2 -> 3 [ label = Normal ]
2727
4 -> 5 [ label = Unreachable , style = "dotted" ]
2828
4 -> 5 [ label = Normal ]
29-
3 -> 6 [ label = Normal ]
30-
4 -> 6 [ label = Backedge ]
29+
3 -> 4 [ label = Normal ]
30+
5 -> 6 [ label = Normal ]
3131
6 -> 7 [ label = Normal ]
32-
6 -> 4 [ label = Normal ]
32+
6 -> 4 [ label = Backedge ]
3333
7 -> 8 [ label = Unreachable , style = "dotted" ]
3434
2 -> 9 [ label = Normal ]
3535
10 -> 11 [ label = Unreachable , style = "dotted" ]
3636
10 -> 11 [ label = Normal ]
37-
9 -> 12 [ label = Normal ]
38-
10 -> 12 [ label = Backedge ]
37+
9 -> 10 [ label = Normal ]
38+
11 -> 12 [ label = Normal ]
3939
12 -> 13 [ label = Normal ]
40-
12 -> 10 [ label = Normal ]
40+
12 -> 10 [ label = Backedge ]
4141
13 -> 14 [ label = Normal ]
4242
0 -> 15 [ label = Normal ]
4343
}

0 commit comments

Comments
 (0)