Commit 08ea61f4 authored by Skylot's avatar Skylot

core: don't traverse exception handlers twice (includes in TryCatchRegion)

parent 1d5368f5
......@@ -67,7 +67,7 @@ public class CheckRegions extends AbstractVisitor {
}
// check loop conditions
DepthRegionTraversal.traverseAll(mth, new AbstractRegionVisitor() {
DepthRegionTraversal.traverse(mth, new AbstractRegionVisitor() {
@Override
public void enterRegion(MethodNode mth, IRegion region) {
if (region instanceof LoopRegion) {
......@@ -82,7 +82,7 @@ public class CheckRegions extends AbstractVisitor {
private static void printRegionsWithBlock(MethodNode mth, final BlockNode block) {
final Set<IRegion> regions = new LinkedHashSet<IRegion>();
DepthRegionTraversal.traverseAll(mth, new TracedRegionVisitor() {
DepthRegionTraversal.traverse(mth, new TracedRegionVisitor() {
@Override
public void processBlockTraced(MethodNode mth, IBlock container, IRegion currentRegion) {
if (block.equals(container)) {
......
......@@ -44,6 +44,6 @@ public class CleanRegions {
}
}
};
DepthRegionTraversal.traverseAll(mth, removeEmptyBlocks);
DepthRegionTraversal.traverse(mth, removeEmptyBlocks);
}
}
......@@ -18,18 +18,18 @@ public class DepthRegionTraversal {
traverseInternal(mth, visitor, mth.getRegion());
}
public static void traverseAll(MethodNode mth, IRegionVisitor visitor) {
public static void traverseIncludingExcHandlers(MethodNode mth, IRegionVisitor visitor) {
traverseInternal(mth, visitor, mth.getRegion());
for (ExceptionHandler h : mth.getExceptionHandlers()) {
traverseInternal(mth, visitor, h.getHandlerRegion());
}
}
public static void traverseAllIterative(MethodNode mth, IRegionIterativeVisitor visitor) {
public static void traverseIterative(MethodNode mth, IRegionIterativeVisitor visitor) {
boolean repeat;
int k = 0;
do {
repeat = traverseAllIterativeInternal(mth, visitor);
repeat = traverseIterativeInternal(mth, visitor, mth.getRegion());
if (k++ > ITERATIVE_LIMIT) {
throw new JadxOverflowException("Iterative traversal limit reached, method: " + mth);
}
......@@ -49,18 +49,6 @@ public class DepthRegionTraversal {
}
}
private static boolean traverseAllIterativeInternal(MethodNode mth, IRegionIterativeVisitor visitor) {
if (traverseIterativeInternal(mth, visitor, mth.getRegion())) {
return true;
}
for (ExceptionHandler h : mth.getExceptionHandlers()) {
if (traverseIterativeInternal(mth, visitor, h.getHandlerRegion())) {
return true;
}
}
return false;
}
private static boolean traverseIterativeInternal(MethodNode mth, IRegionIterativeVisitor visitor,
IContainer container) {
if (container instanceof IRegion) {
......
......@@ -19,20 +19,22 @@ import static jadx.core.utils.RegionUtils.insnsCount;
public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor, IRegionIterativeVisitor {
private static final TernaryVisitor TERNARY_VISITOR = new TernaryVisitor();
@Override
public void visit(MethodNode mth) {
// collapse ternary operators
DepthRegionTraversal.traverseAllIterative(mth, new IRegionIterativeVisitor() {
@Override
public boolean visitRegion(MethodNode mth, IRegion region) {
if (region instanceof IfRegion) {
return TernaryMod.makeTernaryInsn(mth, (IfRegion) region);
}
return false;
}
});
DepthRegionTraversal.traverseAll(mth, this);
DepthRegionTraversal.traverseAllIterative(mth, this);
DepthRegionTraversal.traverseIterative(mth, TERNARY_VISITOR);
DepthRegionTraversal.traverse(mth, this);
DepthRegionTraversal.traverseIterative(mth, this);
}
private static class TernaryVisitor implements IRegionIterativeVisitor {
@Override
public boolean visitRegion(MethodNode mth, IRegion region) {
return region instanceof IfRegion
&& TernaryMod.makeTernaryInsn(mth, (IfRegion) region);
}
}
@Override
......
......@@ -43,7 +43,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
@Override
public void visit(MethodNode mth) {
DepthRegionTraversal.traverseAll(mth, this);
DepthRegionTraversal.traverse(mth, this);
}
@Override
......
......@@ -44,7 +44,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
int k = 0;
while (!tryBlocksMap.isEmpty()) {
DepthRegionTraversal.traverseAll(mth, new AbstractRegionVisitor() {
DepthRegionTraversal.traverseIncludingExcHandlers(mth, new AbstractRegionVisitor() {
@Override
public void leaveRegion(MethodNode mth, IRegion region) {
checkAndWrap(mth, tryBlocksMap, region);
......
......@@ -186,7 +186,7 @@ public class ProcessVariables extends AbstractVisitor {
// collect all variables usage
IRegionVisitor collect = new CollectUsageRegionVisitor(usageMap);
DepthRegionTraversal.traverseAll(mth, collect);
DepthRegionTraversal.traverse(mth, collect);
// reduce assigns map
List<RegisterArg> mthArgs = mth.getArguments(true);
......
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