Log messages when expressions can not be transformed

This commit is contained in:
Brian Norman
2020-09-25 15:59:19 -05:00
parent 3eebeca753
commit 96f5cba332
3 changed files with 29 additions and 6 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.backend.common.ir.asSimpleLambda
import org.jetbrains.kotlin.backend.common.ir.inline
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
@@ -78,6 +79,7 @@ fun FileLoweringPass.runOnFileInOrder(irFile: IrFile) {
class PowerAssertCallTransformer(
private val context: IrPluginContext,
private val messageCollector: MessageCollector,
private val functions: Set<FqName>
) : IrElementTransformerVoidWithContext(), FileLoweringPass {
private lateinit var file: IrFile
@@ -96,9 +98,15 @@ class PowerAssertCallTransformer(
if (functions.none { fqName == it })
return super.visitCall(expression)
// Find a valid delegate function or do not translate
val delegate = findDelegate(fqName) ?: run {
// Find a valid delegate function or do not translate
// TODO log a warning
val line = fileSource.substring(expression.startOffset).count { it == '\n' } + 1
val location = CompilerMessageLocation.create(file.path, line, -1, null)
messageCollector.report(
CompilerMessageSeverity.WARNING,
"Unable to find overload for function $fqName callable as $fqName(Boolean, String) or $fqName(Boolean, () -> String) for power-assertion transformation",
location
)
return super.visitCall(expression)
}
@@ -108,7 +116,16 @@ class PowerAssertCallTransformer(
// If the tree does not contain any children, the expression is not transformable
val tree = buildAssertTree(assertionArgument)
val root = tree.children.singleOrNull() ?: return super.visitCall(expression)
val root = tree.children.singleOrNull() ?: run {
val line = fileSource.substring(expression.startOffset).count { it == '\n' } + 1
val location = CompilerMessageLocation.create(file.path, line, -1, null)
messageCollector.report(
CompilerMessageSeverity.INFO,
"Expression is constant and will not be power-assertion transformed",
location
)
return super.visitCall(expression)
}
val symbol = currentScope!!.scope.scopeOwnerSymbol
DeclarationIrBuilder(context, symbol).run {
@@ -149,7 +166,7 @@ class PowerAssertCallTransformer(
private fun findDelegate(fqName: FqName): FunctionDelegate? {
return context.findOverloads(fqName)
.mapNotNull { overload ->
// TODO allow other signatures than (Boolean, String) and (Boolean, () -> String))
// TODO allow other signatures than (Boolean, String) and (Boolean, () -> String)
val parameters = overload.owner.valueParameters
if (parameters.size != 2) return@mapNotNull null
if (!parameters[0].type.isBoolean()) return@mapNotNull null
@@ -18,6 +18,8 @@ package com.bnorm.power
import com.google.auto.service.AutoService
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
@@ -38,7 +40,9 @@ class PowerAssertComponentRegistrar(
) {
val functions = configuration[KEY_FUNCTIONS]?.map { FqName(it) } ?: functions
if (functions.isEmpty()) return
IrGenerationExtension.registerExtension(project, PowerAssertIrGenerationExtension(functions.toSet()))
val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
IrGenerationExtension.registerExtension(project, PowerAssertIrGenerationExtension(messageCollector, functions.toSet()))
}
}
@@ -18,15 +18,17 @@ package com.bnorm.power
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.name.FqName
class PowerAssertIrGenerationExtension(
private val messageCollector: MessageCollector,
private val functions: Set<FqName>
) : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
for (file in moduleFragment.files) {
PowerAssertCallTransformer(pluginContext, functions).runOnFileInOrder(file)
PowerAssertCallTransformer(pluginContext, messageCollector, functions).runOnFileInOrder(file)
}
}
}