Check lambda inlining in package part files in test framework

This commit is contained in:
Michael Bogdanov
2016-08-17 16:54:06 +03:00
parent f2b8569b4b
commit a12d7b6019
22 changed files with 98 additions and 48 deletions
@@ -40,7 +40,7 @@ object InlineTestUtil {
val skipParameterChecking = sourceFiles.any {
InTextDirectivesUtils.isDirectiveDefined(it.text, "NO_CHECK_LAMBDA_INLINING")
}
} || !doLambdaInliningCheck(files, inlineInfo)
if (!skipParameterChecking) {
val notInlinedParameters = checkParametersInlined(files, inlineInfo)
@@ -54,20 +54,16 @@ object InlineTestUtil {
private fun obtainInlineInfo(files: Iterable<OutputFile>): InlineInfo {
val inlineMethods = HashSet<MethodInfo>()
val binaryClasses = hashMapOf<String, KotlinJvmBinaryClass>()
for (file in files) {
val binaryClass = loadBinaryClass(file)
val inlineFunctions = inlineFunctionsJvmNames(binaryClass.classHeader)
val classVisitor = object : ClassVisitorWithName() {
override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor {
return object : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
override fun visitEnd() {
if (name + desc in inlineFunctions) {
inlineMethods.add(MethodInfo(className, name, this.desc))
}
}
override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor? {
if (name + desc in inlineFunctions) {
inlineMethods.add(MethodInfo(className, name, desc))
}
return null
}
}
@@ -78,6 +74,34 @@ object InlineTestUtil {
return InlineInfo(inlineMethods, binaryClasses)
}
private fun doLambdaInliningCheck(files: Iterable<OutputFile>, inlineInfo: InlineInfo): Boolean {
var doLambdaInliningCheck = true
for (file in files) {
val binaryClass = loadBinaryClass(file)
val inlineFunctions = inlineFunctionsJvmNames(binaryClass.classHeader)
val classVisitor = object : ClassVisitorWithName() {
override fun visitMethod(access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?): MethodVisitor? {
if (name + desc in inlineFunctions) {
return object: MethodNodeWithAnonymousObjectCheck(inlineInfo, access, name, desc, signature, exceptions) {
override fun onAnonymousConstructorCallOrSingletonAccess(owner: String) {
doLambdaInliningCheck = false
}
}
}
return null
}
}
ClassReader(file.asByteArray()).accept(classVisitor, 0)
if (!doLambdaInliningCheck) break
}
return doLambdaInliningCheck
}
private fun checkInlineMethodNotInvoked(files: Iterable<OutputFile>, inlinedMethods: Set<MethodInfo>): List<NotInlinedCall> {
val notInlined = ArrayList<NotInlinedCall>()
@@ -139,23 +163,10 @@ object InlineTestUtil {
return null
}
return object : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
private fun isInlineParameterLikeOwner(owner: String) =
"$" in owner && !isTopLevelOrInnerOrPackageClass(owner, inlineInfo)
override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
if ("<init>".equals(name) && isInlineParameterLikeOwner(owner)) {
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)
return object: MethodNodeWithAnonymousObjectCheck(inlineInfo, access, name, desc, signature, exceptions) {
override fun onAnonymousConstructorCallOrSingletonAccess(owner: String) {
val fromCall = MethodInfo(className, this.name, this.desc)
notInlinedParameters.add(NotInlinedParameter(owner, fromCall))
}
}
}
@@ -174,6 +185,8 @@ object InlineTestUtil {
private fun isClassOrPackagePartKind(klass: KotlinJvmBinaryClass): Boolean {
return klass.classHeader.kind == KotlinClassHeader.Kind.CLASS && !klass.classId.isLocal
|| klass.classHeader.kind == KotlinClassHeader.Kind.FILE_FACADE /*single file facade equals to package part*/
|| klass.classHeader.kind == KotlinClassHeader.Kind.MULTIFILE_CLASS_PART
}
private fun loadBinaryClass(file: OutputFile): KotlinJvmBinaryClass {
@@ -207,4 +220,24 @@ object InlineTestUtil {
super.visit(version, access, name, signature, superName, interfaces)
}
}
private abstract class MethodNodeWithAnonymousObjectCheck(val inlineInfo: InlineInfo, access: Int, name: String, desc: String, signature: String?, exceptions: Array<String>?) : MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) {
private fun isInlineParameterLikeOwner(owner: String) =
"$" in owner && !isTopLevelOrInnerOrPackageClass(owner, inlineInfo)
override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
if ("<init>" == name && isInlineParameterLikeOwner(owner)) {
onAnonymousConstructorCallOrSingletonAccess(owner)
}
}
abstract fun onAnonymousConstructorCallOrSingletonAccess(owner: String)
override fun visitFieldInsn(opcode: Int, owner: String, name: String, desc: String) {
if (opcode == Opcodes.GETSTATIC && isInlineParameterLikeOwner(owner)) {
onAnonymousConstructorCallOrSingletonAccess(owner)
}
super.visitFieldInsn(opcode, owner, name, desc)
}
}
}