Fix source information mapping in PsiSourceManager

Add tests for source information mapping

KT-17108 source information corrupted on PSI -> IR transformation
This commit is contained in:
Dmitry Petrov
2017-03-28 14:40:35 +03:00
parent 4086a84622
commit 2a97fb17ba
9 changed files with 218 additions and 6 deletions
@@ -83,7 +83,7 @@ abstract class AbstractIrGeneratorTestCase : CodegenTestCase() {
protected fun generateIrModule(ignoreErrors: Boolean = false): IrModuleFragment {
assert(myFiles != null) { "myFiles not initialized" }
assert(myEnvironment != null) { "myEnvironment not initialized" }
return generateIrModule(myFiles.psiFiles, myEnvironment, ignoreErrors)
return generateIrModule(myFiles.psiFiles, myEnvironment, Psi2IrTranslator(Psi2IrConfiguration(ignoreErrors)))
}
protected fun generateIrFilesAsSingleModule(testFiles: List<TestFile>, ignoreErrors: Boolean = false): Map<TestFile, IrFile> {
@@ -109,17 +109,20 @@ abstract class AbstractIrGeneratorTestCase : CodegenTestCase() {
return textFile
}
fun generateIrModule(ktFiles: List<KtFile>, environment: KotlinCoreEnvironment, ignoreErrors: Boolean = false): IrModuleFragment {
fun generateIrModule(ktFiles: List<KtFile>, environment: KotlinCoreEnvironment, psi2ir: Psi2IrTranslator): IrModuleFragment {
val analysisResult = JvmResolveUtil.analyze(ktFiles, environment)
if (!ignoreErrors) {
if (!psi2ir.configuration.ignoreErrors) {
analysisResult.throwIfError()
AnalyzingUtils.throwExceptionOnErrors(analysisResult.bindingContext)
}
return generateIrModule(ktFiles, analysisResult.moduleDescriptor, analysisResult.bindingContext, ignoreErrors)
return generateIrModule(ktFiles, analysisResult.moduleDescriptor, analysisResult.bindingContext, psi2ir)
}
fun generateIrModule(ktFiles: List<KtFile>, moduleDescriptor: ModuleDescriptor, bindingContext: BindingContext, ignoreErrors: Boolean = false) =
Psi2IrTranslator(Psi2IrConfiguration(ignoreErrors)).generateModule(moduleDescriptor, ktFiles, bindingContext)
generateIrModule(ktFiles, moduleDescriptor, bindingContext, Psi2IrTranslator(Psi2IrConfiguration(ignoreErrors)))
fun generateIrModule(ktFiles: List<KtFile>, moduleDescriptor: ModuleDescriptor, bindingContext: BindingContext, psi2ir: Psi2IrTranslator) =
psi2ir.generateModule(moduleDescriptor, ktFiles, bindingContext)
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2010-2017 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.util.RenderIrElementVisitor
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.utils.Printer
import java.io.File
abstract class AbstractIrSourceRangesTestCase : AbstractIrGeneratorTestCase() {
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
val dir = wholeFile.parentFile
val testFileToIrFile = generateIrFilesAsSingleModule(testFiles)
for ((testFile, irFile) in testFileToIrFile) {
val irFileDump = irFile.dumpWithSourceLocations(irFile.fileEntry)
val expectedSourceLocations = File(dir, testFile.name.replace(".kt", ".txt"))
KotlinTestUtils.assertEqualsToFile(expectedSourceLocations, irFileDump)
}
}
private fun IrElement.dumpWithSourceLocations(fileEntry: SourceManager.FileEntry): String =
StringBuilder().also {
acceptVoid(DumpSourceLocations(it, fileEntry))
}.toString()
private class DumpSourceLocations(
out: Appendable,
val fileEntry: SourceManager.FileEntry
) : IrElementVisitorVoid {
val printer = Printer(out, " ")
val elementRenderer = RenderIrElementVisitor()
override fun visitElement(element: IrElement) {
val sourceRangeInfo = fileEntry.getSourceRangeInfo(element.startOffset, element.endOffset)
printer.println("@${sourceRangeInfo.render()} ${element.accept(elementRenderer, null)}")
printer.pushIndent()
element.acceptChildrenVoid(this)
printer.popIndent()
}
private fun SourceRangeInfo.render() =
if (startLineNumber == endLineNumber)
"$startLineNumber:$startColumnNumber..$endColumnNumber"
else
"$startLineNumber:$startColumnNumber..$endLineNumber:$endColumnNumber"
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2017 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.TargetBackend;
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/sourceRanges")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class IrSourceRangesTestCaseGenerated extends AbstractIrSourceRangesTestCase {
public void testAllFilesPresentInSourceRanges() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/sourceRanges"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("augmentedAssignmentWithExpression.kt")
public void testAugmentedAssignmentWithExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/sourceRanges/augmentedAssignmentWithExpression.kt");
doTest(fileName);
}
@TestMetadata("kt17108.kt")
public void testKt17108() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/sourceRanges/kt17108.kt");
doTest(fileName);
}
}