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<*>> val functionalTypeSymbols: Set<FirBasedSymbol<*>>
) : PathAwareControlFlowGraphVisitor<LambdaInvocationInfo>() { ) : PathAwareControlFlowGraphVisitor<LambdaInvocationInfo>() {
companion object { companion object {
val EMPTY: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY) private val EMPTY_INFO: PathAwareLambdaInvocationInfo = persistentMapOf(NormalPath to LambdaInvocationInfo.EMPTY)
} }
override val emptyInfo: PathAwareLambdaInvocationInfo override val emptyInfo: PathAwareLambdaInvocationInfo
get() = EMPTY get() = EMPTY_INFO
override fun visitFunctionCallNode( override fun visitFunctionCallNode(
node: FunctionCallNode, 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> { open fun visitEdge(from: CFGNode<*>, to: CFGNode<*>, metadata: Edge, data: PathAwareControlFlowInfo<I>): PathAwareControlFlowInfo<I> {
val label = metadata.label 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 // 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 // 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. // is forwarded along an UncaughtExceptionPath edge, if any, to the next finally block.
if (label == UncaughtExceptionPath) { from is FinallyBlockExitNode -> {
data.mutate { if (label == UncaughtExceptionPath) {
for (other in from.followingNodes) { data.mutate {
val otherLabel = from.edgeTo(other).label for (other in from.followingNodes) {
if (otherLabel != UncaughtExceptionPath) { val otherLabel = from.edgeTo(other).label
it.remove(otherLabel) if (otherLabel != UncaughtExceptionPath) {
it.remove(otherLabel)
}
} }
} }.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case
}.ifEmpty { emptyInfo } // there should always be UncaughtExceptionPath data, but just in case } else {
} else { val info = data[label] ?: return emptyInfo
val info = data[label] ?: return emptyInfo persistentMapOf(NormalPath to info)
persistentMapOf(NormalPath to info) }
} }
} else if (label == NormalPath) {
// A normal path forwards all data. (Non-normal paths should only have data in finally blocks.) // A normal path forwards all data. (Non-normal paths should only have data in finally blocks.)
data label == NormalPath -> data
} else {
// Labeled edge from a jump statement to a `finally` block forks flow. Usually we'd only have // 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` // 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. // (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(), private val declaredVariableCollector: DeclaredVariableCollector = DeclaredVariableCollector(),
) : PathAwareControlFlowGraphVisitor<PropertyInitializationInfo>() { ) : PathAwareControlFlowGraphVisitor<PropertyInitializationInfo>() {
companion object { companion object {
val EMPTY: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY) private val EMPTY_INFO: PathAwarePropertyInitializationInfo = persistentMapOf(NormalPath to PropertyInitializationInfo.EMPTY)
} }
override val emptyInfo: PathAwarePropertyInitializationInfo override val emptyInfo: PathAwarePropertyInitializationInfo
get() = EMPTY get() = EMPTY_INFO
override fun visitVariableAssignmentNode( override fun visitVariableAssignmentNode(
node: VariableAssignmentNode, node: VariableAssignmentNode,
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
object FirPropertyInitializationChecker : FirRegularClassChecker() { object FirPropertyInitializationChecker : FirRegularClassChecker() {
override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) { override fun check(declaration: FirRegularClass, context: CheckerContext, reporter: DiagnosticReporter) {
val interestingProperties = mutableSetOf<FirPropertySymbol>() val declaredLater = mutableSetOf<FirPropertySymbol>()
val visitor = object : FirVisitorVoid() { val visitor = object : FirVisitorVoid() {
override fun visitElement(element: FirElement) = element.acceptChildren(this) override fun visitElement(element: FirElement) = element.acceptChildren(this)
@@ -39,17 +39,17 @@ object FirPropertyInitializationChecker : FirRegularClassChecker() {
override fun visitVariableAssignment(variableAssignment: FirVariableAssignment) { override fun visitVariableAssignment(variableAssignment: FirVariableAssignment) {
variableAssignment.acceptChildren(this) variableAssignment.acceptChildren(this)
val propertySymbol = variableAssignment.lValue.toResolvedCallableSymbol() as? FirPropertySymbol ?: return 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) reporter.reportOn(variableAssignment.lValue.source, FirErrors.INITIALIZATION_BEFORE_DECLARATION, propertySymbol, context)
} }
} }
for (member in declaration.declarations.asReversed()) { for (member in declaration.declarations.asReversed()) {
if (interestingProperties.isNotEmpty()) { if (declaredLater.isNotEmpty()) {
member.accept(visitor) member.accept(visitor)
} }
if (member is FirProperty) { if (member is FirProperty) {
interestingProperties.add(member.symbol) declaredLater.add(member.symbol)
} }
} }
} }
@@ -158,11 +158,11 @@ object UnusedChecker : AbstractFirPropertyInitializationChecker() {
private val localProperties: Set<FirPropertySymbol> private val localProperties: Set<FirPropertySymbol>
) : PathAwareControlFlowGraphVisitor<VariableStatusInfo>() { ) : PathAwareControlFlowGraphVisitor<VariableStatusInfo>() {
companion object { companion object {
val EMPTY: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY) private val EMPTY_INFO: PathAwareVariableStatusInfo = persistentMapOf(NormalPath to VariableStatusInfo.EMPTY)
} }
override val emptyInfo: PathAwareVariableStatusInfo override val emptyInfo: PathAwareVariableStatusInfo
get() = EMPTY get() = EMPTY_INFO
fun getData(graph: ControlFlowGraph): Map<CFGNode<*>, PathAwareVariableStatusInfo> { fun getData(graph: ControlFlowGraph): Map<CFGNode<*>, PathAwareVariableStatusInfo> {
return graph.collectDataForNode(TraverseDirection.Backward, this) 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), class ClassEnterNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs<FirClass>(owner, level),
GraphEnterNodeMarker { GraphEnterNodeMarker {
@set:CfgInternals
override lateinit var subGraphs: List<ControlFlowGraph> override lateinit var subGraphs: List<ControlFlowGraph>
override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R { 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), class ClassExitNode(owner: ControlFlowGraph, override val fir: FirClass, level: Int) : CFGNodeWithSubgraphs<FirClass>(owner, level),
GraphExitNodeMarker, UnionNodeMarker { 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 { override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R {
return visitor.visitClassExitNode(this, data) return visitor.visitClassExitNode(this, data)