Skip to content

Commit

Permalink
fix(semantic): add cfg nodes for ConditionalExpressions. (#3127)
Browse files Browse the repository at this point in the history
It is done similarly to how `IfStatement`s are structured at the moment.
  • Loading branch information
rzvxa committed May 10, 2024
1 parent c91d261 commit 5e36e0d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
48 changes: 48 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [ ]
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ bb0: {
bb1: {

}

bb2: {

}

bb3: {

}

bb4: {

}

bb5: {
Unreachable()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 [ ]
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ input_file: crates/oxc_semantic/tests/cfg_fixtures/conditional_expression.js
bb0: {

}

bb1: {

}

bb2: {

}

bb3: {

}

bb4: {
Unreachable()
}

0 comments on commit 5e36e0d

Please sign in to comment.