[FIR LT] Make syntax error reporting in tests consistent with cli
This commit is contained in:
committed by
Space Team
parent
bdf0b41026
commit
6119606cb6
+2
@@ -51,6 +51,7 @@ import org.jetbrains.kotlin.test.getAnalyzerServices
|
||||
import org.jetbrains.kotlin.test.model.FrontendFacade
|
||||
import org.jetbrains.kotlin.test.model.FrontendKinds
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.runners.lightTreeSyntaxDiagnosticsReporterHolder
|
||||
import org.jetbrains.kotlin.test.services.*
|
||||
import java.nio.file.Paths
|
||||
|
||||
@@ -319,6 +320,7 @@ open class FirFrontendFacade(
|
||||
IrGenerationExtension.getInstances(project),
|
||||
parser,
|
||||
enablePluginPhases,
|
||||
testServices.lightTreeSyntaxDiagnosticsReporterHolder?.reporter,
|
||||
)
|
||||
val firFiles = firAnalyzerFacade.runResolution()
|
||||
val filesMap = firFiles.mapNotNull { firFile ->
|
||||
|
||||
+15
-15
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.test.frontend.fir.handlers
|
||||
|
||||
import com.intellij.lang.impl.PsiBuilderImpl
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.*
|
||||
import org.jetbrains.kotlin.checkers.diagnostics.factories.DebugInfoDiagnosticFactory0
|
||||
@@ -46,6 +45,7 @@ import org.jetbrains.kotlin.test.directives.model.singleValue
|
||||
import org.jetbrains.kotlin.test.frontend.fir.FirOutputArtifact
|
||||
import org.jetbrains.kotlin.test.model.TestFile
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.runners.lightTreeSyntaxDiagnosticsReporterHolder
|
||||
import org.jetbrains.kotlin.test.services.*
|
||||
import org.jetbrains.kotlin.test.utils.AbstractTwoAttributesMetaInfoProcessor
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
@@ -118,20 +118,20 @@ class FirDiagnosticsHandler(testServices: TestServices) : FirAnalysisHandler(tes
|
||||
)
|
||||
}
|
||||
} else {
|
||||
collectLightTreeSyntaxErrors(firFile).flatMap { sourceElement ->
|
||||
FirSyntaxErrors.SYNTAX.on(
|
||||
sourceElement,
|
||||
PsiBuilderImpl.getErrorMessage(sourceElement.lighterASTNode) ?: "Syntax error",
|
||||
positioningStrategy = null
|
||||
).toMetaInfos(
|
||||
module,
|
||||
testFile,
|
||||
globalMetadataInfoHandler1 = globalMetadataInfoHandler,
|
||||
lightTreeEnabled,
|
||||
lightTreeComparingModeEnabled,
|
||||
forceRenderArguments,
|
||||
)
|
||||
}
|
||||
testServices.lightTreeSyntaxDiagnosticsReporterHolder
|
||||
?.reporter
|
||||
?.diagnosticsByFilePath
|
||||
?.get("/${testFile.toLightTreeShortName()}")
|
||||
?.flatMap {
|
||||
it.toMetaInfos(
|
||||
module,
|
||||
testFile,
|
||||
globalMetadataInfoHandler1 = globalMetadataInfoHandler,
|
||||
lightTreeEnabled,
|
||||
lightTreeComparingModeEnabled,
|
||||
forceRenderArguments,
|
||||
)
|
||||
}.orEmpty()
|
||||
}
|
||||
|
||||
globalMetadataInfoHandler.addMetadataInfosForFile(testFile, metaInfos)
|
||||
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.test.frontend.fir.handlers
|
||||
|
||||
import com.intellij.lang.LighterASTNode
|
||||
import com.intellij.psi.TokenType
|
||||
import com.intellij.util.diff.FlyweightCapableTreeStructure
|
||||
import org.jetbrains.kotlin.KtLightSourceElement
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.fir.visitors.FirVisitor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.toKtLightSourceElement
|
||||
import org.jetbrains.kotlin.util.getChildren
|
||||
|
||||
private typealias Tree = FlyweightCapableTreeStructure<LighterASTNode>
|
||||
|
||||
private class LightTreeErrorsCollector(private val tree: Tree) {
|
||||
private inline fun LighterASTNode.forEachChildren(f: (LighterASTNode) -> Unit) {
|
||||
val kidsArray = this.getChildren(tree)
|
||||
for (kid in kidsArray) {
|
||||
val tokenType = kid.tokenType
|
||||
if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.WHITE_SPACE || tokenType == KtTokens.SEMICOLON) continue
|
||||
f(kid)
|
||||
}
|
||||
}
|
||||
|
||||
fun collectErrorNodes(node: LighterASTNode, acc: MutableList<LighterASTNode> = mutableListOf()): List<LighterASTNode> {
|
||||
if (node.tokenType == TokenType.ERROR_ELEMENT) {
|
||||
acc.add(node)
|
||||
} else {
|
||||
node.forEachChildren { child ->
|
||||
collectErrorNodes(child, acc)
|
||||
}
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private data class TreeWithOffset(val tree: Tree, val offset: Int)
|
||||
|
||||
private data class VisitorState(
|
||||
val lastTree: Tree? = null,
|
||||
val visitedTrees: MutableSet<Tree> = mutableSetOf(),
|
||||
val result: MutableList<TreeWithOffset> = mutableListOf()
|
||||
)
|
||||
|
||||
private object FirTreesExtractVisitor : FirVisitor<Unit, VisitorState>() {
|
||||
override fun visitElement(element: FirElement, data: VisitorState) {
|
||||
val source = element.source ?: return
|
||||
val currentTree = source.treeStructure
|
||||
val (lastTree, visitedTrees, result) = data
|
||||
val newData = if (lastTree !== currentTree && visitedTrees.add(currentTree)) {
|
||||
val newTreeWithOffset = TreeWithOffset(currentTree, element.source?.startOffset ?: 0)
|
||||
result.add(newTreeWithOffset)
|
||||
data.copy(lastTree = lastTree)
|
||||
} else {
|
||||
data
|
||||
}
|
||||
element.acceptChildren(this, newData)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun collectLightTreeSyntaxErrors(file: FirFile): List<KtLightSourceElement> {
|
||||
val state = VisitorState()
|
||||
file.accept(FirTreesExtractVisitor,state)
|
||||
return state.result.flatMap { (tree, offset) ->
|
||||
LightTreeErrorsCollector(tree).collectErrorNodes(tree.root).map {
|
||||
it.toKtLightSourceElement(tree, startOffset = it.startOffset + offset, endOffset = it.endOffset + offset)
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
-2
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.test.runners
|
||||
|
||||
import org.jetbrains.kotlin.config.ExplicitApiMode
|
||||
import org.jetbrains.kotlin.diagnostics.impl.SimpleDiagnosticsCollector
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.symbols.FirLazyDeclarationResolver
|
||||
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
|
||||
@@ -39,6 +40,8 @@ import org.jetbrains.kotlin.test.services.sourceProviders.CoroutineHelpersSource
|
||||
import org.jetbrains.kotlin.test.FirParser
|
||||
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives
|
||||
import org.jetbrains.kotlin.test.services.JsLibraryProvider
|
||||
import org.jetbrains.kotlin.test.services.TestService
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
|
||||
abstract class AbstractFirDiagnosticTestBase(val parser: FirParser) : AbstractKotlinCompilerTest() {
|
||||
override fun TestConfigurationBuilder.configuration() {
|
||||
@@ -49,12 +52,23 @@ abstract class AbstractFirDiagnosticTestBase(val parser: FirParser) : AbstractKo
|
||||
}
|
||||
|
||||
abstract class AbstractFirPsiDiagnosticTest : AbstractFirDiagnosticTestBase(FirParser.Psi)
|
||||
abstract class AbstractFirLightTreeDiagnosticsTest : AbstractFirDiagnosticTestBase(FirParser.LightTree)
|
||||
abstract class AbstractFirLightTreeDiagnosticsTest : AbstractFirDiagnosticTestBase(FirParser.LightTree) {
|
||||
override fun configure(builder: TestConfigurationBuilder) {
|
||||
super.configure(builder)
|
||||
builder.useAdditionalService { LightTreeSyntaxDiagnosticsReporterHolder() }
|
||||
}
|
||||
}
|
||||
|
||||
class LightTreeSyntaxDiagnosticsReporterHolder : TestService {
|
||||
val reporter = SimpleDiagnosticsCollector()
|
||||
}
|
||||
|
||||
val TestServices.lightTreeSyntaxDiagnosticsReporterHolder: LightTreeSyntaxDiagnosticsReporterHolder? by TestServices.nullableTestServiceAccessor()
|
||||
|
||||
abstract class AbstractFirWithActualizerDiagnosticsTest(val parser: FirParser) : AbstractKotlinCompilerWithTargetBackendTest(TargetBackend.JVM_IR) {
|
||||
override fun configure(builder: TestConfigurationBuilder) {
|
||||
super.configure(builder)
|
||||
with (builder) {
|
||||
with(builder) {
|
||||
defaultDirectives {
|
||||
+CodegenTestDirectives.IGNORE_FIR2IR_EXCEPTIONS_IF_FIR_CONTAINS_ERRORS
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user