soot.dava.toolkits.base.AST.transformations.UnreachableCodeEliminator Maven / Gradle / Ivy
package soot.dava.toolkits.base.AST.transformations;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import soot.Local;
import soot.SootClass;
import soot.Type;
import soot.dava.internal.AST.ASTNode;
import soot.dava.internal.AST.ASTStatementSequenceNode;
import soot.dava.internal.AST.ASTSwitchNode;
import soot.dava.internal.AST.ASTTryNode;
import soot.dava.internal.asg.AugmentedStmt;
import soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter;
import soot.dava.toolkits.base.AST.structuredAnalysis.UnreachableCodeFinder;
import soot.jimple.Stmt;
public class UnreachableCodeEliminator extends DepthFirstAdapter {
public boolean DUBUG = true;
ASTNode AST;
UnreachableCodeFinder codeFinder;
public UnreachableCodeEliminator(ASTNode AST) {
super();
this.AST = AST;
setup();
}
public UnreachableCodeEliminator(boolean verbose, ASTNode AST) {
super(verbose);
this.AST = AST;
setup();
}
private void setup() {
codeFinder = new UnreachableCodeFinder(AST);
// parentOf = new ASTParentNodeFinder();
// AST.apply(parentOf);
}
public void inASTStatementSequenceNode(ASTStatementSequenceNode node) {
List toRemove = new ArrayList();
for (AugmentedStmt as : node.getStatements()) {
Stmt s = as.get_Stmt();
// System.out.println("HERE!!!"+s.toString());
if (!codeFinder.isConstructReachable(s)) {
toRemove.add(as);
// if(DEBUG) System.out.println("Statement "+s.toString()+ " is NOT REACHABLE REMOVE IT");
}
}
for (AugmentedStmt as : toRemove) {
node.getStatements().remove(as);
}
}
public void normalRetrieving(ASTNode node) {
if (node instanceof ASTSwitchNode) {
dealWithSwitchNode((ASTSwitchNode) node);
return;
}
// from the Node get the subBodes
List toReturn = new ArrayList();
Iterator