Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 799cf64

Browse files
committed
Kotlin: Local variables
1 parent 46add88 commit 799cf64

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFile
1717
import org.jetbrains.kotlin.ir.declarations.IrFunction
1818
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
1919
import org.jetbrains.kotlin.ir.declarations.IrProperty
20+
import org.jetbrains.kotlin.ir.declarations.IrVariable
2021
import org.jetbrains.kotlin.ir.util.dump
2122
import org.jetbrains.kotlin.ir.util.IdSignature
2223
import org.jetbrains.kotlin.ir.util.packageFqName
@@ -305,11 +306,25 @@ class KotlinFileExtractor(val tw: TrapWriter) {
305306
}
306307
}
307308

309+
fun extractVariable(v: IrVariable, parent: Label<out DbStmtparent>, idx: Int) {
310+
val id = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
311+
val locId = tw.getLocation(v.startOffset, v.endOffset)
312+
val typeId = useType(v.type)
313+
tw.writeExprs_localvariabledeclexpr(id, typeId, parent, idx)
314+
tw.writeHasLocation(id, locId)
315+
val varId = tw.getFreshIdLabel<DbLocalvar>()
316+
tw.writeLocalvars(varId, v.name.asString(), typeId, id)
317+
tw.writeHasLocation(varId, locId)
318+
}
319+
308320
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
309321
when(s) {
310322
is IrExpression -> {
311323
extractExpression(s, callable, parent, idx)
312324
}
325+
is IrVariable -> {
326+
extractVariable(s, parent, idx)
327+
}
313328
else -> {
314329
extractorBug("Unrecognised IrStatement: " + s.javaClass)
315330
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
| variables.kt:3:5:3:21 | prop |
1+
| variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int |
2+
| variables.kt:6:9:6:21 | int local | file://:0:0:0:0 | int |

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
class Foo {
33
val prop: Int = 1
4+
5+
fun myFunction(param: Int) {
6+
val local = 2
7+
}
48
}
59

610
// TODO: val topLevel: Int = 1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import java
22

33
from Variable v
4-
select v
4+
select v, v.getType()
55

0 commit comments

Comments
 (0)