Improve heuristic for converting while loops to for loops

We now only convert while loops to for loops if the same variable is
used in the initialisation statement, condition and increment statement.
master
Graham 5 years ago
parent 49c640733c
commit 34110df5f4
  1. 25
      src/org/jetbrains/java/decompiler/modules/decompiler/MergeHelper.java

@ -4,6 +4,7 @@ package org.jetbrains.java.decompiler.modules.decompiler;
import org.jetbrains.java.decompiler.code.cfg.BasicBlock;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AssignmentExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.IfExprent;
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
@ -268,7 +269,7 @@ public class MergeHelper {
}
private static void matchFor(DoStatement stat) {
Exprent lastDoExprent, initDoExprent;
Exprent lastDoExprent, initDoExprent = null;
Statement lastData, preData = null;
// get last exprent
@ -316,7 +317,26 @@ public class MergeHelper {
}
}
if (hasinit) {
if (!hasinit) {
return;
}
Exprent initVar = ((AssignmentExprent) initDoExprent).getLeft();
Exprent incVar;
if (lastDoExprent.type == Exprent.EXPRENT_ASSIGNMENT) {
incVar = ((AssignmentExprent) lastDoExprent).getLeft();
} else {
incVar = lastDoExprent.getAllExprents().get(0);
}
if (!initVar.equals(incVar)) {
return;
}
if (!stat.getConditionExprent().containsExprent(initVar)) {
return;
}
Set<Statement> set = stat.getNeighboursSet(StatEdge.TYPE_CONTINUE, Statement.DIRECTION_BACKWARD);
set.remove(lastData);
@ -327,7 +347,6 @@ public class MergeHelper {
stat.setLooptype(DoStatement.LOOP_FOR);
stat.setInitExprent(preData.getExprents().remove(preData.getExprents().size() - 1));
stat.setIncExprent(lastData.getExprents().remove(lastData.getExprents().size() - 1));
}
if (lastData.getExprents().isEmpty()) {
List<StatEdge> lst = lastData.getAllSuccessorEdges();

Loading…
Cancel
Save