Search in sources :

Example 1 with Pseudocode

use of org.jetbrains.kotlin.cfg.pseudocode.Pseudocode in project kotlin by JetBrains.

the class AbstractPseudocodeTest method processCFData.

private void processCFData(File file, SetMultimap<KtElement, Pseudocode> data, BindingContext bindingContext) throws IOException {
    Collection<Pseudocode> pseudocodes = data.values();
    StringBuilder instructionDump = new StringBuilder();
    int i = 0;
    for (Pseudocode pseudocode : pseudocodes) {
        KtElement correspondingElement = pseudocode.getCorrespondingElement();
        String label;
        assert (correspondingElement instanceof KtNamedDeclaration || correspondingElement instanceof KtPropertyAccessor) : "Unexpected element class is pseudocode: " + correspondingElement.getClass();
        boolean isAnonymousFunction = correspondingElement instanceof KtFunctionLiteral || (correspondingElement instanceof KtNamedFunction && correspondingElement.getName() == null);
        if (isAnonymousFunction) {
            label = "anonymous_" + i++;
        } else if (correspondingElement instanceof KtNamedDeclaration) {
            KtNamedDeclaration namedDeclaration = (KtNamedDeclaration) correspondingElement;
            label = namedDeclaration.getName();
        } else {
            String propertyName = ((KtProperty) correspondingElement.getParent()).getName();
            label = (((KtPropertyAccessor) correspondingElement).isGetter() ? "get" : "set") + "_" + propertyName;
        }
        instructionDump.append("== ").append(label).append(" ==\n");
        instructionDump.append(correspondingElement.getText());
        instructionDump.append("\n---------------------\n");
        dumpInstructions((PseudocodeImpl) pseudocode, instructionDump, bindingContext);
        instructionDump.append("=====================\n");
        checkPseudocode((PseudocodeImpl) pseudocode);
    }
    File expectedInstructionsFile = KotlinTestUtils.replaceExtension(file, getDataFileExtension());
    KotlinTestUtils.assertEqualsToFile(expectedInstructionsFile, instructionDump.toString());
}
Also used : Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) File(java.io.File)

Example 2 with Pseudocode

use of org.jetbrains.kotlin.cfg.pseudocode.Pseudocode in project kotlin by JetBrains.

the class AbstractPseudocodeTest method addDeclaration.

private static void addDeclaration(SetMultimap<KtElement, Pseudocode> data, BindingContext bindingContext, KtDeclaration declaration) {
    Pseudocode pseudocode = PseudocodeUtil.generatePseudocode(declaration, bindingContext);
    data.put(declaration, pseudocode);
    for (LocalFunctionDeclarationInstruction instruction : pseudocode.getLocalDeclarations()) {
        Pseudocode localPseudocode = instruction.getBody();
        data.put(localPseudocode.getCorrespondingElement(), localPseudocode);
    }
}
Also used : LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode)

Example 3 with Pseudocode

use of org.jetbrains.kotlin.cfg.pseudocode.Pseudocode in project kotlin by JetBrains.

the class AbstractPseudocodeTest method doTest.

protected void doTest(String fileName) throws Exception {
    File file = new File(fileName);
    KtFile jetFile = KotlinTestUtils.loadJetFile(getProject(), file);
    SetMultimap<KtElement, Pseudocode> data = LinkedHashMultimap.create();
    AnalysisResult analysisResult = KotlinTestUtils.analyzeFile(jetFile, getEnvironment());
    List<KtDeclaration> declarations = jetFile.getDeclarations();
    BindingContext bindingContext = analysisResult.getBindingContext();
    for (KtDeclaration declaration : declarations) {
        addDeclaration(data, bindingContext, declaration);
        if (declaration instanceof KtDeclarationContainer) {
            for (KtDeclaration member : ((KtDeclarationContainer) declaration).getDeclarations()) {
                // Properties and initializers are processed elsewhere
                if (member instanceof KtNamedFunction || member instanceof KtSecondaryConstructor) {
                    addDeclaration(data, bindingContext, member);
                }
            }
        }
    }
    try {
        processCFData(file, data, bindingContext);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if ("true".equals(System.getProperty("kotlin.control.flow.test.dump.graphs"))) {
            CFGraphToDotFilePrinter.dumpDot(file, data.values());
        }
    }
}
Also used : IOException(java.io.IOException) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) File(java.io.File)

Example 4 with Pseudocode

use of org.jetbrains.kotlin.cfg.pseudocode.Pseudocode in project kotlin by JetBrains.

the class CFGraphToDotFilePrinter method dumpDot.

public static void dumpDot(File file, Collection<Pseudocode> pseudocodes) throws FileNotFoundException {
    File target = KotlinTestUtils.replaceExtension(file, "dot");
    PrintStream out = new PrintStream(target);
    out.println("digraph " + FileUtil.getNameWithoutExtension(file) + " {");
    int[] count = new int[1];
    Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
    for (Pseudocode pseudocode : pseudocodes) {
        dumpNodes(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName, Sets.newHashSet(pseudocode.getInstructions()));
    }
    int i = 0;
    for (Pseudocode pseudocode : pseudocodes) {
        String label;
        KtElement correspondingElement = pseudocode.getCorrespondingElement();
        if (correspondingElement instanceof KtNamedDeclaration) {
            KtNamedDeclaration namedDeclaration = (KtNamedDeclaration) correspondingElement;
            label = namedDeclaration.getName();
        } else {
            label = "anonymous_" + i;
        }
        out.println("subgraph cluster_" + i + " {\n" + "label=\"" + label + "\";\n" + "color=blue;\n");
        dumpEdges(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName);
        out.println("}");
        i++;
    }
    out.println("}");
    out.close();
}
Also used : PrintStream(java.io.PrintStream) KtElement(org.jetbrains.kotlin.psi.KtElement) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction) SubroutineSinkInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction) SubroutineExitInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction) SubroutineEnterInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction) MagicInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction) LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction) File(java.io.File)

Aggregations

Pseudocode (org.jetbrains.kotlin.cfg.pseudocode.Pseudocode)4 File (java.io.File)3 LocalFunctionDeclarationInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 Instruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction)1 MagicInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction)1 SubroutineEnterInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction)1 SubroutineExitInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction)1 SubroutineSinkInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction)1 KtElement (org.jetbrains.kotlin.psi.KtElement)1 KtNamedDeclaration (org.jetbrains.kotlin.psi.KtNamedDeclaration)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1