From e09e303e718d9b4369db9921dd7cbe34a52f91ab Mon Sep 17 00:00:00 2001 From: "Egor.Ushakov" Date: Mon, 3 Apr 2017 17:15:31 +0300 Subject: [PATCH] IDEA-169534 Decompiler overuses ternary operation --- .../decompiler/SimplifyExprentsHelper.java | 11 +- .../java/decompiler/SingleClassesTest.java | 1 + .../classes/pkg/TestIffSimplification.class | Bin 0 -> 1149 bytes testData/results/TestIffSimplification.dec | 107 ++++++++++++++++++ testData/src/pkg/TestIffSimplification.java | 48 ++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 testData/classes/pkg/TestIffSimplification.class create mode 100644 testData/results/TestIffSimplification.dec create mode 100644 testData/src/pkg/TestIffSimplification.java diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java b/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java index 28b76e9..0be7d59 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -854,6 +854,11 @@ public class SimplifyExprentsHelper { return false; } + // avoid flattening to 'iff' if any of the branches is an 'iff' already + if (isIff(ifex.getValue()) || isIff(elseex.getValue())) { + return false; + } + List data = new ArrayList<>(); data.addAll(stif.getFirst().getExprents()); @@ -880,6 +885,10 @@ public class SimplifyExprentsHelper { return false; } + private static boolean isIff(Exprent exp) { + return exp.type == Exprent.EXPRENT_FUNCTION && ((FunctionExprent) exp).getFuncType() == FunctionExprent.FUNCTION_IIF; + } + static { class14Builder.parse( "statement type:if iftype:if exprsize:-1\n" + diff --git a/test/org/jetbrains/java/decompiler/SingleClassesTest.java b/test/org/jetbrains/java/decompiler/SingleClassesTest.java index 2388d8d..1c2779c 100644 --- a/test/org/jetbrains/java/decompiler/SingleClassesTest.java +++ b/test/org/jetbrains/java/decompiler/SingleClassesTest.java @@ -93,6 +93,7 @@ public class SingleClassesTest { @Test public void testExtendingSubclass() { doTest("pkg/TestExtendingSubclass"); } @Test public void testSyntheticAccess() { doTest("pkg/TestSyntheticAccess"); } @Test public void testIllegalVarName() { doTest("pkg/TestIllegalVarName"); } + @Test public void testIffSimplification() { doTest("pkg/TestIffSimplification"); } @Test public void testKotlinConstructor() { doTest("pkg/TestKotlinConstructorKt"); } @Test public void testAsserts() { doTest("pkg/TestAsserts"); } @Test public void testLocalsNames() { doTest("pkg/TestLocalsNames"); } diff --git a/testData/classes/pkg/TestIffSimplification.class b/testData/classes/pkg/TestIffSimplification.class new file mode 100644 index 0000000000000000000000000000000000000000..6dcb9b188a2529473af8cf9d8db3321ca9297fe6 GIT binary patch literal 1149 zcmZ`(O>YuW6g_WdVEC9qTcNgvwrbS^Entm1O=BuCA(`rerY5bMVVKfsfgv#TU%1!A zq{dA*EOcQsE?m0szqmB+jOPs$Yoa9c-o5wTd(J)gz3}bV=Pv;6;Z6);c?IdU#lK+ufFX=-QU=dY$_M(Y}m0JR)0e|Dc+$(&+n^KVYrc zr*mqx1`ZwNfJaj`-?C43tnQd3a#1f3>5#)|REebL_Qz0B+v_x3Nk%T)Yj>@lv+D`O zbJgnhcBN7c2-UW|NjBosx=W#UzbOOTBa@ME-}ZWrj8!finb+`Sq1@=jYTlq{JC9vC zlEr^2E*)E^mH{0R9Yq5rlm!<4J0F2*nO|;Moo4xI{n)X6fyAHCj^!V*WvkBVXt_$h zg6jzJHsvn_jsX#_QF>+WLPiT8A>Pwa5aSvi1reuhjBNu`^vHuGS6Tdw*(&!$<`tqF zYUv!|4Xrd}M7@Nf2PoG@nM9K)tid#90^|f{afL`JMnh0yoUK55LUJ%BF-W45WcU|F zB7luUBXEQ@vf&YxF`+UhRK_JL6+#YS4p+HKHK!!fxTi>z%#y4aDh-PyEMC?$#~zu( z%rpt-NU}iqrGU6Fs-F<2Cd4VsQ>_wIO0{aBR)b=&4I(U#d-y>FDZ$KS&p6iXTj<(3 zG-bGy*3OXRI*H7O=Jn<$C@H8fUt>BQO~-hqFk2hSGX}@C#2ekmGA|yuK?SSq>^8e! zqgTKt*0IGmCz+oIvoI$bOk|K{ckGEOvw;Q^%UB5pZ*tfxfohXaOd`v?Ra%nC#7$Pn d*_yaTD;B7pK|Jx0e-``}_{I4NHY1 5 +11 <-> 9 +12 <-> 9 +15 <-> 9 +20 <-> 13 +21 <-> 14 +22 <-> 14 +25 <-> 14 +29 <-> 16 +34 <-> 21 +35 <-> 22 +38 <-> 23 +39 <-> 24 +42 <-> 25 +43 <-> 26 +46 <-> 28 diff --git a/testData/src/pkg/TestIffSimplification.java b/testData/src/pkg/TestIffSimplification.java new file mode 100644 index 0000000..5b87363 --- /dev/null +++ b/testData/src/pkg/TestIffSimplification.java @@ -0,0 +1,48 @@ +package pkg; + +import java.lang.Math; + +public class TestIffSimplification { + public int simpleIff(boolean status, int[] values) { + return status ? values[0] : values[1]; + } + + public int simpleIf(boolean status, int[] values) { + if (status) { + return values[0]; + } + else { + return values[1]; + } + } + + public int nestedIf(boolean status, boolean condition, int[] values) { + if (status) { + if (condition) { + return values[2]; + } + else { + return values[0]; + } + } + else { + return values[1]; + } + } + + public int compareTo(int mc1, int mc2, byte csg1, byte csg2, double score1, double score2, int doc1, int doc2) { + if (mc1 != mc2) { + return mc1 < mc2 ? 1 : -1; + } + + if (csg1 != csg2) { + return csg1 < csg2 ? 1 : -1; + } + + if (Math.abs(score1 - score2) < 1e-6) { + return doc1 < doc2 ? -1 : 1; + } + + return score1 < score2 ? 1 : -1; + } +} \ No newline at end of file