Make correct traverse in both FIR consistency tests
This commit is contained in:
+15
-7
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.FirElement
|
|||||||
import org.jetbrains.kotlin.fir.FirRenderer
|
import org.jetbrains.kotlin.fir.FirRenderer
|
||||||
import org.jetbrains.kotlin.fir.FirSessionBase
|
import org.jetbrains.kotlin.fir.FirSessionBase
|
||||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||||
|
import org.jetbrains.kotlin.fir.render
|
||||||
import org.jetbrains.kotlin.fir.types.FirType
|
import org.jetbrains.kotlin.fir.types.FirType
|
||||||
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
||||||
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
||||||
@@ -31,6 +32,7 @@ import org.jetbrains.kotlin.test.KotlinTestUtils
|
|||||||
import org.jetbrains.kotlin.test.testFramework.KtParsingTestCase
|
import org.jetbrains.kotlin.test.testFramework.KtParsingTestCase
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.reflect.full.memberProperties
|
import kotlin.reflect.full.memberProperties
|
||||||
|
import kotlin.reflect.jvm.isAccessible
|
||||||
|
|
||||||
abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
||||||
"",
|
"",
|
||||||
@@ -78,9 +80,15 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
|||||||
if (!result.add(this)) {
|
if (!result.add(this)) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
for (property in this::class.memberProperties) {
|
propertyLoop@ for (property in this::class.memberProperties) {
|
||||||
val childElement = property.getter.call(this) as? FirElement ?: continue
|
val childElement = property.getter.apply { isAccessible = true }.call(this)
|
||||||
childElement.traverseChildren(result)
|
|
||||||
|
when (childElement) {
|
||||||
|
is FirElement -> childElement.traverseChildren(result)
|
||||||
|
is List<*> -> childElement.filterIsInstance<FirElement>().forEach { it.traverseChildren(result) }
|
||||||
|
else -> continue@propertyLoop
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -93,7 +101,7 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
|||||||
|
|
||||||
private fun FirFile.transformChildren(): Set<FirElement> =
|
private fun FirFile.transformChildren(): Set<FirElement> =
|
||||||
ConsistencyTransformer().let {
|
ConsistencyTransformer().let {
|
||||||
this@transformChildren.accept(it, Unit)
|
this@transformChildren.transform<FirFile, Unit>(it, Unit)
|
||||||
it.result
|
it.result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +111,7 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
|||||||
children.removeAll(visitedChildren)
|
children.removeAll(visitedChildren)
|
||||||
if (children.isNotEmpty()) {
|
if (children.isNotEmpty()) {
|
||||||
val element = children.first()
|
val element = children.first()
|
||||||
val elementDump = StringBuilder().also { element.accept(FirRenderer(it)) }.toString()
|
val elementDump = element.render()
|
||||||
throw AssertionError("FirElement ${element.javaClass} is not visited: $elementDump")
|
throw AssertionError("FirElement ${element.javaClass} is not visited: $elementDump")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +122,7 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
|||||||
children.removeAll(transformedChildren)
|
children.removeAll(transformedChildren)
|
||||||
if (children.isNotEmpty()) {
|
if (children.isNotEmpty()) {
|
||||||
val element = children.first()
|
val element = children.first()
|
||||||
val elementDump = StringBuilder().also { element.accept(FirRenderer(it)) }.toString()
|
val elementDump = element.render()
|
||||||
throw AssertionError("FirElement ${element.javaClass} is not transformed: $elementDump")
|
throw AssertionError("FirElement ${element.javaClass} is not transformed: $elementDump")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,7 +153,7 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
|||||||
throw AssertionError("FirElement ${element.javaClass} is visited twice: $elementDump")
|
throw AssertionError("FirElement ${element.javaClass} is visited twice: $elementDump")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
element.acceptChildren(this, Unit)
|
element.transformChildren(this, Unit)
|
||||||
}
|
}
|
||||||
return CompositeTransformResult(element)
|
return CompositeTransformResult(element)
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-16
@@ -141,7 +141,7 @@ class RawFirBuilderTotalKotlinTestCase : AbstractRawFirBuilderTestCase() {
|
|||||||
testTotalKotlinWithGivenMode(stubMode = true)
|
testTotalKotlinWithGivenMode(stubMode = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testVisitConsistency() {
|
private fun testConsistency(checkConsistency: FirFile.() -> Unit) {
|
||||||
val root = File(testDataPath)
|
val root = File(testDataPath)
|
||||||
for (file in root.walkTopDown()) {
|
for (file in root.walkTopDown()) {
|
||||||
if (file.isDirectory) continue
|
if (file.isDirectory) continue
|
||||||
@@ -150,7 +150,7 @@ class RawFirBuilderTotalKotlinTestCase : AbstractRawFirBuilderTestCase() {
|
|||||||
val ktFile = createKtFile(file.toRelativeString(root))
|
val ktFile = createKtFile(file.toRelativeString(root))
|
||||||
val firFile = ktFile.toFirFile(stubMode = false)
|
val firFile = ktFile.toFirFile(stubMode = false)
|
||||||
try {
|
try {
|
||||||
firFile.checkChildren()
|
firFile.checkConsistency()
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
println("EXCEPTION in: " + file.toRelativeString(root))
|
println("EXCEPTION in: " + file.toRelativeString(root))
|
||||||
throw e
|
throw e
|
||||||
@@ -158,20 +158,11 @@ class RawFirBuilderTotalKotlinTestCase : AbstractRawFirBuilderTestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testVisitConsistency() {
|
||||||
|
testConsistency { checkChildren() }
|
||||||
|
}
|
||||||
|
|
||||||
fun testTransformConsistency() {
|
fun testTransformConsistency() {
|
||||||
val root = File(testDataPath)
|
testConsistency { checkTransformedChildren() }
|
||||||
for (file in root.walkTopDown()) {
|
|
||||||
if (file.isDirectory) continue
|
|
||||||
if (file.path.contains("testData") || file.path.contains("resources")) continue
|
|
||||||
if (file.extension != "kt") continue
|
|
||||||
val ktFile = createKtFile(file.toRelativeString(root))
|
|
||||||
val firFile = ktFile.toFirFile(stubMode = false)
|
|
||||||
try {
|
|
||||||
firFile.checkTransformedChildren()
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
println("EXCEPTION in: " + file.toRelativeString(root))
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user