Commit e6e8f636 authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

fix: variable usage with enhanced for loop (#535) (PR #547)

parent 3970fce5
...@@ -125,6 +125,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor ...@@ -125,6 +125,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
// all checks passed // all checks passed
initInsn.add(AFlag.DONT_GENERATE); initInsn.add(AFlag.DONT_GENERATE);
incrInsn.add(AFlag.DONT_GENERATE); incrInsn.add(AFlag.DONT_GENERATE);
LoopType arrForEach = checkArrayForEach(mth, loopRegion, initInsn, incrInsn, condition); LoopType arrForEach = checkArrayForEach(mth, loopRegion, initInsn, incrInsn, condition);
if (arrForEach != null) { if (arrForEach != null) {
loopRegion.setType(arrForEach); loopRegion.setType(arrForEach);
...@@ -281,6 +282,8 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor ...@@ -281,6 +282,8 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
} }
assignInsn.add(AFlag.DONT_GENERATE); assignInsn.add(AFlag.DONT_GENERATE);
assignInsn.getResult().add(AFlag.DONT_GENERATE);
for (InsnNode insnNode : toSkip) { for (InsnNode insnNode : toSkip) {
insnNode.add(AFlag.DONT_GENERATE); insnNode.add(AFlag.DONT_GENERATE);
} }
......
...@@ -18,13 +18,8 @@ import jadx.core.dex.regions.loops.LoopType; ...@@ -18,13 +18,8 @@ import jadx.core.dex.regions.loops.LoopType;
import jadx.core.dex.visitors.regions.TracedRegionVisitor; import jadx.core.dex.visitors.regions.TracedRegionVisitor;
class CollectUsageRegionVisitor extends TracedRegionVisitor { class CollectUsageRegionVisitor extends TracedRegionVisitor {
private final List<RegisterArg> args; private final List<RegisterArg> args = new ArrayList<>();
private final Map<SSAVar, VarUsage> usageMap; private final Map<SSAVar, VarUsage> usageMap = new LinkedHashMap<>();
public CollectUsageRegionVisitor() {
this.usageMap = new LinkedHashMap<>();
this.args = new ArrayList<>();
}
public Map<SSAVar, VarUsage> getUsageMap() { public Map<SSAVar, VarUsage> getUsageMap() {
return usageMap; return usageMap;
...@@ -37,9 +32,6 @@ class CollectUsageRegionVisitor extends TracedRegionVisitor { ...@@ -37,9 +32,6 @@ class CollectUsageRegionVisitor extends TracedRegionVisitor {
int len = block.getInstructions().size(); int len = block.getInstructions().size();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
InsnNode insn = block.getInstructions().get(i); InsnNode insn = block.getInstructions().get(i);
if (insn.contains(AFlag.DONT_GENERATE)) {
continue;
}
processInsn(insn, usePlace); processInsn(insn, usePlace);
} }
} }
...@@ -73,11 +65,10 @@ class CollectUsageRegionVisitor extends TracedRegionVisitor { ...@@ -73,11 +65,10 @@ class CollectUsageRegionVisitor extends TracedRegionVisitor {
args.clear(); args.clear();
insn.getRegisterArgs(args); insn.getRegisterArgs(args);
for (RegisterArg arg : args) { for (RegisterArg arg : args) {
if (arg.contains(AFlag.DONT_GENERATE)) { if (!arg.contains(AFlag.DONT_GENERATE)) {
continue; VarUsage usage = getUsage(arg.getSVar());
usage.getUses().add(usePlace);
} }
VarUsage usage = getUsage(arg.getSVar());
usage.getUses().add(usePlace);
} }
} }
......
...@@ -8,13 +8,12 @@ import java.util.List; ...@@ -8,13 +8,12 @@ import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jadx.NotYetImplemented;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest; import jadx.tests.api.IntegrationTest;
public class TestVariables7 extends IntegrationTest { public class TestVariablesUsageWithLoops extends IntegrationTest {
public static class TestCls { public static class TestEnhancedFor {
public void test() { public void test() {
List list; List list;
...@@ -28,9 +27,29 @@ public class TestVariables7 extends IntegrationTest { ...@@ -28,9 +27,29 @@ public class TestVariables7 extends IntegrationTest {
} }
@Test @Test
@NotYetImplemented public void testEnhancedFor() {
public void test() { ClassNode cls = getClassNode(TestEnhancedFor.class);
ClassNode cls = getClassNode(TestCls.class); String code = cls.getCode().toString();
assertThat(code, containsString(" list = new ArrayList"));
}
public static class TestForLoop {
public void test() {
List list;
synchronized (this) {
list = new ArrayList();
}
for (int i = 0; i < list.size(); i++) {
System.out.println(i);
}
}
}
@Test
public void testForLoop() {
ClassNode cls = getClassNode(TestForLoop.class);
String code = cls.getCode().toString(); String code = cls.getCode().toString();
assertThat(code, containsString(" list = new ArrayList")); assertThat(code, containsString(" list = new ArrayList"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment