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());
}
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);
}
}
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());
}
}
}
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();
}
Aggregations