Skip to content

Commit 8a6d56a

Browse files
authored
Merge pull request #10520 from tamasvajk/kotlin-fix-anonymous-object-comment
Kotlin: Fix comment extraction for anonymous objects
2 parents 0c79c28 + 633f99d commit 8a6d56a

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ comments
1414
| comments.kt:65:9:67:11 | /**\n * An accessor comment\n */ | /**\n * An accessor comment\n */ |
1515
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | /**\n * An anonymous function comment\n */ |
1616
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ | /**\n * A local function comment\n */ |
17+
| comments.kt:88:10:90:11 | /**\n * An anonymous object comment\n */ | /**\n * An anonymous object comment\n */ |
1718
commentOwners
1819
| comments.kt:4:1:11:3 | /**\n * A group of *members*.\n *\n * This class has no useful logic; it's just a documentation example.\n *\n * @property name the name of this group.\n * @constructor Creates an empty group.\n */ | comments.kt:12:1:31:1 | Group |
1920
| comments.kt:4:1:11:3 | /**\n * A group of *members*.\n *\n * This class has no useful logic; it's just a documentation example.\n *\n * @property name the name of this group.\n * @constructor Creates an empty group.\n */ | comments.kt:12:1:31:1 | Group |
@@ -32,6 +33,8 @@ commentOwners
3233
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | l |
3334
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | l |
3435
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ | comments.kt:82:9:82:24 | localFn |
36+
| 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(...) { ... } |
3538
commentNoOwners
3639
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ |
3740
| comments.kt:24:9:24:25 | // A line comment |
@@ -52,6 +55,7 @@ commentSections
5255
| comments.kt:65:9:67:11 | /**\n * An accessor comment\n */ | An accessor comment |
5356
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | An anonymous function comment |
5457
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ | A local function comment |
58+
| comments.kt:88:10:90:11 | /**\n * An anonymous object comment\n */ | An anonymous object comment |
5559
commentSectionContents
5660
| A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n | A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n |
5761
| A local function comment | A local function comment |
@@ -61,6 +65,7 @@ commentSectionContents
6165
| Adds a [member] to this group.\n | Adds a [member] to this group.\n |
6266
| An accessor comment | An accessor comment |
6367
| An anonymous function comment | An anonymous function comment |
68+
| An anonymous object comment | An anonymous object comment |
6469
| An init block comment | An init block comment |
6570
| Creates an empty group. | Creates an empty group. |
6671
| Kdoc with no owner | Kdoc with no owner |

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class InitBlock {
5757
init { }
5858
}
5959

60-
class X {
60+
open class X {
6161
/**
6262
* A prop comment
6363
*/
@@ -82,3 +82,12 @@ class X {
8282
fun localFn() {}
8383
}
8484
}
85+
86+
class XX {
87+
fun f() = object :
88+
/**
89+
* An anonymous object comment
90+
*/
91+
X() {
92+
}
93+
}

0 commit comments

Comments
 (0)