[Native] Replace consequence line and column calls with a single one

This commit is contained in:
Ivan Kylchik
2023-09-18 11:28:56 +02:00
committed by Space Team
parent 1be98b8f08
commit 350ee4be1d
10 changed files with 79 additions and 65 deletions
@@ -64,7 +64,8 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
} catch (e: Throwable) {
CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name) { offset ->
irFile.fileEntry.takeIf { it.supportsDebugInfo }?.let {
it.getLineNumber(offset) to it.getColumnNumber(offset)
val (line, column) = it.getLineAndColumnNumbers(offset)
line to column
}
}
}
@@ -113,7 +114,8 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
thrownFromThread.get()?.let { (e, irFile) ->
CodegenUtil.reportBackendException(e, "Experimental parallel IR backend", irFile.fileEntry.name) { offset ->
irFile.fileEntry.takeIf { it.supportsDebugInfo }?.let {
it.getLineNumber(offset) to it.getColumnNumber(offset)
val (line, column) = it.getLineAndColumnNumbers(offset)
line to column
}
}
}
@@ -12,7 +12,8 @@ import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
import org.jetbrains.kotlin.ir.backend.js.JsStatementOrigins
import org.jetbrains.kotlin.ir.backend.js.lower.*
import org.jetbrains.kotlin.ir.backend.js.lower.isBoxParameter
import org.jetbrains.kotlin.ir.backend.js.lower.isEs6ConstructorReplacement
import org.jetbrains.kotlin.ir.backend.js.sourceMapsInfo
import org.jetbrains.kotlin.ir.backend.js.utils.*
import org.jetbrains.kotlin.ir.declarations.*
@@ -618,8 +619,7 @@ inline fun IrElement.getSourceLocation(fileEntry: IrFileEntry, offsetSelector: I
if (startOffset == UNDEFINED_OFFSET || endOffset == UNDEFINED_OFFSET) return null
val path = fileEntry.name
val offset = offsetSelector()
val startLine = fileEntry.getLineNumber(offset)
val startColumn = fileEntry.getColumnNumber(offset)
val (startLine, startColumn) = fileEntry.getLineAndColumnNumbers(offset)
return JsLocation(path, startLine, startColumn)
}
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.backend.wasm.ir2wasm
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.wasm.ir.WasmExpressionBuilder
import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
@@ -15,8 +14,7 @@ fun IrElement.getSourceLocation(fileEntry: IrFileEntry?): SourceLocation {
if (fileEntry == null) return SourceLocation.NoLocation("fileEntry is null")
val path = fileEntry.name
val startLine = fileEntry.getLineNumber(startOffset)
val startColumn = fileEntry.getColumnNumber(startOffset)
val (startLine, startColumn) = fileEntry.getLineAndColumnNumbers(startOffset)
if (startLine < 0 || startColumn < 0) return SourceLocation.NoLocation("startLine or startColumn < 0")
@@ -55,14 +55,17 @@ abstract class AbstractIrFileEntry : IrFileEntry {
return LineAndColumn(lineNumber, columnNumber)
}
override fun getSourceRangeInfo(beginOffset: Int, endOffset: Int): SourceRangeInfo =
SourceRangeInfo(
override fun getSourceRangeInfo(beginOffset: Int, endOffset: Int): SourceRangeInfo {
val (startLineNumber, startColumnNumber) = getLineAndColumnNumbers(beginOffset)
val (endLineNumber, endColumnNumber) = getLineAndColumnNumbers(endOffset)
return SourceRangeInfo(
filePath = name,
startOffset = beginOffset,
startLineNumber = getLineNumber(beginOffset),
startColumnNumber = getColumnNumber(beginOffset),
startLineNumber = startLineNumber,
startColumnNumber = startColumnNumber,
endOffset = endOffset,
endLineNumber = getLineNumber(endOffset),
endColumnNumber = getColumnNumber(endOffset)
endLineNumber = endLineNumber,
endColumnNumber = endColumnNumber
)
}
}
@@ -65,12 +65,15 @@ object PartialLinkageUtils {
data class IrBased(private val file: IrFile) : File {
override val module = Module.Real(file.module.name)
override fun computeLocationForOffset(offset: Int) = PartialLinkageLogger.Location(
moduleName = module.name,
filePath = file.fileEntry.name,
lineNumber = if (offset == UNDEFINED_OFFSET) UNDEFINED_LINE_NUMBER else file.fileEntry.getLineNumber(offset) + 1, // since humans count from 1, not 0
columnNumber = if (offset == UNDEFINED_OFFSET) UNDEFINED_COLUMN_NUMBER else file.fileEntry.getColumnNumber(offset) + 1
)
override fun computeLocationForOffset(offset: Int): PartialLinkageLogger.Location {
val (line, column) = file.fileEntry.getLineAndColumnNumbers(offset)
return PartialLinkageLogger.Location(
moduleName = module.name,
filePath = file.fileEntry.name,
lineNumber = if (offset == UNDEFINED_OFFSET) UNDEFINED_LINE_NUMBER else line + 1, // since humans count from 1, not 0
columnNumber = if (offset == UNDEFINED_OFFSET) UNDEFINED_COLUMN_NUMBER else column + 1
)
}
}
class LazyIrBased(packageFragmentDescriptor: PackageFragmentDescriptor) : File {
@@ -6,16 +6,20 @@
package org.jetbrains.kotlin.backend.konan.llvm
import kotlinx.cinterop.*
import kotlinx.cinterop.cValuesOf
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.toCValues
import kotlinx.cinterop.toKString
import llvm.*
import org.jetbrains.kotlin.backend.konan.*
import org.jetbrains.kotlin.backend.konan.MemoryModel
import org.jetbrains.kotlin.backend.konan.NativeGenerationState
import org.jetbrains.kotlin.backend.konan.RuntimeNames
import org.jetbrains.kotlin.backend.konan.cgen.CBridgeOrigin
import org.jetbrains.kotlin.backend.konan.descriptors.ClassGlobalHierarchyInfo
import org.jetbrains.kotlin.backend.konan.ir.*
import org.jetbrains.kotlin.backend.konan.ir.isAny
import org.jetbrains.kotlin.backend.konan.llvm.ThreadState.Native
import org.jetbrains.kotlin.backend.konan.llvm.ThreadState.Runnable
import org.jetbrains.kotlin.backend.konan.llvm.objc.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.backend.konan.llvm.objc.ObjCDataGenerator
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.objcinterop.*
@@ -343,7 +347,8 @@ private fun CodeGenerator.getVirtualFunctionTrampolineImpl(irFunction: IrSimpleF
}
}
@Suppress("UNCHECKED_CAST") val location = diFunctionScope?.let {
LocationInfo(it as DIScopeOpaqueRef, file.fileEntry.line(offset!!), file.fileEntry.column(offset))
val (line, column) = file.fileEntry.lineAndColumn(offset!!)
LocationInfo(it as DIScopeOpaqueRef, line, column)
}
generateFunction(this, proto, needSafePoint = false, startLocation = location, endLocation = location) {
val args = proto.signature.parameterTypes.indices.map { param(it) }
@@ -259,7 +259,7 @@ internal class DebugInfo(override val generationState: NativeGenerationState) :
/**
* File entry starts offsets from zero while dwarf number lines/column starting from 1.
*/
private val NO_SOURCE_FILE = "no source file"
private const val NO_SOURCE_FILE = "no source file"
private fun IrFileEntry.location(offset: Int, offsetToNumber: (Int) -> Int): Int {
// Part "name.isEmpty() || name == NO_SOURCE_FILE" is an awful hack, @minamoto, please fix properly.
if (offset == UNDEFINED_OFFSET) return 0
@@ -270,9 +270,12 @@ private fun IrFileEntry.location(offset: Int, offsetToNumber: (Int) -> Int): Int
return result
}
internal fun IrFileEntry.line(offset: Int) = location(offset, this::getLineNumber)
internal fun IrFileEntry.lineAndColumn(offset: Int): Pair<Int, Int> {
val (line, column) = this.getLineAndColumnNumbers(offset)
return location(offset) { line } to location(offset) { column }
}
internal fun IrFileEntry.column(offset: Int) = location(offset, this::getColumnNumber)
internal fun IrFileEntry.line(offset: Int) = location(offset, this::getLineNumber)
internal data class FileAndFolder(val file: String, val folder: String) {
companion object {
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.backend.konan.llvm
import kotlinx.cinterop.*
import llvm.*
import org.jetbrains.kotlin.ir.util.inlineFunction
import org.jetbrains.kotlin.backend.common.ir.isUnconditional
import org.jetbrains.kotlin.backend.common.lower.coroutines.getOrCreateFunctionWithContinuationStub
import org.jetbrains.kotlin.backend.common.lower.inline.INLINED_FUNCTION_ARGUMENTS
@@ -28,10 +27,10 @@ import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.objcinterop.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.objcinterop.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
@@ -781,7 +780,12 @@ internal class CodeGeneratorVisitor(
}
private val fileScope = (fileScope() as? FileScope)
override fun location(offset: Int) = scope?.let { scope -> fileScope?.let{LocationInfo(scope, it.file.fileEntry.line(offset), it.file.fileEntry.column(offset)) } }
override fun location(offset: Int) = scope?.let { scope ->
fileScope?.let {
val (line, column) = it.file.fileEntry.lineAndColumn(offset)
LocationInfo(scope, line, column)
}
}
override fun scope() = scope
@@ -909,12 +913,12 @@ internal class CodeGeneratorVisitor(
verifyModule(llvm.module, "${ir2string(declaration.parent)}::${ir2string(declaration)}")
}
private fun IrFunction.location(start: Boolean) =
if (context.shouldContainLocationDebugInfo() && startOffset != UNDEFINED_OFFSET) LocationInfo(
scope = scope()!!,
line = if (start) startLine() else endLine(),
column = if (start) startColumn() else endColumn())
else null
private fun IrFunction.location(start: Boolean): LocationInfo? {
if (!context.shouldContainLocationDebugInfo() || startOffset == UNDEFINED_OFFSET) return null
val (line, column) = if (start) startLineAndColumn() else endLineAndColumn()
return LocationInfo(scope = scope()!!, line = line, column = column)
}
//-------------------------------------------------------------------------//
@@ -2115,7 +2119,8 @@ internal class CodeGeneratorVisitor(
return if (returnableBlock.inlineFunction != null) {
val diScope = functionScope ?: return null
val inlinedAt = outerContext.location(returnableBlock.startOffset) ?: return null
LocationInfo(diScope, file.fileEntry.line(offset), file.fileEntry.column(offset), inlinedAt)
val (line, column) = file.fileEntry.lineAndColumn(offset)
LocationInfo(diScope, line, column, inlinedAt)
} else {
outerContext.location(offset)
}
@@ -2128,7 +2133,8 @@ internal class CodeGeneratorVisitor(
if (!context.shouldContainLocationDebugInfo() || returnableBlock.startOffset == UNDEFINED_OFFSET)
return@lazy null
val lexicalBlockFile = DICreateLexicalBlockFile(debugInfo.builder, functionScope()!!.scope(), super.file.diFileScope())
DICreateLexicalBlock(debugInfo.builder, lexicalBlockFile, super.file.diFileScope(), returnableBlock.startLine(), returnableBlock.startColumn())!!
val (line, column) = returnableBlock.startLineAndColumn()
DICreateLexicalBlock(debugInfo.builder, lexicalBlockFile, super.file.diFileScope(), line, column)!!
}
override fun scope() = scope
@@ -2140,7 +2146,10 @@ internal class CodeGeneratorVisitor(
private open inner class FileScope(val file: IrFile) : InnerScopeImpl() {
override fun fileScope(): CodeContext? = this
override fun location(offset: Int) = scope()?.let { LocationInfo(it, file.fileEntry.line(offset), file.fileEntry.column(offset)) }
override fun location(offset: Int) = scope()?.let {
val (line, column) = file.fileEntry.lineAndColumn(offset)
LocationInfo(it, line, column)
}
@Suppress("UNCHECKED_CAST")
private val scope by lazy {
@@ -2271,13 +2280,10 @@ internal class CodeGeneratorVisitor(
private fun IrElement.startLine() = file().fileEntry.line(this.startOffset)
//-------------------------------------------------------------------------//
private fun IrElement.startColumn() = file().fileEntry.column(this.startOffset)
private fun IrElement.startLineAndColumn() = file().fileEntry.lineAndColumn(this.startOffset)
//-------------------------------------------------------------------------//
private fun IrElement.endLine() = file().fileEntry.line(this.endOffset)
//-------------------------------------------------------------------------//
private fun IrElement.endColumn() = file().fileEntry.column(this.endOffset)
private fun IrElement.endLineAndColumn() = file().fileEntry.lineAndColumn(this.endOffset)
//-------------------------------------------------------------------------//
private fun debugFieldDeclaration(expression: IrField) {
@@ -4,9 +4,8 @@
*/
package org.jetbrains.kotlin.backend.konan.llvm.coverage
import org.jetbrains.kotlin.backend.konan.llvm.column
import org.jetbrains.kotlin.backend.konan.llvm.line
import org.jetbrains.kotlin.backend.konan.llvm.computeSymbolName
import org.jetbrains.kotlin.backend.konan.llvm.lineAndColumn
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.IrFile
@@ -25,17 +24,11 @@ class Region(
val file: IrFile,
val kind: RegionKind
) {
val startLine: Int
get() = file.fileEntry.line(startOffset)
val startLineAndColumn: Pair<Int, Int>
get() = file.fileEntry.lineAndColumn(startOffset)
val startColumn: Int
get() = file.fileEntry.column(startOffset)
val endLine: Int
get() = file.fileEntry.line(endOffset)
val endColumn: Int
get() = file.fileEntry.column(endOffset)
val endLineAndColumn: Pair<Int, Int>
get() = file.fileEntry.lineAndColumn(endOffset)
companion object {
fun fromIr(irElement: IrElement, irFile: IrFile, kind: RegionKind = RegionKind.Code) =
@@ -48,7 +41,7 @@ class Region(
override fun toString(): String {
val expansion = (kind as? RegionKind.Expansion)?.let { " expand to " + it.expandedFile.name } ?: ""
return "${file.name}$expansion: ${kind::class.simpleName} $startLine, $startColumn -> $endLine, $endColumn"
return "${file.name}$expansion: ${kind::class.simpleName} $startLineAndColumn -> $endLineAndColumn"
}
}
@@ -6,9 +6,7 @@ package org.jetbrains.kotlin.backend.konan.llvm.coverage
import kotlinx.cinterop.*
import llvm.*
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.NativeGenerationState
import org.jetbrains.kotlin.backend.konan.llvm.name
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.path
import org.jetbrains.kotlin.konan.file.File
@@ -20,11 +18,14 @@ private fun RegionKind.toLLVMCoverageRegionKind(): LLVMCoverageRegionKind = when
}
private fun LLVMCoverageRegion.populateFrom(region: Region, regionId: Int, filesIndex: Map<IrFile, Int>) = apply {
val (lineStart, columnStart) = region.startLineAndColumn
val (lineEnd, columnEnd) = region.endLineAndColumn
fileId = filesIndex.getValue(region.file)
lineStart = region.startLine
columnStart = region.startColumn
lineEnd = region.endLine
columnEnd = region.endColumn
this.lineStart = lineStart
this.columnStart = columnStart
this.lineEnd = lineEnd
this.columnEnd = columnEnd
counterId = regionId
kind = region.kind.toLLVMCoverageRegionKind()
expandedFileId = if (region.kind is RegionKind.Expansion) filesIndex.getValue(region.kind.expandedFile) else 0