This repository was archived by the owner on Jan 13, 2025. It is now read-only.
forked from jacoco/jacoco
-
Notifications
You must be signed in to change notification settings - Fork 0
For loop filter #3
Open
ChristopherKoellner
wants to merge
8
commits into
master
Choose a base branch
from
feature/forloopfilter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f24afa0
Added filter to exclude branch that is most of the time not coverable
ChristopherKoellner 71479e5
Added test cases for KotlinForLoopFilter.java
ChristopherKoellner 31d4861
Updated KotlinForLoopFilter.java and KotlinForLoopFilterTest
ChristopherKoellner a80722f
more precise variable naming
poseidon-mysugr b4db76c
Merge branch 'master' into feature/forloopfilter
poseidon-mysugr 10b0723
Merge tag 'v0.8.8' into feature/forloopfilter
poseidon-mysugr 066931b
update copyright
poseidon-mysugr b8ddf5d
Merge tag 'v0.8.11' into feature/forloopfilter
poseidon-mysugr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
157 changes: 157 additions & 0 deletions
157
...acoco.core.test/src/org/jacoco/core/internal/analysis/filter/KotlinForLoopFilterTest.java
This file contains hidden or 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,157 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors | ||
* This program and the accompanying materials are made available under | ||
* the terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Fabian Mastenbroek - 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.AbstractInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
/** | ||
* Unit tests for {@link KotlinForLoopFilter}. | ||
*/ | ||
public class KotlinForLoopFilterTest extends FilterTestBase { | ||
|
||
private final KotlinForLoopFilter filter = new KotlinForLoopFilter(); | ||
|
||
/** | ||
* <pre> | ||
* class Example { | ||
* fun example() { | ||
* for (j in 0 until i1()) {} | ||
* } | ||
* private fun i1() = 1 | ||
* } | ||
* </pre> | ||
*/ | ||
@Test | ||
public void should_filter_Kotlin_1_5_until() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"example", "()V", null, null); | ||
final Label label1 = new Label(); | ||
final Label label2 = new Label(); | ||
|
||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitVarInsn(Opcodes.ISTORE, 0); | ||
m.visitMethodInsn(Opcodes.INVOKESTATIC, "ExampleKt", "i1", "()I", | ||
false); | ||
m.visitVarInsn(Opcodes.ISTORE, 1); | ||
m.visitVarInsn(Opcodes.ILOAD, 0); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IF_ICMPGE, label1); | ||
final AbstractInsnNode ignored1 = m.instructions.getLast(); | ||
m.visitLabel(label2); | ||
m.visitVarInsn(Opcodes.ILOAD, 0); | ||
m.visitVarInsn(Opcodes.ISTORE, 2); | ||
m.visitInsn(Opcodes.IINC); | ||
m.visitVarInsn(Opcodes.ILOAD, 0); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IF_ICMPLT, label2); | ||
final AbstractInsnNode ignored2 = m.instructions.getLast(); | ||
m.visitLabel(label1); | ||
m.visitInsn(Opcodes.RETURN); | ||
|
||
filter.filter(m, context, output); | ||
assertIgnored(new Range(ignored1, ignored1), | ||
new Range(ignored2, ignored2)); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* class Example { | ||
* fun example() { | ||
* for (i in i1() downTo 0) {} | ||
* } | ||
* private fun i1() = 1 | ||
* } | ||
* </pre> | ||
*/ | ||
@Test | ||
public void should_filter_Kotlin_1_5_downTo() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"example", "()V", null, null); | ||
final Label label1 = new Label(); | ||
final Label label2 = new Label(); | ||
|
||
m.visitMethodInsn(Opcodes.INVOKESTATIC, "ExampleKt", "i1", "()I", | ||
false); | ||
m.visitVarInsn(Opcodes.ISTORE, 0); | ||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitVarInsn(Opcodes.ISTORE, 1); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IF_ICMPGT, label1); | ||
final AbstractInsnNode ignored1 = m.instructions.getLast(); | ||
m.visitLabel(label2); | ||
m.visitVarInsn(Opcodes.ILOAD, 0); | ||
m.visitVarInsn(Opcodes.ISTORE, 1); | ||
m.visitInsn(Opcodes.IINC); | ||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IF_ICMPLE, label2); | ||
final AbstractInsnNode ignored2 = m.instructions.getLast(); | ||
m.visitLabel(label1); | ||
m.visitInsn(Opcodes.RETURN); | ||
|
||
filter.filter(m, context, output); | ||
assertIgnored(new Range(ignored1, ignored1), | ||
new Range(ignored2, ignored2)); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* class Example { | ||
* fun example() { | ||
* val limit = 10 | ||
* for (j in limit downTo i1()) {} | ||
* } | ||
* private fun i1() = 1 | ||
* } | ||
* </pre> | ||
*/ | ||
@Test | ||
public void should_filter_Kotlin_1_5_downTo_val() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"example", "()V", null, null); | ||
final Label label1 = new Label(); | ||
final Label label2 = new Label(); | ||
|
||
m.visitVarInsn(Opcodes.BIPUSH, 10); | ||
m.visitVarInsn(Opcodes.ISTORE, 0); | ||
m.visitVarInsn(Opcodes.ILOAD, 0); | ||
m.visitVarInsn(Opcodes.ISTORE, 1); | ||
m.visitMethodInsn(Opcodes.INVOKESTATIC, "ExampleKt", "i1", "()I", | ||
false); | ||
m.visitVarInsn(Opcodes.ISTORE, 2); | ||
m.visitVarInsn(Opcodes.ILOAD, 2); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IF_ICMPGT, label1); | ||
final AbstractInsnNode ignored1 = m.instructions.getLast(); | ||
m.visitLabel(label2); | ||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitVarInsn(Opcodes.ISTORE, 3); | ||
m.visitInsn(Opcodes.IINC); | ||
m.visitVarInsn(Opcodes.ILOAD, 3); | ||
m.visitVarInsn(Opcodes.ILOAD, 2); | ||
m.visitJumpInsn(Opcodes.IF_ICMPNE, label2); | ||
final AbstractInsnNode ignored2 = m.instructions.getLast(); | ||
m.visitLabel(label1); | ||
m.visitInsn(Opcodes.RETURN); | ||
|
||
filter.filter(m, context, output); | ||
assertIgnored(new Range(ignored1, ignored1), | ||
new Range(ignored2, ignored2)); | ||
} | ||
|
||
} |
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinForLoopFilter.java
This file contains hidden or 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,73 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors | ||
* This program and the accompanying materials are made available under | ||
* the terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Fabian Mastenbroek - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.JumpInsnNode; | ||
import org.objectweb.asm.tree.LabelNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
/** | ||
* Filters branches in bytecode that the Kotlin compiler generates for | ||
* <code>for</code> loops as they are not coverable most of the time. | ||
*/ | ||
public class KotlinForLoopFilter implements IFilter { | ||
|
||
public void filter(final MethodNode methodNode, | ||
final IFilterContext context, final IFilterOutput output) { | ||
final Matcher matcher = new Matcher(); | ||
for (final AbstractInsnNode node : methodNode.instructions) { | ||
matcher.match(node, output); | ||
} | ||
} | ||
|
||
private static class Matcher extends AbstractMatcher { | ||
|
||
public void match(final AbstractInsnNode start, IFilterOutput output) { | ||
if (start.getOpcode() != Opcodes.IF_ICMPGE | ||
&& start.getOpcode() != Opcodes.IF_ICMPGT) { | ||
return; | ||
} | ||
cursor = start; | ||
AbstractInsnNode loopLabelNode = start.getNext(); | ||
if (loopLabelNode instanceof LabelNode) { | ||
Label loopLabel = ((LabelNode) loopLabelNode).getLabel(); | ||
LabelNode jumpTarget = ((JumpInsnNode) cursor).label; | ||
if (isLoop(jumpTarget, loopLabel)) { | ||
output.ignore(start, start); | ||
output.ignore(jumpTarget.getPrevious(), | ||
jumpTarget.getPrevious()); | ||
} | ||
} | ||
} | ||
|
||
private boolean isLoop(LabelNode jumpTarget, Label loopLabel) { | ||
nextIs(Opcodes.ILOAD); | ||
nextIs(Opcodes.ISTORE); | ||
nextIs(Opcodes.IINC); | ||
// follow the jump node | ||
for (AbstractInsnNode j = cursor; j != null; j = j.getNext()) { | ||
if (j == jumpTarget) { | ||
// if the label prior to the jump target matches | ||
// we can be sure that this is the loop we are looking for | ||
AbstractInsnNode previousOpcode = j.getPrevious(); | ||
return ((JumpInsnNode) previousOpcode).label.getLabel() | ||
.equals(loopLabel); | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.