[K/N] Improve the exception message in code generator exceptions
^KT-62335
This commit is contained in:
committed by
Space Team
parent
7bdcaff89a
commit
a561a8e531
+43
@@ -41,6 +41,35 @@ import org.jetbrains.kotlin.library.KotlinLibrary
|
|||||||
import org.jetbrains.kotlin.library.uniqueName
|
import org.jetbrains.kotlin.library.uniqueName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
|
internal class NativeCodeGeneratorException(val declarations: List<IrElement>, cause: Throwable?): IllegalStateException(cause) {
|
||||||
|
override val message: String
|
||||||
|
get() = try {
|
||||||
|
buildString {
|
||||||
|
appendLine("Exception during generating code for following declaration:")
|
||||||
|
declarations.dropLast(1).forEach {
|
||||||
|
append("Inside: ")
|
||||||
|
appendLine(it.render())
|
||||||
|
}
|
||||||
|
declarations.lastOrNull()?.let {
|
||||||
|
appendLine(it.dumpKotlinLike())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) { // shouldn't happen, but if it somehow does, it would be better to have at least a cause printed
|
||||||
|
"Exception during code generation"
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun wrap(e: Exception, element: IrElement?) = if (e is NativeCodeGeneratorException) {
|
||||||
|
if (element == null || e.declarations.firstOrNull() === element)
|
||||||
|
e
|
||||||
|
else
|
||||||
|
NativeCodeGeneratorException(listOfNotNull(element) + e.declarations, e.cause)
|
||||||
|
} else {
|
||||||
|
NativeCodeGeneratorException(listOfNotNull(element), e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal enum class FieldStorageKind {
|
internal enum class FieldStorageKind {
|
||||||
GLOBAL, // In the old memory model these are only accessible from the "main" thread.
|
GLOBAL, // In the old memory model these are only accessible from the "main" thread.
|
||||||
SHARED_FROZEN,
|
SHARED_FROZEN,
|
||||||
@@ -204,6 +233,11 @@ private interface CodeContext {
|
|||||||
* Called, when context is removed from stack
|
* Called, when context is removed from stack
|
||||||
*/
|
*/
|
||||||
fun onExit() {}
|
fun onExit() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called, when exception is caught in this block. Result expception would be rethrown instead.
|
||||||
|
*/
|
||||||
|
fun wrapException(e: Exception) : Exception
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
@@ -291,6 +325,8 @@ internal class CodeGeneratorVisitor(
|
|||||||
override fun location(offset: Int): LocationInfo? = unsupported()
|
override fun location(offset: Int): LocationInfo? = unsupported()
|
||||||
|
|
||||||
override fun scope(): DIScopeOpaqueRef? = unsupported()
|
override fun scope(): DIScopeOpaqueRef? = unsupported()
|
||||||
|
|
||||||
|
override fun wrapException(e: Exception) = e
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -313,6 +349,8 @@ internal class CodeGeneratorVisitor(
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return block()
|
return block()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw (codeContext?.wrapException(e) ?: e)
|
||||||
} finally {
|
} finally {
|
||||||
codeContext?.onExit()
|
codeContext?.onExit()
|
||||||
currentCodeContext = oldCodeContext
|
currentCodeContext = oldCodeContext
|
||||||
@@ -745,6 +783,8 @@ internal class CodeGeneratorVisitor(
|
|||||||
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{LocationInfo(scope, it.file.fileEntry.line(offset), it.file.fileEntry.column(offset)) } }
|
||||||
|
|
||||||
override fun scope() = scope
|
override fun scope() = scope
|
||||||
|
|
||||||
|
override fun wrapException(e: Exception) = NativeCodeGeneratorException.wrap(e, declaration)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val functionGenerationContext
|
private val functionGenerationContext
|
||||||
@@ -2112,6 +2152,8 @@ internal class CodeGeneratorVisitor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun scope() = scope
|
override fun scope() = scope
|
||||||
|
|
||||||
|
override fun wrapException(e: Exception) = NativeCodeGeneratorException.wrap(e, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
@@ -2126,6 +2168,7 @@ internal class CodeGeneratorVisitor(
|
|||||||
debugInfo.objHeaderPointerType
|
debugInfo.objHeaderPointerType
|
||||||
else null
|
else null
|
||||||
override fun classScope(): CodeContext? = this
|
override fun classScope(): CodeContext? = this
|
||||||
|
override fun wrapException(e: Exception) = NativeCodeGeneratorException.wrap(e, clazz)
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|||||||
Reference in New Issue
Block a user