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

Skip to content

Commit 3daec43

Browse files
committed
Kotlin: Variable initialisers
1 parent f5e2826 commit 3daec43

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,19 @@ class KotlinFileExtractor(val tw: TrapWriter) {
319319
}
320320
}
321321

322-
fun extractVariable(v: IrVariable, parent: Label<out DbStmtparent>, idx: Int) {
323-
val id = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
322+
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>) {
323+
val id = tw.getFreshIdLabel<DbLocalvar>()
324324
val locId = tw.getLocation(v.startOffset, v.endOffset)
325325
val typeId = useType(v.type)
326-
tw.writeExprs_localvariabledeclexpr(id, typeId, parent, idx)
326+
val decId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
327+
tw.writeLocalvars(id, v.name.asString(), typeId, decId)
327328
tw.writeHasLocation(id, locId)
328-
val varId = tw.getFreshIdLabel<DbLocalvar>()
329-
tw.writeLocalvars(varId, v.name.asString(), typeId, id)
330-
tw.writeHasLocation(varId, locId)
329+
tw.writeExprs_localvariabledeclexpr(decId, typeId, id, 0)
330+
tw.writeHasLocation(id, locId)
331+
val i = v.initializer
332+
if(i != null) {
333+
extractExpression(i, callable, decId, 0)
334+
}
331335
}
332336

333337
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
@@ -336,7 +340,7 @@ class KotlinFileExtractor(val tw: TrapWriter) {
336340
extractExpression(s, callable, parent, idx)
337341
}
338342
is IrVariable -> {
339-
extractVariable(s, parent, idx)
343+
extractVariable(s, callable)
340344
}
341345
else -> {
342346
extractorBug("Unrecognised IrStatement: " + s.javaClass)
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any |
2-
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any |
3-
| variables.kt:2:1:8:1 | other | variables.kt:0:0:0:0 | Any |
4-
| variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int |
5-
| variables.kt:5:20:5:29 | param | file://:0:0:0:0 | int |
6-
| variables.kt:6:9:6:21 | int local | file://:0:0:0:0 | int |
1+
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
2+
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
3+
| variables.kt:2:1:8:1 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
4+
| variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int | file://:0:0:0:0 | <none> |
5+
| variables.kt:5:20:5:29 | param | file://:0:0:0:0 | int | file://:0:0:0:0 | <none> |
6+
| variables.kt:6:9:6:25 | int local | file://:0:0:0:0 | int | variables.kt:6:21:6:25 | ... + ... |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Foo {
33
val prop: Int = 1
44

55
fun myFunction(param: Int) {
6-
val local = 2
6+
val local = 2 + 3
77
}
88
}
99

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
import java
22

3+
newtype TMaybeExpr =
4+
TExpr(Expr c) or
5+
TNoExpr()
6+
7+
class MaybeExpr extends TMaybeExpr {
8+
abstract string toString();
9+
abstract Location getLocation();
10+
}
11+
12+
class YesMaybeExpr extends MaybeExpr {
13+
Expr c;
14+
15+
YesMaybeExpr() { this = TExpr(c) }
16+
override string toString() { result = c.toString() }
17+
override Location getLocation() { result = c.getLocation() }
18+
}
19+
20+
class NoMaybeExpr extends MaybeExpr {
21+
NoMaybeExpr() { this = TNoExpr() }
22+
23+
override string toString() { result = "<none>" }
24+
override Location getLocation() { none() }
25+
}
26+
27+
MaybeExpr initializer(Variable v) {
28+
if exists(v.getInitializer())
29+
then result = TExpr(v.getInitializer())
30+
else result = TNoExpr()
31+
}
32+
333
from Variable v
4-
select v, v.getType()
34+
select v, v.getType(), initializer(v)
535

0 commit comments

Comments
 (0)