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. 39
      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,19 +317,37 @@ public class MergeHelper {
}
}
if (hasinit) {
Set<Statement> set = stat.getNeighboursSet(StatEdge.TYPE_CONTINUE, Statement.DIRECTION_BACKWARD);
set.remove(lastData);
if (!hasinit) {
return;
}
if (!set.isEmpty()) {
return;
}
Exprent initVar = ((AssignmentExprent) initDoExprent).getLeft();
Exprent incVar;
if (lastDoExprent.type == Exprent.EXPRENT_ASSIGNMENT) {
incVar = ((AssignmentExprent) lastDoExprent).getLeft();
} else {
incVar = lastDoExprent.getAllExprents().get(0);
}
stat.setLooptype(DoStatement.LOOP_FOR);
stat.setInitExprent(preData.getExprents().remove(preData.getExprents().size() - 1));
stat.setIncExprent(lastData.getExprents().remove(lastData.getExprents().size() - 1));
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);
if (!set.isEmpty()) {
return;
}
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();
if (!lst.isEmpty()) {

Loading…
Cancel
Save