Skip to content

Commit

Permalink
improvement(semantic/cfg): CFG API for detecting conditional paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jun 1, 2024
1 parent 62f0432 commit 1150447
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_semantic/src/control_flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ impl ControlFlowGraph {
})
.is_err()
}

pub fn has_conditional_path(&self, from: BasicBlockId, to: BasicBlockId) -> bool {
let graph = &self.graph;
// All nodes should be able to reach the `to` node, Otherwise we have a conditional/branching flow.
petgraph::algo::dijkstra(graph, from, Some(to), |e| match e.weight() {
EdgeType::NewFunction => 1,
EdgeType::Jump | EdgeType::Unreachable | EdgeType::Backedge | EdgeType::Normal => 0,
})
.into_iter()
.filter(|(_, val)| *val == 0)
.any(|(f, _)| !self.is_reachabale(f, to))
}
}

pub struct PreservedExpressionState {
Expand Down

0 comments on commit 1150447

Please sign in to comment.