Search in sources :

Example 16 with PsiCFGMethod

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

the class CFGUtil method outputCallGraphDotFile.

public static void outputCallGraphDotFile(Callgraph cg) {
    String path = PathManager.getLogPath();
    String fileName = "CallGraph.dot";
    BufferedWriter bw = null;
    try {
        File dotFile = new File(path, fileName);
        if (dotFile.exists()) {
            dotFile.delete();
        }
        dotFile.createNewFile();
        PsiCFGDebugUtil.LOG.info("Log Callgraph to file: " + dotFile.getAbsolutePath());
        FileWriter fw = new FileWriter(dotFile);
        bw = new BufferedWriter(fw);
        Map<PsiCFGMethod, Integer> allNodes = getAllMethodNodesFromCallGraph(cg);
        bw.write("digraph G{\n");
        for (PsiCFGMethod method : allNodes.keySet()) {
            String label = method.getName();
            String line = String.format("n%d[label=\"%s\"];\n", allNodes.get(method), label);
            bw.write(line);
        }
        bw.write("\n");
        for (PsiCFGMethod curMethod : cg.callerMethodToCalleeMethodMap.keySet()) {
            Integer sId = allNodes.get(curMethod);
            for (PsiCFGMethod tgtMethod : cg.callerMethodToCalleeMethodMap.get(curMethod)) {
                Integer tId = allNodes.get(tgtMethod);
                String line = String.format("n%d -> n%d;\n", sId, tId);
                bw.write(line);
            }
        }
        bw.write("\n");
        bw.write("}");
    //Output digraphG
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bw != null) {
                bw.close();
            }
        } catch (Exception ex) {
        }
    }
}
Also used : PsiCFGMethod(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 17 with PsiCFGMethod

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

the class PsiCFGAnalysisUtil method performStage3.

/**
   * The purpose of the Stage3 is create IntraProcedural
   * CFG for the methods and lambdas inside the app
   * class, including the constructor and the init code
   */
public void performStage3() {
    PsiCFGClass[] appClasses = mScene.getAllApplicationClasses();
    mScene.workingList.clear();
    mScene.workingList.addAll(Arrays.asList(appClasses));
    while (!mScene.workingList.isEmpty()) {
        //While the working list is not empty
        //Process the working list
        PsiCFGClass currentClass = mScene.workingList.removeFirst();
        PsiCFGMethod[] allMethods = currentClass.getAllMethods();
        for (PsiCFGMethod currentMethod : allMethods) {
            //Lambda methods' CFG is created by the time it is decleared
            if (currentMethod.isAbstract() || currentMethod.isLambda()) {
                continue;
            }
            PsiMethod methodRef = currentMethod.getMethodRef();
            if (methodRef != null) {
                PsiCodeBlock codeBlock = methodRef.getBody();
                if (codeBlock == null) {
                    PsiCFGDebugUtil.LOG.info("In " + currentClass.getQualifiedClassName() + "." + currentMethod.getName() + "Code block is null");
                    continue;
                }
                MethodGraph cfg = CFGUtil.constructMethodGraph(mScene, codeBlock, currentMethod);
                currentMethod.setControlFlowGraph(cfg);
            }
        }
    }
}
Also used : PsiCFGMethod(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod) MethodGraph(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.MethodGraph) PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)

Aggregations

PsiCFGMethod (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod)17 PsiCFGClass (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)8 PsiCFGPartialMethodSignature (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGPartialMethodSignature)3 GraphNode (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.node.GraphNode)3 PsiClassType (com.intellij.psi.PsiClassType)2 MethodGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.MethodGraph)1 Pair (com.intellij.openapi.util.Pair)1 PsiClass (com.intellij.psi.PsiClass)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1