Skip to content

Commit 5ef24cb

Browse files
committed
Fix incorrect SSAU in empty while loops, fix edges being removed from extracted loops
1 parent 5de1cc3 commit 5ef24cb

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

src/org/jetbrains/java/decompiler/modules/decompiler/LoopExtractHelper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.jetbrains.java.decompiler.modules.decompiler.exps.IfExprent;
77
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
88
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement.EdgeDirection;
9+
import org.jetbrains.java.decompiler.util.DotExporter;
910

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

5455
if (stat instanceof DoStatement) {
5556
if (extractLoop((DoStatement)stat)) {
57+
ValidationHelper.validateStatement(stat.getTopParent());
5658
return 2;
5759
}
5860
}
@@ -195,7 +197,8 @@ private static boolean extractFirstIf(DoStatement stat, List<Statement> stats) {
195197
extractIfBlockIntoLoop(stat, firstif, check.getSuccessorEdges(StatEdge.TYPE_REGULAR).get(0).getDestination());
196198

197199
if (newDest) {
198-
removeFrom.removeSuccessor(continues.get(0));
200+
// TODO: is this correct?
201+
// removeFrom.removeSuccessor(continues.get(0));
199202
}
200203

201204
return true;
@@ -299,9 +302,9 @@ private static void extractIfBlockIntoLoop(DoStatement loop, IfStatement ifStat,
299302
loop.addLabeledEdge(ifedge);
300303
ifStat.setIfEdge(ifedge);
301304

302-
SequenceStatement block = new SequenceStatement(Arrays.asList(ifStat, target));
303-
ifStat.replaceWith( block);
304-
block.setAllParent();
305+
SequenceStatement seq = new SequenceStatement(Arrays.asList(ifStat, target));
306+
ifStat.replaceWith(seq);
307+
seq.setAllParent();
305308

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

src/org/jetbrains/java/decompiler/modules/decompiler/MergeHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ protected static boolean matchWhile(DoStatement stat) {
210210
if (firstif == stat.getFirst()) {
211211
BasicBlockStatement bstat = BasicBlockStatement.create();
212212
stat.replaceStatement(firstif, bstat);
213+
// We destroyed the loop body, but now we need to add a continue edge to make sure the empty body stays correct
214+
bstat.addSuccessor(new StatEdge(StatEdge.TYPE_CONTINUE, bstat, stat, stat));
213215
}
214216
else {
215217
// precondition: sequence must contain more than one statement!

test/org/jetbrains/java/decompiler/SingleClassesTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ private void registerDefault() {
672672
// TODO: Param type information is lost for lambdas where a more specific type is not required by the context
673673
register(JAVA_8, "TestLambdaParamTypes");
674674
register(JAVA_8, "TestGenericComparison");
675-
// TODO: Can't inline field initializer for a final field that depends on initialization in a static call
676675
register(JAVA_8, "TestStaticBlockFinalField");
677676
register(JAVA_8, "TestWhileLambda");
678677
register(JAVA_8, "TestTrySplit");

testData/results/pkg/TestAssignmentInDoWhile.dec

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class TestAssignmentInDoWhile {
2727
int[] array = new int[100];// 29
2828
int x = 3;// 31
2929

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

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

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

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

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

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

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

6363
System.out.println("Hi");// 81
@@ -134,11 +134,9 @@ class 'pkg/TestAssignmentInDoWhile' {
134134
a 29
135135
b 29
136136
c 29
137-
d 29
138137
e 29
139138
f 29
140139
10 29
141-
11 29
142140
13 29
143141
14 29
144142
15 29
@@ -168,14 +166,12 @@ class 'pkg/TestAssignmentInDoWhile' {
168166
a 39
169167
b 39
170168
c 39
171-
d 39
172169
e 39
173170
f 39
174171
10 39
175172
11 39
176173
12 39
177174
13 39
178-
14 39
179175
16 39
180176
17 39
181177
18 39
@@ -205,11 +201,9 @@ class 'pkg/TestAssignmentInDoWhile' {
205201
a 49
206202
b 49
207203
c 49
208-
d 49
209204
e 49
210205
f 49
211206
10 49
212-
11 49
213207
13 49
214208
14 49
215209
15 49
@@ -245,11 +239,9 @@ class 'pkg/TestAssignmentInDoWhile' {
245239
a 59
246240
b 59
247241
c 59
248-
d 59
249242
e 59
250243
f 59
251244
10 59
252-
11 59
253245
13 59
254246
14 59
255247
15 59
@@ -308,4 +300,4 @@ Lines mapping:
308300
75 <-> 60
309301
80 <-> 60
310302
81 <-> 63
311-
82 <-> 64
303+
82 <-> 64

0 commit comments

Comments
 (0)