Skip to content

Commit

Permalink
fix: remove redundant wrapping for same arith operations (PR #559)
Browse files Browse the repository at this point in the history
  • Loading branch information
asashour authored and skylot committed Apr 4, 2019
1 parent 9d257cd commit 058e4c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public String getSymbol() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.slf4j.LoggerFactory;

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.utils.InsnUtils;

Expand Down Expand Up @@ -111,6 +113,12 @@ public InsnArg wrapInstruction(InsnNode insn) {
insn.add(AFlag.WRAPPED);
InsnArg arg = wrapArg(insn);
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.junit.jupiter.api.Test;

import jadx.NotYetImplemented;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;

Expand All @@ -21,6 +20,22 @@ public int test1(int a) {
public int test2(int a, int b, int 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
Expand All @@ -30,15 +45,20 @@ public void test() {

assertThat(code, containsString("return (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, 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;")));
}
}

0 comments on commit 058e4c9

Please sign in to comment.