Search in sources :

Example 1 with DAbruptStmt

use of soot.dava.internal.javaRep.DAbruptStmt in project soot by Sable.

the class IfElseSplitter method bodyTargetsLabel.

/*
	 * Check that label is non null and the string inside is non null... if yes return false
	 * Check that the given list (sequeneof ASTNodes have no abrupt edge targeting the label.
	 *
	 */
public boolean bodyTargetsLabel(SETNodeLabel label, List<Object> body) {
    // no SETNodeLabel is good
    if (label == null)
        return false;
    // SETNodeLabel but with no string is also good
    if (label.toString() == null)
        return false;
    final String strLabel = label.toString();
    // go through the body use traversal to find whether there is an abrupt stmt targeting this
    Iterator<Object> it = body.iterator();
    targeted = false;
    while (it.hasNext()) {
        ASTNode temp = (ASTNode) it.next();
        temp.apply(new DepthFirstAdapter() {

            // set targeted to true if DAbruptStmt targets it
            public void inStmt(Stmt s) {
                // only interested in abrupt stmts
                if (!(s instanceof DAbruptStmt))
                    return;
                DAbruptStmt abrupt = (DAbruptStmt) s;
                SETNodeLabel label = abrupt.getLabel();
                if (label != null && label.toString() != null && label.toString().equals(strLabel)) {
                    targeted = true;
                }
            }
        });
        if (targeted)
            break;
    }
    return targeted;
}
Also used : DepthFirstAdapter(soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter) SETNodeLabel(soot.dava.internal.SET.SETNodeLabel) ASTNode(soot.dava.internal.AST.ASTNode) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) ReturnStmt(soot.jimple.ReturnStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Example 2 with DAbruptStmt

use of soot.dava.internal.javaRep.DAbruptStmt in project soot by Sable.

the class UselessAbruptStmtRemover method caseASTStatementSequenceNode.

public void caseASTStatementSequenceNode(ASTStatementSequenceNode node) {
    Iterator<AugmentedStmt> it = node.getStatements().iterator();
    AugmentedStmt remove = null;
    ASTLabeledNode target = null;
    while (it.hasNext()) {
        AugmentedStmt as = it.next();
        Stmt s = as.get_Stmt();
        // we only care about break and continue stmts
        if (!(s instanceof DAbruptStmt)) {
            continue;
        }
        DAbruptStmt abrupt = (DAbruptStmt) s;
        String label = abrupt.getLabel().toString();
        if (label == null) {
            // analysis with implicit abrupt flow but not needed currently
            continue;
        }
        if (it.hasNext()) {
            // afterwards...that is for sure dead code
            throw new DecompilationException("Dead code detected. Report to developer");
        }
        // get the target node
        Object temp = mapper.getTarget(label);
        if (temp == null) {
            continue;
        // throw new DecompilationException("Could not find target for abrupt stmt"+abrupt.toString());
        }
        target = (ASTLabeledNode) temp;
        // will need to find parents of ancestors see if we need to initialize the finder
        if (finder == null) {
            finder = new ASTParentNodeFinder();
            methodNode.apply(finder);
        }
        if (DEBUG)
            System.out.println("Starting useless check for abrupt stmt: " + abrupt);
        // start condition is that ancestor is the stmt seq node
        ASTNode ancestor = node;
        while (ancestor != target) {
            Object tempParent = finder.getParentOf(ancestor);
            if (tempParent == null)
                throw new DecompilationException("Parent found was null!!. Report to Developer");
            ASTNode ancestorsParent = (ASTNode) tempParent;
            if (DEBUG)
                System.out.println("\tCurrent ancestorsParent has type" + ancestorsParent.getClass());
            // ancestor should be last child of ancestorsParent
            if (!checkChildLastInParent(ancestor, ancestorsParent)) {
                if (DEBUG)
                    System.out.println("\t\tCurrent ancestorParent has more children after this ancestor");
                // return from the method since this is the last stmt and we cant do anything
                return;
            }
            // ancestorsParent should not be a loop of any kind OR A SWITCH
            if (ancestorsParent instanceof ASTWhileNode || ancestorsParent instanceof ASTDoWhileNode || ancestorsParent instanceof ASTUnconditionalLoopNode || ancestorsParent instanceof ASTForLoopNode || ancestorsParent instanceof ASTSwitchNode) {
                if (DEBUG)
                    System.out.println("\t\tAncestorsParent is a loop shouldnt remove abrupt stmt");
                return;
            }
            ancestor = ancestorsParent;
        }
        if (DEBUG)
            System.out.println("\tGot to target without returning means we can remove stmt");
        remove = as;
    }
    if (remove != null) {
        List<AugmentedStmt> stmts = node.getStatements();
        stmts.remove(remove);
        if (DEBUG)
            System.out.println("\tRemoved abrupt stmt");
        if (target != null) {
            if (DEBUG)
                System.out.println("Invoking findAndKill on the target");
            UselessLabelFinder.v().findAndKill(target);
        }
        // TODO what if we just emptied a stmt seq block??
        // not doing this for the moment
        // set modified flag make finder null
        G.v().ASTTransformations_modified = true;
        finder = null;
    }
}
Also used : ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) DecompilationException(soot.dava.DecompilationException) ASTLabeledNode(soot.dava.internal.AST.ASTLabeledNode) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DAbruptStmt(soot.dava.internal.javaRep.DAbruptStmt) Stmt(soot.jimple.Stmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) ASTDoWhileNode(soot.dava.internal.AST.ASTDoWhileNode) ASTWhileNode(soot.dava.internal.AST.ASTWhileNode) ASTNode(soot.dava.internal.AST.ASTNode) ASTUnconditionalLoopNode(soot.dava.internal.AST.ASTUnconditionalLoopNode) ASTForLoopNode(soot.dava.internal.AST.ASTForLoopNode) ASTSwitchNode(soot.dava.internal.AST.ASTSwitchNode)

Aggregations

ASTNode (soot.dava.internal.AST.ASTNode)2 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)2 DAbruptStmt (soot.dava.internal.javaRep.DAbruptStmt)2 Stmt (soot.jimple.Stmt)2 DecompilationException (soot.dava.DecompilationException)1 ASTDoWhileNode (soot.dava.internal.AST.ASTDoWhileNode)1 ASTForLoopNode (soot.dava.internal.AST.ASTForLoopNode)1 ASTLabeledNode (soot.dava.internal.AST.ASTLabeledNode)1 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)1 ASTUnconditionalLoopNode (soot.dava.internal.AST.ASTUnconditionalLoopNode)1 ASTWhileNode (soot.dava.internal.AST.ASTWhileNode)1 SETNodeLabel (soot.dava.internal.SET.SETNodeLabel)1 DepthFirstAdapter (soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter)1 ASTParentNodeFinder (soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder)1 ReturnStmt (soot.jimple.ReturnStmt)1 ReturnVoidStmt (soot.jimple.ReturnVoidStmt)1