Skip to content

Commit 633f99d

Browse files
committed
Kotlin: Fix comment extraction for anonymous objects
1 parent 7927e1d commit 633f99d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,14 @@ open class KotlinUsesExtractor(
461461
)
462462
}
463463

464+
fun getExistingAnonymousClassLabel(c: IrClass): Label<out DbType>? {
465+
if (!c.isAnonymousObject){
466+
return null
467+
}
468+
469+
return tw.lm.anonymousTypeMapping[c]?.javaResult?.id
470+
}
471+
464472
fun fakeKotlinType(): Label<out DbKt_type> {
465473
val fakeKotlinPackageId: Label<DbPackage> = tw.getLabelFor("@\"FakeKotlinPackage\"", {
466474
tw.writePackages(it, "fake.kotlin")

java/kotlin-extractor/src/main/kotlin/comments/CommentExtractor.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.ir.IrElement
1010
import org.jetbrains.kotlin.ir.declarations.*
1111
import org.jetbrains.kotlin.ir.expressions.IrBody
1212
import org.jetbrains.kotlin.ir.expressions.IrExpression
13+
import org.jetbrains.kotlin.ir.util.isAnonymousObject
1314
import org.jetbrains.kotlin.ir.util.parentClassOrNull
1415
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
1516
import org.jetbrains.kotlin.lexer.KtTokens
@@ -126,6 +127,10 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
126127
// local functions are not named globally, so we need to get them from the local function label cache
127128
label = "local function ${element.name.asString()}"
128129
fileExtractor.getExistingLocallyVisibleFunctionLabel(element)
130+
} else if (element is IrClass && element.isAnonymousObject) {
131+
// anonymous objects are not named globally, so we need to get them from the cache
132+
label = "anonymous class ${element.name.asString()}"
133+
fileExtractor.getExistingAnonymousClassLabel(element)
129134
}
130135
else {
131136
label = getLabelForNamedElement(element) ?: return null
@@ -140,7 +145,12 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
140145

141146
private fun getLabelForNamedElement(element: IrElement) : String? {
142147
when (element) {
143-
is IrClass -> return fileExtractor.getClassLabel(element, listOf()).classLabel
148+
is IrClass ->
149+
return if (element.isAnonymousObject) {
150+
null
151+
} else {
152+
fileExtractor.getClassLabel(element, listOf()).classLabel
153+
}
144154
is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
145155
is IrFunction -> {
146156
return if (element.isLocalFunction()) {

java/ql/test/kotlin/library-tests/comments/comments.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ commentOwners
3434
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | l |
3535
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ | comments.kt:82:9:82:24 | localFn |
3636
| comments.kt:88:10:90:11 | /**\n * An anonymous object comment\n */ | comments.kt:87:15:92:5 | |
37+
| comments.kt:88:10:90:11 | /**\n * An anonymous object comment\n */ | comments.kt:87:15:92:5 | new X(...) { ... } |
3738
commentNoOwners
3839
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ |
3940
| comments.kt:24:9:24:25 | // A line comment |

0 commit comments

Comments
 (0)