From c98415631f892c37703b2dca69de90d2ffbc6a0d Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Thu, 29 Aug 2024 18:14:46 +0100 Subject: [PATCH] KE2: Start extracting declaration parents --- .../src/main/kotlin/KotlinFileExtractor.kt | 3 --- .../src/main/kotlin/KotlinUsesExtractor.kt | 21 +++++++++++++------ .../src/main/kotlin/TrapWriter.kt | 14 ++++--------- .../src/main/kotlin/utils/ClassNames.kt | 11 +++++++--- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt index 14af22b27b6..1e24f86673d 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt @@ -285,9 +285,6 @@ OLD: KE1 */ } is KtFunction -> { - println("=== Dec is fun") - println(declaration) - println(declaration.parent) val parentId = useDeclarationParentOf(declaration, false)?.cast() /* OLD: KE1 diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt index 71bb582f239..7670965a208 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt @@ -56,11 +56,11 @@ OLD: KE1 val javaLangObject by lazy { referenceExternalClass("java.lang.Object") } val javaLangObjectType by lazy { javaLangObject?.typeWith() } +*/ private fun usePackage(pkg: String): Label { return extractPackage(pkg) } -*/ fun extractPackage(pkg: String): Label { val pkgLabel = "@\"package;$pkg\"" @@ -84,24 +84,28 @@ OLD: KE1 TypeResult(extractFileClass(pkg, jvmName), "", ""), TypeResult(fakeKotlinType(), "", "") ) +*/ - fun extractFileClass(f: IrFile): Label { + fun extractFileClass(f: KtFile): Label { val pkg = f.packageFqName.asString() val jvmName = getFileClassName(f) val id = extractFileClass(pkg, jvmName) if (tw.lm.fileClassLocationsExtracted.add(f)) { - val fileId = tw.mkFileId(f.path, false) + val fileId = tw.mkFileId(f.virtualFilePath, false) val locId = tw.getWholeFileLocation(fileId) tw.writeHasLocation(id, locId) } return id } +/* +OLD: KE1 private fun extractFileClass(fqName: FqName): Label { val pkg = if (fqName.isRoot()) "" else fqName.parent().asString() val jvmName = fqName.shortName().asString() return extractFileClass(pkg, jvmName) } +*/ private fun extractFileClass(pkg: String, jvmName: String): Label { val qualClassName = if (pkg.isEmpty()) jvmName else "$pkg.$jvmName" @@ -112,11 +116,16 @@ OLD: KE1 tw.writeClasses_or_interfaces(it, jvmName, pkgId, it) tw.writeFile_class(it) +/* +OLD: KE1 addModifiers(it, "public", "final") +*/ } return id } +/* +OLD: KE1 data class UseClassInstanceResult( val typeResult: TypeResult, val javaClass: IrClass @@ -1016,14 +1025,14 @@ OLD: KE1 */ ): Label? = when (dp) { -/* -OLD: KE1 - is IrFile -> + is KtFile -> if (canBeTopLevel) { usePackage(dp.packageFqName.asString()) } else { extractFileClass(dp) } +/* +OLD: KE1 is IrClass -> if (classTypeArguments != null) { useClassInstance(dp, classTypeArguments, inReceiverContext).typeResult.id diff --git a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt index a43aad63caa..642a3795d28 100644 --- a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt +++ b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt @@ -51,15 +51,15 @@ OLD: KE1 * labels, so we just accept this duplication. */ val genericSpecialisationsExtracted = HashSet() +*/ /** - * Sometimes, when we extract a file class we don't have the IrFile for it, so we are not able + * Sometimes, when we extract a file class we don't have the KtFile for it, so we are not able * to give it a location. This means that the location is written outside of the label creation. * This allows us to keep track of whether we've written the location already in this TRAP file, * to avoid duplication. */ - val fileClassLocationsExtracted = HashSet() -*/ + val fileClassLocationsExtracted = HashSet() } /** @@ -67,15 +67,9 @@ OLD: KE1 * `TrapWriter`s for the same file, as different instances will have different additional state, but * they must all share the same `TrapLabelManager` and `BufferedWriter`. */ -/* -OLD: KE1: -// TODO lm was `protected` before anonymousTypeMapping and locallyVisibleFunctionLabelMapping moved -// into it. Should we re-protect it and provide accessors? -Protected again for now in KE2. -*/ abstract class TrapWriter( protected val loggerBase: LoggerBase, - protected val lm: TrapLabelManager, + val lm: TrapLabelManager, private val bw: BufferedWriter ) { abstract fun getDiagnosticTrapWriter(): DiagnosticTrapWriter diff --git a/java/kotlin-extractor2/src/main/kotlin/utils/ClassNames.kt b/java/kotlin-extractor2/src/main/kotlin/utils/ClassNames.kt index eeebaf1480c..b362eb668fc 100644 --- a/java/kotlin-extractor2/src/main/kotlin/utils/ClassNames.kt +++ b/java/kotlin-extractor2/src/main/kotlin/utils/ClassNames.kt @@ -1,5 +1,7 @@ package com.github.codeql +import org.jetbrains.kotlin.psi.* + /* OLD: KE1 import com.github.codeql.utils.getJvmName @@ -25,14 +27,17 @@ import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClass // for `that`. private fun getName(d: IrDeclarationWithName) = (d as? IrAnnotationContainer)?.let { getJvmName(it) } ?: d.name.asString() +*/ -fun getFileClassName(f: IrFile) = - getJvmName(f) - ?: ((f.fileEntry.name +fun getFileClassName(f: KtFile): String = + null /* OLD: KE1: getJvmName(f) */ + ?: ((f.virtualFilePath .replaceFirst(Regex(""".*[/\\]"""), "") .replaceFirst(Regex("""\.kt$"""), "") .replaceFirstChar { it.uppercase() }) + "Kt") +/* +OLD: KE1 fun getIrElementBinaryName(that: IrElement): String { if (that is IrFile) { val shortName = getFileClassName(that)