[K2, CLI] Support endOffset in Kotlin CLI
Duplicated messages in testdata appeared because default renderer renders diagnostic spans ambiguously. It shows only start position. In fact, there are 3 failures, 2 of them distinguish only by the diagnostic end offset. See youtrack for more information. The issue about inconvenient rendering is KT-64989. #KT-64608
This commit is contained in:
committed by
Space Team
parent
6404cede07
commit
f05c972efb
@@ -12,6 +12,7 @@ import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult
|
||||
import org.jetbrains.kotlin.cli.common.CLITool
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler
|
||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||
@@ -59,6 +60,7 @@ class AnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
|
||||
klass: KClass<out ComponentRegistrar>,
|
||||
expectedExitCode: ExitCode = ExitCode.OK,
|
||||
extras: List<String> = emptyList(),
|
||||
messageRenderer: MessageRenderer? = null,
|
||||
) {
|
||||
val mainKt = tmpdir.resolve(src.name).apply {
|
||||
writeText(src.content)
|
||||
@@ -70,7 +72,7 @@ class AnalysisHandlerExtensionTest : TestCaseWithTmpdir() {
|
||||
"-d", tmpdir.resolve("out").absolutePath
|
||||
)
|
||||
|
||||
val (output, exitCode) = CompilerTestUtil.executeCompiler(compiler, args + outputPath + extras)
|
||||
val (output, exitCode) = CompilerTestUtil.executeCompiler(compiler, args + outputPath + extras, messageRenderer)
|
||||
assertEquals(expectedExitCode, exitCode, output)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.cli
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||
@@ -104,15 +107,78 @@ class CustomCliTest : TestCaseWithTmpdir() {
|
||||
compileAndCheckMainClass(listOf(mainKt), expectedMainClass = null)
|
||||
}
|
||||
|
||||
private fun compileAndCheckMainClass(sourceFiles: List<File>, expectedMainClass: String?) {
|
||||
private fun makeCompilerArgs(sourceFiles: List<File>, jarFile: File): List<String> {
|
||||
// TODO: remove explicit version after implementing main fun detector (KT-44557)
|
||||
return listOf("-language-version", "1.9", "-include-runtime", "-d", jarFile.absolutePath) + sourceFiles.map { it.absolutePath }
|
||||
}
|
||||
|
||||
private fun compileAndCheckMainClass(sourceFiles: List<File>, expectedMainClass: String?, messageRenderer: MessageRenderer? = null) {
|
||||
val jarFile = tmpdir.resolve("output.jar")
|
||||
// TODO: remove explicit verion after implementing main fun detector (KT-44557)
|
||||
val args = listOf("-language-version", "1.9", "-include-runtime", "-d", jarFile.absolutePath) + sourceFiles.map { it.absolutePath }
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), args)
|
||||
val args = makeCompilerArgs(sourceFiles, jarFile)
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), args, messageRenderer)
|
||||
|
||||
JarFile(jarFile).use {
|
||||
val mainClassAttr = it.manifest.mainAttributes.getValue("Main-Class")
|
||||
Assert.assertEquals(expectedMainClass, mainClassAttr)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compileAndGetDiagnostics(sourceFiles: List<File>): List<Diagnostic> {
|
||||
val jarFile = tmpdir.resolve("output.jar")
|
||||
val args = makeCompilerArgs(sourceFiles, jarFile)
|
||||
val diagnostics = mutableListOf<Diagnostic>()
|
||||
CompilerTestUtil.executeCompiler(K2JVMCompiler(), args, LoggingMessageRenderer(diagnostics))
|
||||
return diagnostics
|
||||
}
|
||||
|
||||
|
||||
private data class Diagnostic(
|
||||
val severity: CompilerMessageSeverity,
|
||||
val message: String,
|
||||
val location: CompilerMessageSourceLocation?
|
||||
)
|
||||
|
||||
private class LoggingMessageRenderer(val diagnostics: MutableList<Diagnostic>) : MessageRenderer {
|
||||
override fun renderPreamble(): String = ""
|
||||
|
||||
override fun render(
|
||||
severity: CompilerMessageSeverity,
|
||||
message: String,
|
||||
location: CompilerMessageSourceLocation?
|
||||
): String {
|
||||
diagnostics.add(Diagnostic(severity, message, location))
|
||||
return ""
|
||||
}
|
||||
|
||||
override fun renderUsage(usage: String): String =
|
||||
render(CompilerMessageSeverity.STRONG_WARNING, usage, null)
|
||||
|
||||
override fun renderConclusion(): String = ""
|
||||
|
||||
override fun getName(): String = "Redirector"
|
||||
}
|
||||
|
||||
fun testDiagnosticRanges() {
|
||||
val mainKt = tmpdir.resolve("main.kt").apply {
|
||||
val quotes = "\"".repeat(3)
|
||||
writeText(
|
||||
"""
|
||||
|fun main(args: Array<String>) {
|
||||
| val x: Int = $quotes
|
||||
| some
|
||||
| multiline
|
||||
| string
|
||||
| $quotes
|
||||
|}""".trimMargin()
|
||||
)
|
||||
}
|
||||
|
||||
val diagnostics = compileAndGetDiagnostics(listOf(mainKt))
|
||||
require(diagnostics.size == 1) { "Expected 1 diagnostic, but found ${diagnostics.size}:\n${diagnostics.joinToString("\n")}" }
|
||||
val diagnostic = diagnostics.single()
|
||||
assertEquals(2, diagnostic.location?.line)
|
||||
assertEquals(18, diagnostic.location?.column)
|
||||
assertEquals(6, diagnostic.location?.lineEnd)
|
||||
assertEquals(8, diagnostic.location?.columnEnd)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.cli
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.test.CompilerTestUtil
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||
@@ -42,16 +43,17 @@ class FriendPathsTest : TestCaseWithTmpdir() {
|
||||
doTestFriendPaths(File(tmpdir, "lib").relativeTo(File("").absoluteFile))
|
||||
}
|
||||
|
||||
private fun doTestFriendPaths(libDest: File) {
|
||||
private fun doTestFriendPaths(libDest: File, messageRenderer: MessageRenderer? = null) {
|
||||
val libSrc = File(getTestDataDirectory(), "lib.kt")
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path), messageRenderer)
|
||||
|
||||
CompilerTestUtil.executeCompilerAssertSuccessful(
|
||||
K2JVMCompiler(),
|
||||
listOf(
|
||||
"-d", tmpdir.path, "-cp", libDest.path, File(getTestDataDirectory(), "usage.kt").path,
|
||||
"-Xfriend-paths=${libDest.path}"
|
||||
)
|
||||
),
|
||||
messageRenderer,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.config.ApiVersion
|
||||
import org.jetbrains.kotlin.config.LanguageVersion
|
||||
@@ -34,7 +35,8 @@ class JvmModuleProtoBufTest : KtUsefulTestCase() {
|
||||
relativeDirectory: String,
|
||||
compileWith: LanguageVersion = LanguageVersion.LATEST_STABLE,
|
||||
loadWith: LanguageVersion = LanguageVersion.LATEST_STABLE,
|
||||
extraOptions: List<String> = emptyList()
|
||||
extraOptions: List<String> = emptyList(),
|
||||
messageRenderer: MessageRenderer? = null,
|
||||
) {
|
||||
val directory = KtTestUtil.getTestDataPathBase() + relativeDirectory
|
||||
val tmpdir = KtTestUtil.tmpDir(this::class.simpleName)
|
||||
@@ -46,7 +48,8 @@ class JvmModuleProtoBufTest : KtUsefulTestCase() {
|
||||
"-d", tmpdir.path,
|
||||
"-module-name", moduleName,
|
||||
"-language-version", compileWith.versionString
|
||||
) + extraOptions
|
||||
) + extraOptions,
|
||||
messageRenderer
|
||||
)
|
||||
|
||||
val mapping = ModuleMapping.loadModuleMapping(
|
||||
|
||||
Reference in New Issue
Block a user