Commit 058e4c9f authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

fix: remove redundant wrapping for same arith operations (PR #559)

parent 9d257cd1
...@@ -25,4 +25,10 @@ public enum ArithOp { ...@@ -25,4 +25,10 @@ public enum ArithOp {
return this.symbol; return this.symbol;
} }
public boolean noWrapWith(ArithOp other) {
return (this == ADD && other == ADD)
|| (this == MUL && other == MUL)
|| (this == AND && other == AND)
|| (this == OR && other == OR);
}
} }
...@@ -9,6 +9,8 @@ import org.slf4j.Logger; ...@@ -9,6 +9,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jadx.core.dex.attributes.AFlag; import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.instructions.ArithNode;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.utils.InsnUtils; import jadx.core.utils.InsnUtils;
...@@ -111,6 +113,12 @@ public abstract class InsnArg extends Typed { ...@@ -111,6 +113,12 @@ public abstract class InsnArg extends Typed {
insn.add(AFlag.WRAPPED); insn.add(AFlag.WRAPPED);
InsnArg arg = wrapArg(insn); InsnArg arg = wrapArg(insn);
parent.setArg(i, arg); parent.setArg(i, arg);
if (insn.getType() == InsnType.ARITH && parent.getType() == InsnType.ARITH
&& ((ArithNode) insn).getOp().noWrapWith(((ArithNode) parent).getOp())) {
insn.add(AFlag.DONT_WRAP);
}
return arg; return arg;
} }
......
...@@ -6,7 +6,6 @@ import static org.hamcrest.MatcherAssert.assertThat; ...@@ -6,7 +6,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jadx.NotYetImplemented;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest; import jadx.tests.api.IntegrationTest;
...@@ -21,6 +20,22 @@ public class TestArith2 extends IntegrationTest { ...@@ -21,6 +20,22 @@ public class TestArith2 extends IntegrationTest {
public int test2(int a, int b, int c) { public int test2(int a, int b, int c) {
return a + b + c; return a + b + c;
} }
public boolean test3(boolean a, boolean b, boolean c) {
return a | b | c;
}
public boolean test4(boolean a, boolean b, boolean c) {
return a & b & c;
}
public int substract(int a, int b, int c) {
return a - (b - c);
}
public int divide(int a, int b, int c) {
return a / (b / c);
}
} }
@Test @Test
...@@ -30,15 +45,20 @@ public class TestArith2 extends IntegrationTest { ...@@ -30,15 +45,20 @@ public class TestArith2 extends IntegrationTest {
assertThat(code, containsString("return (a + 2) * 3;")); assertThat(code, containsString("return (a + 2) * 3;"));
assertThat(code, not(containsString("a + 2 * 3"))); assertThat(code, not(containsString("a + 2 * 3")));
}
@Test
@NotYetImplemented
public void test2() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("return a + b + c;")); assertThat(code, containsString("return a + b + c;"));
assertThat(code, not(containsString("return (a + b) + c;"))); assertThat(code, not(containsString("return (a + b) + c;")));
assertThat(code, containsString("return a | b | c;"));
assertThat(code, not(containsString("return (a | b) | c;")));
assertThat(code, containsString("return a & b & c;"));
assertThat(code, not(containsString("return (a & b) & c;")));
assertThat(code, containsString("return a - (b - c);"));
assertThat(code, not(containsString("return a - b - c;")));
assertThat(code, containsString("return a / (b / c);"));
assertThat(code, not(containsString("return a / b / c;")));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment