Minor: FIR DFA: fix code style

This commit is contained in:
pyos
2022-12-20 13:32:32 +01:00
committed by Dmitriy Novozhilov
parent 5d4b44500a
commit 308e1362ab
6 changed files with 29 additions and 27 deletions
@@ -227,11 +227,11 @@ object FirCallsEffectAnalyzer : FirControlFlowChecker() {
val functionalTypeSymbols: Set<FirBasedSymbol<*>>
) : PathAwareControlFlowGraphVisitor<LambdaInvocationInfo>() {
companion object {
val EMPTY: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY)
private val EMPTY_INFO: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY)
}
override val emptyInfo: PathAwareLambdaInvocationInfo
get() = EMPTY
get() = EMPTY_INFO
override fun visitFunctionCallNode(
node: FunctionCallNode,
@@ -33,31 +33,31 @@ abstract class PathAwareControlFlowGraphVisitor<I : ControlFlowInfo<I, *, *>> :
open fun visitEdge(from: CFGNode<*>, to: CFGNode<*>, metadata: Edge, data: PathAwareControlFlowInfo<I>): PathAwareControlFlowInfo<I> {
val label = metadata.label
return if (from is FinallyBlockExitNode) {
return when {
// Finally exit is splitting labeled flow. So if we have data for different labels, then
// data for each only goes along an edge with the same label, and the leftover data
// is forwarded along an UncaughtExceptionPath edge, if any, to the next finally block.
if (label == UncaughtExceptionPath) {
data.mutate {
for (other in from.followingNodes) {
val otherLabel = from.edgeTo(other).label
if (otherLabel != UncaughtExceptionPath) {
it.remove(otherLabel)
from is FinallyBlockExitNode -> {
if (label == UncaughtExceptionPath) {
data.mutate {
for (other in from.followingNodes) {
val otherLabel = from.edgeTo(other).label
if (otherLabel != UncaughtExceptionPath) {
it.remove(otherLabel)
}
}
}
}.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case
} else {
val info = data[label] ?: return emptyInfo
persistentMapOf(NormalPath to info)
}.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case
} else {
val info = data[label] ?: return emptyInfo
persistentMapOf(NormalPath to info)
}
}
} else if (label == NormalPath) {
// A normal path forwards all data. (Non-normal paths should only have data in finally blocks.)
data
} else {
label == NormalPath -> data
// Labeled edge from a jump statement to a `finally` block forks flow. Usually we'd only have
// NormalPath data here, but technically it's possible (though questionable) to jump from a `finally`
// (discarding the exception or aborting a previous jump in the process) so merge all data just in case.
persistentMapOf(label to data.values.reduce { a, b -> a.merge(b) })
else -> persistentMapOf(label to data.values.reduce { a, b -> a.merge(b) })
}
}
@@ -26,11 +26,11 @@ class PropertyInitializationInfoCollector(
private val declaredVariableCollector: DeclaredVariableCollector = DeclaredVariableCollector(),
) : PathAwareControlFlowGraphVisitor<PropertyInitializationInfo>() {
companion object {
val EMPTY: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY)
private val EMPTY_INFO: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY)
}
override val emptyInfo: PathAwarePropertyInitializationInfo
get() = EMPTY
get() = EMPTY_INFO
override fun visitVariableAssignmentNode(
node: VariableAssignmentNode,
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
object FirPropertyInitializationChecker : FirRegularClassChecker() {
override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) {
val interestingProperties = mutableSetOf<FirPropertySymbol>()
val declaredLater = mutableSetOf<FirPropertySymbol>()
val visitor = object : FirVisitorVoid() {
override fun visitElement(element: FirElement) = element.acceptChildren(this)
@@ -39,17 +39,17 @@ object FirPropertyInitializationChecker : FirRegularClassChecker() {
override fun visitVariableAssignment(variableAssignment: FirVariableAssignment) {
variableAssignment.acceptChildren(this)
val propertySymbol = variableAssignment.lValue.toResolvedCallableSymbol() as? FirPropertySymbol ?: return
if (propertySymbol !in interestingProperties) return
if (propertySymbol !in declaredLater) return
reporter.reportOn(variableAssignment.lValue.source, FirErrors.INITIALIZATION_BEFORE_DECLARATION, propertySymbol, context)
}
}
for (member in declaration.declarations.asReversed()) {
if (interestingProperties.isNotEmpty()) {
if (declaredLater.isNotEmpty()) {
member.accept(visitor)
}
if (member is FirProperty) {
interestingProperties.add(member.symbol)
declaredLater.add(member.symbol)
}
}
}
@@ -158,11 +158,11 @@ object UnusedChecker : AbstractFirPropertyInitializationChecker() {
private val localProperties: Set<FirPropertySymbol>
) : PathAwareControlFlowGraphVisitor<VariableStatusInfo>() {
companion object {
val EMPTY: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY)
private val EMPTY_INFO: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY)
}
override val emptyInfo: PathAwareVariableStatusInfo
get() = EMPTY
get() = EMPTY_INFO
fun getData(graph: ControlFlowGraph): Map<CFGNode<*>, PathAwareVariableStatusInfo> {
return graph.collectDataForNode(TraverseDirection.Backward, this)
@@ -229,6 +229,7 @@ class AnonymousFunctionExpressionNode(owner: ControlFlowGraph, override val fir:
class ClassEnterNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs<FirClass>(owner, level),
GraphEnterNodeMarker {
@set:CfgInternals
override lateinit var subGraphs: List<ControlFlowGraph>
override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R {
@@ -239,7 +240,8 @@ class ClassEnterNode(owner: ControlFlowGraph, override val fir: FirClass, level:
class ClassExitNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs<FirClass>(owner, level),
GraphExitNodeMarker, UnionNodeMarker {
lateinit override var subGraphs: List<ControlFlowGraph>
@set:CfgInternals
override lateinit var subGraphs: List<ControlFlowGraph>
override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R {
return visitor.visitClassExitNode(this, data)