mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
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:
parent
c91d26129c
commit
5e36e0d575
5 changed files with 98 additions and 2 deletions
|
|
@ -765,6 +765,54 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
||||||
self.leave_node(kind);
|
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>) {
|
fn visit_for_statement(&mut self, stmt: &ForStatement<'a>) {
|
||||||
let kind = AstKind::ForStatement(self.alloc(stmt));
|
let kind = AstKind::ForStatement(self.alloc(stmt));
|
||||||
let is_lexical_declaration =
|
let is_lexical_declaration =
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,14 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/cond_expr_in_arrow_fn.js
|
||||||
digraph {
|
digraph {
|
||||||
0 [ label = ""]
|
0 [ label = ""]
|
||||||
1 [ label = ""]
|
1 [ label = ""]
|
||||||
|
2 [ label = ""]
|
||||||
|
3 [ label = ""]
|
||||||
|
4 [ label = ""]
|
||||||
|
5 [ label = "Unreachable()"]
|
||||||
0 -> 1 [ ]
|
0 -> 1 [ ]
|
||||||
|
4 -> 5 [ ]
|
||||||
|
2 -> 4 [ ]
|
||||||
|
1 -> 2 [ ]
|
||||||
|
1 -> 3 [ ]
|
||||||
|
3 -> 4 [ ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,19 @@ bb0: {
|
||||||
bb1: {
|
bb1: {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bb2: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb3: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb4: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb5: {
|
||||||
|
Unreachable()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,13 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/conditional_expression.js
|
||||||
---
|
---
|
||||||
digraph {
|
digraph {
|
||||||
0 [ label = ""]
|
0 [ label = ""]
|
||||||
|
1 [ label = ""]
|
||||||
|
2 [ label = ""]
|
||||||
|
3 [ label = ""]
|
||||||
|
4 [ label = "Unreachable()"]
|
||||||
|
3 -> 4 [ ]
|
||||||
|
1 -> 3 [ ]
|
||||||
|
0 -> 1 [ ]
|
||||||
|
0 -> 2 [ ]
|
||||||
|
2 -> 3 [ ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,3 +6,19 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/conditional_expression.js
|
||||||
bb0: {
|
bb0: {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bb1: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb2: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb3: {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bb4: {
|
||||||
|
Unreachable()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue