From 215f7668ed71165375b2cf804ae0974647050a25 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Sat, 18 Aug 2018 20:07:22 +0200 Subject: Add filter for Kotlin when-expressions that list all cases of enum (#729) --- .../core/internal/analysis/filter/Filters.java | 2 +- .../internal/analysis/filter/KotlinWhenFilter.java | 113 +++++++++++++++++++++ .../analysis/filter/KotlinWhenSealedFilter.java | 62 ----------- 3 files changed, 114 insertions(+), 63 deletions(-) create mode 100644 org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenFilter.java delete mode 100644 org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenSealedFilter.java (limited to 'org.jacoco.core/src') diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java index a8446eac..03c85d7d 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java @@ -34,7 +34,7 @@ public final class Filters implements IFilter { new StringSwitchJavacFilter(), new StringSwitchEcjFilter(), new EnumEmptyConstructorFilter(), new AnnotationGeneratedFilter(), new KotlinGeneratedFilter(), new KotlinLateinitFilter(), - new KotlinWhenSealedFilter(), new KotlinWhenStringFilter()); + new KotlinWhenFilter(), new KotlinWhenStringFilter()); private final IFilter[] filters; diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenFilter.java new file mode 100644 index 00000000..24b90c0e --- /dev/null +++ b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenFilter.java @@ -0,0 +1,113 @@ +/******************************************************************************* + * 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.List; +import java.util.Set; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.JumpInsnNode; +import org.objectweb.asm.tree.LabelNode; +import org.objectweb.asm.tree.LookupSwitchInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.TableSwitchInsnNode; + +/** + * Filters bytecode that Kotlin compiler generates for when + * expressions which list all cases of enum or + * sealed class, i.e. which don't require explicit + * else. + */ +public final class KotlinWhenFilter implements IFilter { + + private static final String EXCEPTION = "kotlin/NoWhenBranchMatchedException"; + + private final Matcher matcher = new Matcher(); + + public void filter(final MethodNode methodNode, + final IFilterContext context, final IFilterOutput output) { + for (AbstractInsnNode i = methodNode.instructions + .getFirst(); i != null; i = i.getNext()) { + matcher.match(i, output); + } + } + + private static class Matcher extends AbstractMatcher { + void match(final AbstractInsnNode start, final IFilterOutput output) { + if (start.getType() != InsnNode.LABEL) { + return; + } + cursor = start; + + nextIsNew(EXCEPTION); + nextIs(Opcodes.DUP); + nextIsInvokeSuper(EXCEPTION, "()V"); + nextIs(Opcodes.ATHROW); + + for (AbstractInsnNode i = cursor; i != null; i = i.getPrevious()) { + if (i.getOpcode() == Opcodes.IFEQ + && ((JumpInsnNode) i).label == start) { + output.ignore(i, i); + output.ignore(start, cursor); + return; + + } else if (getDefaultLabel(i) == start) { + ignoreDefaultBranch(i, output); + output.ignore(start, cursor); + return; + + } + } + } + } + + private static LabelNode getDefaultLabel(final AbstractInsnNode i) { + switch (i.getOpcode()) { + case Opcodes.LOOKUPSWITCH: + return ((LookupSwitchInsnNode) i).dflt; + case Opcodes.TABLESWITCH: + return ((TableSwitchInsnNode) i).dflt; + default: + return null; + } + } + + private static void ignoreDefaultBranch(final AbstractInsnNode switchNode, + final IFilterOutput output) { + final List labels; + if (switchNode.getOpcode() == Opcodes.LOOKUPSWITCH) { + labels = ((LookupSwitchInsnNode) switchNode).labels; + } else { + labels = ((TableSwitchInsnNode) switchNode).labels; + } + final Set newTargets = new HashSet(); + for (LabelNode label : labels) { + newTargets.add(instructionAfterLabel(label)); + } + output.replaceBranches(switchNode, newTargets); + } + + private static AbstractInsnNode instructionAfterLabel( + final LabelNode label) { + AbstractInsnNode i = label.getNext(); + while (i.getType() == AbstractInsnNode.FRAME + || i.getType() == AbstractInsnNode.LABEL + || i.getType() == AbstractInsnNode.LINE) { + i = i.getNext(); + } + return i; + } + +} diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenSealedFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenSealedFilter.java deleted file mode 100644 index ae0d8455..00000000 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinWhenSealedFilter.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * 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.objectweb.asm.Opcodes; -import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.InsnNode; -import org.objectweb.asm.tree.JumpInsnNode; -import org.objectweb.asm.tree.MethodNode; - -/** - * Filters bytecode that Kotlin compiler generates for when - * expressions which list all cases of sealed class, i.e. which - * don't require explicit else. - */ -public final class KotlinWhenSealedFilter implements IFilter { - - private static final String EXCEPTION = "kotlin/NoWhenBranchMatchedException"; - - private final Matcher matcher = new Matcher(); - - public void filter(final MethodNode methodNode, - final IFilterContext context, final IFilterOutput output) { - for (AbstractInsnNode i = methodNode.instructions - .getFirst(); i != null; i = i.getNext()) { - matcher.match(i, output); - } - } - - private static class Matcher extends AbstractMatcher { - void match(final AbstractInsnNode start, final IFilterOutput output) { - if (start.getType() != InsnNode.LABEL) { - return; - } - cursor = start; - - nextIsNew(EXCEPTION); - nextIs(Opcodes.DUP); - nextIsInvokeSuper(EXCEPTION, "()V"); - nextIs(Opcodes.ATHROW); - - for (AbstractInsnNode i = cursor; i != null; i = i.getPrevious()) { - if (i.getOpcode() == Opcodes.IFEQ - && ((JumpInsnNode) i).label == start) { - output.ignore(i, i); - output.ignore(start, cursor); - return; - } - } - } - } - -} -- cgit v1.2.3