core: remove redundant parenthesis for arithmetic operations
This commit is contained in:
@@ -720,22 +720,19 @@ public class InsnGen {
|
||||
}
|
||||
|
||||
private void makeArith(ArithNode insn, CodeWriter code, EnumSet<Flags> state) throws CodegenException {
|
||||
ArithOp op = insn.getOp();
|
||||
if (state.contains(Flags.BODY_ONLY)) {
|
||||
// wrap insn in brackets for save correct operation order
|
||||
// wrap insn in brackets for save correct operation order
|
||||
boolean wrap = state.contains(Flags.BODY_ONLY)
|
||||
&& !insn.getAttributes().contains(AttributeFlag.DONT_WRAP);
|
||||
if (wrap) {
|
||||
code.add('(');
|
||||
addArg(code, insn.getArg(0));
|
||||
code.add(' ');
|
||||
code.add(op.getSymbol());
|
||||
code.add(' ');
|
||||
addArg(code, insn.getArg(1));
|
||||
}
|
||||
addArg(code, insn.getArg(0));
|
||||
code.add(' ');
|
||||
code.add(insn.getOp().getSymbol());
|
||||
code.add(' ');
|
||||
addArg(code, insn.getArg(1));
|
||||
if (wrap) {
|
||||
code.add(')');
|
||||
} else {
|
||||
addArg(code, insn.getArg(0));
|
||||
code.add(' ');
|
||||
code.add(op.getSymbol());
|
||||
code.add(' ');
|
||||
addArg(code, insn.getArg(1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ public enum AttributeFlag {
|
||||
RETURN, // block contains only return instruction
|
||||
|
||||
DECLARE_VAR,
|
||||
DONT_WRAP,
|
||||
|
||||
DONT_SHRINK,
|
||||
DONT_GENERATE,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package jadx.core.dex.visitors;
|
||||
|
||||
import jadx.core.dex.attributes.AttributeFlag;
|
||||
import jadx.core.dex.instructions.ArithNode;
|
||||
import jadx.core.dex.instructions.ArithOp;
|
||||
import jadx.core.dex.instructions.InsnType;
|
||||
import jadx.core.dex.instructions.args.InsnArg;
|
||||
import jadx.core.dex.instructions.args.InsnWrapArg;
|
||||
import jadx.core.dex.instructions.args.RegisterArg;
|
||||
import jadx.core.dex.instructions.mods.ConstructorInsn;
|
||||
import jadx.core.dex.nodes.BlockNode;
|
||||
@@ -23,6 +26,7 @@ public class PrepareForCodeGen extends AbstractVisitor {
|
||||
}
|
||||
for (BlockNode block : blocks) {
|
||||
removeInstructions(block);
|
||||
removeBrackets(block);
|
||||
modifyArith(block);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +52,36 @@ public class PrepareForCodeGen extends AbstractVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private static void removeBrackets(BlockNode block) {
|
||||
for (InsnNode insn : block.getInstructions()) {
|
||||
checkInsn(insn);
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkInsn(InsnNode insn) {
|
||||
if (insn.getType() == InsnType.ARITH) {
|
||||
ArithNode arith = (ArithNode) insn;
|
||||
ArithOp op = arith.getOp();
|
||||
if (op == ArithOp.ADD || op == ArithOp.SUB) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
InsnArg arg = arith.getArg(i);
|
||||
if (arg.isInsnWrap()) {
|
||||
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
|
||||
wrapInsn.getAttributes().add(AttributeFlag.DONT_WRAP);
|
||||
checkInsn(wrapInsn);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (InsnArg arg : insn.getArguments()) {
|
||||
if (arg.isInsnWrap()) {
|
||||
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
|
||||
checkInsn(wrapInsn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void modifyArith(BlockNode block) {
|
||||
List<InsnNode> list = block.getInstructions();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
|
||||
@@ -16,6 +16,10 @@ public class TestArith2 extends InternalJadxTest {
|
||||
public int test1(int a) {
|
||||
return (a + 2) * 3;
|
||||
}
|
||||
|
||||
public int test2(int a, int b, int c) {
|
||||
return a + b + c;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -26,5 +30,8 @@ public class TestArith2 extends InternalJadxTest {
|
||||
|
||||
assertThat(code, containsString("return (a + 2) * 3;"));
|
||||
assertThat(code, not(containsString("a + 2 * 3")));
|
||||
|
||||
assertThat(code, containsString("return a + b + c;"));
|
||||
assertThat(code, not(containsString("return (a + b) + c;")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user