From 072757cddeb39cabf372c35c0121ac207e5645d2 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 28 Sep 2023 14:59:19 +0200 Subject: [PATCH] Test: convert IR source ranges test to the new infrastructure Related to KT-59864, KT-60111, KT-59584 --- .../testData/ir/sourceRanges/comments.txt | 2 +- .../sourceRanges/declarations/classFuns.txt | 2 +- .../declarations/classProperties.txt | 2 +- .../ir/sourceRanges/declarations/classes.txt | 2 +- .../declarations/fakeOverrides.txt | 2 +- .../declarations/primaryConstructors.txt | 2 +- .../declarations/secondaryConstructors.txt | 2 +- .../synthesizedDataClassMembers.txt | 2 +- .../declarations/topLevelFuns.txt | 2 +- .../declarations/topLevelProperties.txt | 2 +- compiler/testData/ir/sourceRanges/kt17108.txt | 2 +- compiler/testData/ir/sourceRanges/kt24258.txt | 4 +- ...lassicJvmIrSourceRangesTestGenerated.java} | 46 ++++--- .../handlers/IrSourceRangesDumpHandler.kt | 114 ++++++++++++++++++ .../test/directives/CodegenTestDirectives.kt | 4 + .../ir/AbstractJvmIrSourceRangesTest.kt | 46 +++++++ .../ir/AbstractIrSourceRangesTestCase.kt | 80 ------------ .../generators/GenerateJUnit3CompilerTests.kt | 5 - .../generators/GenerateJUnit5CompilerTests.kt | 4 + 19 files changed, 209 insertions(+), 116 deletions(-) rename compiler/{tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java => tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/ClassicJvmIrSourceRangesTestGenerated.java} (84%) create mode 100644 compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrSourceRangesDumpHandler.kt create mode 100644 compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/ir/AbstractJvmIrSourceRangesTest.kt delete mode 100644 compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt diff --git a/compiler/testData/ir/sourceRanges/comments.txt b/compiler/testData/ir/sourceRanges/comments.txt index af49d320200..54522520b1e 100644 --- a/compiler/testData/ir/sourceRanges/comments.txt +++ b/compiler/testData/ir/sourceRanges/comments.txt @@ -1,4 +1,4 @@ -@0:0..33:1 FILE fqName: fileName:/comments.kt +@0:0..34:0 FILE fqName: fileName:/comments.kt @1:0..33:1 CLASS CLASS name:Foo modality:FINAL visibility:public superTypes:[kotlin.Any] @1:0..33:1 CONSTRUCTOR visibility:public <> () returnType:.Foo [primary] @1:0..33:1 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/classFuns.txt b/compiler/testData/ir/sourceRanges/declarations/classFuns.txt index ba0bee21335..6cb4bdab048 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classFuns.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classFuns.txt @@ -1,4 +1,4 @@ -@0:0..23:1 FILE fqName:test fileName:/classFuns.kt +@0:0..24:0 FILE fqName:test fileName:/classFuns.kt @3:0..23:1 CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any] @3:0..23:1 CONSTRUCTOR visibility:public <> () returnType:test.Test [primary] @3:0..23:1 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/classProperties.txt b/compiler/testData/ir/sourceRanges/declarations/classProperties.txt index 9627df66c5a..b3cda7566ab 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classProperties.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classProperties.txt @@ -1,4 +1,4 @@ -@0:0..77:1 FILE fqName:test fileName:/classProperties.kt +@0:0..78:0 FILE fqName:test fileName:/classProperties.kt @2:0..77:1 CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any] @2:0..77:1 CONSTRUCTOR visibility:public <> () returnType:test.Test [primary] @2:0..77:1 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/classes.txt b/compiler/testData/ir/sourceRanges/declarations/classes.txt index bfc72c1d171..93c0f63ecaf 100644 --- a/compiler/testData/ir/sourceRanges/declarations/classes.txt +++ b/compiler/testData/ir/sourceRanges/declarations/classes.txt @@ -1,4 +1,4 @@ -@0:0..22:11 FILE fqName: fileName:/classes.kt +@0:0..23:0 FILE fqName: fileName:/classes.kt @0:0..11 CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[kotlin.Any] @0:0..11 CONSTRUCTOR visibility:public <> () returnType:.Test1 [primary] @0:0..11 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/fakeOverrides.txt b/compiler/testData/ir/sourceRanges/declarations/fakeOverrides.txt index 863def43801..e1a20327636 100644 --- a/compiler/testData/ir/sourceRanges/declarations/fakeOverrides.txt +++ b/compiler/testData/ir/sourceRanges/declarations/fakeOverrides.txt @@ -1,4 +1,4 @@ -@0:0..10 FILE fqName: fileName:/fakeOverrides.kt +@0:0..1:0 FILE fqName: fileName:/fakeOverrides.kt @0:0..10 CLASS CLASS name:Test modality:FINAL visibility:public superTypes:[kotlin.Any] @0:0..10 CONSTRUCTOR visibility:public <> () returnType:.Test [primary] @0:0..10 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt b/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt index d8caf9c3c1b..7dc3d9b339c 100644 --- a/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt +++ b/compiler/testData/ir/sourceRanges/declarations/primaryConstructors.txt @@ -1,4 +1,4 @@ -@0:0..13:23 FILE fqName: fileName:/primaryConstructors.kt +@0:0..14:0 FILE fqName: fileName:/primaryConstructors.kt @0:0..23 CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[kotlin.Any] @0:0..23 CONSTRUCTOR visibility:public <> (x:kotlin.Int) returnType:.Test1 [primary] @0:12..22 VALUE_PARAMETER name:x index:0 type:kotlin.Int diff --git a/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt b/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt index 2c4daad71e3..ef1093570ab 100644 --- a/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt +++ b/compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.txt @@ -1,4 +1,4 @@ -@0:0..13:1 FILE fqName: fileName:/secondaryConstructors.kt +@0:0..14:0 FILE fqName: fileName:/secondaryConstructors.kt @0:0..13:1 CLASS CLASS name:C modality:FINAL visibility:public superTypes:[kotlin.Any] @1:4..27 CONSTRUCTOR visibility:public <> () returnType:.C @1:4..27 BLOCK_BODY diff --git a/compiler/testData/ir/sourceRanges/declarations/synthesizedDataClassMembers.txt b/compiler/testData/ir/sourceRanges/declarations/synthesizedDataClassMembers.txt index ede9b58250e..da5349865e7 100644 --- a/compiler/testData/ir/sourceRanges/declarations/synthesizedDataClassMembers.txt +++ b/compiler/testData/ir/sourceRanges/declarations/synthesizedDataClassMembers.txt @@ -1,4 +1,4 @@ -@0:0..4:1 FILE fqName: fileName:/synthesizedDataClassMembers.kt +@0:0..5:0 FILE fqName: fileName:/synthesizedDataClassMembers.kt @0:0..4:1 CLASS CLASS name:C modality:FINAL visibility:public [data] superTypes:[kotlin.Any] @0:5..4:1 CONSTRUCTOR visibility:public <> (x:kotlin.Int, y:kotlin.String, z:kotlin.Any) returnType:.C [primary] @1:8..18 VALUE_PARAMETER name:x index:0 type:kotlin.Int diff --git a/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt b/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt index 86242203c1a..17018e4edcb 100644 --- a/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt +++ b/compiler/testData/ir/sourceRanges/declarations/topLevelFuns.txt @@ -1,4 +1,4 @@ -@0:0..19:14 FILE fqName:test fileName:/topLevelFuns.kt +@0:0..20:0 FILE fqName:test fileName:/topLevelFuns.kt @3:0..14 FUN name:test0 visibility:public modality:FINAL <> () returnType:kotlin.Unit @3:12..14 BLOCK_BODY @9:0..14 FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit diff --git a/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt b/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt index 3babcdb4c78..69e09574ca6 100644 --- a/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt +++ b/compiler/testData/ir/sourceRanges/declarations/topLevelProperties.txt @@ -1,4 +1,4 @@ -@0:0..68:32 FILE fqName:test fileName:/topLevelProperties.kt +@0:0..69:0 FILE fqName:test fileName:/topLevelProperties.kt @3:0..14 PROPERTY name:test0 visibility:public modality:FINAL [val] @3:0..14 FIELD PROPERTY_BACKING_FIELD name:test0 type:kotlin.Int visibility:private [final,static] @3:12..14 EXPRESSION_BODY diff --git a/compiler/testData/ir/sourceRanges/kt17108.txt b/compiler/testData/ir/sourceRanges/kt17108.txt index 079149261d0..9df0fed6b77 100644 --- a/compiler/testData/ir/sourceRanges/kt17108.txt +++ b/compiler/testData/ir/sourceRanges/kt17108.txt @@ -1,4 +1,4 @@ -@0:0..26:1 FILE fqName: fileName:/kt17108.kt +@0:0..27:0 FILE fqName: fileName:/kt17108.kt @4:0..26:1 CLASS INTERFACE name:Boolean modality:ABSTRACT visibility:public superTypes:[kotlin.Any] @8:20..38 FUN name:not visibility:public modality:ABSTRACT <> ($this:.Boolean) returnType:.Boolean [operator] @8:4..38 VALUE_PARAMETER name: type:.Boolean diff --git a/compiler/testData/ir/sourceRanges/kt24258.txt b/compiler/testData/ir/sourceRanges/kt24258.txt index 4fe61650207..b32884b8f67 100644 --- a/compiler/testData/ir/sourceRanges/kt24258.txt +++ b/compiler/testData/ir/sourceRanges/kt24258.txt @@ -2,7 +2,7 @@ @3:0..53 PROPERTY name:lazyNullString visibility:public modality:FINAL [delegated,val] @3:27..53 FIELD PROPERTY_DELEGATE name:lazyNullString$delegate type:kotlin.Lazy<@[FlexibleNullability] kotlin.String?> visibility:private [final,static] @3:30..53 EXPRESSION_BODY - @3:30..53 CALL 'public final fun lazy (initializer: kotlin.Function0): kotlin.Lazy declared in kotlin' type=kotlin.Lazy<@[FlexibleNullability] kotlin.String?> origin=null + @3:30..53 CALL 'public final fun lazy (initializer: kotlin.Function0): kotlin.Lazy declared in kotlin.LazyKt' type=kotlin.Lazy<@[FlexibleNullability] kotlin.String?> origin=null @3:35..53 FUN_EXPR type=kotlin.Function0<@[FlexibleNullability] kotlin.String?> origin=LAMBDA @3:35..53 FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:@[FlexibleNullability] kotlin.String? @3:37..51 BLOCK_BODY @@ -12,7 +12,7 @@ @3:30..53 BLOCK_BODY @3:30..53 RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in ' @3:30..53 TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String - @3:30..53 CALL 'public final fun getValue (thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): T of kotlin.getValue [inline,operator] declared in kotlin' type=@[FlexibleNullability] kotlin.String? origin=null + @3:30..53 CALL 'public final fun getValue (thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): T of kotlin.LazyKt.getValue [inline,operator] declared in kotlin.LazyKt' type=@[FlexibleNullability] kotlin.String? origin=null @3:27..53 GET_FIELD 'FIELD PROPERTY_DELEGATE name:lazyNullString$delegate type:kotlin.Lazy<@[FlexibleNullability] kotlin.String?> visibility:private [final,static]' type=kotlin.Lazy<@[FlexibleNullability] kotlin.String?> origin=null @-1:-1..-1 CONST Null type=kotlin.Nothing? value=null @3:27..53 PROPERTY_REFERENCE 'public final lazyNullString: kotlin.String [delegated,val]' field=null getter='public final fun (): kotlin.String declared in ' setter=null type=kotlin.reflect.KProperty0 origin=PROPERTY_REFERENCE_FOR_DELEGATE diff --git a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/ClassicJvmIrSourceRangesTestGenerated.java similarity index 84% rename from compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java rename to compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/ClassicJvmIrSourceRangesTestGenerated.java index c9e6938f221..f1dc3ffe0c9 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/ir/IrSourceRangesTestCaseGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/ClassicJvmIrSourceRangesTestGenerated.java @@ -3,14 +3,14 @@ * 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.ir; +package org.jetbrains.kotlin.test.runners.ir; import com.intellij.testFramework.TestDataPath; -import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; -import org.jetbrains.kotlin.test.KotlinTestUtils; import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.kotlin.test.TargetBackend; import org.jetbrains.kotlin.test.TestMetadata; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; import java.io.File; import java.util.regex.Pattern; @@ -19,108 +19,118 @@ import java.util.regex.Pattern; @SuppressWarnings("all") @TestMetadata("compiler/testData/ir/sourceRanges") @TestDataPath("$PROJECT_ROOT") -@RunWith(JUnit3RunnerWithInners.class) -public class IrSourceRangesTestCaseGenerated extends AbstractIrSourceRangesTestCase { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); - } - +public class ClassicJvmIrSourceRangesTestGenerated extends AbstractClassicJvmIrSourceRangesTest { + @Test public void testAllFilesPresentInSourceRanges() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/sourceRanges"), Pattern.compile("^(.+)\\.kt$"), null, true); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/sourceRanges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test @TestMetadata("annotations.kt") public void testAnnotations() throws Exception { runTest("compiler/testData/ir/sourceRanges/annotations.kt"); } + @Test @TestMetadata("augmentedAssignmentWithExpression.kt") public void testAugmentedAssignmentWithExpression() throws Exception { runTest("compiler/testData/ir/sourceRanges/augmentedAssignmentWithExpression.kt"); } + @Test @TestMetadata("comments.kt") public void testComments() throws Exception { runTest("compiler/testData/ir/sourceRanges/comments.kt"); } + @Test @TestMetadata("elvis.kt") public void testElvis() throws Exception { runTest("compiler/testData/ir/sourceRanges/elvis.kt"); } + @Test @TestMetadata("kt17108.kt") public void testKt17108() throws Exception { runTest("compiler/testData/ir/sourceRanges/kt17108.kt"); } + @Test @TestMetadata("kt24258.kt") public void testKt24258() throws Exception { runTest("compiler/testData/ir/sourceRanges/kt24258.kt"); } + @Test @TestMetadata("postfixIncrementDecrement.kt") public void testPostfixIncrementDecrement() throws Exception { runTest("compiler/testData/ir/sourceRanges/postfixIncrementDecrement.kt"); } + @Nested @TestMetadata("compiler/testData/ir/sourceRanges/declarations") @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class Declarations extends AbstractIrSourceRangesTestCase { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); - } - + public class Declarations { + @Test public void testAllFilesPresentInDeclarations() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/sourceRanges/declarations"), Pattern.compile("^(.+)\\.kt$"), null, true); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/ir/sourceRanges/declarations"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test @TestMetadata("classFuns.kt") public void testClassFuns() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/classFuns.kt"); } + @Test @TestMetadata("classProperties.kt") public void testClassProperties() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/classProperties.kt"); } + @Test @TestMetadata("classes.kt") public void testClasses() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/classes.kt"); } + @Test @TestMetadata("fakeOverrides.kt") public void testFakeOverrides() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/fakeOverrides.kt"); } + @Test @TestMetadata("kt29862.kt") public void testKt29862() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/kt29862.kt"); } + @Test @TestMetadata("primaryConstructors.kt") public void testPrimaryConstructors() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/primaryConstructors.kt"); } + @Test @TestMetadata("secondaryConstructors.kt") public void testSecondaryConstructors() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/secondaryConstructors.kt"); } + @Test @TestMetadata("synthesizedDataClassMembers.kt") public void testSynthesizedDataClassMembers() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/synthesizedDataClassMembers.kt"); } + @Test @TestMetadata("topLevelFuns.kt") public void testTopLevelFuns() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/topLevelFuns.kt"); } + @Test @TestMetadata("topLevelProperties.kt") public void testTopLevelProperties() throws Exception { runTest("compiler/testData/ir/sourceRanges/declarations/topLevelProperties.kt"); diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrSourceRangesDumpHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrSourceRangesDumpHandler.kt new file mode 100644 index 00000000000..5e64602ecbe --- /dev/null +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/IrSourceRangesDumpHandler.kt @@ -0,0 +1,114 @@ +/* + * Copyright 2010-2023 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.backend.handlers + +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.IrFileEntry +import org.jetbrains.kotlin.ir.SourceRangeInfo +import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer +import org.jetbrains.kotlin.ir.util.RenderIrElementVisitor +import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid +import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid +import org.jetbrains.kotlin.ir.visitors.acceptVoid +import org.jetbrains.kotlin.test.backend.ir.IrBackendInput +import org.jetbrains.kotlin.test.directives.CodegenTestDirectives +import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives +import org.jetbrains.kotlin.test.directives.model.DirectivesContainer +import org.jetbrains.kotlin.test.model.BackendKind +import org.jetbrains.kotlin.test.model.TestModule +import org.jetbrains.kotlin.test.services.TestServices +import org.jetbrains.kotlin.test.services.moduleStructure +import org.jetbrains.kotlin.test.utils.MultiModuleInfoDumper +import org.jetbrains.kotlin.test.utils.withExtension +import org.jetbrains.kotlin.utils.Printer +import java.io.File + +class IrSourceRangesDumpHandler( + testServices: TestServices, + artifactKind: BackendKind, +) : AbstractIrHandler(testServices, artifactKind) { + companion object { + const val DUMP_EXTENSION = "txt" + } + + override val directiveContainers: List + get() = listOf(CodegenTestDirectives, FirDiagnosticsDirectives) + + private val baseDumper = MultiModuleInfoDumper() + private val buildersForSeparateFileDumps: MutableMap = mutableMapOf() + + override fun processModule(module: TestModule, info: IrBackendInput) { + if (CodegenTestDirectives.DUMP_SOURCE_RANGES_IR !in module.directives) return + info.processAllIrModuleFragments(module) { irModuleFragment, moduleName -> + val builder = baseDumper.builderForModule(moduleName) + + for (irFile in irModuleFragment.files) { + builder.append(irFile.dumpWithSourceLocations(irFile.fileEntry)) + } + } + } + + private fun IrElement.dumpWithSourceLocations(fileEntry: IrFileEntry): String = + StringBuilder().also { + acceptVoid(DumpSourceLocations(it, fileEntry)) + }.toString() + + private class DumpSourceLocations( + out: Appendable, + val fileEntry: IrFileEntry + ) : IrElementVisitorVoid { + val printer = Printer(out, " ") + val elementRenderer = RenderIrElementVisitor() + + private fun printElement(element: IrElement) { + val sourceRangeInfo = fileEntry.getSourceRangeInfo(element.startOffset, element.endOffset) + printer.println("@${sourceRangeInfo.render()} ${element.accept(elementRenderer, null)}") + } + + override fun visitElement(element: IrElement) { + printElement(element) + printer.pushIndent() + if (element is IrAnnotationContainer && element.annotations.isNotEmpty()) { + printer.println("annotations:") + printer.pushIndent() + for (annotation in element.annotations) { + printElement(annotation) + printer.pushIndent() + annotation.acceptChildrenVoid(this) + printer.popIndent() + } + printer.popIndent() + } + element.acceptChildrenVoid(this) + printer.popIndent() + } + + private fun SourceRangeInfo.render() = + if (startLineNumber == endLineNumber) + "$startLineNumber:$startColumnNumber..$endColumnNumber" + else + "$startLineNumber:$startColumnNumber..$endLineNumber:$endColumnNumber" + } + + + override fun processAfterAllModules(someAssertionWasFailed: Boolean) { + val moduleStructure = testServices.moduleStructure + val defaultExpectedFile = moduleStructure.originalTestDataFiles.first() + .withExtension(moduleStructure.modules.first().getDumpExtension()) + checkOneExpectedFile(defaultExpectedFile, baseDumper.generateResultingDump()) + buildersForSeparateFileDumps.entries.forEach { (expectedFile, dump) -> checkOneExpectedFile(expectedFile, dump.toString()) } + } + + private fun checkOneExpectedFile(expectedFile: File, actualDump: String) { + if (actualDump.isNotEmpty()) { + assertions.assertEqualsToFile(expectedFile, actualDump) + } + } + + private fun TestModule.getDumpExtension(ignoreFirIdentical: Boolean = false): String { + return IrTextDumpHandler.computeDumpExtension(this, DUMP_EXTENSION, ignoreFirIdentical) + } +} diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/CodegenTestDirectives.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/CodegenTestDirectives.kt index 69c3009fa8e..1416450f9c4 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/CodegenTestDirectives.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/directives/CodegenTestDirectives.kt @@ -147,6 +147,10 @@ object CodegenTestDirectives : SimpleDirectivesContainer() { description = "Dumps generated backend IR in pretty kotlin dump (enables ${IrPrettyKotlinDumpHandler::class})" ) + val DUMP_SOURCE_RANGES_IR by directive( + description = "Dumps generated backend IR together with elements source ranges (enables ${IrSourceRangesDumpHandler::class})" + ) + val SKIP_KT_DUMP by directive( description = "Skips check pretty kt IR dump (disables ${IrPrettyKotlinDumpHandler::class})" ) diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/ir/AbstractJvmIrSourceRangesTest.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/ir/AbstractJvmIrSourceRangesTest.kt new file mode 100644 index 00000000000..d64ede29054 --- /dev/null +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/ir/AbstractJvmIrSourceRangesTest.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2023 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.runners.ir + +import org.jetbrains.kotlin.test.Constructor +import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder +import org.jetbrains.kotlin.test.backend.ir.IrBackendInput +import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_IR +import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_KT_IR +import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_SIGNATURES +import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_SOURCE_RANGES_IR +import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2IrConverter +import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendFacade +import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendOutputArtifact +import org.jetbrains.kotlin.test.model.* + +abstract class AbstractJvmIrSourceRangesTest> : + AbstractJvmIrTextTest() { + + override fun configure(builder: TestConfigurationBuilder) { + super.configure(builder) + with(builder) { + defaultDirectives { + +DUMP_SOURCE_RANGES_IR + -DUMP_KT_IR + -DUMP_IR + -DUMP_SIGNATURES + } + } + } +} + +open class AbstractClassicJvmIrSourceRangesTest : AbstractJvmIrSourceRangesTest() { + + override val frontend: FrontendKind<*> + get() = FrontendKinds.ClassicFrontend + + override val frontendFacade: Constructor> + get() = ::ClassicFrontendFacade + + override val converter: Constructor> + get() = ::ClassicFrontend2IrConverter +} diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt deleted file mode 100644 index f24f032275e..00000000000 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrSourceRangesTestCase.kt +++ /dev/null @@ -1,80 +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.ir - -import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer -import org.jetbrains.kotlin.ir.util.RenderIrElementVisitor -import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid -import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid -import org.jetbrains.kotlin.ir.visitors.acceptVoid -import org.jetbrains.kotlin.test.KotlinTestUtils -import org.jetbrains.kotlin.utils.Printer -import java.io.File - -abstract class AbstractIrSourceRangesTestCase : AbstractIrGeneratorTestCase() { - override fun doTest(wholeFile: File, testFiles: List) { - val dir = wholeFile.parentFile - val testFileToIrFile = generateIrFilesAsSingleModule(testFiles) - for ((testFile, irFile) in testFileToIrFile) { - val irFileDump = irFile.dumpWithSourceLocations(irFile.fileEntry) - val expectedSourceLocations = File(dir, testFile.name.replace(".kt", ".txt")) - KotlinTestUtils.assertEqualsToFile(expectedSourceLocations, irFileDump) - } - } - - private fun IrElement.dumpWithSourceLocations(fileEntry: IrFileEntry): String = - StringBuilder().also { - acceptVoid(DumpSourceLocations(it, fileEntry)) - }.toString() - - private class DumpSourceLocations( - out: Appendable, - val fileEntry: IrFileEntry - ) : IrElementVisitorVoid { - val printer = Printer(out, " ") - val elementRenderer = RenderIrElementVisitor() - - private fun printElement(element: IrElement) { - val sourceRangeInfo = fileEntry.getSourceRangeInfo(element.startOffset, element.endOffset) - printer.println("@${sourceRangeInfo.render()} ${element.accept(elementRenderer, null)}") - } - - override fun visitElement(element: IrElement) { - printElement(element) - printer.pushIndent() - if (element is IrAnnotationContainer && element.annotations.isNotEmpty()) { - printer.println("annotations:") - printer.pushIndent() - for (annotation in element.annotations) { - printElement(annotation) - printer.pushIndent() - annotation.acceptChildrenVoid(this) - printer.popIndent() - } - printer.popIndent() - } - element.acceptChildrenVoid(this) - printer.popIndent() - } - - private fun SourceRangeInfo.render() = - if (startLineNumber == endLineNumber) - "$startLineNumber:$startColumnNumber..$endColumnNumber" - else - "$startLineNumber:$startColumnNumber..$endLineNumber:$endColumnNumber" - } -} diff --git a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit3CompilerTests.kt b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit3CompilerTests.kt index 41f7a62ed28..6ea6f06100c 100644 --- a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit3CompilerTests.kt +++ b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit3CompilerTests.kt @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.generators.util.TestGeneratorUtil import org.jetbrains.kotlin.generators.util.TestGeneratorUtil.KT_OR_KTS_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.integration.AbstractAntTaskTest import org.jetbrains.kotlin.ir.AbstractIrCfgTestCase -import org.jetbrains.kotlin.ir.AbstractIrSourceRangesTestCase import org.jetbrains.kotlin.jvm.compiler.* import org.jetbrains.kotlin.jvm.compiler.ir.AbstractIrCompileJavaAgainstKotlinTest import org.jetbrains.kotlin.jvm.compiler.ir.AbstractIrCompileKotlinAgainstJavaTest @@ -115,10 +114,6 @@ fun generateJUnit3CompilerTests(args: Array, mainClassName: String?) { model("ir/irCfg") } - testClass { - model("ir/sourceRanges") - } - testClass { model("codegen/topLevelMemberInvocation", extension = null, recursive = false) } diff --git a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt index 90c92605658..cd225a76104 100644 --- a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt +++ b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt @@ -123,6 +123,10 @@ fun generateJUnit5CompilerTests(args: Array, mainClassName: String?) { model("ir/irText") } + testClass { + model("ir/sourceRanges") + } + testClass { model("codegen/bytecodeText") }