Skip to content

Commit

Permalink
Cleanup control-flow graph
Browse files Browse the repository at this point in the history
  • Loading branch information
artemmukhin committed Mar 22, 2021
1 parent 3269d66 commit b17e2d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/intellij_rust.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/main/kotlin/org/rust/lang/core/dfa/ControlFlowGraph.kt
Expand Up @@ -40,7 +40,7 @@ sealed class CFGNodeData(val element: RsElement? = null) : PresentableNodeData {

override val text: String
get() = when (this) {
is AST -> element?.cfgText()?.trim() ?: "AST"
is AST -> element!!.cfgText().trim()
Entry -> "Entry"
Exit -> "Exit"
Termination -> "Termination"
Expand All @@ -56,6 +56,7 @@ sealed class CFGNodeData(val element: RsElement? = null) : PresentableNodeData {
is RsLoopExpr -> "LOOP"
is RsForExpr -> "FOR"
is RsMatchExpr -> "MATCH"
is RsLambdaExpr -> "CLOSURE"
is RsExprStmt -> expr.cfgText() + ";"
else -> this.text
}
Expand All @@ -64,10 +65,11 @@ sealed class CFGNodeData(val element: RsElement? = null) : PresentableNodeData {
class CFGEdgeData(val exitingScopes: List<RsElement>)

typealias CFGNode = Node<CFGNodeData, CFGEdgeData>
typealias CFGGraph = PresentableGraph<CFGNodeData, CFGEdgeData>

class ControlFlowGraph private constructor(
val owner: RsElement,
val graph: PresentableGraph<CFGNodeData, CFGEdgeData>,
val graph: CFGGraph,
val body: RsBlock,
val regionScopeTree: ScopeTree,
val entry: CFGNode,
Expand Down Expand Up @@ -97,7 +99,7 @@ class ControlFlowGraph private constructor(
*
* Only collects [RsExprStmt]s, [RsLetDecl]s and tail expressions, ignoring conditionally disabled.
*/
private fun collectUnreachableElements(graph: PresentableGraph<CFGNodeData, CFGEdgeData>, entry: CFGNode): Set<RsElement> {
private fun collectUnreachableElements(graph: CFGGraph, entry: CFGNode): Set<RsElement> {
/**
* In terms of our control-flow graph, a [RsElement]'s node is not reachable if it cannot be fully executed,
* since [CFGBuilder] processes elements in post-order.
Expand Down
Expand Up @@ -867,7 +867,7 @@ class RsControlFlowGraphTest : RsTestBase() {
x + 1
BLOCK
BLOCK
|x: i32| { x + 1 }
CLOSURE
f
f
let f = |x: i32| { x + 1 };
Expand Down Expand Up @@ -950,7 +950,7 @@ class RsControlFlowGraphTest : RsTestBase() {
Entry
panic!()
Termination
|| { panic!() }
CLOSURE
f
f
let f = || { panic!() };
Expand Down Expand Up @@ -1072,6 +1072,6 @@ class RsControlFlowGraphTest : RsTestBase() {
val cfg = ControlFlowGraph.buildFor(function.block!!, getRegionScopeTree(function))
val expected = expectedIndented.trimIndent()
val actual = cfg.graph.depthFirstTraversalTrace(cfg.entry)
check(actual == expected) { throw ComparisonFailure("Comparision failed", expected, actual) }
check(actual == expected) { throw ComparisonFailure("Comparison failed", expected, actual) }
}
}

0 comments on commit b17e2d9

Please sign in to comment.