fix: improve switch out search in loop (#2246)

This commit is contained in:
Skylot
2024-08-12 22:04:53 +01:00
parent 1051dacb1e
commit c94201be4a
2 changed files with 48 additions and 0 deletions
@@ -864,6 +864,10 @@ public class RegionMaker {
// works if no fallthrough cases and no returns inside switch
BitSet outs = BlockUtils.newBlocksBitSet(mth);
for (BlockNode s : block.getCleanSuccessors()) {
if (s.contains(AFlag.LOOP_END)) {
// loop end dom frontier is loop start, ignore it
continue;
}
outs.or(s.getDomFrontier());
}
outs.clear(block.getId());
@@ -0,0 +1,44 @@
package jadx.tests.integration.switches;
import org.junit.jupiter.api.Test;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestSwitchInLoop3 extends IntegrationTest {
@SuppressWarnings("SwitchStatementWithTooFewBranches")
public static class TestCls {
public int test(int k) {
int a = 0;
while (true) {
int x = 0; // keep this: force to generate the necessary CFG
switch (k) {
case 0:
return a;
default:
a++;
k >>= 1;
}
}
}
public void check() {
assertThat(test(1)).isEqualTo(1);
}
}
@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsLines(3,
"switch (k) {",
indent() + "case 0:",
indent(2) + "return a;",
indent() + "default:",
indent(2) + "a++;",
indent(2) + "k >>= 1;");
}
}