Skip to content

Commit

Permalink
Bump Kotlin to 1.7.20
Browse files Browse the repository at this point in the history
Disable transformation for functions all non simple functions with synthetic bodies

Signed-off-by: Ivan Milisavljevic <[email protected]>
  • Loading branch information
milis92 committed Oct 22, 2022
1 parent 908af7b commit 81833d0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dependencies/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
#build
kotlin = "1.7.10"
kotlin = "1.7.20"
dokka = "1.7.10"
maven-publish = "0.21.0"
build-config = "3.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,45 @@
package com.herman.krang.internal

import com.herman.krang.internal.transformers.toKrangFunction
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrSyntheticBody
import org.jetbrains.kotlin.ir.interpreter.getLastOverridden
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.ir.util.isFakeOverriddenFromAny
import org.jetbrains.kotlin.ir.util.parentClassOrNull
import org.jetbrains.kotlin.ir.util.superTypes
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid

class KrangTransformer(
private val pluginContext: IrPluginContext,
private val godMode: Boolean
) : IrElementTransformerVoidWithContext() {
) : IrElementTransformerVoid() {

override fun visitFunctionNew(
declaration: IrFunction
): IrStatement {
override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement {
if (declaration.shouldVisit()) {
// Construct a new block body and filter out all value parameters that should not be passed to Krang
declaration.body = declaration.toKrangFunction(pluginContext) { valueParameter ->
// Filter all value parameters that are annotated with Redact Annotation
valueParameter.annotatedOrExtendsAnnotated(pluginContext.krangRedactAnnotation)
}
}
return super.visitFunctionNew(declaration)
return super.visitSimpleFunction(declaration)
}

/**
* Determines if the function should be transformed by krang
* Order of checks:
* - If the function body is null - escape
* - If the function is synthetic function for lambda - escape
* - If godMode is enabled - run the transformation (if previous conditions apply)
* - If a function has the Intercept annotation - run the transformation (if previous conditions apply)
*
* Synthetic functions or functions with empty or synthetic bodies will not be transformed
*/
private fun IrFunction.shouldVisit(): Boolean {
return if (body == null || this.origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA)
return if (body == null || body is IrSyntheticBody || name.isSpecial)
false
else godMode || hasAnnotation(pluginContext.krangInterceptAnnotation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.herman.krang.internal.transformers

import com.herman.krang.internal.krangInterceptFunctionCall
import com.herman.krang.internal.krangRuntime
import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
Expand All @@ -13,6 +12,7 @@ import org.jetbrains.kotlin.ir.builders.irGetObject
import org.jetbrains.kotlin.ir.builders.irString
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl
Expand Down Expand Up @@ -55,11 +55,7 @@ fun IrFunction.toKrangFunction(
putValueArgument(1, argsAsVarArg.deepCopyWithVariables())
dispatchReceiver = irGetObject(context.krangRuntime)
}
// Apply original statements
runCatching {
//swallow Synthetic body contains no statements error
for (statement in body.statements) +statement
}
for (statement in body.statements) +statement
}

// Constructs a new vararg of value parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class CompilerTest {
val clazz = classLoader.loadClass("Main")
val func = clazz.methods.single { it.name == "foo" && it.parameterCount == 0 }

assertInvoke(listOf("Testable.<init>", "Testable.<init>"), emptyList()) {
assertInvoke(listOf(), emptyList()) {
func.invoke(clazz.getDeclaredConstructor().newInstance())
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ class CompilerTest {
val clazz = classLoader.loadClass("Main")
val func = clazz.methods.single { it.name == "foo" }

assertInvoke(listOf("Main.<init>", "Main.foo"), arguments) {
assertInvoke(listOf("Main.foo"), arguments) {
func.invoke(clazz.getDeclaredConstructor().newInstance(), *arguments.toTypedArray())
}
}
Expand All @@ -265,7 +265,7 @@ class CompilerTest {
val clazz = classLoader.loadClass("Main")
val func = clazz.methods.single { it.name == "foo" }

assertInvoke(listOf("Main.<init>", "Main.foo"), arguments) {
assertInvoke(listOf("Main.foo"), arguments) {
func.invoke(clazz.getDeclaredConstructor().newInstance(), *arguments.toTypedArray())
}
}
Expand All @@ -279,7 +279,7 @@ class CompilerTest {
val clazz = classLoader.loadClass("Main")
val func = clazz.methods.single { it.name == "foo" }

assertInvoke(listOf("Main.<init>", "Main.foo"), arguments) {
assertInvoke(listOf("Main.foo"), arguments) {
func.invoke(clazz.getDeclaredConstructor().newInstance(), *arguments.toTypedArray())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GodModeTest {
val clazz = classLoader.loadClass("Main")
val func = clazz.methods.single { it.name == "foo" }

assertInvoke(listOf("Main.<init>", "Main.foo"), arguments) {
assertInvoke(listOf("Main.foo"), arguments) {
func.invoke(clazz.getDeclaredConstructor().newInstance(), *arguments.toTypedArray())
}
}
Expand Down
18 changes: 2 additions & 16 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@file:Suppress("UnstableApiUsage")

import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation

plugins {
kotlin("multiplatform") version "1.7.10"
kotlin("multiplatform") version "1.7.20"
id("com.android.application") version "7.2.2"
id("com.github.milis92.krang") version "2.5.0"
id("com.github.milis92.krang") version "2.7.0-SNAPSHOT"
}

group = "com.herman.sample"
Expand All @@ -15,17 +12,6 @@ version = "1.0"
krang {
enabled.set(true)
godMode.set(true)

variantFilter {
val kotlinCompilation: KotlinCompilation<*> = this
when (kotlinCompilation) {
is KotlinJvmAndroidCompilation -> {
if (kotlinCompilation.androidVariant.buildType.name == "release") {
enabled.set(false)
}
}
}
}
}

android {
Expand Down
1 change: 0 additions & 1 deletion sample/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dependencyResolutionManagement {
google()
mavenLocal()
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots")
}
}

Expand Down

0 comments on commit 81833d0

Please sign in to comment.