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

Skip to content

Commit cbd265a

Browse files
committed
Kotlin: Add support for try statements
1 parent 6b5663d commit cbd265a

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,17 +1000,22 @@ class X {
10001000
}
10011001

10021002
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
1003+
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
1004+
val locId = tw.getLocation(v)
1005+
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
1006+
tw.writeHasLocation(stmtId, locId)
1007+
extractVariableExpr(v, callable, stmtId, 1)
1008+
}
1009+
1010+
fun extractVariableExpr(v: IrVariable, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
10031011
val varId = useVariable(v)
10041012
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
1005-
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
10061013
val locId = tw.getLocation(v)
10071014
val type = useType(v.type)
10081015
tw.writeLocalvars(varId, v.name.asString(), type.javaResult.id, exprId) // TODO: KT type
10091016
tw.writeHasLocation(varId, locId)
1010-
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, type.kotlinResult.id, stmtId, 1)
1017+
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, type.kotlinResult.id, parent, idx)
10111018
tw.writeHasLocation(exprId, locId)
1012-
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
1013-
tw.writeHasLocation(stmtId, locId)
10141019
val i = v.initializer
10151020
if(i != null) {
10161021
extractExpressionExpr(i, callable, exprId, 0)
@@ -1303,6 +1308,25 @@ class X {
13031308
tw.writeHasLocation(id, locId)
13041309
extractExpressionExpr(e.value, callable, id, 0)
13051310
}
1311+
is IrTry -> {
1312+
val stmtParent = parent.stmt(e, callable)
1313+
val id = tw.getFreshIdLabel<DbTrystmt>()
1314+
val locId = tw.getLocation(e)
1315+
tw.writeStmts_trystmt(id, stmtParent.parent, stmtParent.idx, callable)
1316+
tw.writeHasLocation(id, locId)
1317+
extractExpressionExpr(e.tryResult, callable, id, -1)
1318+
val finallyStmt = e.finallyExpression
1319+
if(finallyStmt != null) {
1320+
extractExpressionExpr(finallyStmt, callable, id, -2)
1321+
}
1322+
for((catchIdx, catchClause) in e.catches.withIndex()) {
1323+
val catchId = tw.getFreshIdLabel<DbCatchclause>()
1324+
tw.writeStmts_catchclause(catchId, id, catchIdx, callable)
1325+
// TODO: Index -1: unannotatedtypeaccess
1326+
extractVariableExpr(catchClause.catchParameter, callable, catchId, 0)
1327+
extractExpressionExpr(catchClause.result, callable, catchId, 1)
1328+
}
1329+
}
13061330
is IrContainerExpression -> {
13071331
val stmtParent = parent.stmt(e, callable)
13081332
val id = tw.getFreshIdLabel<DbBlock>()

java/ql/test/kotlin/library-tests/stmts/exprs.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@
5656
| stmts.kt:28:11:28:11 | x | VarAccess |
5757
| stmts.kt:28:11:28:15 | ... > ... | GTExpr |
5858
| stmts.kt:28:15:28:15 | y | VarAccess |
59+
| stmts.kt:33:9:35:5 | <Stmt> | StmtExpr |
60+
| stmts.kt:34:15:34:30 | new Exception(...) | ClassInstanceExpr |
61+
| stmts.kt:34:26:34:28 | Foo | StringLiteral |
62+
| stmts.kt:36:12:36:23 | e | LocalVariableDeclExpr |
63+
| stmts.kt:36:26:38:5 | <Stmt> | StmtExpr |
64+
| stmts.kt:37:16:37:16 | 1 | IntegerLiteral |
65+
| stmts.kt:39:13:41:5 | <Stmt> | StmtExpr |
66+
| stmts.kt:40:16:40:16 | 2 | IntegerLiteral |

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| file://:0:0:0:0 | catch (...) | CatchClause |
12
| stmts.kt:2:41:20:1 | { ... } | BlockStmt |
23
| stmts.kt:3:5:6:5 | <Expr>; | ExprStmt |
34
| stmts.kt:3:15:4:5 | { ... } | BlockStmt |
@@ -32,3 +33,11 @@
3233
| stmts.kt:25:24:25:33 | break | BreakStmt |
3334
| stmts.kt:28:5:29:16 | while (...) | WhileStmt |
3435
| stmts.kt:29:9:29:16 | continue | ContinueStmt |
36+
| stmts.kt:32:23:42:1 | { ... } | BlockStmt |
37+
| stmts.kt:33:5:41:5 | try ... | TryStmt |
38+
| stmts.kt:33:9:35:5 | { ... } | BlockStmt |
39+
| stmts.kt:34:9:34:30 | throw ... | ThrowStmt |
40+
| stmts.kt:36:26:38:5 | { ... } | BlockStmt |
41+
| stmts.kt:37:9:37:16 | return ... | ReturnStmt |
42+
| stmts.kt:39:13:41:5 | { ... } | BlockStmt |
43+
| stmts.kt:40:9:40:16 | return ... | ReturnStmt |

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ fun loops(x: Int, y: Int) {
2828
while(x > y)
2929
continue
3030
}
31+
32+
fun exceptions(): Int {
33+
try {
34+
throw Exception("Foo")
35+
}
36+
catch (e: Exception) {
37+
return 1
38+
}
39+
finally {
40+
return 2
41+
}
42+
}

0 commit comments

Comments
 (0)