Skip to content

Commit

Permalink
Fix incorrect SSAU in empty while loops, fix edges being removed from…
Browse files Browse the repository at this point in the history
… extracted loops
  • Loading branch information
jaskarth committed Jan 10, 2025
1 parent 5de1cc3 commit 5ef24cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.java.decompiler.modules.decompiler.exps.IfExprent;
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement.EdgeDirection;
import org.jetbrains.java.decompiler.util.DotExporter;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -53,6 +54,7 @@ private static int extractLoopsRec(Statement stat) {

if (stat instanceof DoStatement) {
if (extractLoop((DoStatement)stat)) {
ValidationHelper.validateStatement(stat.getTopParent());
return 2;
}
}
Expand Down Expand Up @@ -195,7 +197,8 @@ private static boolean extractFirstIf(DoStatement stat, List<Statement> stats) {
extractIfBlockIntoLoop(stat, firstif, check.getSuccessorEdges(StatEdge.TYPE_REGULAR).get(0).getDestination());

if (newDest) {
removeFrom.removeSuccessor(continues.get(0));
// TODO: is this correct?
// removeFrom.removeSuccessor(continues.get(0));
}

return true;
Expand Down Expand Up @@ -299,9 +302,9 @@ private static void extractIfBlockIntoLoop(DoStatement loop, IfStatement ifStat,
loop.addLabeledEdge(ifedge);
ifStat.setIfEdge(ifedge);

SequenceStatement block = new SequenceStatement(Arrays.asList(ifStat, target));
ifStat.replaceWith( block);
block.setAllParent();
SequenceStatement seq = new SequenceStatement(Arrays.asList(ifStat, target));
ifStat.replaceWith(seq);
seq.setAllParent();

ifStat.addSuccessor(new StatEdge(StatEdge.TYPE_REGULAR, ifStat, target));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ protected static boolean matchWhile(DoStatement stat) {
if (firstif == stat.getFirst()) {
BasicBlockStatement bstat = BasicBlockStatement.create();
stat.replaceStatement(firstif, bstat);
// We destroyed the loop body, but now we need to add a continue edge to make sure the empty body stays correct
bstat.addSuccessor(new StatEdge(StatEdge.TYPE_CONTINUE, bstat, stat, stat));
}
else {
// precondition: sequence must contain more than one statement!
Expand Down
1 change: 0 additions & 1 deletion test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ private void registerDefault() {
// TODO: Param type information is lost for lambdas where a more specific type is not required by the context
register(JAVA_8, "TestLambdaParamTypes");
register(JAVA_8, "TestGenericComparison");
// TODO: Can't inline field initializer for a final field that depends on initialization in a static call
register(JAVA_8, "TestStaticBlockFinalField");
register(JAVA_8, "TestWhileLambda");
register(JAVA_8, "TestTrySplit");
Expand Down
18 changes: 5 additions & 13 deletions testData/results/pkg/TestAssignmentInDoWhile.dec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestAssignmentInDoWhile {
int[] array = new int[100];// 29
int x = 3;// 31

while (x < 50 && (x = x + array[x]) < 100) {// 33 38
while (x < 50 && (x += array[x]) < 100) {// 33 38
}

System.out.println("Hi");// 39
Expand All @@ -37,7 +37,7 @@ public class TestAssignmentInDoWhile {
int[] array = new int[100];// 43
int x = 3;// 45

while (x < 50 && (x = x + (array[x] & 7)) < 100) {// 47 52
while (x < 50 && (x += array[x] & 7) < 100) {// 47 52
}

System.out.println("Hi");// 53
Expand All @@ -47,7 +47,7 @@ public class TestAssignmentInDoWhile {
int[] array = new int[100];// 57
int x = 3;// 59

while (x < 50 && (x = x + array[x]) < 100 && x > 10) {// 61 66
while (x < 50 && (x += array[x]) < 100 && x > 10) {// 61 66
}

System.out.println("Hi");// 67
Expand All @@ -57,7 +57,7 @@ public class TestAssignmentInDoWhile {
int[] array = new int[100];// 71
int x = 3;// 73

while (x < 50 && ((x = x + array[x]) < 100 || x > 10)) {// 75 80
while (x < 50 && ((x += array[x]) < 100 || x > 10)) {// 75 80
}

System.out.println("Hi");// 81
Expand Down Expand Up @@ -134,11 +134,9 @@ class 'pkg/TestAssignmentInDoWhile' {
a 29
b 29
c 29
d 29
e 29
f 29
10 29
11 29
13 29
14 29
15 29
Expand Down Expand Up @@ -168,14 +166,12 @@ class 'pkg/TestAssignmentInDoWhile' {
a 39
b 39
c 39
d 39
e 39
f 39
10 39
11 39
12 39
13 39
14 39
16 39
17 39
18 39
Expand Down Expand Up @@ -205,11 +201,9 @@ class 'pkg/TestAssignmentInDoWhile' {
a 49
b 49
c 49
d 49
e 49
f 49
10 49
11 49
13 49
14 49
15 49
Expand Down Expand Up @@ -245,11 +239,9 @@ class 'pkg/TestAssignmentInDoWhile' {
a 59
b 59
c 59
d 59
e 59
f 59
10 59
11 59
13 59
14 59
15 59
Expand Down Expand Up @@ -308,4 +300,4 @@ Lines mapping:
75 <-> 60
80 <-> 60
81 <-> 63
82 <-> 64
82 <-> 64

0 comments on commit 5ef24cb

Please sign in to comment.