diff --git a/build.gradle.kts b/build.gradle.kts index 6007fd0238b..788c430a18c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -714,7 +714,6 @@ tasks { dependsOn("dist") dependsOn( ":kotlin-annotation-processing:test", - ":kotlin-source-sections-compiler-plugin:test", ":kotlin-allopen-compiler-plugin:test", ":kotlin-noarg-compiler-plugin:test", ":kotlin-sam-with-receiver-compiler-plugin:test", diff --git a/idea/testData/gradle/highlighting/complexBuildGradleKts/build.gradle.kts b/idea/testData/gradle/highlighting/complexBuildGradleKts/build.gradle.kts index 70024fbb05b..038a432da41 100644 --- a/idea/testData/gradle/highlighting/complexBuildGradleKts/build.gradle.kts +++ b/idea/testData/gradle/highlighting/complexBuildGradleKts/build.gradle.kts @@ -401,7 +401,6 @@ tasks { create("plugins-tests") { dependsOn("dist") dependsOn(":kotlin-annotation-processing:test", - ":kotlin-source-sections-compiler-plugin:test", ":kotlin-allopen-compiler-plugin:test", ":kotlin-noarg-compiler-plugin:test", ":kotlin-sam-with-receiver-compiler-plugin:test", diff --git a/plugins/allopen/allopen-ide/build.gradle.kts b/plugins/allopen/allopen-ide/build.gradle.kts index 5187b068b62..340eb114ce5 100644 --- a/plugins/allopen/allopen-ide/build.gradle.kts +++ b/plugins/allopen/allopen-ide/build.gradle.kts @@ -32,7 +32,6 @@ dependencies { testCompileOnly(project(":kotlin-allopen-compiler-plugin")) testCompileOnly(project(":allopen-ide-plugin")) testCompileOnly(project(":kotlin-imports-dumper-compiler-plugin")) - testCompileOnly(project(":kotlin-source-sections-compiler-plugin")) testCompileOnly(project(":kotlinx-serialization-compiler-plugin")) testCompileOnly(project(":kotlinx-serialization-ide-plugin")) testCompileOnly(project(":kotlin-sam-with-receiver-compiler-plugin")) diff --git a/plugins/noarg/noarg-ide/build.gradle.kts b/plugins/noarg/noarg-ide/build.gradle.kts index 43e3dfabe18..d9445012ce9 100644 --- a/plugins/noarg/noarg-ide/build.gradle.kts +++ b/plugins/noarg/noarg-ide/build.gradle.kts @@ -33,7 +33,6 @@ dependencies { testCompileOnly(project(":kotlin-allopen-compiler-plugin")) testCompileOnly(project(":allopen-ide-plugin")) testCompileOnly(project(":kotlin-imports-dumper-compiler-plugin")) - testCompileOnly(project(":kotlin-source-sections-compiler-plugin")) testCompileOnly(project(":kotlinx-serialization-compiler-plugin")) testCompileOnly(project(":kotlinx-serialization-ide-plugin")) testCompileOnly(project(":kotlin-sam-with-receiver-compiler-plugin")) diff --git a/plugins/source-sections/source-sections-compiler/build.gradle.kts b/plugins/source-sections/source-sections-compiler/build.gradle.kts deleted file mode 100644 index 2ac2c026d58..00000000000 --- a/plugins/source-sections/source-sections-compiler/build.gradle.kts +++ /dev/null @@ -1,52 +0,0 @@ - -description = "Kotlin SourceSections Compiler Plugin" - -plugins { - kotlin("jvm") - id("jps-compatible") -} - - -dependencies { - compileOnly(project(":compiler:frontend")) - compileOnly(project(":compiler:plugin-api")) - compileOnly(intellijCoreDep()) { includeJars("intellij-core") } - - testCompile(project(":compiler:frontend")) - testCompile(project(":compiler:plugin-api")) - testCompile(project(":compiler:util")) - testCompile(project(":compiler:cli")) - testCompile(project(":compiler:cli-common")) - testCompile(project(":compiler:frontend.java")) - testCompile(project(":daemon-common")) - testCompile(projectRuntimeJar(":kotlin-daemon-client")) - testCompile(projectTests(":compiler:tests-common")) - testCompile(commonDep("junit:junit")) - testCompileOnly(intellijCoreDep()) { includeJars("intellij-core") } - Platform[193].orLower { - testCompileOnly(intellijDep()) { includeJars("openapi") } - } - testCompileOnly(intellijDep()) { includeJars("idea", "idea_rt", "log4j", "jdom", "jps-model") } - testRuntime(project(":kotlin-reflect")) - - testRuntimeOnly(intellijCoreDep()) { includeJars("intellij-core") } - - Platform[192].orHigher { - testRuntimeOnly(intellijDep()) { includeJars("platform-concurrency") } - } -} - -sourceSets { - "main" { projectDefault() } - "test" { projectDefault() } -} - -projectTest(parallel = true) { - workingDir = rootDir -} - -publish() - -runtimeJar() -sourcesJar() -javadocJar() diff --git a/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor b/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor deleted file mode 100644 index 06b9af7fa14..00000000000 --- a/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.sourceSections.SourceSectionsCommandLineProcessor \ No newline at end of file diff --git a/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar b/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar deleted file mode 100644 index 784b7430fa9..00000000000 --- a/plugins/source-sections/source-sections-compiler/resources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.sourceSections.SourceSectionsComponentRegistrar \ No newline at end of file diff --git a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt b/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt deleted file mode 100644 index b986c19cd53..00000000000 --- a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFile.kt +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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) : 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 = 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) : 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, 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): StringBuilder { - for (i in 0..content.length - 1) { - if (content[i] == '\r') { - content[i] = ' ' - } - } - 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) : Iterator { - - 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!! - } -} - diff --git a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFileExtension.kt b/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFileExtension.kt deleted file mode 100644 index 347d37ef6ec..00000000000 --- a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/FilteredSectionsVirtualFileExtension.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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?) : 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) - } -} \ No newline at end of file diff --git a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/SourceSectionsPlugin.kt b/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/SourceSectionsPlugin.kt deleted file mode 100644 index 72f6d923b42..00000000000 --- a/plugins/source-sections/source-sections-compiler/src/org/jetbrains/kotlin/sourceSections/SourceSectionsPlugin.kt +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.* -import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.config.CompilerConfigurationKey -import org.jetbrains.kotlin.extensions.PreprocessedVirtualFileFactoryExtension - -object SourceSectionsConfigurationKeys { - val SECTIONS_OPTION: CompilerConfigurationKey> = - CompilerConfigurationKey.create("allowed section name") -} - -class SourceSectionsCommandLineProcessor : CommandLineProcessor { - companion object { - val SECTIONS_OPTION = CliOption("allowedSection", "", "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: AbstractCliOption, 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.optionName}") - } -} - -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) { - if (!sections.isEmpty()) { - PreprocessedVirtualFileFactoryExtension.registerExtension(project, FilteredSectionsVirtualFileExtension(sections.toHashSet())) - } -} - diff --git a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts b/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts deleted file mode 100644 index d2559359b57..00000000000 --- a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts +++ /dev/null @@ -1,22 +0,0 @@ - -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") diff --git a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.filtered b/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.filtered deleted file mode 100644 index cc4c93da87d..00000000000 --- a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.filtered +++ /dev/null @@ -1,22 +0,0 @@ - - - - - -let { - println("Hello, World!") -} - - - - - - - - - -apply { - println("That's all, folks!") -} - - diff --git a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.out b/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.out deleted file mode 100644 index 8f9188b07ba..00000000000 --- a/plugins/source-sections/source-sections-compiler/testData/sourceSectionsBasics.kts.out +++ /dev/null @@ -1,2 +0,0 @@ -Hello, World! -That's all, folks! diff --git a/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt b/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt deleted file mode 100644 index 3a236734e74..00000000000 --- a/plugins/source-sections/source-sections-compiler/tests/org/jetbrains/kotlin/sourceSections/SourceSectionsTest.kt +++ /dev/null @@ -1,305 +0,0 @@ -/* - * 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.common.CLIConfigurationKeys -import org.jetbrains.kotlin.cli.common.CLITool -import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoots -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.daemon.client.DaemonReportingTargets -import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient -import org.jetbrains.kotlin.daemon.common.* -import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase.getKotlinPaths -import org.jetbrains.kotlin.script.loadScriptingPlugin -import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys -import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition -import org.jetbrains.kotlin.scripting.definitions.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 -import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration - -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.kotlinPathsForDistDirectory - TestCase.assertTrue("Lib directory doesn't exist. Run 'ant dist'", paths.libPath.absoluteFile.isDirectory) - paths - } - - val compilerClassPath = getKotlinPaths().classPath(KotlinPaths.ClassPaths.Compiler) - val scriptRuntimeClassPath = listOf( kotlinPaths.stdlibPath, kotlinPaths.scriptRuntimePath) - val sourceSectionsPluginJar = File(kotlinPaths.libPath, "kotlin-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( - CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, - PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false) - ) - configuration.add(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, ScriptDefinition.getDefault(defaultJvmScriptingHostConfiguration)) - if (withSourceSectionsPlugin) { - configuration.addAll(SourceSectionsConfigurationKeys.SECTIONS_OPTION, TEST_ALLOWED_SECTIONS) - configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, SourceSectionsComponentRegistrar()) - } - - loadScriptingPlugin(configuration) - - 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 { - 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 = use { - bufferedReader(charset) - .lineSequence() - .map(String::trimEnd) - .toList() - .dropLastWhile { it.isBlank() } - } - - 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 filteredBytes = filteredVF!!.contentsToByteArray() - val actual = ByteArrayInputStream(filteredBytes).trimmedLines(filteredVF.charset) - TestCase.assertEquals("Unexpected result on preprocessing file '${source.name}'", expected, actual) - } - } - - fun testSourceSectionsFilterWithCRLF() { - val sourceToFiltered = getTestFiles(".filtered") - - createEnvironment() // creates VirtualFileManager - val fileCreator = FilteredSectionsVirtualFileExtension(TEST_ALLOWED_SECTIONS.toSet()) - - sourceToFiltered.forEach { (source, expectedResult) -> - val sourceWithCRLF = createTempFile(source.name) - sourceWithCRLF.writeText(source.readText().replace("\r\n", "\n").replace("\n", "\r\n")) - val filteredVF = fileCreator.createPreprocessedFile(StandardFileSystems.local().findFileByPath(sourceWithCRLF.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) - } - } - - // Note: the test is flaky, because it is statistical and the thresholds are not big enough. - // Therefore it was decided to ignore it, but leave in the code in order to be able to quickly check overheads when needed. - @Suppress("unused") - fun ignored_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 = arrayOf(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) = captureOut { - CLITool.doMainNoExit(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, inheritOtherJvmOptions = 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:\ncompiler id: $compilerId\ndaemon opts: $daemonOptions\njvm opts: $daemonJVMOptions\nalive file: $aliveFile\n", 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" }, - "-Xreport-output-files") - - messageCollector.clear() - val outputs = arrayListOf() - - val code = KotlinCompilerClient.compile(daemonWithSession!!.compileService, daemonWithSession.sessionId, 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!!.compileService.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()) - }.first.lines() - .map(String::trimEnd) - .dropLastWhile { it.isBlank() } - - val expected = expectedOutput.inputStream().trimmedLines(Charset.defaultCharset()) - - TestCase.assertEquals("Unexpected result on evaluating ${scriptClass?.name}", 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: () -> T): Pair { - val outStream = ByteArrayOutputStream() - val prevOut = System.out - val prevErr = System.err - System.setOut(PrintStream(outStream)) - System.setErr(PrintStream(outStream)) - val res =try { - body() - } - finally { - System.out.flush() - System.setOut(prevOut) - System.err.flush() - System.setErr(prevErr) - } - return outStream.toString() to res -} - -class TestMessageCollector : MessageCollector { - data class Message(val severity: CompilerMessageSeverity, val message: String, val location: CompilerMessageLocation?) - - val messages = arrayListOf() - - 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 } -} diff --git a/prepare/compiler/build.gradle.kts b/prepare/compiler/build.gradle.kts index 440129a83a5..bbda1b24e29 100644 --- a/prepare/compiler/build.gradle.kts +++ b/prepare/compiler/build.gradle.kts @@ -95,7 +95,6 @@ val distLibraryProjects = listOfNotNull( ":kotlin-scripting-jvm", ":kotlin-scripting-js", ":js:js.engines", - ":kotlin-source-sections-compiler-plugin", ":kotlin-test:kotlin-test-junit", ":kotlin-test:kotlin-test-junit5", ":kotlin-test:kotlin-test-jvm", diff --git a/settings.gradle b/settings.gradle index 9f5fbd1126d..c065aa8f13b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -197,7 +197,6 @@ include ":kotlin-build-common", ":kotlin-sam-with-receiver-compiler-plugin", ":sam-with-receiver-ide-plugin", ":kotlin-imports-dumper-compiler-plugin", - ":kotlin-source-sections-compiler-plugin", ":plugins:uast-kotlin", ":plugins:uast-kotlin-idea", ":plugins:annotation-based-compiler-plugins-ide-support", @@ -436,7 +435,6 @@ project(':kotlin-noarg-compiler-plugin').projectDir = "$rootDir/plugins/noarg/no project(':noarg-ide-plugin').projectDir = "$rootDir/plugins/noarg/noarg-ide" as File project(':kotlin-sam-with-receiver-compiler-plugin').projectDir = "$rootDir/plugins/sam-with-receiver/sam-with-receiver-cli" as File project(':sam-with-receiver-ide-plugin').projectDir = "$rootDir/plugins/sam-with-receiver/sam-with-receiver-ide" as File -project(':kotlin-source-sections-compiler-plugin').projectDir = "$rootDir/plugins/source-sections/source-sections-compiler" as File project(':tools:kotlinp').projectDir = "$rootDir/libraries/tools/kotlinp" as File project(':kotlin-gradle-plugin-api').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-api" as File project(':kotlin-gradle-plugin-dsl-codegen').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-dsl-codegen" as File