New source mapping

This commit is contained in:
Michael Bogdanov
2016-04-18 17:56:08 +03:00
parent cb615c557a
commit ff820bddc0
18 changed files with 575 additions and 18 deletions
@@ -24,6 +24,8 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.codegen.context.*;
import org.jetbrains.kotlin.codegen.inline.*;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.DefaultSourceMapper;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
import org.jetbrains.kotlin.descriptors.*;
@@ -557,7 +559,7 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
@NotNull
public SourceMapper getOrCreateSourceMapper() {
if (sourceMapper == null) {
sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()), null);
sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()));
}
return sourceMapper;
}
@@ -27,6 +27,8 @@ import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment;
import org.jetbrains.kotlin.codegen.*;
import org.jetbrains.kotlin.codegen.context.*;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.NestedSourceMapper;
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicArrayConstructorsKt;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
@@ -22,6 +22,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.codegen.ClosureCodegen;
import org.jetbrains.kotlin.codegen.StackValue;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.InlineLambdaSourceMapper;
import org.jetbrains.kotlin.codegen.inline2.NestedSourceMapper;
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods;
import org.jetbrains.kotlin.codegen.optimization.MandatoryMethodTransformer;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.inline
import gnu.trove.TIntIntHashMap
import org.jetbrains.kotlin.codegen.ClassBuilder
import org.jetbrains.kotlin.codegen.SourceInfo
import org.jetbrains.kotlin.codegen.inline2.LineNumberToMap
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.MethodVisitor
import java.util.*
@@ -121,6 +122,7 @@ open class InlineLambdaSourceMapper(
interface SourceMapper {
val resultMappings: List<FileMapping>
val parent: SourceMapper?
get() = null
open fun visitSource(name: String, path: String) {
throw UnsupportedOperationException("fail")
@@ -134,6 +136,10 @@ interface SourceMapper {
throw UnsupportedOperationException("fail")
}
open fun visitLineNumber(iv: MethodVisitor, destLineNumber: Int, start: Label, source: LineNumberToMap): Int {
throw UnsupportedOperationException("fail")
}
open fun endMapping() {
parent?.visitOrigin()
}
@@ -145,8 +151,8 @@ interface SourceMapper {
}
}
fun createFromSmap(smap: SMAP): DefaultSourceMapper {
val sourceMapper = DefaultSourceMapper(smap.sourceInfo, null)
fun createFromSmap(smap: SMAP): SourceMapper {
val sourceMapper = org.jetbrains.kotlin.codegen.inline2.DefaultSourceMapper(smap.sourceInfo)
smap.fileMappings.asSequence()
//default one mapped through sourceInfo
.filterNot { it == smap.default }
@@ -239,8 +245,6 @@ class SMAP(val fileMappings: List<FileMapping>) {
val sourceInfo: SourceInfo
init {
//val defaultMapping = default.lineMappings.single()
//temporary workaround for KT-11478 ("Couldn't inline method call" error)
val defaultMapping = default.lineMappings.first()
sourceInfo = SourceInfo(default.name, default.path, defaultMapping.source + defaultMapping.range - 1)
}
@@ -0,0 +1,213 @@
/*
* 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.codegen.inline2
import gnu.trove.TIntIntHashMap
import org.jetbrains.kotlin.codegen.SourceInfo
import org.jetbrains.kotlin.codegen.inline.FileMapping
import org.jetbrains.kotlin.codegen.inline.RangeMapping
import org.jetbrains.kotlin.codegen.inline.SMAPAndMethodNode
import org.jetbrains.kotlin.codegen.inline.SourceMapper
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.MethodVisitor
import java.util.*
open class LineNumberToMap(val lineNumberToMap: Int, val source: Int, val name: String, val path: String)
open class NestedSourceMapper(
override val parent: SourceMapper, val ranges: List<RangeMapping>, sourceInfo: SourceInfo
) : DefaultSourceMapper(sourceInfo) {
val visited = TIntIntHashMap()
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
val mappedLineNumber = visited.get(lineNumber)
if (mappedLineNumber > 0) {
iv.visitLineNumber(mappedLineNumber, start)
} else {
val findMappingIfExists = findMappingIfExists(lineNumber)!!
val sourceLineNumber = findMappingIfExists.map(lineNumber)
val visitLineNumber = parent.visitLineNumber(iv, lineNumber, start, LineNumberToMap(lineNumber, sourceLineNumber, findMappingIfExists.parent!!.name, findMappingIfExists.parent!!.path))
if (visitLineNumber > 0) {
visited.put(lineNumber, visitLineNumber)
}
}
}
fun findMappingIfExists(lineNumber: Int): RangeMapping? {
val index = ranges.binarySearch(RangeMapping(lineNumber, lineNumber, 1), Comparator {
value, key ->
if (key.dest in value) 0 else RangeMapping.Comparator.compare(value, key)
})
return if (index < 0) null else ranges[index];
}
}
open class InlineLambdaSourceMapper(
parent: SourceMapper, smap: SMAPAndMethodNode
) : NestedSourceMapper(parent, smap.ranges, smap.classSMAP.sourceInfo) {
init {
assert(smap.ranges.isNotEmpty()) {
"Mapping ragnes should be present in inlined lambda: ${smap.node}"
}
}
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
val mapping = findMappingIfExists(lineNumber)!!
if (mapping == ranges.firstOrNull()) { //TODO rewrite
//don't remap origin lambda line numbers
iv.visitLineNumber(lineNumber, start)
}
else {
super.visitLineNumber(iv, lineNumber, start)
}
}
}
open class DefaultSourceMapper(val sourceInfo: SourceInfo): SourceMapper {
protected var maxUsedValue: Int = sourceInfo.linesInFile
var lastVisited: RawFileMapping? = null
private var lastMappedWithChanges: RawFileMapping? = null
private var fileMappings: LinkedHashMap<String, RawFileMapping> = linkedMapOf()
protected val origin: RawFileMapping
init {
val name = sourceInfo.source
val path = sourceInfo.pathOrCleanFQN
origin = RawFileMapping(name, path)
origin.initRange(1, maxUsedValue)
fileMappings.put(createKey(name, path), origin)
lastVisited = origin
}
private fun createKey(name: String, path: String) = "$name#$path"
override val resultMappings: List<FileMapping>
get() = fileMappings.values.map { it.toFileMapping() }
override fun visitSource(name: String, path: String) {
lastVisited = fileMappings.getOrPut(createKey(name, path)) { RawFileMapping(name, path) }
}
override fun visitOrigin() {
lastVisited = origin
}
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
if (lineNumber < 0) {
//no source information, so just skip this linenumber
return
}
//TODO add assertion that mapping exists
//val sourceLineNumber = createMapping(lineNumberToMap)
val sourceLineNumber = lineNumber
assert(lineNumber == sourceLineNumber)
iv.visitLineNumber(lineNumber, start)
}
override fun visitLineNumber(iv: MethodVisitor, destLineNumber: Int, start: Label, source: LineNumberToMap): Int {
if (source.source < 0) {
//no source information, so just skip this linenumber
return -1
}
visitSource(source.name, source.path)
val mappedLineIndex = createMapping(source.source)
iv.visitLineNumber(mappedLineIndex, start)
return mappedLineIndex
}
protected fun createMapping(lineNumber: Int): Int {
val fileMapping = lastVisited!!
//val mappedLineIndex = fileMapping.mapLine(lineNumber, maxUsedValue, lastMappedWithChanges == lastVisited)
val mappedLineIndex = fileMapping.mapNewLineNumber(lineNumber, maxUsedValue, lastMappedWithChanges == lastVisited)
if (mappedLineIndex > maxUsedValue) {
lastMappedWithChanges = fileMapping
maxUsedValue = mappedLineIndex
}
return mappedLineIndex
}
}
class RawFileMapping(val name: String, val path: String) {
private val lineMappings = TIntIntHashMap()
private val rangeMappings = arrayListOf<RangeMapping>()
private var lastMappedWithNewIndex = -1000
fun toFileMapping() =
FileMapping(name, path).apply {
for (range in rangeMappings) {
addRangeMapping(range)
}
}
fun initRange(start: Int, end: Int) {
assert(lineMappings.isEmpty) { "initRange should only be called for empty mapping" }
for (index in start..end) {
lineMappings.put(index, index)
}
rangeMappings.add(RangeMapping(start, start, end - start + 1))
lastMappedWithNewIndex = end
}
fun mapLine(source: Int, currentIndex: Int, isLastMapped: Boolean): Int {
var dest = lineMappings[source]
if (dest == 0) { // line numbers are 1-based, so 0 is ok to indicate missing value
dest = mapNewLineNumber(source, currentIndex, isLastMapped)
}
return dest
}
fun mapNewLineNumber(source: Int, currentIndex: Int, isLastMapped: Boolean): Int {
val dest: Int
val rangeMapping: RangeMapping
if (rangeMappings.isNotEmpty() && isLastMapped && couldFoldInRange(lastMappedWithNewIndex, source)) {
rangeMapping = rangeMappings.last()
rangeMapping.range += source - lastMappedWithNewIndex
dest = lineMappings[lastMappedWithNewIndex] + source - lastMappedWithNewIndex
}
else {
dest = currentIndex + 1
rangeMapping = RangeMapping(source, dest)
rangeMappings.add(rangeMapping)
}
lineMappings.put(source, dest)
lastMappedWithNewIndex = source
return dest
}
fun mapNewInterval(source: Int, dest: Int, range: Int) {
val rangeMapping = RangeMapping(source, dest, range)
rangeMappings.add(rangeMapping)
(source..(source + range - 1)).forEach {
lineMappings.put(source, dest)
}
}
private fun couldFoldInRange(first: Int, second: Int): Boolean {
//TODO
val delta = second - first
return delta > 0 && delta <= 10
}
}
@@ -35,7 +35,7 @@ private fun createLineNumberSequence(node: MethodNode, classSMAP: SMAP): Sequenc
if (key.dest in value) 0 else RangeMapping.Comparator.compare(value, key)
})
if (index < 0) {
error("Unmapped label in inlined function $node ${lineNumber.line}")
error("Unmapped line number in inlined function ${node.name}:${lineNumber.line}")
}
LabelAndMapping(lineNumber, classSMAP.intervals[index])
}
@@ -33,7 +33,10 @@ fun box(): String {
// FILE: 2.smap
//TODO SHOULD BE LESS
//TODO
//7#1,3:26
//10#1,6:30 - could be merged in one big interval due preprocessing of inline function
SMAP
2.kt
Kotlin
@@ -45,7 +48,10 @@ _2Kt
builders/_1Kt
*L
1#1,24:1
7#1,3:26
10#1,6:30
6#2:25
6#2:29
*E
SMAP
@@ -35,7 +35,9 @@ fun box(): String {
// FILE: 2.smap
//TODO SHOULD BE LESS
//7#1,3:28
//10#1,8:32 - could be merged in one big interval due preprocessing of inline function
SMAP
2.kt
Kotlin
@@ -47,7 +49,10 @@ _2Kt
builders/_1Kt
*L
1#1,26:1
7#1,3:28
10#1,8:32
6#2:27
6#2:31
*E
SMAP
@@ -32,7 +32,6 @@ inline fun test(z: () -> Unit) {
// FILE: 2.smap
//PROBLEM of KT-11478 in additional line mapping for default source (so 'single' was replaces with 'first' in SMAP class init):
//*L
//1#1,15:1
//17#1:19
@@ -66,11 +65,7 @@ test/_1Kt
_2Kt
*L
1#1,17:1
21#1:23
13#2,2:18
5#2:18
18#2:19
9#2:20
19#2,2:20
7#3:22
7#3:21
*E
+3 -2
View File
@@ -36,7 +36,7 @@ fun box(): String {
}
// FILE: 1.smap
//TODO maybe do smth with default method body mapping
SMAP
1.kt
Kotlin
@@ -46,6 +46,7 @@ Kotlin
test/_1Kt
*L
1#1,25:1
18#1,6:26
*E
// FILE: 2.smap
@@ -63,4 +64,4 @@ test/_1Kt
1#1,14:1
17#2,7:15
8#2,7:22
*E
*E
@@ -0,0 +1,46 @@
// FILE: 1.kt
package test
inline fun test(s: () -> Unit) {
val z = 1;
s()
val x = 1;
}
// FILE: 2.kt
import test.*
fun box(): String {
var result = "fail"
test {
result = "O"
}
test {
result += "K"
}
return result
}
// FILE: 1.smap
// FILE: 2.smap
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt
+ 2 1.kt
test/_1Kt
*L
1#1,18:1
6#2,4:19
6#2,4:23
*E
@@ -0,0 +1,79 @@
// FILE: 1.kt
package test
inline fun myrun(s: () -> Unit) {
val z = "myrun"
s()
}
inline fun test(crossinline s: () -> Unit) {
{
val z = 1;
myrun(s)
val x = 1;
}()
}
// FILE: 2.kt
import test.*
fun box(): String {
var result = "fail"
test {
result = "OK"
}
return result
}
// FILE: 1.smap
SMAP
1.kt
Kotlin
*S Kotlin
*F
+ 1 1.kt
test/_1Kt$test$1
+ 2 1.kt
test/_1Kt
*L
1#1,18:1
6#2,3:19
*E
// FILE: 2.smap
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt
+ 2 1.kt
test/_1Kt
*L
1#1,16:1
11#2,6:17
*E
SMAP
1.kt
Kotlin
*S Kotlin
*F
+ 1 1.kt
test/_1Kt$test$1
+ 2 1.kt
test/_1Kt
+ 3 2.kt
_2Kt
*L
1#1,18:1
6#2,3:19
9#3,2:22
*E
@@ -0,0 +1,66 @@
// FILE: 1.kt
package test
inline fun test(s: () -> Unit) {
val z = 1;
s()
val x = 1;
}
inline fun test2(s: () -> String): String {
val z = 1;
val res = s()
return res
}
// FILE: 2.kt
import test.*
fun box(): String {
var result = "fail"
test {
{
result = test2 {
"OK"
}
}()
}
return result
}
// FILE: 1.smap
// FILE: 2.smap
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt
+ 2 1.kt
test/_1Kt
*L
1#1,20:1
6#2,4:21
*E
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt$box$1$1
+ 2 1.kt
test/_1Kt
*L
1#1,20:1
12#2,3:21
*E
@@ -0,0 +1,66 @@
// FILE: 1.kt
package test
inline fun test(s: () -> Unit) {
val z = 1;
s()
val x = 1;
}
// FILE: 2.kt
import test.*
inline fun test2(s: () -> String): String {
val z = 1;
val res = s()
return res
}
fun box(): String {
var result = "fail"
test {
{
result = test2 {
"OK"
}
}()
}
return result
}
// FILE: 1.smap
// FILE: 2.smap
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt
+ 2 1.kt
test/_1Kt
*L
1#1,26:1
6#2,4:27
*E
SMAP
2.kt
Kotlin
*S Kotlin
*F
+ 1 2.kt
_2Kt$box$1$1
+ 2 2.kt
_2Kt
*L
1#1,26:1
6#2,3:27
*E
+1
View File
@@ -32,4 +32,5 @@ Kotlin
_2Kt
*L
1#1,15:1
10#1,3:16
*E
+4 -2
View File
@@ -11,7 +11,7 @@ inline fun initTag2(init: () -> Unit) {
init()
}
//{val p = initTag2(init); return p} to remove difference in linenumber processing through MethodNode and MethodVisitor should be: = initTag2(init)
inline fun head(init: () -> Unit) {val p = initTag2(init); return p}
inline fun head(init: () -> Unit) { val p = initTag2(init); return p}
inline fun html(init: () -> Unit) {
@@ -44,7 +44,6 @@ fun box(): String {
// FILE: 1.smap
//TODO SHOULD BE EMPTY
SMAP
1.kt
Kotlin
@@ -54,10 +53,13 @@ Kotlin
builders/_1Kt
*L
1#1,21:1
10#1,3:22
6#1,2:25
*E
// FILE: 2.smap
SMAP
2.kt
Kotlin
@@ -1930,6 +1930,39 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
}
}
@TestMetadata("compiler/testData/codegen/boxInline/smap/newsmap")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Newsmap extends AbstractBlackBoxInlineCodegenTest {
public void testAllFilesPresentInNewsmap() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/smap/newsmap"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("differentMapping.kt")
public void testDifferentMapping() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/differentMapping.kt");
doTest(fileName);
}
@TestMetadata("mappingInInlineFunLambda.kt")
public void testMappingInInlineFunLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInInlineFunLambda.kt");
doTest(fileName);
}
@TestMetadata("mappingInSubInlineLambda.kt")
public void testMappingInSubInlineLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInSubInlineLambda.kt");
doTest(fileName);
}
@TestMetadata("mappingInSubInlineLambdaSameFileInline.kt")
public void testMappingInSubInlineLambdaSameFileInline() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInSubInlineLambdaSameFileInline.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/boxInline/smap/resolve")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1930,6 +1930,39 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
}
}
@TestMetadata("compiler/testData/codegen/boxInline/smap/newsmap")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Newsmap extends AbstractCompileKotlinAgainstInlineKotlinTest {
public void testAllFilesPresentInNewsmap() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/smap/newsmap"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("differentMapping.kt")
public void testDifferentMapping() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/differentMapping.kt");
doTest(fileName);
}
@TestMetadata("mappingInInlineFunLambda.kt")
public void testMappingInInlineFunLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInInlineFunLambda.kt");
doTest(fileName);
}
@TestMetadata("mappingInSubInlineLambda.kt")
public void testMappingInSubInlineLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInSubInlineLambda.kt");
doTest(fileName);
}
@TestMetadata("mappingInSubInlineLambdaSameFileInline.kt")
public void testMappingInSubInlineLambdaSameFileInline() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/smap/newsmap/mappingInSubInlineLambdaSameFileInline.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/codegen/boxInline/smap/resolve")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)