Skip to content

Commit 897a628

Browse files
mpiekutowskiSpace Team
authored andcommitted
[Test] Add handler for dumping preprocessed inline functions
^KT-80645
1 parent 311fd26 commit 897a628

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.test.backend.handlers
7+
8+
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
9+
import org.jetbrains.kotlin.ir.util.DumpIrTreeOptions
10+
import org.jetbrains.kotlin.ir.util.callableId
11+
import org.jetbrains.kotlin.ir.util.dump
12+
import org.jetbrains.kotlin.ir.util.preparedInlineFunctionCopies
13+
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
14+
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_IR_OF_PREPROCESSED_INLINE_FUNCTIONS
15+
import org.jetbrains.kotlin.test.model.BackendKind
16+
import org.jetbrains.kotlin.test.model.TestModule
17+
import org.jetbrains.kotlin.test.services.TestServices
18+
import org.jetbrains.kotlin.test.services.moduleStructure
19+
import org.jetbrains.kotlin.test.utils.MultiModuleInfoDumper
20+
import org.jetbrains.kotlin.test.utils.withExtension
21+
import java.lang.StringBuilder
22+
23+
private const val PREPROCESSED_INLINE_FUNCTIONS_EXTENSION = "preprocessed.ir.txt"
24+
25+
class IrPreprocessedInlineFunctionDumpHandler(
26+
testServices: TestServices,
27+
artifactKind: BackendKind<IrBackendInput>,
28+
) : AbstractIrHandler(testServices, artifactKind) {
29+
private val dumper = MultiModuleInfoDumper()
30+
31+
override fun processModule(
32+
module: TestModule,
33+
info: IrBackendInput,
34+
) {
35+
if (DUMP_IR_OF_PREPROCESSED_INLINE_FUNCTIONS !in module.directives) return
36+
37+
val functionsToDump = info.irModuleFragment.preparedInlineFunctionCopies?.takeIf { it.isNotEmpty() } ?: return
38+
val builder = dumper.builderForModule(module)
39+
val dumpOptions = DumpIrTreeOptions(printFilePath = false)
40+
41+
builder.append("Module: ${module.name}\n")
42+
functionsToDump.forEach { it.dumpWithCallableId(builder, dumpOptions) }
43+
}
44+
45+
override fun processAfterAllModules(someAssertionWasFailed: Boolean) {
46+
if (dumper.isEmpty()) return
47+
val expectedFile = testServices.moduleStructure.originalTestDataFiles.first().withExtension(PREPROCESSED_INLINE_FUNCTIONS_EXTENSION)
48+
assertions.assertEqualsToFile(expectedFile, dumper.generateResultingDump())
49+
}
50+
51+
private fun IrSimpleFunction.dumpWithCallableId(stringBuilder: StringBuilder, dumpOptions: DumpIrTreeOptions) {
52+
stringBuilder.append("// CallableId=$callableId\n")
53+
stringBuilder.append(dump(dumpOptions))
54+
}
55+
}

compiler/tests-common-new/testFixtures/org/jetbrains/kotlin/test/directives/CodegenTestDirectives.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ object CodegenTestDirectives : SimpleDirectivesContainer() {
118118
description = "Dumps generated backend IR after inlining (enables ${IrTextDumpHandler::class} after inlining)"
119119
)
120120

121+
val DUMP_IR_OF_PREPROCESSED_INLINE_FUNCTIONS by directive(
122+
description = "Dumps generated backend IR of preprocessed inline functions (enables ${IrPreprocessedInlineFunctionDumpHandler::class})"
123+
)
124+
121125
val DUMP_EXTERNAL_CLASS by stringDirective(
122126
description = "Specifies names of external classes which IR should be dumped"
123127
)

0 commit comments

Comments
 (0)