|
| 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 | +} |
0 commit comments