Search in sources :

Example 6 with BlockGraph

use of com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph in project android by JetBrains.

the class CFGBuilder method dfsPsiDoWhileStatementBuilder.

/**
   * Build nodes for "Do While" loops
   * @param statement The while statement
   * @param label The label for this loop. In case of labeled break.
   */
public void dfsPsiDoWhileStatementBuilder(@NotNull PsiDoWhileStatement statement, @Nullable PsiIdentifier label) {
    PsiStatement loopBody = statement.getBody();
    int loopType;
    LoopBranchingNodeImpl loopNode;
    GraphNode conditionCheckEntry;
    ConditionCheckNode conditionCheckExit;
    GraphNode postLoopEntry;
    GraphNode postLoopExit;
    loopType = LoopBranchingNode.DOWHILE_LOOP;
    PsiDoWhileStatement dowhileStmt = (PsiDoWhileStatement) statement;
    loopNode = new LoopBranchingNodeImpl(this.mGraph, loopType);
    connectCurrentWorkingNode(loopNode);
    //Push this loopNode into the stack
    pushLoopNode(loopNode, label);
    //Eval the loopbody
    BlockGraph loopBodyGraph = loopbodyBuilder(loopBody, statement);
    connectCurrentWorkingNode(loopBodyGraph.getEntryNode());
    curWorkingNodeList.clear();
    curWorkingNodeList.add(loopBodyGraph.getExitNode());
    //Eval the condition check
    PsiExpression psiConditionCheckCode = dowhileStmt.getCondition();
    Value finalCheckVal = dfsExpressionBuilder(psiConditionCheckCode);
    conditionCheckExit = new ConditionCheckNodeImpl(this.mGraph, finalCheckVal);
    if (loopBodyGraph.getExitNode().getOut().length == 0) {
        //The dummy loop node does not have any out edges
        //In this case the conditionCheckEntry is the same
        //as the conditionCheckExit
        conditionCheckEntry = conditionCheckExit;
    } else {
        //At this point the dummy LoopNode is connected to the entry
        //of the condition check code.
        conditionCheckEntry = loopBodyGraph.getExitNode().getOut()[0];
    }
    connectCurrentWorkingNode(conditionCheckExit);
    //True branch connect back to the body
    GraphNodeUtil.connectGraphNode(conditionCheckExit.getTrueBranch(), loopBodyGraph.getEntryNode());
    curWorkingNodeList.clear();
    curWorkingNodeList.add(conditionCheckExit.getFalseBranch());
    loopNode.setConditionCheckEntry(conditionCheckEntry);
    loopNode.setConditionCheckExitNode(conditionCheckExit);
    loopNode.setLoopBody(loopBodyGraph);
    loopNode.setPostLoopEntryNode(null);
    loopNode.setPostLoopExitNode(null);
    //Do while loop build complete;
    //Process the break and continue nodes
    loopNode.connectSpecialNodes();
    popLoopNode();
}
Also used : BlockGraph(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph)

Example 7 with BlockGraph

use of com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph in project android by JetBrains.

the class CFGBuilder method dfsPsiWhileStatementBuilder.

/**
   * Build nodes for "while" loops
   * @param statement The while statement
   * @param label The label for this loop. In case of labeled break.
   */
public void dfsPsiWhileStatementBuilder(@NotNull PsiWhileStatement statement, @Nullable PsiIdentifier label) {
    PsiStatement loopBody = statement.getBody();
    int loopType;
    LoopBranchingNodeImpl loopNode;
    GraphNode conditionCheckEntry;
    ConditionCheckNode conditionCheckExit;
    GraphNode postLoopEntry;
    GraphNode postLoopExit;
    PsiWhileStatement whileStmt = (PsiWhileStatement) statement;
    loopType = LoopBranchingNode.WHILE_LOOP;
    loopNode = new LoopBranchingNodeImpl(this.mGraph, loopType);
    connectCurrentWorkingNode(loopNode);
    //Push this loopNode into the stack
    pushLoopNode(loopNode, label);
    //Eval the condition check
    PsiExpression psiConditionCheckCode = whileStmt.getCondition();
    Value finalCheckVal = dfsExpressionBuilder(psiConditionCheckCode);
    //Build the final condition check for this loop
    conditionCheckExit = new ConditionCheckNodeImpl(this.mGraph, finalCheckVal);
    if (loopNode.getOut().length == 0) {
        //The dummy loop node does not have any out edges
        //In this case the conditionCheckEntry is the same
        //as the conditionCheckExit
        conditionCheckEntry = conditionCheckExit;
    } else {
        //At this point the dummy LoopNode is connected to the entry
        //of the condition check code.
        conditionCheckEntry = loopNode.getOut()[0];
    }
    connectCurrentWorkingNode(conditionCheckExit);
    //Build the loop body
    BlockGraph loopBodyGraph = loopbodyBuilder(loopBody, statement);
    //Connect the condition check code true branch to loop entry
    GraphNodeUtil.connectGraphNode(conditionCheckExit.getTrueBranch(), loopBodyGraph.getEntryNode());
    curWorkingNodeList.clear();
    curWorkingNodeList.add(loopBodyGraph.getExitNode());
    //Connect the loopbody exit to the condition check entry
    connectCurrentWorkingNode(conditionCheckEntry);
    curWorkingNodeList.clear();
    curWorkingNodeList.add(conditionCheckExit.getFalseBranch());
    loopNode.setConditionCheckEntry(conditionCheckEntry);
    loopNode.setConditionCheckExitNode(conditionCheckExit);
    loopNode.setPostLoopEntryNode(null);
    loopNode.setPostLoopExitNode(null);
    loopNode.setLoopBody(loopBodyGraph);
    //While loop build complete
    //Process the break and continue nodes
    loopNode.connectSpecialNodes();
    popLoopNode();
}
Also used : BlockGraph(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph)

Aggregations

BlockGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph)7 Nullable (org.jetbrains.annotations.Nullable)2 Graph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.Graph)1 MethodGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.MethodGraph)1 SwitchCaseGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.SwitchCaseGraph)1 AssignStmtImpl (com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl)1 DeclarationStmtImpl (com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.DeclarationStmtImpl)1