fix(semantic): add cfg nodes for ConditionalExpressions. (#3127)

It is done similarly to how `IfStatement`s are structured at the moment.
This commit is contained in:
Ali Rezvani 2024-05-10 17:46:55 +03:30 committed by GitHub
parent c91d26129c
commit 5e36e0d575
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 98 additions and 2 deletions

View file

@ -765,6 +765,54 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.leave_node(kind);
}
fn visit_conditional_expression(&mut self, expr: &ConditionalExpression<'a>) {
let kind = AstKind::ConditionalExpression(self.alloc(expr));
self.enter_node(kind);
self.visit_expression(&expr.test);
/* cfg */
let before_conditional_expr_graph_ix = self.cfg.current_node_ix;
// conditional expression basic block
let before_consequent_expr_graph_ix = self.cfg.new_basic_block();
/* cfg */
self.visit_expression(&expr.consequent);
/* cfg */
let after_consequent_expr_graph_ix = self.cfg.current_node_ix;
let start_alternate_graph_ix = self.cfg.new_basic_block();
/* cfg */
self.visit_expression(&expr.alternate);
/* cfg */
let after_alternate_graph_ix = self.cfg.current_node_ix;
/* bb after conditional expression joins consequent and alternate */
let after_conditional_graph_ix = self.cfg.new_basic_block();
/* cfg */
self.cfg.put_unreachable();
self.cfg.add_edge(
after_consequent_expr_graph_ix,
after_conditional_graph_ix,
EdgeType::Normal,
);
self.cfg.add_edge(
before_conditional_expr_graph_ix,
before_consequent_expr_graph_ix,
EdgeType::Normal,
);
self.cfg.add_edge(
before_conditional_expr_graph_ix,
start_alternate_graph_ix,
EdgeType::Normal,
);
self.cfg.add_edge(after_alternate_graph_ix, after_conditional_graph_ix, EdgeType::Normal);
self.leave_node(kind);
}
fn visit_for_statement(&mut self, stmt: &ForStatement<'a>) {
let kind = AstKind::ForStatement(self.alloc(stmt));
let is_lexical_declaration =

View file

@ -6,6 +6,14 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/cond_expr_in_arrow_fn.js
digraph {
0 [ label = ""]
1 [ label = ""]
2 [ label = ""]
3 [ label = ""]
4 [ label = ""]
5 [ label = "Unreachable()"]
0 -> 1 [ ]
4 -> 5 [ ]
2 -> 4 [ ]
1 -> 2 [ ]
1 -> 3 [ ]
3 -> 4 [ ]
}

View file

@ -10,3 +10,19 @@ bb0: {
bb1: {
}
bb2: {
}
bb3: {
}
bb4: {
}
bb5: {
Unreachable()
}

View file

@ -5,5 +5,13 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/conditional_expression.js
---
digraph {
0 [ label = ""]
1 [ label = ""]
2 [ label = ""]
3 [ label = ""]
4 [ label = "Unreachable()"]
3 -> 4 [ ]
1 -> 3 [ ]
0 -> 1 [ ]
0 -> 2 [ ]
2 -> 3 [ ]
}

View file

@ -6,3 +6,19 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/conditional_expression.js
bb0: {
}
bb1: {
}
bb2: {
}
bb3: {
}
bb4: {
Unreachable()
}