[FIR] add utilities to FIR compiler to throw error with attachment

This commit is contained in:
Ilya Kirillov
2023-06-30 18:17:50 +02:00
committed by Space Team
parent c5014d4765
commit a9cd881a07
20 changed files with 127 additions and 69 deletions
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.utils.exceptions
import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirElementWithResolveState
import org.jetbrains.kotlin.fir.FirModuleData
import org.jetbrains.kotlin.fir.renderer.*
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassifierSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.utils.exceptions.ExceptionAttachmentBuilder
fun ExceptionAttachmentBuilder.withFirEntry(name: String, fir: FirElement) {
withEntry(name, fir) {
FirRenderer(
resolvePhaseRenderer = FirResolvePhaseRenderer(),
declarationRenderer = FirDeclarationRendererWithAttributes(),
fileAnnotationsContainerRenderer = FirFileAnnotationsContainerRenderer(),
).renderElementAsString(it)
}
withEntry("${name}ElementKind", fir.source?.kind?.let { it::class.simpleName })
if (fir is FirElementWithResolveState) {
withModuleDataEntry("${name}ModuleData", fir.moduleData)
}
withSourceEntry("${name}Source", fir.source)
}
fun ExceptionAttachmentBuilder.withFirSymbolIdEntry(name: String, symbol: FirBasedSymbol<*>?) {
when (symbol) {
is FirClassifierSymbol -> withFirLookupTagEntry(name, symbol.toLookupTag())
is FirCallableSymbol -> withEntry(name, symbol.callableId.toString())
else -> withEntry(name, symbol.toString())
}
}
fun ExceptionAttachmentBuilder.withFirLookupTagEntry(name: String, lookupTag: ConeClassifierLookupTag?) {
withEntry(name, lookupTag) { tag ->
when (tag) {
is ConeClassLikeLookupTag -> tag.classId.asString()
else -> tag.name.asString()
}
}
}
fun ExceptionAttachmentBuilder.withSourceEntry(name: String, source: KtSourceElement?) {
withEntry(name, source) { it.getElementTextInContextForDebug() }
}
fun ExceptionAttachmentBuilder.withModuleDataEntry(name: String, moduleData: FirModuleData?) {
withEntry(name, moduleData) { module ->
buildString {
append("Name: ${module.name}, ")
append("Platform: ${module.platform}")
}
}
}
fun ExceptionAttachmentBuilder.withFirSymbolEntry(name: String, symbol: FirBasedSymbol<*>) {
withFirEntry("${name}Fir", symbol.fir)
}
fun ExceptionAttachmentBuilder.withConeTypeEntry(name: String, coneType: ConeKotlinType?) {
withEntry(name, coneType) {
buildString { ConeTypeRendererForDebugging(this).render(it) }
}
}
@@ -14,6 +14,7 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.tree.IElementType
import com.intellij.util.diff.FlyweightCapableTreeStructure
import org.jetbrains.kotlin.utils.getElementTextWithContext
sealed class KtSourceElementKind {
abstract val shouldSkipErrorTypeReporting: Boolean
@@ -309,6 +310,8 @@ sealed class KtSourceElement : AbstractKtSourceElement() {
abstract val lighterASTNode: LighterASTNode
abstract val treeStructure: FlyweightCapableTreeStructure<LighterASTNode>
abstract fun getElementTextInContextForDebug(): String
/** Implementation must compute the hashcode from the source element. */
abstract override fun hashCode(): Int
@@ -336,6 +339,10 @@ sealed class KtPsiSourceElement(val psi: PsiElement) : KtSourceElement() {
WrappedTreeStructure(psi.containingFile)
}
override fun getElementTextInContextForDebug(): String {
return getElementTextWithContext(psi)
}
internal class WrappedTreeStructure(file: PsiFile) : FlyweightCapableTreeStructure<LighterASTNode> {
private val lighterAST = TreeBackedLighterAST(file.node)
@@ -521,6 +528,10 @@ class KtLightSourceElement(
return node.psi?.toKtPsiSourceElement(kind)
}
override fun getElementTextInContextForDebug(): String {
return treeStructure.toString(lighterASTNode).toString()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.utils.exceptions
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.utils.getElementTextWithContext
fun ExceptionAttachmentBuilder.withPsiEntry(name: String, psi: PsiElement?) {
withEntry(name, psi) { psiElement ->
getElementTextWithContext(psiElement)
}
}