Skip to content

Commit f7d2b27

Browse files
authored
Merge pull request #9151 from tamasvajk/kotlin-comments-variables-1
Kotlin: Handle variables as comment owners
2 parents 616b12d + 7376ec5 commit f7d2b27

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ open class TrapWriter (protected val loggerBase: LoggerBase, val lm: TrapLabelMa
110110
}
111111
}
112112

113+
fun getExistingVariableLabelFor(v: IrVariable): Label<out DbLocalvar>? {
114+
return variableLabelMapping.get(v)
115+
}
116+
113117
/**
114118
* This returns a label for the location described by its arguments.
115119
* Typically users will not want to call this directly, but instead

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import com.github.codeql.utils.versions.Psi2Ir
66
import com.intellij.psi.PsiComment
77
import com.intellij.psi.PsiElement
88
import org.jetbrains.kotlin.ir.IrElement
9-
import org.jetbrains.kotlin.ir.declarations.path
10-
import org.jetbrains.kotlin.ir.declarations.IrFile
11-
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
9+
import org.jetbrains.kotlin.ir.declarations.*
1210
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
1311
import org.jetbrains.kotlin.lexer.KtTokens
1412
import org.jetbrains.kotlin.psi.KtVisitor
@@ -98,8 +96,14 @@ class CommentExtractor(private val fileExtractor: KotlinFileExtractor, private v
9896
// Don't attribute comments to the implicit `this` parameter of a function.
9997
continue
10098
}
101-
val label = fileExtractor.getLabel(ownerIr) ?: continue
102-
val existingLabel = tw.getExistingLabelFor<DbTop>(label)
99+
val label: String
100+
val existingLabel = if (ownerIr is IrVariable) {
101+
label = "variable ${ownerIr.name.asString()}"
102+
tw.getExistingVariableLabelFor(ownerIr)
103+
} else {
104+
label = fileExtractor.getLabel(ownerIr) ?: continue
105+
tw.getExistingLabelFor<DbTop>(label)
106+
}
103107
if (existingLabel == null) {
104108
logger.warn("Couldn't get existing label for $label")
105109
continue

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ comments
77
| comments.kt:28:5:30:6 | /*\n A block comment\n */ | /*\n A block comment\n */ |
88
| comments.kt:35:5:35:34 | /** Medium is in the middle */ | /** Medium is in the middle */ |
99
| comments.kt:37:5:37:23 | /** This is high */ | /** This is high */ |
10+
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | /**\n * A variable.\n */ |
1011
commentOwners
1112
| 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 |
1213
| 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 |
@@ -16,6 +17,7 @@ commentOwners
1617
| comments.kt:19:5:22:7 | /**\n * Adds a [member] to this group.\n * @return the new size of the group.\n */ | comments.kt:23:5:26:5 | add |
1718
| comments.kt:35:5:35:34 | /** Medium is in the middle */ | comments.kt:36:5:36:14 | Medium |
1819
| comments.kt:37:5:37:23 | /** This is high */ | comments.kt:38:5:38:11 | High |
20+
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | comments.kt:45:5:45:13 | int a |
1921
commentSections
2022
| comments.kt:1:1:1:25 | /** Kdoc with no owner */ | Kdoc with no owner |
2123
| 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 |
@@ -25,8 +27,10 @@ commentSections
2527
| comments.kt:19:5:22:7 | /**\n * Adds a [member] to this group.\n * @return the new size of the group.\n */ | Adds a [member] to this group.\n |
2628
| comments.kt:35:5:35:34 | /** Medium is in the middle */ | Medium is in the middle |
2729
| comments.kt:37:5:37:23 | /** This is high */ | This is high |
30+
| comments.kt:42:5:44:6 | /**\n * A variable.\n */ | A variable. |
2831
commentSectionContents
2932
| 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 |
33+
| A variable. | A variable. |
3034
| Adds a [member] to this group.\n | Adds a [member] to this group.\n |
3135
| Creates an empty group. | Creates an empty group. |
3236
| Kdoc with no owner | Kdoc with no owner |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ enum class Severity(val sev: Int) {
3737
/** This is high */
3838
High(3)
3939
}
40+
41+
fun fn1() {
42+
/**
43+
* A variable.
44+
*/
45+
val a = 1
46+
}

0 commit comments

Comments
 (0)