Search in sources :

Example 1 with ASTLabeledNode

use of soot.dava.internal.AST.ASTLabeledNode in project soot by Sable.

the class EliminateConditions method change.

public boolean change(Boolean returned, ASTNode temp) {
    if (bodyContainingNode != null && returned != null && temp != null) {
        int index = bodyContainingNode.indexOf(temp);
        if (DEBUG)
            System.out.println("in change");
        if (temp instanceof ASTIfNode) {
            bodyContainingNode.remove(temp);
            if (returned.booleanValue()) {
                // if statement and value was true put the body of if into
                // the code
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(0));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
                }
            }
            if (DEBUG)
                System.out.println("Removed if" + temp);
            return true;
        } else if (temp instanceof ASTIfElseNode) {
            bodyContainingNode.remove(temp);
            if (returned.booleanValue()) {
                // true so the if branch's body has to be added
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(0));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
                }
            } else {
                // if its a labeled stmt we need a labeled block instead
                // notice that its okkay to put a labeled block since other
                // transformations might remove it
                String label = ((ASTLabeledNode) temp).get_Label().toString();
                if (label != null) {
                    ASTLabeledBlockNode labeledNode = new ASTLabeledBlockNode(((ASTLabeledNode) temp).get_Label(), (List<Object>) temp.get_SubBodies().get(1));
                    bodyContainingNode.add(index, labeledNode);
                } else {
                    bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(1));
                }
            }
            return true;
        } else if (temp instanceof ASTWhileNode && returned.booleanValue() == false) {
            // notice we only remove if ASTWhileNode has false condition
            bodyContainingNode.remove(temp);
            return true;
        } else if (temp instanceof ASTDoWhileNode && returned.booleanValue() == false) {
            // System.out.println("in try dowhile false");
            // remove the loop copy the body out since it gets executed once
            bodyContainingNode.remove(temp);
            bodyContainingNode.addAll(index, (List) temp.get_SubBodies().get(0));
            return true;
        } else if (temp instanceof ASTForLoopNode && returned.booleanValue() == false) {
            bodyContainingNode.remove(temp);
            ASTStatementSequenceNode newNode = new ASTStatementSequenceNode(((ASTForLoopNode) temp).getInit());
            bodyContainingNode.add(index, newNode);
            return true;
        }
    }
    return false;
}
Also used : ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTDoWhileNode(soot.dava.internal.AST.ASTDoWhileNode) ASTWhileNode(soot.dava.internal.AST.ASTWhileNode) List(java.util.List) ASTIfElseNode(soot.dava.internal.AST.ASTIfElseNode) ASTLabeledNode(soot.dava.internal.AST.ASTLabeledNode) ASTForLoopNode(soot.dava.internal.AST.ASTForLoopNode) ASTStatementSequenceNode(soot.dava.internal.AST.ASTStatementSequenceNode) ASTLabeledBlockNode(soot.dava.internal.AST.ASTLabeledBlockNode)

Example 2 with ASTLabeledNode

use of soot.dava.internal.AST.ASTLabeledNode 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

ASTDoWhileNode (soot.dava.internal.AST.ASTDoWhileNode)2 ASTForLoopNode (soot.dava.internal.AST.ASTForLoopNode)2 ASTLabeledNode (soot.dava.internal.AST.ASTLabeledNode)2 ASTWhileNode (soot.dava.internal.AST.ASTWhileNode)2 List (java.util.List)1 DecompilationException (soot.dava.DecompilationException)1 ASTIfElseNode (soot.dava.internal.AST.ASTIfElseNode)1 ASTIfNode (soot.dava.internal.AST.ASTIfNode)1 ASTLabeledBlockNode (soot.dava.internal.AST.ASTLabeledBlockNode)1 ASTNode (soot.dava.internal.AST.ASTNode)1 ASTStatementSequenceNode (soot.dava.internal.AST.ASTStatementSequenceNode)1 ASTSwitchNode (soot.dava.internal.AST.ASTSwitchNode)1 ASTUnconditionalLoopNode (soot.dava.internal.AST.ASTUnconditionalLoopNode)1 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)1 DAbruptStmt (soot.dava.internal.javaRep.DAbruptStmt)1 ASTParentNodeFinder (soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder)1 Stmt (soot.jimple.Stmt)1