From 6c4d33db4f31738611c66f56f1658985fbc8ac07 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Fri, 21 Jun 2024 12:23:05 +0200 Subject: [PATCH] Fix line filtering: cannot remove line, if it was previously used --- .../compose/ComposeSkipGroupLineFilter.java | 3 +- .../ComposeTraceEventStartLineFilter.java | 3 +- .../lines/KotlinTryFinallyLineFilter.java | 6 ++- .../filters/lines/TryFinallyLineFilter.java | 3 +- .../TryWithResourcesJava8LineFilter.java | 3 +- .../lines/TryWithResourcesLineFilter.java | 3 +- .../basicJava/tryWithResources/java/Test.java | 4 +- .../src/testData/compose/inline/test.kt | 38 +++++++++++++++++++ .../rt/coverage/Jdk11CoverageRunTest.kt | 3 ++ 9 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 test-kotlin/test-sources11/src/testData/compose/inline/test.kt diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeSkipGroupLineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeSkipGroupLineFilter.java index 12dab892..7d967077 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeSkipGroupLineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeSkipGroupLineFilter.java @@ -72,7 +72,8 @@ private void tryRemoveLine() { @Override public void visitLineNumber(int line, Label start) { tryRemoveLine(); - myState = -1; + // do not remove lines that are previously used + myState = myContext.getLineData(line) == null ? -1 : 0; myCurrentLine = line; super.visitLineNumber(line, start); } diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeTraceEventStartLineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeTraceEventStartLineFilter.java index 42030d35..d5dd2e6c 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeTraceEventStartLineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/compose/ComposeTraceEventStartLineFilter.java @@ -61,7 +61,8 @@ private void tryRemoveLine() { @Override public void visitLineNumber(int line, Label start) { tryRemoveLine(); - myState = -1; + // do not remove lines that are previously used + myState = myContext.getLineData(line) == null ? -1 : 0; myCurrentLine = line; super.visitLineNumber(line, start); } diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/KotlinTryFinallyLineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/KotlinTryFinallyLineFilter.java index 017c1e3e..f3f92ca0 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/KotlinTryFinallyLineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/KotlinTryFinallyLineFilter.java @@ -84,9 +84,13 @@ public void visitInsn(int opcode) { @Override public void visitLineNumber(int line, Label start) { tryRemoveLine(); + boolean lineExists = myContext.getLineData(line) != null; super.visitLineNumber(line, start); myCurrentLine = line; - if (myState == State.USER_CODE) { + if (lineExists) { + // do not remove lines that are previously used + myState = State.USER_CODE; + } else if (myState == State.USER_CODE) { myState = State.INITIAL; } else if (myState == State.TRY_START_USER) { myState = State.TRY_START; diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryFinallyLineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryFinallyLineFilter.java index 3db34ec2..651a9ac6 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryFinallyLineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryFinallyLineFilter.java @@ -114,9 +114,10 @@ public void visitInsn(int opcode) { @Override public void visitLineNumber(int line, Label start) { tryRemoveLine(); + // do not remove lines that are previously used + myState = myContext.getLineData(line) == null ? State.INITIAL : State.USER_CODE; super.visitLineNumber(line, start); myCurrentLine = line; - myState = State.INITIAL; } @Override diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesJava8LineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesJava8LineFilter.java index fa6662ff..c3b732c7 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesJava8LineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesJava8LineFilter.java @@ -52,12 +52,13 @@ private void tryRemoveLine() { public void visitLineNumber(int line, Label start) { myCandidates.put(myCurrentLine, myHasInstructions ? -1 : myState); tryRemoveLine(); + // do not remove lines that are previously used + myHasInstructions = myContext.getLineData(line) != null; super.visitLineNumber(line, start); myCurrentLine = line; int previousState = myCandidates.get(myCurrentLine); myState = previousState == 0 ? 1 : previousState; - myHasInstructions = false; } @Override diff --git a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesLineFilter.java b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesLineFilter.java index 007b8834..9f39bf65 100644 --- a/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesLineFilter.java +++ b/instrumentation/src/com/intellij/rt/coverage/instrumentation/filters/lines/TryWithResourcesLineFilter.java @@ -87,10 +87,11 @@ private void tryRemoveLine() { @Override public void visitLineNumber(int line, Label start) { tryRemoveLine(); + // do not remove lines that are previously used + myHasInstructions = myContext.getLineData(line) != null; super.visitLineNumber(line, start); myCurrentLine = line; myState = State.INITIAL; - myHasInstructions = false; myJumpsToRemove = 0; } diff --git a/test-kotlin/test-sources11/src/testData/basicJava/tryWithResources/java/Test.java b/test-kotlin/test-sources11/src/testData/basicJava/tryWithResources/java/Test.java index 6a640ba4..542dfd13 100644 --- a/test-kotlin/test-sources11/src/testData/basicJava/tryWithResources/java/Test.java +++ b/test-kotlin/test-sources11/src/testData/basicJava/tryWithResources/java/Test.java @@ -32,8 +32,8 @@ public static void main(String[] args) throws IOException { private static void test1() { var y = new AutoCloseableImpl(); // coverage: FULL - try (y) { - System.out.println("action"); // coverage: FULL + try (y) { // coverage: FULL + System.out.println("action"); // coverage: FULL } System.out.println(); // coverage: FULL } diff --git a/test-kotlin/test-sources11/src/testData/compose/inline/test.kt b/test-kotlin/test-sources11/src/testData/compose/inline/test.kt new file mode 100644 index 00000000..7ae86611 --- /dev/null +++ b/test-kotlin/test-sources11/src/testData/compose/inline/test.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2024 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package testData.compose.inline + +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.runDesktopComposeUiTest + +@Composable +inline fun Greeting(name: String) { + Text( // coverage: FULL + text = "Hello $name!" // coverage: FULL + ) +} + +@OptIn(ExperimentalTestApi::class) +fun main() { + runDesktopComposeUiTest { // coverage: FULL + setContent { // coverage: FULL + Greeting("xx") // coverage: FULL + } + } +} \ No newline at end of file diff --git a/test-kotlin/test-sources11/test/com/intellij/rt/coverage/Jdk11CoverageRunTest.kt b/test-kotlin/test-sources11/test/com/intellij/rt/coverage/Jdk11CoverageRunTest.kt index b7a61241..c9be1f9f 100644 --- a/test-kotlin/test-sources11/test/com/intellij/rt/coverage/Jdk11CoverageRunTest.kt +++ b/test-kotlin/test-sources11/test/com/intellij/rt/coverage/Jdk11CoverageRunTest.kt @@ -44,6 +44,9 @@ internal class Jdk11CoverageRunTest(override val coverage: Coverage, override va @Test fun testComposeDefaultArgs() = test("compose.defaultArgs") + @Test + fun testComposeInline() = test("compose.inline") + @Test fun testComposeSynthetic() = test("compose.synthetic")