forked from bazelbuild/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report code coverage correctly for Kotlin methods with default argume…
…nts (bazelbuild#774)
- Loading branch information
Showing
10 changed files
with
316 additions
and
7 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...idation.kotlin/src/org/jacoco/core/test/validation/kotlin/KotlinDefaultArgumentsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.validation.kotlin; | ||
|
||
import org.jacoco.core.test.validation.ValidationTestBase; | ||
import org.jacoco.core.test.validation.kotlin.targets.KotlinDefaultArgumentsTarget; | ||
|
||
/** | ||
* Test of functions with default arguments. | ||
*/ | ||
public class KotlinDefaultArgumentsTest extends ValidationTestBase { | ||
|
||
public KotlinDefaultArgumentsTest() { | ||
super(KotlinDefaultArgumentsTarget.class); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...kotlin/src/org/jacoco/core/test/validation/kotlin/targets/KotlinDefaultArgumentsTarget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.validation.kotlin.targets | ||
|
||
/** | ||
* Test target for functions with default arguments. | ||
*/ | ||
object KotlinDefaultArgumentsTarget { | ||
|
||
private fun f(a: String = "a", b: String = "b") { // assertFullyCovered(0, 0) | ||
} | ||
|
||
private fun branch(a: Boolean, b: String = if (a) "a" else "b") { // assertFullyCovered(0, 2) | ||
} | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
f(a = "a") | ||
f(b = "b") | ||
/* next invocation doesn't use synthetic method: */ | ||
f("a", "b") | ||
|
||
branch(false) | ||
branch(true) | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...e.test/src/org/jacoco/core/internal/analysis/filter/KotlinDefaultArgumentsFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import org.jacoco.core.internal.instr.InstrSupport; | ||
import org.junit.Test; | ||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
/** | ||
* Unit test for {@link KotlinDefaultArgumentsFilter}. | ||
*/ | ||
public class KotlinDefaultArgumentsFilterTest extends FilterTestBase { | ||
|
||
private final IFilter filter = new KotlinDefaultArgumentsFilter(); | ||
|
||
private static MethodNode createMethod(final int access, final String name, | ||
final String descriptor) { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, | ||
access, name, descriptor, null, null); | ||
|
||
m.visitVarInsn(Opcodes.ILOAD, 2); | ||
m.visitInsn(Opcodes.ICONST_1); | ||
m.visitInsn(Opcodes.IAND); | ||
final Label label = new Label(); | ||
m.visitJumpInsn(Opcodes.IFEQ, label); | ||
// default argument | ||
m.visitLdcInsn(Integer.valueOf(42)); | ||
m.visitVarInsn(Opcodes.ISTORE, 1); | ||
m.visitLabel(label); | ||
|
||
m.visitVarInsn(Opcodes.ALOAD, 0); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "Target", "origin", "(I)V", | ||
false); | ||
m.visitInsn(Opcodes.RETURN); | ||
|
||
return m; | ||
} | ||
|
||
@Test | ||
public void should_filter() { | ||
final MethodNode m = createMethod(Opcodes.ACC_SYNTHETIC, | ||
"origin$default", "(LTarget;IILjava/lang/Object;)V"); | ||
context.classAnnotations | ||
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(new Range(m.instructions.get(3), m.instructions.get(3))); | ||
} | ||
|
||
@Test | ||
public void should_not_filter_when_not_kotlin() { | ||
final MethodNode m = createMethod(Opcodes.ACC_SYNTHETIC, | ||
"not_kotlin_synthetic$default", | ||
"(LTarget;IILjava/lang/Object;)V"); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(); | ||
} | ||
|
||
@Test | ||
public void should_not_filter_when_suffix_absent() { | ||
final MethodNode m = createMethod(Opcodes.ACC_SYNTHETIC, | ||
"synthetic_without_suffix", "(LTarget;IILjava/lang/Object;)V"); | ||
context.classAnnotations | ||
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(); | ||
} | ||
|
||
@Test | ||
public void should_not_filter_when_not_synthetic() { | ||
final MethodNode m = createMethod(0, "not_synthetic$default", | ||
"(LTarget;IILjava/lang/Object;)V"); | ||
context.classAnnotations | ||
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); | ||
|
||
filter.filter(m, context, output); | ||
|
||
assertIgnored(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...acoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinDefaultArgumentsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.JumpInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import org.objectweb.asm.tree.VarInsnNode; | ||
|
||
/** | ||
* Filters branches that Kotlin compiler generates for default arguments. | ||
* | ||
* For each default argument Kotlin compiler generates following bytecode to | ||
* determine if it should be used or not: | ||
* | ||
* <pre> | ||
* ILOAD maskVar | ||
* ICONST_x, BIPUSH, SIPUSH, LDC or LDC_W | ||
* IAND | ||
* IFEQ label | ||
* default argument | ||
* label: | ||
* </pre> | ||
* | ||
* Where <code>maskVar</code> is penultimate argument of synthetic method with | ||
* suffix "$default". And its value can't be zero - invocation with all | ||
* arguments uses original non synthetic method, thus <code>IFEQ</code> | ||
* instructions should be ignored. | ||
*/ | ||
public final class KotlinDefaultArgumentsFilter implements IFilter { | ||
|
||
static boolean isDefaultArgumentsMethodName(final String methodName) { | ||
return methodName.endsWith("$default"); | ||
} | ||
|
||
public void filter(final MethodNode methodNode, | ||
final IFilterContext context, final IFilterOutput output) { | ||
if ((methodNode.access & Opcodes.ACC_SYNTHETIC) == 0) { | ||
return; | ||
} | ||
if (!isDefaultArgumentsMethodName(methodNode.name)) { | ||
return; | ||
} | ||
if (!KotlinGeneratedFilter.isKotlinClass(context)) { | ||
return; | ||
} | ||
|
||
new Matcher().match(methodNode, output); | ||
} | ||
|
||
private static class Matcher extends AbstractMatcher { | ||
public void match(final MethodNode methodNode, | ||
final IFilterOutput output) { | ||
cursor = methodNode.instructions.getFirst(); | ||
|
||
final Set<AbstractInsnNode> ignore = new HashSet<AbstractInsnNode>(); | ||
final int maskVar = Type.getMethodType(methodNode.desc) | ||
.getArgumentTypes().length - 2; | ||
while (true) { | ||
if (cursor.getOpcode() != Opcodes.ILOAD) { | ||
break; | ||
} | ||
if (((VarInsnNode) cursor).var != maskVar) { | ||
break; | ||
} | ||
next(); | ||
nextIs(Opcodes.IAND); | ||
nextIs(Opcodes.IFEQ); | ||
if (cursor == null) { | ||
return; | ||
} | ||
ignore.add(cursor); | ||
cursor = ((JumpInsnNode) cursor).label; | ||
skipNonOpcodes(); | ||
} | ||
|
||
for (AbstractInsnNode i : ignore) { | ||
output.ignore(i, i); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters