Skip to content

Commit c77f573

Browse files
committed
Kotlin: fix doc comment extraction for local functions
1 parent 46c52ae commit c77f573

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,10 +1121,6 @@ open class KotlinUsesExtractor(
11211121
// Note not using `parentsWithSelf` as that only works if `d` is an IrDeclarationParent
11221122
d.parents.any { (it as? IrAnnotationContainer)?.hasAnnotation(jvmWildcardSuppressionAnnotaton) == true }
11231123

1124-
protected fun IrFunction.isLocalFunction(): Boolean {
1125-
return this.visibility == DescriptorVisibilities.LOCAL
1126-
}
1127-
11281124
/**
11291125
* Class to hold labels for generated classes around local functions, lambdas, function references, and property references.
11301126
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.codeql.comments
22

33
import com.github.codeql.*
44
import com.github.codeql.utils.IrVisitorLookup
5+
import com.github.codeql.utils.isLocalFunction
56
import com.github.codeql.utils.versions.Psi2Ir
67
import com.intellij.psi.PsiComment
78
import com.intellij.psi.PsiElement
@@ -104,7 +105,11 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
104105
val existingLabel = if (ownerIr is IrVariable) {
105106
label = "variable ${ownerIr.name.asString()}"
106107
tw.getExistingVariableLabelFor(ownerIr)
107-
} else {
108+
} else if (ownerIr is IrFunction && ownerIr.isLocalFunction()) {
109+
label = "local function ${ownerIr.name.asString()}"
110+
fileExtractor.getLocallyVisibleFunctionLabels(ownerIr).function
111+
}
112+
else {
108113
label = getLabel(ownerIr) ?: continue
109114
tw.getExistingLabelFor<DbTop>(label)
110115
}
@@ -130,7 +135,13 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
130135
when (element) {
131136
is IrClass -> return fileExtractor.getClassLabel(element, listOf()).classLabel
132137
is IrTypeParameter -> return fileExtractor.getTypeParameterLabel(element)
133-
is IrFunction -> return fileExtractor.getFunctionLabel(element, null)
138+
is IrFunction -> {
139+
return if (element.isLocalFunction()) {
140+
null
141+
} else {
142+
fileExtractor.getFunctionLabel(element, null)
143+
}
144+
}
134145
is IrValueParameter -> return fileExtractor.getValueParameterLabel(element, null)
135146
is IrProperty -> return fileExtractor.getPropertyLabel(element)
136147
is IrField -> return fileExtractor.getFieldLabel(element)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.codeql.utils
2+
3+
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
4+
import org.jetbrains.kotlin.ir.declarations.IrFunction
5+
6+
fun IrFunction.isLocalFunction(): Boolean {
7+
return this.visibility == DescriptorVisibilities.LOCAL
8+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ commentOwners
3131
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | getL |
3232
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | l |
3333
| comments.kt:71:9:73:11 | /**\n * An anonymous function comment\n */ | comments.kt:70:5:76:10 | l |
34+
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ | comments.kt:82:9:82:24 | localFn |
3435
commentNoOwners
3536
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ |
3637
| comments.kt:24:9:24:25 | // A line comment |
3738
| comments.kt:28:5:30:6 | /*\n A block comment\n */ |
38-
| comments.kt:79:9:81:11 | /**\n * A local function comment\n */ |
3939
commentSections
4040
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ | Kdoc with no owner |
4141
| 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 */ | A group of *members*.\n\nThis class has no useful logic; it's just a documentation example.\n\n |

0 commit comments

Comments
 (0)