IR to CFG test generator, sequential function tests

This commit is contained in:
Mikhail Glukhikh
2016-09-20 16:01:43 +03:00
committed by Dmitry Petrov
parent 65db7aa1ba
commit eaf10a4675
17 changed files with 241 additions and 6 deletions
@@ -45,7 +45,7 @@ class FunctionGenerator(val function: IrFunction) {
if (data) {
builder.add(declaration)
}
val result = declaration.body?.process()
val result = declaration.body?.process() ?: if (data) declaration else null
if (result != null && !result.isNothing()) {
builder.jump(exit, from = result)
}
@@ -63,7 +63,7 @@ class FunctionGenerator(val function: IrFunction) {
private fun IrElement?.isNothing() = this is IrExpression && KotlinBuiltIns.isNothing(type)
override fun visitBlockBody(body: IrBlockBody, data: Boolean): IrElement? {
return body.process() ?: body
return body.process()
}
override fun visitBlock(expression: IrBlock, data: Boolean): IrElement? {
+1
View File
@@ -0,0 +1 @@
fun foo(arg: Int) = arg
+14
View File
@@ -0,0 +1,14 @@
// FILE: /expressionFun.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(arg: kotlin.Int): kotlin.Int
2 GET_VAR 'value-parameter arg: Int' type=kotlin.Int origin=null
3 RETURN type=kotlin.Nothing from='foo(Int): Int'
OUTGOING -> NONE
Function exit: FUN public fun foo(arg: kotlin.Int): kotlin.Int
// END FUN: foo
// END FILE: /expressionFun.kt
+1
View File
@@ -0,0 +1 @@
fun foo() = Unit
+14
View File
@@ -0,0 +1,14 @@
// FILE: /expressionUnit.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(): kotlin.Unit
2 GET_OBJECT 'Unit' type=kotlin.Unit
3 RETURN type=kotlin.Nothing from='foo(): Unit'
OUTGOING -> NONE
Function exit: FUN public fun foo(): kotlin.Unit
// END FUN: foo
// END FILE: /expressionUnit.kt
+3
View File
@@ -0,0 +1,3 @@
fun foo() {
return Unit
}
+14
View File
@@ -0,0 +1,14 @@
// FILE: /returnUnit.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(): kotlin.Unit
2 GET_OBJECT 'Unit' type=kotlin.Unit
3 RETURN type=kotlin.Nothing from='foo(): Unit'
OUTGOING -> NONE
Function exit: FUN public fun foo(): kotlin.Unit
// END FUN: foo
// END FILE: /returnUnit.kt
+4
View File
@@ -0,0 +1,4 @@
fun foo(arg: Int): Int {
val dbl = arg * 2
return dbl
}
+16
View File
@@ -0,0 +1,16 @@
// FILE: /sequentialFun.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(arg: kotlin.Int): kotlin.Int
2 CALL 'times(Int): Int' type=kotlin.Int origin=MUL
3 VAR val dbl: kotlin.Int
4 GET_VAR 'dbl: Int' type=kotlin.Int origin=null
5 RETURN type=kotlin.Nothing from='foo(Int): Int'
OUTGOING -> NONE
Function exit: FUN public fun foo(arg: kotlin.Int): kotlin.Int
// END FUN: foo
// END FILE: /sequentialFun.kt
+1
View File
@@ -0,0 +1 @@
fun foo() {}
+12
View File
@@ -0,0 +1,12 @@
// FILE: /simpleFun.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(): kotlin.Unit
OUTGOING -> NONE
Function exit: FUN public fun foo(): kotlin.Unit
// END FUN: foo
// END FILE: /simpleFun.kt
+3
View File
@@ -0,0 +1,3 @@
fun foo() {
return
}
+14
View File
@@ -0,0 +1,14 @@
// FILE: /simpleReturn.kt
// FUN: foo
BB 0
CONTENT
1 FUN public fun foo(): kotlin.Unit
2 GET_OBJECT 'Unit' type=kotlin.Unit
3 RETURN type=kotlin.Nothing from='foo(): Unit'
OUTGOING -> NONE
Function exit: FUN public fun foo(): kotlin.Unit
// END FUN: foo
// END FILE: /simpleReturn.kt
+1
View File
@@ -71,5 +71,6 @@
<orderEntry type="module" module-name="ir.tree" scope="TEST" />
<orderEntry type="module" module-name="backend.common" scope="TEST" />
<orderEntry type="module" module-name="ir.psi2ir" scope="TEST" />
<orderEntry type="module" module-name="ir.ir2cfg" scope="TEST" />
</component>
</module>
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2016 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.kotlin.ir
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.name
import org.jetbrains.kotlin.ir2cfg.generators.FunctionGenerator
import org.jetbrains.kotlin.ir2cfg.util.dump
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
abstract class AbstractIrCfgTestCase : AbstractIrGeneratorTestCase() {
private val IrFunction.name: String get() = this.descriptor.name.asString()
private fun IrFile.cfgDump(): String {
val builder = StringBuilder()
for (declaration in this.declarations) {
if (declaration is IrFunction) {
builder.appendln("// FUN: ${declaration.name}")
val cfg = FunctionGenerator(declaration).generate()
builder.appendln(cfg.dump())
builder.appendln("// END FUN: ${declaration.name}")
}
}
return builder.toString()
}
private fun IrModuleFragment.cfgDump(): String {
val builder = StringBuilder()
for (file in this.files) {
builder.appendln("// FILE: ${file.name}")
builder.appendln(file.cfgDump())
builder.appendln("// END FILE: ${file.name}")
builder.appendln()
}
return builder.toString()
}
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
val irModule = generateIrModule(false)
val irModuleDump = irModule.cfgDump()
val expectedPath = wholeFile.canonicalPath.replace(".kt", ".txt")
val expectedFile = File(expectedPath)
KotlinTestUtils.assertEqualsToFile(expectedFile, irModuleDump)
}
}
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2016 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.kotlin.ir;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/ir/irCfg")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class IrCfgTestCaseGenerated extends AbstractIrCfgTestCase {
public void testAllFilesPresentInIrCfg() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irCfg"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("expressionFun.kt")
public void testExpressionFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/expressionFun.kt");
doTest(fileName);
}
@TestMetadata("expressionUnit.kt")
public void testExpressionUnit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/expressionUnit.kt");
doTest(fileName);
}
@TestMetadata("returnUnit.kt")
public void testReturnUnit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/returnUnit.kt");
doTest(fileName);
}
@TestMetadata("sequentialFun.kt")
public void testSequentialFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/sequentialFun.kt");
doTest(fileName);
}
@TestMetadata("simpleFun.kt")
public void testSimpleFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/simpleFun.kt");
doTest(fileName);
}
@TestMetadata("simpleReturn.kt")
public void testSimpleReturn() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/simpleReturn.kt");
doTest(fileName);
}
}
@@ -122,10 +122,7 @@ import org.jetbrains.kotlin.idea.stubs.AbstractMultiFileHighlightingTest
import org.jetbrains.kotlin.idea.stubs.AbstractResolveByStubTest
import org.jetbrains.kotlin.idea.stubs.AbstractStubBuilderTest
import org.jetbrains.kotlin.integration.AbstractAntTaskTest
import org.jetbrains.kotlin.ir.AbstractClosureAnnotatorTestCase
import org.jetbrains.kotlin.ir.AbstractIrTextTestCase
import org.jetbrains.kotlin.ir.AbstractPsi2IrBoxTestCase
import org.jetbrains.kotlin.ir.AbstractPsi2IrDiagnosticsTest
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterForWebDemoTest
import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterMultiFileTest
import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterSingleFileTest
@@ -252,6 +249,10 @@ fun main(args: Array<String>) {
model("ir/irText")
}
testClass<AbstractIrCfgTestCase>() {
model("ir/irCfg")
}
// Uncomment the following lines to generate IR generator tests based on existing compiler tests.
//
// testClass<AbstractPsi2IrBoxTestCase>() {