Test framewrk update: check lambda inlining

This commit is contained in:
Michael Bogdanov
2015-04-13 11:57:23 +03:00
parent 27c40edeb4
commit 7025a4f933
41 changed files with 176 additions and 51 deletions
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun box() : String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun box() : String {
@@ -20,5 +21,4 @@ fun box() : String {
if (result != "OK11") return "fail $result"
return "OK"
}
}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(): String {
@@ -26,5 +27,4 @@ fun box() : String {
if (result2 != "OKGOOD") return "fail2 $result2"
return "OK"
}
}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(): String {
@@ -26,5 +27,4 @@ fun box() : String {
if (result2 != "OK11") return "fail2 $result2"
return "OK"
}
}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import builders.*
fun testAllInline() : String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import builders.*
import kotlin.InlineOption.*
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun box(): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun box(): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun Data.test1(d: Data) : Long {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import zzz.*
fun box(): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(): Int {
return 1.inlineMethod()
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(s: Long): String {
@@ -1,10 +1,11 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import java.util.*
fun test1(prefix: String): String {
var result = "fail"
mfun {
concat("start") {
noInline("start") {
if (it.startsWith(prefix)) {
result = "OK"
}
@@ -4,6 +4,6 @@ inline fun <R> mfun(f: () -> R) {
f()
}
fun concat(suffix: String, l: (s: String) -> Unit) {
fun noInline(suffix: String, l: (s: String) -> Unit) {
l(suffix)
}
@@ -1,5 +1,5 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import kotlin.InlineOption.*
fun test1(param: String): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun sameName(s: Long): Long {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import kotlin.InlineOption.*
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(d: Data): Int {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
fun box() : String {
test {"123"}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
fun box() : String {
test {"123"}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
fun test1(): Int {
return calc( { l: Int -> 2*l}, { l: Int -> 4*l})
}
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(param: String): String {
@@ -1,5 +1,5 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
import kotlin.InlineOption.*
inline fun test1(inlineOptions(ONLY_LOCAL_RETURN) param: () -> String): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
fun test1(): Int {
val inlineX = Inline()
var p = { l : Int -> l};
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun test1(b: Boolean): String {
@@ -90,7 +90,7 @@ fun box(): String {
}
public fun assertError(index: Int, h: Holder, expected: String, l: (h: Holder) -> Holder) {
inline fun assertError(index: Int, h: Holder, expected: String, l: (h: Holder) -> Holder) {
try {
l(h)
fail("fail $index: no error")
@@ -90,7 +90,7 @@ fun box(): String {
}
public fun assertError(index: Int, h: Holder, expected: String, l: (h: Holder) -> Holder) {
inline fun assertError(index: Int, h: Holder, expected: String, l: (h: Holder) -> Holder) {
try {
l(h)
fail("fail $index: no error")
@@ -53,7 +53,7 @@ fun box() : String {
return "OK"
}
public fun assertError(index: Int, expected: String, l: Test.()->Unit) {
inline fun assertError(index: Int, expected: String, l: Test.()->Unit) {
val testLocal = Test()
try {
testLocal.l()
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
fun box(): String {
@@ -1,3 +1,4 @@
//NO_CHECK_LAMBDA_INLINING
import test.*
val s = doWork({11})
@@ -15,7 +15,7 @@ fun test(): String {
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//SMAP
//lambda.1.kt
@@ -3,5 +3,5 @@ package builders
inline fun call(inlineOptions(InlineOption.ONLY_LOCAL_RETURN) init: () -> Unit) {
return init()
}
//NO_CHECK_LAMBDA_INLINING
//SMAP ABSENT
@@ -17,8 +17,8 @@ inline fun test(): String {
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//TODO SHOULD BE LESS
//SMAP
//lambdaOnInlineCallSite.1.kt
//Kotlin
@@ -15,7 +15,7 @@ fun test(): String {
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//SMAP
//object.1.kt
//Kotlin
@@ -19,7 +19,7 @@ fun test(): String {
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//SMAP
//objectOnCallSite.1.kt
@@ -19,8 +19,8 @@ inline fun test(): String {
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//TODO SHOULD BE LESS
//SMAP
//objectOnInlineCallSite.1.kt
//Kotlin
@@ -4,7 +4,7 @@ import kotlin.InlineOption.*
fun box(): String {
return test()
}
//NO_CHECK_LAMBDA_INLINING
//SXMAP
//objectOnInlineCallSite2.1.kt
@@ -4,6 +4,6 @@ import kotlin.InlineOption.*
fun box(): String {
return test{"OK"}
}
//NO_CHECK_LAMBDA_INLINING
//TODO
@@ -18,81 +18,100 @@ package org.jetbrains.kotlin.codegen;
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin
import org.jetbrains.kotlin.backend.common.output.OutputFile
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
import org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache
import org.jetbrains.kotlin.load.kotlin.PackageClassUtils
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
import org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.utils.join
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.tree.MethodNode
import java.util.ArrayList
import java.util.HashSet
import kotlin.properties.Delegates
public object InlineTestUtil {
public val INLINE_ANNOTATION_CLASS: String = "kotlin/inline"
public fun checkNoCallsToInline(files: List<OutputFile>) {
val inlinedMethods = collectInlineMethods(files)
assert(!inlinedMethods.isEmpty(), "There are no inline methods")
public fun checkNoCallsToInline(files: List<OutputFile>, sourceFiles: List<JetFile>) {
val inlineInfo = obtainInlineInfo(files)
val inlineMethods = inlineInfo.inlineMethods
assert(!inlineMethods.isEmpty(), "There are no inline methods")
val notInlinedCalls = checkInlineNotInvoked(files, inlinedMethods)
assert(notInlinedCalls.isEmpty()) { "All inline methods should be inlined but " + StringUtil.join(notInlinedCalls, "\n") }
val notInlinedCalls = checkInlineMethodNotInvoked(files, inlineMethods)
assert(notInlinedCalls.isEmpty()) { "All inline methods should be inlined but:\n" + notInlinedCalls.joinToString("\n") }
val skipParameterChecking =
sourceFiles.sequence().filter {
InTextDirectivesUtils.isDirectiveDefined(it.getText(), "NO_CHECK_LAMBDA_INLINING")
}.any()
if (!skipParameterChecking) {
val notInlinedParameters = checkParametersInlined(files, inlineInfo)
assert(notInlinedParameters.isEmpty()) { "All inline parameters should be inlined but:\n${notInlinedParameters.joinToString("\n")}\n" +
"but if you have not inlined lambdas or anonymous objects enable NO_CHECK_LAMBDA_INLINING directive" }
}
}
private fun collectInlineMethods(files: List<OutputFile>): Set<MethodInfo> {
private fun obtainInlineInfo(files: List<OutputFile>): InlineInfo {
val inlineMethods = HashSet<MethodInfo>()
val classHeaders = hashMapOf<String, KotlinClassHeader>()
for (file in files) {
val cr = ClassReader(file.asByteArray())
var className: String? = null
cr.accept(object : ClassVisitor(Opcodes.ASM4) {
override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array<String>?) {
className = name
super.visit(version, access, name, signature, superName, interfaces)
}
val classVisitor = object : ClassVisitorWithName() {
override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor {
return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) {
return object : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
public override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor {
val type = Type.getType(desc)
val annotationClass = type.getInternalName()
if (INLINE_ANNOTATION_CLASS == annotationClass) {
inlineMethods.add(MethodInfo(className!!, name, this.desc))
inlineMethods.add(MethodInfo(className, name, this.desc))
}
return super.visitAnnotation(desc, visible)
}
}
}
}, 0)
}
cr.accept(classVisitor, 0)
classHeaders.put(classVisitor.className, getClassHeader(file))
}
return inlineMethods
return InlineInfo(inlineMethods, classHeaders)
}
private fun checkInlineNotInvoked(files: List<OutputFile>, inlinedMethods: Set<MethodInfo>): List<NotInlinedCall> {
private fun checkInlineMethodNotInvoked(files: List<OutputFile>, inlinedMethods: Set<MethodInfo>): List<NotInlinedCall> {
val notInlined = ArrayList<NotInlinedCall>()
files.forEach { file ->
val cr = ClassReader(file.asByteArray())
var className: String? = null
cr.accept(object : ClassVisitor(Opcodes.ASM4) {
override fun visit(version: Int, access: Int, name: String, signature: String, superName: String, interfaces: Array<String>) {
className = name
super.visit(version, access, name, signature, superName, interfaces)
}
cr.accept(object : ClassVisitorWithName() {
override fun visitMethod(access: Int, name: String, desc: String, signature: String, exceptions: Array<String>): MethodVisitor? {
val classFqName = JvmClassName.byInternalName(className!!).getFqNameForClassNameWithoutDollars()
val classFqName = JvmClassName.byInternalName(className).getFqNameForClassNameWithoutDollars()
if (PackageClassUtils.isPackageClassFqName(classFqName)) {
return null
}
return object : MethodNode(Opcodes.ASM4, access, name, desc, signature, exceptions) {
return object : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
public override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
val methodCall = MethodInfo(owner, name, desc)
if (inlinedMethods.contains(methodCall)) {
val fromCall = MethodInfo(className!!, this.name, this.desc)
val fromCall = MethodInfo(className, this.name, this.desc)
//skip delegation to trait impl from child class
if (methodCall.owner.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX) && fromCall.owner != methodCall.owner) {
@@ -109,7 +128,92 @@ public object InlineTestUtil {
return notInlined
}
private data class NotInlinedCall(public val fromCall: MethodInfo, public val inlineMethod: MethodInfo)
private fun checkParametersInlined(files: List<OutputFile>, inlineInfo: InlineInfo): ArrayList<NotInlinedParameter> {
val inlinedMethods = inlineInfo.inlineMethods
val notInlinedParameters = ArrayList<NotInlinedParameter>()
for (file in files) {
val kotlinClassHeader = getClassHeader(file)
if (isClassOrPackagePartKind(kotlinClassHeader)) {
val cr = ClassReader(file.asByteArray())
cr.accept(object : ClassVisitorWithName() {
override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor? {
val classFqName = JvmClassName.byInternalName(className).getFqNameForClassNameWithoutDollars()
val declaration = MethodInfo(className, name, desc)
//do not check anonymous object creation in inline functions and in package facades
if (PackageClassUtils.isPackageClassFqName(classFqName) || declaration in inlinedMethods) {
return null
}
return object : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
private fun isInlineParameterLikeOwner(owner: String) = owner.contains("$") && !isTopLevelOrInnerOrPackageClass(owner, inlineInfo)
public override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
if ("<init>".equals(name) && isInlineParameterLikeOwner(owner)) {
/*constuctor creation*/
val fromCall = MethodInfo(className, this.name, this.desc)
notInlinedParameters.add(NotInlinedParameter(owner, fromCall))
}
}
override fun visitFieldInsn(opcode: Int, owner: String, name: String, desc: String) {
if (opcode == Opcodes.GETSTATIC && isInlineParameterLikeOwner(owner)) {
val fromCall = MethodInfo(className, this.name, this.desc)
notInlinedParameters.add(NotInlinedParameter(owner, fromCall))
}
super.visitFieldInsn(opcode, owner, name, desc)
}
}
}
}, 0)
}
}
return notInlinedParameters
}
private fun isTopLevelOrInnerOrPackageClass(classInternalName: String, inlineInfo: InlineInfo): Boolean {
if (classInternalName.startsWith("kotlin/jvm/internal/"))
return true
return isClassOrPackagePartKind(inlineInfo.classHeaders[classInternalName]!!)
}
private fun isClassOrPackagePartKind(header: KotlinClassHeader): Boolean {
return header.classKind == JvmAnnotationNames.KotlinClass.Kind.CLASS
|| header.syntheticClassKind == JvmAnnotationNames.KotlinSyntheticClass.Kind.PACKAGE_PART
|| header.syntheticClassKind == JvmAnnotationNames.KotlinSyntheticClass.Kind.TRAIT_IMPL
}
private fun getClassHeader(file: OutputFile): KotlinClassHeader {
return FileBasedKotlinClass.create(file.asByteArray()) {
className, classHeader, innerClasses ->
object : FileBasedKotlinClass(className, classHeader, innerClasses) {
override fun getLocation(): String = throw UnsupportedOperationException()
override fun getFileContents(): ByteArray = throw UnsupportedOperationException()
override fun hashCode(): Int = throw UnsupportedOperationException()
override fun equals(other: Any?): Boolean = throw UnsupportedOperationException()
override fun toString(): String? = throw UnsupportedOperationException()
}
}.getClassHeader()
}
private class InlineInfo(val inlineMethods: Set<MethodInfo>, val classHeaders: Map<String, KotlinClassHeader>)
private data class NotInlinedCall(val fromCall: MethodInfo, val inlineMethod: MethodInfo)
private data class NotInlinedParameter(val parameterClassName: String, val fromCall: MethodInfo)
private data class MethodInfo(val owner: String, val name: String, val desc: String)
open private class ClassVisitorWithName() : ClassVisitor(Opcodes.ASM5) {
var className: String by Delegates.notNull()
override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array<String>?) {
className = name
super.visit(version, access, name, signature, superName, interfaces)
}
}
}
@@ -28,7 +28,7 @@ public abstract class AbstractBlackBoxInlineCodegenTest : AbstractBlackBoxCodege
doTestMultiFile(inputFiles)
try {
InlineTestUtil.checkNoCallsToInline(initializedClassLoader.getAllGeneratedFiles())
InlineTestUtil.checkNoCallsToInline(initializedClassLoader.getAllGeneratedFiles(), myFiles.getPsiFiles())
checkSMAP(myFiles.getPsiFiles(), generateClassesInFile().asList())
}
catch (e: Throwable) {
@@ -30,8 +30,8 @@ public abstract class AbstractCompileKotlinAgainstInlineKotlinTest : AbstractCom
val allGeneratedFiles = factory1.asList() + factory2.asList()
try {
InlineTestUtil.checkNoCallsToInline(allGeneratedFiles)
val sourceFiles = factory1.getInputFiles() + factory2.getInputFiles()
InlineTestUtil.checkNoCallsToInline(allGeneratedFiles, sourceFiles)
checkSMAP(sourceFiles, allGeneratedFiles)
}
catch (e: Throwable) {