Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -631,20 +631,30 @@ private int find(byte[] key, int off, int len) {
return lookup(table, names, nameOffsets, key, off, len);
}

/* Creates an entry for key[0..len), plus any missing ancestors, and links it to its parent. */
/*
* Creates an entry for key[0..len), plus any missing ancestors, and links each to its parent.
* Iterative because one entry name can hold tens of thousands of segments. The entry and its
* missing ancestors get consecutive indices, deepest first, so the links are made by walking
* those indices back from the shallowest.
*/
private int create(byte[] key, int len) {
int i = append(key, 0, len);
int slash = len - 1;
while (key[slash] != '/') {
slash--;
}
int parent = slash == 0 ? 0 : find(key, 0, slash);
if (parent < 0) {
parent = create(key, slash);
final int first = count;
int end = len;
int parent;
do {
append(key, 0, end);
end--;
while (key[end] != '/') {
end--;
}
parent = end == 0 ? 0 : find(key, 0, end);
} while (parent < 0);
for (int i = count - 1; i >= first; i--) {
nextSiblings[i] = firstChildren[parent];
firstChildren[parent] = i;
parent = i;
}
nextSiblings[i] = firstChildren[parent];
firstChildren[parent] = i;
return i;
return first;
}

private int append(byte[] src, int off, int len) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@ internal class DeclarationProvider(
override val hasSpecificCallablePackageNamesComputation = false
override val hasSpecificClassifierPackageNamesComputation = false

@OptIn(ResolutionSideKtFileAccess::class)
override fun ktFilesForPackage(fqName: FqName): Sequence<KtFile> =
index
.sourceFilesForPackage(fqName)
.asSequence()
.filter { it in scope }
.mapNotNull { index.getKtFile(it) }
override fun ktFilesForPackage(fqName: FqName): Sequence<KtFile> = index.ktFilesInPackage(fqName, scope)
}

/** The Kotlin files in [scope] that the file index records under [packageFqName]. */
@OptIn(ResolutionSideKtFileAccess::class)
internal fun KtSymbolIndex.ktFilesInPackage(
packageFqName: FqName,
scope: GlobalSearchScope,
): Sequence<KtFile> =
sourceFilesForPackage(packageFqName)
.asSequence()
.filter { it in scope }
.mapNotNull { getKtFile(it) }
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.cli.jvm.index.JavaRoot
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
import org.jetbrains.kotlin.com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
Expand All @@ -24,6 +25,8 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.psi.KtTypeAlias
Expand All @@ -40,9 +43,6 @@ internal class DirectInheritorsProvider :
private lateinit var modules: List<KtModule>
private lateinit var project: Project

private val classesBySupertypeName = mutableMapOf<Name, MutableSet<KtClassOrObject>>()
private val inheritableTypeAliasesByAliasedName = mutableMapOf<Name, MutableSet<KtTypeAlias>>()

override fun setupWith(
project: MockProject,
index: KtSymbolIndex,
Expand All @@ -60,28 +60,64 @@ internal class DirectInheritorsProvider :
scope: GlobalSearchScope,
includeLocalInheritors: Boolean,
): Iterable<KtClassOrObject> {
computeIndex()
val inheritors = computeIndex()

val classId = ktClass.getClassId() ?: return emptyList()
val baseModule = KotlinProjectStructureProvider.getModule(project, ktClass, useSiteModule = null)
val baseFirClass = classId.toFirSymbol(baseModule)?.fir as? FirClass ?: return emptyList()

val baseClassNames = mutableSetOf(classId.shortClassName)
calculateAliases(classId.shortClassName, baseClassNames)
calculateAliases(inheritors.inheritableTypeAliasesByAliasedName, classId.shortClassName, baseClassNames)

val possibleInheritors = baseClassNames.flatMap { classesBySupertypeName[it].orEmpty() }
val possibleInheritors = baseClassNames.flatMap { inheritors.classesBySupertypeName[it].orEmpty() }
if (possibleInheritors.isEmpty()) {
return emptyList()
}

return possibleInheritors.filter { isValidInheritor(it, baseFirClass, scope, includeLocalInheritors) }
}

// Let's say this operation is not frequently called, if we discover it's not the case we should cache it
/*
* Reads only the files of [ktClass]'s package. The sealed inheritors search calls this, and the
* whole-project walk of getDirectKotlinInheritors parsed every source file on each call: ~18 s of
* the first completion after a cold open on a phone.
*/
@OptIn(SymbolInternals::class)
override fun getSamePackageDirectKotlinInheritors(
ktClass: KtClass,
scope: GlobalSearchScope,
includeLocalInheritors: Boolean,
): Iterable<KtClassOrObject> {
val classId = ktClass.getClassId() ?: return emptyList()
val baseModule = KotlinProjectStructureProvider.getModule(project, ktClass, useSiteModule = null)
val baseFirClass = classId.toFirSymbol(baseModule)?.fir as? FirClass ?: return emptyList()

/*
* Every class with a supertype is a candidate: FIR's subclass check expands type aliases, so
* an inheritor written against an alias declared in another package is still found.
*/
return index
.ktFilesInPackage(classId.packageFqName, scope)
.flatMap { it.classesOrObjects(includeLocalInheritors) }
.filter { it.superTypeListEntries.isNotEmpty() }
.filter { isValidInheritor(it, baseFirClass, scope, includeLocalInheritors) }
.toList()
}

/*
* Built per call and dropped on return: each KtClassOrObject pins its file's whole PSI tree, so a
* long-lived copy keeps every source file's tree in the heap.
*/
private class InheritorsIndex {
val classesBySupertypeName = mutableMapOf<Name, MutableSet<KtClassOrObject>>()
val inheritableTypeAliasesByAliasedName = mutableMapOf<Name, MutableSet<KtTypeAlias>>()
}

@OptIn(ResolutionSideKtFileAccess::class)
private fun computeIndex() {
classesBySupertypeName.clear()
inheritableTypeAliasesByAliasedName.clear()
private fun computeIndex(): InheritorsIndex {
val result = InheritorsIndex()
val classesBySupertypeName = result.classesBySupertypeName
val inheritableTypeAliasesByAliasedName = result.inheritableTypeAliasesByAliasedName

modules
.asFlatSequence()
Expand Down Expand Up @@ -114,17 +150,19 @@ internal class DirectInheritorsProvider :
},
)
}
return result
}

private fun calculateAliases(
aliasesByAliasedName: Map<Name, Set<KtTypeAlias>>,
aliasedName: Name,
aliases: MutableSet<Name>,
) {
inheritableTypeAliasesByAliasedName[aliasedName].orEmpty().forEach { alias ->
aliasesByAliasedName[aliasedName].orEmpty().forEach { alias ->
val aliasName = alias.nameAsSafeName
val isNewAliasName = aliases.add(aliasName)
if (isNewAliasName) {
calculateAliases(aliasName, aliases)
calculateAliases(aliasesByAliasedName, aliasName, aliases)
}
}
}
Expand Down Expand Up @@ -186,3 +224,14 @@ private fun findInheritableSimpleNames(typeElement: KtTypeElement): List<String>
}
}
}

private fun KtFile.classesOrObjects(includeLocal: Boolean): List<KtClassOrObject> =
if (includeLocal) {
PsiTreeUtil.collectElementsOfType(this, KtClassOrObject::class.java).toList()
} else {
declarations.nonLocalClassesOrObjects()
}

// Descends through class bodies only, so lazily parsed function bodies stay unparsed.
private fun List<KtDeclaration>.nonLocalClassesOrObjects(): List<KtClassOrObject> =
filterIsInstance<KtClassOrObject>().flatMap { listOf(it) + it.declarations.nonLocalClassesOrObjects() }
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,21 @@ internal class ListingJarHandler(
val size = listing.size(index)
if (size > MAX_ENTRY_SIZE) throw ZipException("$jarPath!${listing.name(index)} is too large: $size bytes")
return when (val method = listing.method(index)) {
METHOD_STORED -> readCompressed(index, extra = 0)
METHOD_DEFLATED -> inflate(readCompressed(index, extra = 1), size.toInt(), index)
else -> throw ZipException("$jarPath!${listing.name(index)} has unsupported method $method")
METHOD_STORED -> {
readCompressed(index, extra = 0)
}

METHOD_DEFLATED -> {
// The output array is sized from metadata, so refuse a size no deflate stream of this length can produce.
if (size > (listing.compressedSize(index) + 1) * MAX_DEFLATE_RATIO) {
throw ZipException("$jarPath!${listing.name(index)}: $size bytes cannot inflate from ${listing.compressedSize(index)}")
}
inflate(readCompressed(index, extra = 1), size.toInt(), index)
}

else -> {
throw ZipException("$jarPath!${listing.name(index)} has unsupported method $method")
}
}
}

Expand Down Expand Up @@ -127,6 +139,10 @@ internal class ListingJarHandler(
): ByteArray {
val compressedSize = listing.compressedSize(index)
if (compressedSize > MAX_ENTRY_SIZE) throw ZipException("$jarPath!${listing.name(index)} is too large: $compressedSize bytes")
// Checked before allocating, so a corrupt central directory cannot claim more heap than the file holds.
if (listing.localHeaderOffset(index) + LOC_HEADER_SIZE + compressedSize > snapshot.size()) {
throw ZipException("$jarPath!${listing.name(index)}: $compressedSize bytes run past the end of the JAR")
}
val data = ByteArray(compressedSize.toInt() + extra)
val header = ByteArray(LOC_HEADER_SIZE)
Comment thread
itsaky-adfa marked this conversation as resolved.
Resolved
synchronized(jarLock) {
Expand Down Expand Up @@ -208,6 +224,9 @@ internal class ListingJarHandler(
const val ROOT = 0
const val NANOS_PER_MILLI = 1_000_000L
const val MAX_ENTRY_SIZE = Int.MAX_VALUE - 16L

// Deflate's upper bound: a 258-byte match costs at least 2 bits (1-bit length and distance codes).
const val MAX_DEFLATE_RATIO = 1032L
const val METHOD_STORED = 0
const val METHOD_DEFLATED = 8
const val LOC_SIGNATURE = 0x04034b50L
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.itsaky.androidide.lsp.kotlin.compiler.services

import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest
import org.jetbrains.kotlin.analysis.api.platform.declarations.KotlinDirectInheritorsProvider
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider
import org.jetbrains.kotlin.com.intellij.psi.impl.source.PsiFileImpl
import org.jetbrains.kotlin.com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class DirectInheritorsProviderTest : KtLspTest() {
@Test
fun `same-package search finds inheritors declared in other files of the base class's package`() {
val base = createSourceFile("p/Base.kt", "package p\nsealed class Base")
createSourceFile("p/A.kt", "package p\nclass A : Base()")
createSourceFile("p/Outer.kt", "package p\nclass Outer { class B : Base() }")
createSourceFile("p/Unrelated.kt", "package p\nclass Unrelated : Any()")

assertEquals(listOf("p.A", "p.Outer.B"), samePackageInheritorsOf(base, "Base"))
}

@Test
fun `same-package search finds inheritors that extend the base class through a type alias from another package`() {
val base = createSourceFile("p/Base.kt", "package p\nsealed class Base")
createSourceFile("q/Alias.kt", "package q\ntypealias Aliased = p.Base")
createSourceFile("p/ViaAlias.kt", "package p\nimport q.Aliased\nclass ViaAlias : Aliased()")

assertEquals(listOf("p.ViaAlias"), samePackageInheritorsOf(base, "Base"))
}

@Test
fun `same-package search does not load files outside the base class's package`() {
val base = createSourceFile("p/Base.kt", "package p\nsealed class Base")
// Not registered with the file index, so nothing but a whole-module walk can reach it.
val otherPackage = env.createFile("src", "q/Other.kt", "package q\nclass Other : Any()") as PsiFileImpl
assertNull(otherPackage.treeElement)

samePackageInheritorsOf(base, "Base")

assertNull(otherPackage.treeElement)
}

@Test
fun `full search still finds inheritors in other packages`() {
val base = createSourceFile("p/Base.kt", "package p\nopen class Base")
createSourceFile("p/A.kt", "package p\nclass A : Base()")
createSourceFile("q/B.kt", "package q\nclass B : p.Base()")

val all = inheritorsOf(base, "Base") { ktClass, scope -> getDirectKotlinInheritors(ktClass, scope, false) }

assertEquals(listOf("p.A", "q.B"), all)
assertEquals(listOf("p.A"), samePackageInheritorsOf(base, "Base"))
}

private fun samePackageInheritorsOf(
file: KtFile,
className: String,
): List<String> =
inheritorsOf(file, className) { ktClass, scope ->
getSamePackageDirectKotlinInheritors(ktClass, scope, false)
}

private fun inheritorsOf(
file: KtFile,
className: String,
search: KotlinDirectInheritorsProvider.(KtClass, GlobalSearchScope) -> Iterable<KtClassOrObject>,
): List<String> {
val ktClass = file.declarations.filterIsInstance<KtClass>().single { it.name == className }
return analyzeMaybeDanglingForTest(file) {
val scope = KotlinProjectStructureProvider.getModule(env.project, ktClass, useSiteModule = null).contentScope
KotlinDirectInheritorsProvider
.getInstance(env.project)
.search(ktClass, scope)
.map { it.fqName!!.asString() }
.sorted()
}
}
}
Loading
Loading