added DataFlowInfoTest

This commit is contained in:
Svetlana Isakova
2014-03-05 16:27:38 +04:00
parent ab9e470ea9
commit 88f2c32724
16 changed files with 543 additions and 7 deletions
@@ -16,21 +16,24 @@
package org.jetbrains.jet.cfg;
import com.google.common.collect.Sets;
import kotlin.Function1;
import kotlin.Function3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.cfg.pseudocode.Instruction;
import org.jetbrains.jet.lang.cfg.pseudocode.InstructionImpl;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.*;
public abstract class AbstractControlFlowTest extends AbstractPseudocodeTest {
@Override
protected void dumpInstructions(PseudocodeImpl pseudocode, @NotNull StringBuilder out) {
protected void dumpInstructions(
@NotNull PseudocodeImpl pseudocode,
@NotNull StringBuilder out,
@NotNull BindingContext bindingContext
) {
final int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(pseudocode.getAllInstructions());
dumpInstructions(pseudocode, out, new Function3<Instruction, Instruction, Instruction, String>() {
@@ -0,0 +1,110 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cfg;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.Function3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.cfg.PseudocodeVariablesData;
import org.jetbrains.jet.lang.cfg.pseudocode.Instruction;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.Edges;
import static org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableInitState;
import static org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableUseState;
public abstract class AbstractDataFlowTest extends AbstractPseudocodeTest {
@Override
public void dumpInstructions(
@NotNull PseudocodeImpl pseudocode,
@NotNull StringBuilder out,
@NotNull BindingContext bindingContext
) {
PseudocodeVariablesData pseudocodeVariablesData = new PseudocodeVariablesData(pseudocode, bindingContext);
final Map<Instruction, Edges<Map<VariableDescriptor, VariableInitState>>> variableInitializers =
pseudocodeVariablesData.getVariableInitializers();
final Map<Instruction, Edges<Map<VariableDescriptor, VariableUseState>>> useStatusData =
pseudocodeVariablesData.getVariableUseStatusData();
final String initPrefix = " INIT:";
final String usePrefix = " USE:";
final int initializersColumnWidth = countDataColumnWidth(initPrefix, pseudocode.getAllInstructions(), variableInitializers);
dumpInstructions(pseudocode, out, new Function3<Instruction, Instruction, Instruction, String>() {
@Override
public String invoke(Instruction instruction, Instruction next, Instruction prev) {
StringBuilder result = new StringBuilder();
Edges<Map<VariableDescriptor, VariableInitState>> initializersEdges = variableInitializers.get(instruction);
Edges<Map<VariableDescriptor, VariableInitState>> previousInitializersEdges = variableInitializers.get(prev);
String initializersData = "";
if (initializersEdges != null && !initializersEdges.equals(previousInitializersEdges)) {
initializersData = dumpEdgesData(initPrefix, initializersEdges);
}
result.append(String.format("%1$-" + initializersColumnWidth + "s", initializersData));
Edges<Map<VariableDescriptor, VariableUseState>> useStatusEdges = useStatusData.get(instruction);
Edges<Map<VariableDescriptor, VariableUseState>> nextUseStatusEdges = useStatusData.get(next);
if (useStatusEdges != null && !useStatusEdges.equals(nextUseStatusEdges)) {
result.append(dumpEdgesData(usePrefix, useStatusEdges));
}
return result.toString();
}
});
}
private <D> int countDataColumnWidth(
@NotNull String prefix,
@NotNull List<Instruction> instructions,
@NotNull Map<Instruction, Edges<Map<VariableDescriptor, VariableInitState>>> data
) {
int maxWidth = 0;
for (Instruction instruction : instructions) {
Edges<Map<VariableDescriptor, VariableInitState>> edges = data.get(instruction);
if (edges == null) continue;
int length = dumpEdgesData(prefix, edges).length();
if (maxWidth < length) {
maxWidth = length;
}
}
return maxWidth;
}
@NotNull
private <D> String dumpEdgesData(String prefix, @NotNull Edges<Map<VariableDescriptor, D>> edges) {
return prefix +
" in: " + renderVariableMap(edges.in) +
" out: " + renderVariableMap(edges.out);
}
private <D> String renderVariableMap(Map<VariableDescriptor, D> map) {
List<String> result = Lists.newArrayList();
for (Map.Entry<VariableDescriptor, D> entry : map.entrySet()) {
VariableDescriptor variable = entry.getKey();
D data = entry.getValue();
result.add(variable.getName() + "=" + data);
}
Collections.sort(result);
return "{" + StringUtil.join(result, ", ") + "}";
}
}
@@ -65,7 +65,7 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
}
try {
processCFData(file, data);
processCFData(file, data, bindingContext);
}
catch (IOException e) {
throw new RuntimeException(e);
@@ -86,7 +86,7 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
}
}
private void processCFData(File file, Map<JetElement, Pseudocode> data) throws IOException {
private void processCFData(File file, Map<JetElement, Pseudocode> data, BindingContext bindingContext) throws IOException {
Collection<Pseudocode> pseudocodes = data.values();
StringBuilder instructionDump = new StringBuilder();
@@ -113,7 +113,7 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
instructionDump.append(correspondingElement.getText());
instructionDump.append("\n---------------------\n");
dumpInstructions((PseudocodeImpl) pseudocode, instructionDump);
dumpInstructions((PseudocodeImpl) pseudocode, instructionDump, bindingContext);
instructionDump.append("=====================\n");
checkPseudocode((PseudocodeImpl) pseudocode);
}
@@ -142,7 +142,11 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
return sb.toString();
}
protected abstract void dumpInstructions(PseudocodeImpl pseudocode, @NotNull StringBuilder out);
protected abstract void dumpInstructions(
@NotNull PseudocodeImpl pseudocode,
@NotNull StringBuilder out,
@NotNull BindingContext bindingContext
);
protected void dumpInstructions(
@NotNull PseudocodeImpl pseudocode,
@@ -0,0 +1,79 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.cfg;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.cfg.AbstractDataFlowTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/cfg-variables")
@InnerTestClasses({DataFlowTestGenerated.Basic.class})
public class DataFlowTestGenerated extends AbstractDataFlowTest {
public void testAllFilesPresentInCfg_variables() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/cfg-variables"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("compiler/testData/cfg-variables/basic")
public static class Basic extends AbstractDataFlowTest {
public void testAllFilesPresentInBasic() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/cfg-variables/basic"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("IfWithUninitialized.kt")
public void testIfWithUninitialized() throws Exception {
doTest("compiler/testData/cfg-variables/basic/IfWithUninitialized.kt");
}
@TestMetadata("InitializedNotDeclared.kt")
public void testInitializedNotDeclared() throws Exception {
doTest("compiler/testData/cfg-variables/basic/InitializedNotDeclared.kt");
}
@TestMetadata("UsageInFunctionLiteral.kt")
public void testUsageInFunctionLiteral() throws Exception {
doTest("compiler/testData/cfg-variables/basic/UsageInFunctionLiteral.kt");
}
@TestMetadata("VariablesInitialization.kt")
public void testVariablesInitialization() throws Exception {
doTest("compiler/testData/cfg-variables/basic/VariablesInitialization.kt");
}
@TestMetadata("VariablesUsage.kt")
public void testVariablesUsage() throws Exception {
doTest("compiler/testData/cfg-variables/basic/VariablesUsage.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("DataFlowTestGenerated");
suite.addTestSuite(DataFlowTestGenerated.class);
suite.addTestSuite(Basic.class);
return suite;
}
}