Implement source sections compiler plugin
allows to compile only parts of the source files, denoted by top-level "sections" (function with lambda param calls), but preserving original file line/column numbers for easier diagnostics. Allow e.g. to compile gradle "buildscript" section without preprocessing original file in advance. See tests for examples.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/testData" type="java-test-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="kotlin-runtime" level="project" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="tests-common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="cli" scope="TEST" />
|
||||
<orderEntry type="library" scope="TEST" name="junit-4.12" level="project" />
|
||||
<orderEntry type="module" module-name="frontend.java" scope="TEST" />
|
||||
<orderEntry type="module" module-name="cli-common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="util" scope="TEST" />
|
||||
<orderEntry type="module" module-name="daemon-client" scope="TEST" />
|
||||
<orderEntry type="module" module-name="daemon-common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="compiler-tests" scope="TEST" />
|
||||
</component>
|
||||
</module>
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.sourceSections.SourceSectionsCommandLineProcessor
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.sourceSections.SourceSectionsComponentRegistrar
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.sourceSections
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.VirtualFileSystem
|
||||
import com.intellij.testFramework.LightVirtualFile
|
||||
import org.jetbrains.kotlin.lexer.KotlinLexer
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.CharBuffer
|
||||
import java.nio.charset.Charset
|
||||
|
||||
class FilteredSectionsVirtualFile(val delegate: VirtualFile, val sectionIds: Collection<String>) : VirtualFile() {
|
||||
|
||||
override fun refresh(asynchronous: Boolean, recursive: Boolean, postRunnable: Runnable?) {
|
||||
delegate.refresh(asynchronous, recursive, postRunnable)
|
||||
}
|
||||
|
||||
override fun getLength(): Long = delegate.length
|
||||
override fun getFileSystem(): VirtualFileSystem = delegate.fileSystem
|
||||
override fun getPath(): String = delegate.path
|
||||
override fun isDirectory(): Boolean = delegate.isDirectory
|
||||
override fun getTimeStamp(): Long = delegate.timeStamp
|
||||
override fun getName(): String = delegate.name
|
||||
|
||||
override fun contentsToByteArray(): ByteArray = filterByteContents(sectionIds, delegate.contentsToByteArray(), delegate.charset)
|
||||
|
||||
override fun getInputStream(): InputStream = ByteArrayInputStream(contentsToByteArray())
|
||||
|
||||
override fun isValid(): Boolean =delegate.isValid
|
||||
override fun getParent(): VirtualFile = delegate.parent
|
||||
override fun getChildren(): Array<VirtualFile> = delegate.children
|
||||
override fun isWritable(): Boolean = delegate.isWritable
|
||||
override fun getOutputStream(requestor: Any?, newModificationStamp: Long, newTimeStamp: Long): OutputStream =
|
||||
delegate.getOutputStream(requestor, newModificationStamp, newTimeStamp)
|
||||
}
|
||||
|
||||
class FilteredSectionsLightVirtualFile(val delegate: LightVirtualFile, val sectionIds: Collection<String>) : LightVirtualFile(delegate.name, delegate.fileType, delegate.content, delegate.charset, delegate.modificationStamp) {
|
||||
override fun getContent(): CharSequence = filterStringBuilderContents(StringBuilder(delegate.content), sectionIds)
|
||||
override fun contentsToByteArray(): ByteArray = filterByteContents(sectionIds, delegate.contentsToByteArray(), delegate.charset)
|
||||
}
|
||||
|
||||
private fun filterByteContents(sectionIds: Collection<String>, bytes: ByteArray, charset: Charset): ByteArray {
|
||||
val content = StringBuilder(charset.decode(ByteBuffer.wrap(bytes)))
|
||||
filterStringBuilderContents(content, sectionIds)
|
||||
val buffer = charset.encode(CharBuffer.wrap(content))
|
||||
return if (buffer.limit() == buffer.capacity()) buffer.array()
|
||||
else {
|
||||
val res = ByteArray(buffer.limit())
|
||||
buffer.get(res)
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
private fun filterStringBuilderContents(content: StringBuilder, sectionIds: Collection<String>): StringBuilder {
|
||||
var curPos = 0
|
||||
val sectionsIter = FilteredSectionsTokensRangeIterator(content, sectionIds)
|
||||
for (range in sectionsIter) {
|
||||
for (i in curPos..range.start - 1) {
|
||||
if (content[i] != '\n') {
|
||||
content.setCharAt(i, ' ')
|
||||
}
|
||||
}
|
||||
curPos = range.end
|
||||
}
|
||||
for (i in curPos..content.length - 1) {
|
||||
if (content[i] != '\n') {
|
||||
content.setCharAt(i, ' ')
|
||||
}
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
|
||||
private class TokenRange(val start: Int, val end: Int)
|
||||
|
||||
private class FilteredSectionsTokensRangeIterator(script: CharSequence, val sectionIds: Collection<String>) : Iterator<TokenRange> {
|
||||
|
||||
private val lexer = KotlinLexer().apply {
|
||||
start(script)
|
||||
}
|
||||
|
||||
private var currentRange: TokenRange? = advanceToNextFilteredSection()
|
||||
|
||||
fun advanceToNextFilteredSection(): TokenRange? {
|
||||
|
||||
fun KotlinLexer.skipWhiteSpaceAndComments() {
|
||||
while (tokenType in KtTokens.WHITE_SPACE_OR_COMMENT_BIT_SET) {
|
||||
advance()
|
||||
}
|
||||
}
|
||||
|
||||
var depth = 0
|
||||
var sectionStartPos = 0
|
||||
var inside = false
|
||||
with(lexer) {
|
||||
loop@ while (tokenType != null) {
|
||||
if (!inside && depth == 0 && tokenType == KtTokens.IDENTIFIER && tokenText in sectionIds) {
|
||||
sectionStartPos = currentPosition.offset
|
||||
advance()
|
||||
skipWhiteSpaceAndComments()
|
||||
inside = (tokenType == KtTokens.LBRACE)
|
||||
}
|
||||
when (tokenType) {
|
||||
KtTokens.LBRACE -> depth += 1
|
||||
KtTokens.RBRACE -> depth -= 1
|
||||
}
|
||||
if (inside && depth == 0) {
|
||||
advance()
|
||||
break@loop
|
||||
}
|
||||
advance()
|
||||
}
|
||||
}
|
||||
return if (lexer.tokenType == null && !inside) null
|
||||
else TokenRange(sectionStartPos, lexer.currentPosition.offset)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = currentRange != null
|
||||
|
||||
override fun next(): TokenRange {
|
||||
val ret = currentRange
|
||||
currentRange = advanceToNextFilteredSection()
|
||||
return ret!!
|
||||
}
|
||||
}
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.sourceSections
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.testFramework.LightVirtualFile
|
||||
import org.jetbrains.kotlin.extensions.PreprocessedVirtualFileFactoryExtension
|
||||
|
||||
class FilteredSectionsVirtualFileExtension(val allowedSections: Set<String>?) : PreprocessedVirtualFileFactoryExtension {
|
||||
|
||||
override fun isPassThrough(): Boolean = allowedSections == null || allowedSections.isEmpty()
|
||||
|
||||
override fun createPreprocessedFile(file: VirtualFile?): VirtualFile? =
|
||||
when {
|
||||
file == null || allowedSections == null || allowedSections.isEmpty() -> file
|
||||
file is LightVirtualFile -> FilteredSectionsLightVirtualFile(file, allowedSections)
|
||||
else -> FilteredSectionsVirtualFile(file, allowedSections)
|
||||
}
|
||||
|
||||
override fun createPreprocessedLightFile(file: LightVirtualFile?): LightVirtualFile? =
|
||||
when {
|
||||
file == null || allowedSections == null || allowedSections.isEmpty() -> file
|
||||
else -> FilteredSectionsLightVirtualFile(file, allowedSections)
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.sourceSections
|
||||
|
||||
import com.intellij.mock.MockProject
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.compiler.plugin.CliOption
|
||||
import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException
|
||||
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
|
||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.CompilerConfigurationKey
|
||||
import org.jetbrains.kotlin.extensions.PreprocessedVirtualFileFactoryExtension
|
||||
import org.jetbrains.kotlin.extensions.PreprocessedVirtualFileFactoryExtension.Companion
|
||||
|
||||
object SourceSectionsConfigurationKeys {
|
||||
val SECTIONS_OPTION: CompilerConfigurationKey<List<String>> =
|
||||
CompilerConfigurationKey.create("allowed section name")
|
||||
}
|
||||
|
||||
class SourceSectionsCommandLineProcessor : CommandLineProcessor {
|
||||
companion object {
|
||||
val SECTIONS_OPTION = CliOption("allowedSection", "<name>", "Allowed section name",
|
||||
required = true, allowMultipleOccurrences = true)
|
||||
|
||||
val PLUGIN_ID = "org.jetbrains.kotlin.sourceSections"
|
||||
}
|
||||
|
||||
override val pluginId = PLUGIN_ID
|
||||
override val pluginOptions = listOf(SECTIONS_OPTION)
|
||||
|
||||
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
|
||||
SECTIONS_OPTION -> {
|
||||
val paths = configuration.getList(SourceSectionsConfigurationKeys.SECTIONS_OPTION).toMutableList()
|
||||
paths.add(value)
|
||||
configuration.put(SourceSectionsConfigurationKeys.SECTIONS_OPTION, paths)
|
||||
}
|
||||
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
|
||||
}
|
||||
}
|
||||
|
||||
class SourceSectionsComponentRegistrar : ComponentRegistrar {
|
||||
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
|
||||
val sections = configuration.get(SourceSectionsConfigurationKeys.SECTIONS_OPTION) ?: return
|
||||
registerAllowedSourceSections(project, sections)
|
||||
}
|
||||
}
|
||||
|
||||
fun registerAllowedSourceSections(project: Project, sections: List<String>) {
|
||||
if (!sections.isEmpty()) {
|
||||
PreprocessedVirtualFileFactoryExtension.registerExtension(project, FilteredSectionsVirtualFileExtension(sections.toHashSet()))
|
||||
}
|
||||
}
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
|
||||
println("should not be printed")
|
||||
|
||||
fun unlistedSectionName(body: () -> Unit): Unit = body()
|
||||
|
||||
let {
|
||||
println("Hello, World!")
|
||||
}
|
||||
|
||||
println("ignore here")
|
||||
|
||||
unlistedSectionName {
|
||||
println("should not be printed too")
|
||||
}
|
||||
|
||||
println("ignore here")
|
||||
|
||||
apply {
|
||||
println("That's all, folks!")
|
||||
}
|
||||
|
||||
println("ignore here")
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
let {
|
||||
println("Hello, World!")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
apply {
|
||||
println("That's all, folks!")
|
||||
}
|
||||
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
Hello, World!
|
||||
That's all, folks!
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.sourceSections
|
||||
|
||||
import com.intellij.openapi.vfs.StandardFileSystems
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.cli.AbstractCliTest
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.*
|
||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.addKotlinSourceRoots
|
||||
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
|
||||
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
|
||||
import org.jetbrains.kotlin.daemon.common.*
|
||||
import org.jetbrains.kotlin.script.StandardScriptDefinition
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||
import org.jetbrains.kotlin.test.TestJdkKind
|
||||
import org.jetbrains.kotlin.utils.KotlinPaths
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
|
||||
import java.io.*
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.net.URLClassLoader
|
||||
import java.nio.charset.Charset
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class SourceSectionsTest : TestCaseWithTmpdir() {
|
||||
|
||||
companion object {
|
||||
val TEST_ALLOWED_SECTIONS = listOf("let", "apply") // using standard function names that can be used as sections in the script context without crafting special ones
|
||||
val TEST_DATA_DIR = File(KotlinTestUtils.getHomeDirectory(), "plugins/source-sections/source-sections-compiler/testData")
|
||||
}
|
||||
|
||||
private val kotlinPaths: KotlinPaths by lazy {
|
||||
val paths = PathUtil.getKotlinPathsForDistDirectory()
|
||||
TestCase.assertTrue("Lib directory doesn't exist. Run 'ant dist'", paths.libPath.absoluteFile.isDirectory)
|
||||
paths
|
||||
}
|
||||
|
||||
val compilerClassPath = listOf(kotlinPaths.compilerPath)
|
||||
val scriptRuntimeClassPath = listOf( kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath)
|
||||
val sourceSectionsPluginJar = File(kotlinPaths.libPath, "source-sections-compiler-plugin.jar")
|
||||
val compilerId by lazy(LazyThreadSafetyMode.NONE) { CompilerId.makeCompilerId(compilerClassPath) }
|
||||
|
||||
private fun createEnvironment(vararg sources: String, withSourceSectionsPlugin: Boolean = true): KotlinCoreEnvironment {
|
||||
val configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.NO_KOTLIN_REFLECT, TestJdkKind.FULL_JDK)
|
||||
|
||||
configuration.addKotlinSourceRoots(sources.asList())
|
||||
configuration.put<MessageCollector>(
|
||||
CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY,
|
||||
PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false)
|
||||
)
|
||||
configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, StandardScriptDefinition)
|
||||
if (withSourceSectionsPlugin) {
|
||||
configuration.addAll(SourceSectionsConfigurationKeys.SECTIONS_OPTION, TEST_ALLOWED_SECTIONS)
|
||||
configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, SourceSectionsComponentRegistrar())
|
||||
}
|
||||
|
||||
val environment = KotlinCoreEnvironment.createForTests(testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
return environment
|
||||
}
|
||||
|
||||
private data class SourceToExpectedResults(val source: File, val expectedResults: File)
|
||||
|
||||
private fun getTestFiles(expectedExt: String): List<SourceToExpectedResults> {
|
||||
val testDataFiles = TEST_DATA_DIR.listFiles()
|
||||
val sourceToExpected = testDataFiles.filter { it.isFile && it.extension == "kts" }
|
||||
.mapNotNull { testFile ->
|
||||
testDataFiles.find { it.isFile && it.name == testFile.name + expectedExt }?.let { SourceToExpectedResults(testFile, it) }
|
||||
}
|
||||
TestCase.assertTrue("No test files found", sourceToExpected.isNotEmpty())
|
||||
return sourceToExpected
|
||||
}
|
||||
|
||||
private fun InputStream.trimmedLines(charset: Charset): List<String> = use {
|
||||
bufferedReader(charset)
|
||||
.lineSequence()
|
||||
.map(String::trimEnd)
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun testSourceSectionsFilter() {
|
||||
val sourceToFiltered = getTestFiles(".filtered")
|
||||
|
||||
createEnvironment() // creates VirtualFileManager
|
||||
val fileCreator = FilteredSectionsVirtualFileExtension(TEST_ALLOWED_SECTIONS.toSet())
|
||||
|
||||
sourceToFiltered.forEach { (source, expectedResult) ->
|
||||
val filteredVF = fileCreator.createPreprocessedFile(StandardFileSystems.local().findFileByPath(source.canonicalPath))
|
||||
TestCase.assertNotNull("Cannot generate preprocessed file", filteredVF)
|
||||
val expected = expectedResult.inputStream().trimmedLines(Charset.defaultCharset())
|
||||
val actual = ByteArrayInputStream(filteredVF!!.contentsToByteArray()).trimmedLines(filteredVF.charset)
|
||||
TestCase.assertEquals("Unexpected result on preprocessing file '${source.name}'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
fun testSourceSectionsRun() {
|
||||
val sourceToOutput = getTestFiles(".out")
|
||||
|
||||
sourceToOutput.forEach { (source, expectedOutput) ->
|
||||
val environment = createEnvironment(source.canonicalPath)
|
||||
val scriptClass = KotlinToJVMBytecodeCompiler.compileScript(environment, Thread.currentThread().contextClassLoader)
|
||||
TestCase.assertNotNull("Compilation errors", scriptClass)
|
||||
verifyScriptOutput(scriptClass, expectedOutput)
|
||||
}
|
||||
}
|
||||
|
||||
fun testSourceSectionsRunBench() {
|
||||
val mxBeans = ManagementFactory.getThreadMXBean()
|
||||
val (source, _) = getTestFiles(".out").first()
|
||||
|
||||
// warming up application environment
|
||||
KotlinToJVMBytecodeCompiler.compileScript(createEnvironment(source.canonicalPath, withSourceSectionsPlugin = false), Thread.currentThread().contextClassLoader)
|
||||
|
||||
val times = generateSequence {
|
||||
val t0 = mxBeans.threadCpuTime()
|
||||
KotlinToJVMBytecodeCompiler.compileScript(createEnvironment(source.canonicalPath, withSourceSectionsPlugin = false), Thread.currentThread().contextClassLoader)
|
||||
val t1 = mxBeans.threadCpuTime()
|
||||
KotlinToJVMBytecodeCompiler.compileScript(createEnvironment(source.canonicalPath, withSourceSectionsPlugin = true), Thread.currentThread().contextClassLoader)
|
||||
val t2 = mxBeans.threadCpuTime()
|
||||
Triple(t1 - t0, t2 - t1, t2 - t1)
|
||||
}.take(10).toList()
|
||||
|
||||
val adjustedMaxDiff = times.sortedByDescending { (_, _, diff) -> diff }.drop(2).first()
|
||||
|
||||
fun Long.ms() = TimeUnit.NANOSECONDS.toMillis(this)
|
||||
TestCase.assertTrue("sourceSections plugin brings too much overheads: ${times.joinToString { "(${it.first.ms()}, ${it.second.ms()})" }} (expecting it to be faster than regular compilation due to less lines compiled)",
|
||||
adjustedMaxDiff.third < 20 /* assuming it measurement error */ || adjustedMaxDiff.first >= adjustedMaxDiff.second )
|
||||
}
|
||||
|
||||
fun testSourceSectionCompileLocal() {
|
||||
val sourceToOutput = getTestFiles(".out")
|
||||
|
||||
sourceToOutput.forEach { (source, expectedOutput) ->
|
||||
|
||||
val args = listOf(source.canonicalPath, "-d", tmpdir.canonicalPath,
|
||||
"-Xplugin", sourceSectionsPluginJar.canonicalPath,
|
||||
"-P", TEST_ALLOWED_SECTIONS.joinToString(",") { "plugin:${SourceSectionsCommandLineProcessor.PLUGIN_ID}:${SourceSectionsCommandLineProcessor.SECTIONS_OPTION.name}=$it" })
|
||||
val (output, code) = AbstractCliTest.executeCompilerGrabOutput(K2JVMCompiler(), args)
|
||||
|
||||
TestCase.assertEquals("Compilation failed:\n$output", 0, code.code)
|
||||
|
||||
val scriptClass = loadScriptClass(File(tmpdir, source.nameWithoutExtension.capitalize() + ".class"))
|
||||
|
||||
verifyScriptOutput(scriptClass, expectedOutput)
|
||||
}
|
||||
}
|
||||
|
||||
fun testSourceSectionCompileOnDaemon() {
|
||||
val sourceToOutput = getTestFiles(".out")
|
||||
|
||||
withFlagFile("sourceSections", ".alive") { aliveFile ->
|
||||
|
||||
val daemonOptions = DaemonOptions(runFilesPath = File(tmpdir, getTestName(true)).absolutePath, verbose = true, reportPerf = true)
|
||||
val daemonJVMOptions = configureDaemonJVMOptions(inheritMemoryLimits = false, inheritAdditionalProperties = false)
|
||||
val messageCollector = TestMessageCollector()
|
||||
|
||||
val daemonWithSession = KotlinCompilerClient.connectAndLease(compilerId, aliveFile, daemonJVMOptions, daemonOptions,
|
||||
DaemonReportingTargets(messageCollector = messageCollector), autostart = true, leaseSession = true)
|
||||
assertNotNull("failed to connect daemon", daemonWithSession)
|
||||
|
||||
try {
|
||||
|
||||
sourceToOutput.forEach { (source, expectedOutput) ->
|
||||
|
||||
val args = arrayOf(source.canonicalPath, "-d", tmpdir.canonicalPath,
|
||||
"-Xplugin", sourceSectionsPluginJar.canonicalPath,
|
||||
"-P", TEST_ALLOWED_SECTIONS.joinToString(",") { "plugin:${SourceSectionsCommandLineProcessor.PLUGIN_ID}:${SourceSectionsCommandLineProcessor.SECTIONS_OPTION.name}=$it" })
|
||||
|
||||
messageCollector.clear()
|
||||
val outputs = arrayListOf<OutputMessageUtil.Output>()
|
||||
|
||||
val code = KotlinCompilerClient.compile(daemonWithSession!!.first, daemonWithSession.second, CompileService.TargetPlatform.JVM,
|
||||
args, messageCollector,
|
||||
{ outFile, srcFiles -> outputs.add(OutputMessageUtil.Output(srcFiles, outFile)) },
|
||||
reportSeverity = ReportSeverity.DEBUG)
|
||||
|
||||
TestCase.assertEquals("Compilation failed:\n${messageCollector.messages.joinToString("\n")}", 0, code)
|
||||
TestCase.assertFalse("Compilation failed:\n${messageCollector.messages.joinToString("\n")}", messageCollector.hasErrors())
|
||||
val scriptClassFile = outputs.first().outputFile
|
||||
TestCase.assertEquals("unexpected class file generated", source.nameWithoutExtension.capitalize(), scriptClassFile?.nameWithoutExtension)
|
||||
|
||||
verifyScriptOutput(loadScriptClass(scriptClassFile), expectedOutput)
|
||||
}
|
||||
}
|
||||
finally {
|
||||
daemonWithSession!!.first.shutdown()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadScriptClass(scriptClassFile: File?): Class<*>? {
|
||||
val cl = URLClassLoader((scriptRuntimeClassPath + tmpdir).map { it.toURI().toURL() }.toTypedArray())
|
||||
val scriptClass = cl.loadClass(scriptClassFile!!.nameWithoutExtension)
|
||||
|
||||
TestCase.assertNotNull("Unable to load class $scriptClassFile", scriptClass)
|
||||
return scriptClass
|
||||
}
|
||||
|
||||
private fun verifyScriptOutput(scriptClass: Class<*>?, expectedOutput: File) {
|
||||
val scriptOut = captureOut {
|
||||
tryConstructClassFromStringArgs(scriptClass!!, emptyList())
|
||||
}
|
||||
val expected = expectedOutput.readText()
|
||||
|
||||
TestCase.assertEquals(expected, scriptOut)
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun withFlagFile(prefix: String, suffix: String? = null, body: (File) -> Unit) {
|
||||
val file = createTempFile(prefix, suffix)
|
||||
try {
|
||||
body(file)
|
||||
}
|
||||
finally {
|
||||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun captureOut(body: () -> Unit): String {
|
||||
val outStream = ByteArrayOutputStream()
|
||||
val prevOut = System.out
|
||||
System.setOut(PrintStream(outStream))
|
||||
try {
|
||||
body()
|
||||
}
|
||||
finally {
|
||||
System.out.flush()
|
||||
System.setOut(prevOut)
|
||||
}
|
||||
return outStream.toString()
|
||||
}
|
||||
|
||||
class TestMessageCollector : MessageCollector {
|
||||
|
||||
data class Message(val severity: CompilerMessageSeverity, val message: String, val location: CompilerMessageLocation)
|
||||
|
||||
val messages = arrayListOf<Message>()
|
||||
|
||||
override fun clear() {
|
||||
messages.clear()
|
||||
}
|
||||
|
||||
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
|
||||
messages.add(Message(severity, message, location))
|
||||
}
|
||||
|
||||
override fun hasErrors(): Boolean = messages.any { it.severity == CompilerMessageSeverity.EXCEPTION || it.severity == CompilerMessageSeverity.ERROR }
|
||||
}
|
||||
Reference in New Issue
Block a user