Restore location and name reporting for symbols

This commit is contained in:
Chris Smowton
2024-11-27 16:49:37 +00:00
parent bfdb5e0b17
commit e29d9ddacb
4 changed files with 39 additions and 12 deletions

View File

@@ -117,14 +117,15 @@ open class KotlinFileExtractor(
val metaAnnotationSupport = MetaAnnotationSupport(logger, pluginContext, this)
*/
inline fun <T> with(kind: String, element: KtElement?, f: () -> T): T {
val name =
when (element) {
is KtFile -> element.virtualFilePath
is KtNamed -> element.getNameAsName()?.asString() ?: "<missing name>"
else -> "<no name>"
}
val loc = element?.let { tw.getLocationString(it) } ?: "<unknown location>"
inline fun <T> with(kind: String, element: PsiElement, f: () -> T) = with(kind, PsiElementWrapper(element), f)
inline fun <T> with(kind: String, element: KaSymbol, f: () -> T) =
with(kind,
element.psiSafe<PsiElement>()?.let { PsiElementWrapper(it) } ?: SymbolWrapper(element),
f)
inline fun <T> with(kind: String, element: PsiElementOrSymbol, f: () -> T): T {
val name = element.getName()
val loc = element.getLocationString(tw)
val context = logger.loggerState.extractorContextStack
context.push(ExtractorContext(kind, element, name, loc))
try {
@@ -299,7 +300,7 @@ open class KotlinFileExtractor(
extractAnnotations: Boolean
*/
) {
with("declaration", declaration.psiSafe()) {
with("declaration", declaration) {
/*
OLD: KE1
if (!shouldExtractDecl(declaration, extractPrivateMembers)) return

View File

@@ -20,7 +20,7 @@ fun KotlinFileExtractor.extractClassSource(
extractFunctionBodies: Boolean
*/
): Label<out DbClassorinterface> {
with("class source", c.psiSafe()) {
with("class source", c) {
// OLD: KE1: DeclarationStackAdjuster(c).use {
val id = useClassSource(c)
val pkg = c.classId?.packageFqName?.asString() ?: ""

View File

@@ -409,7 +409,7 @@ private fun KotlinFileExtractor.forceExtractFunction(
overriddenAttributes: OverriddenFunctionAttributes? = null
*/
): Label<out DbCallable> {
with("function", f.psiSafe()) {
with("function", f) {
/*
OLD: KE1
DeclarationStackAdjuster(f, overriddenAttributes).use {

View File

@@ -1,6 +1,10 @@
package com.github.codeql
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KaNamedSymbol
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamed
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
@@ -99,9 +103,31 @@ class LogMessage(private val kind: String, private val message: String) {
}
}
sealed class PsiElementOrSymbol {
abstract fun getLocationString(ftw: FileTrapWriter): String
abstract fun getName(): String
}
data class PsiElementWrapper(val e: PsiElement) : PsiElementOrSymbol() {
override fun getLocationString(ftw: FileTrapWriter) = ftw.getLocationString(e)
override fun getName() = when (e) {
is KtFile -> e.virtualFilePath
is KtNamed -> e.nameAsName?.asString() ?: "<missing name>"
else -> "<no name>"
}
}
data class SymbolWrapper(val e: KaSymbol) : PsiElementOrSymbol() {
override fun getLocationString(ftw: FileTrapWriter) = "file://${ftw.filePath}"
override fun getName() = when (e) {
is KaNamedSymbol -> e.name.asString()
else -> "<no name>"
}
}
data class ExtractorContext(
val kind: String,
val element: PsiElement?,
val element: PsiElementOrSymbol,
val name: String,
val loc: String
)