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

Skip to content

Commit 2fb54de

Browse files
smowtonigfoo
authored andcommitted
Extract ordinary array get and set operations as ArrayAccesses, not calls
1 parent 387e8db commit 2fb54de

7 files changed

Lines changed: 125 additions & 14 deletions

File tree

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,10 @@ open class KotlinFileExtractor(
11511151

11521152
fun extractCall(c: IrCall, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int, enclosingStmt: Label<out DbStmt>) {
11531153
with("call", c) {
1154-
fun isFunction(pkgName: String, className: String, fName: String, hasQuestionMark: Boolean? = false): Boolean {
1154+
fun isFunction(pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, fName: String, hasQuestionMark: Boolean? = false): Boolean {
11551155
val verbose = false
11561156
fun verboseln(s: String) { if(verbose) println(s) }
1157-
verboseln("Attempting match for $pkgName $className $fName")
1157+
verboseln("Attempting match for $pkgName $classNameLogged $fName")
11581158
val target = c.symbol.owner
11591159
if (target.name.asString() != fName) {
11601160
verboseln("No match as function name is ${target.name.asString()} not $fName")
@@ -1179,8 +1179,8 @@ open class KotlinFileExtractor(
11791179
verboseln("No match as didn't find target class")
11801180
return false
11811181
}
1182-
if (targetClass.name.asString() != className) {
1183-
verboseln("No match as class name is ${targetClass.name.asString()} not $className")
1182+
if (!classNamePredicate(targetClass.name.asString())) {
1183+
verboseln("No match as class name is ${targetClass.name.asString()} not $classNameLogged")
11841184
return false
11851185
}
11861186
val targetPkg = targetClass.parent
@@ -1195,7 +1195,10 @@ open class KotlinFileExtractor(
11951195
verboseln("Match")
11961196
return true
11971197
}
1198-
1198+
1199+
fun isFunction(pkgName: String, className: String, fName: String, hasQuestionMark: Boolean? = false) =
1200+
isFunction(pkgName, className, { it == className }, fName, hasQuestionMark)
1201+
11991202
fun isNumericFunction(fName: String): Boolean {
12001203
return isFunction("kotlin", "Int", fName) ||
12011204
isFunction("kotlin", "Byte", fName) ||
@@ -1205,6 +1208,20 @@ open class KotlinFileExtractor(
12051208
isFunction("kotlin", "Double", fName)
12061209
}
12071210

1211+
fun isArrayType(typeName: String) =
1212+
when(typeName) {
1213+
"Array" -> true
1214+
"IntArray" -> true
1215+
"ByteArray" -> true
1216+
"ShortArray" -> true
1217+
"LongArray" -> true
1218+
"FloatArray" -> true
1219+
"DoubleArray" -> true
1220+
"CharArray" -> true
1221+
"BooleanArray" -> true
1222+
else -> false
1223+
}
1224+
12081225
fun extractMethodAccess(syntacticCallTarget: IrFunction, extractMethodTypeArguments: Boolean = true, extractClassTypeArguments: Boolean = false) {
12091226
val typeArgs =
12101227
if (extractMethodTypeArguments)
@@ -1561,6 +1578,46 @@ open class KotlinFileExtractor(
15611578
}
15621579
}
15631580
}
1581+
isFunction("kotlin", "(some array type)", { isArrayType(it) }, "get") && c.origin == IrStatementOrigin.GET_ARRAY_ELEMENT -> {
1582+
val id = tw.getFreshIdLabel<DbArrayaccess>()
1583+
val type = useType(c.type)
1584+
tw.writeExprs_arrayaccess(id, type.javaResult.id, parent, idx)
1585+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
1586+
binopDisp(id)
1587+
}
1588+
isFunction("kotlin", "(some array type)", { isArrayType(it) }, "set") && c.origin == IrStatementOrigin.EQ -> {
1589+
val array = c.dispatchReceiver
1590+
val arrayIdx = c.getValueArgument(0)
1591+
val assignedValue = c.getValueArgument(1)
1592+
1593+
if (array != null && arrayIdx != null && assignedValue != null) {
1594+
1595+
val assignId = tw.getFreshIdLabel<DbAssignexpr>()
1596+
val type = useType(c.type)
1597+
val locId = tw.getLocation(c)
1598+
tw.writeExprs_assignexpr(assignId, type.javaResult.id, parent, idx)
1599+
tw.writeExprsKotlinType(assignId, type.kotlinResult.id)
1600+
tw.writeHasLocation(assignId, locId)
1601+
tw.writeCallableEnclosingExpr(assignId, callable)
1602+
tw.writeStatementEnclosingExpr(assignId, enclosingStmt)
1603+
1604+
val arrayAccessId = tw.getFreshIdLabel<DbArrayaccess>()
1605+
val arrayType = useType(array.type)
1606+
tw.writeExprs_arrayaccess(arrayAccessId, arrayType.javaResult.id, assignId, 0)
1607+
tw.writeExprsKotlinType(arrayAccessId, arrayType.kotlinResult.id)
1608+
tw.writeHasLocation(arrayAccessId, locId)
1609+
tw.writeCallableEnclosingExpr(arrayAccessId, callable)
1610+
tw.writeStatementEnclosingExpr(arrayAccessId, enclosingStmt)
1611+
1612+
extractExpressionExpr(array, callable, arrayAccessId, 0, enclosingStmt)
1613+
extractExpressionExpr(arrayIdx, callable, arrayAccessId, 1, enclosingStmt)
1614+
1615+
extractExpressionExpr(assignedValue, callable, assignId, 1, enclosingStmt)
1616+
1617+
} else {
1618+
logger.errorElement("Unexpected Array.set function signature", c)
1619+
}
1620+
}
15641621
isBuiltinCall(c, "<unsafe-coerce>", "kotlin.jvm.internal") -> {
15651622

15661623
if (c.valueArgumentsCount != 1) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
| arrayGetsSets.kt:12:3:12:7 | ...[...] | arrayGetsSets.kt:12:3:12:7 | ...=... |
2+
| arrayGetsSets.kt:12:11:12:15 | ...[...] | arrayGetsSets.kt:12:3:12:7 | ...=... |
3+
| arrayGetsSets.kt:13:3:13:7 | ...[...] | arrayGetsSets.kt:13:3:13:7 | ...=... |
4+
| arrayGetsSets.kt:13:11:13:15 | ...[...] | arrayGetsSets.kt:13:3:13:7 | ...=... |
5+
| arrayGetsSets.kt:14:3:14:7 | ...[...] | arrayGetsSets.kt:14:3:14:7 | ...=... |
6+
| arrayGetsSets.kt:14:11:14:15 | ...[...] | arrayGetsSets.kt:14:3:14:7 | ...=... |
7+
| arrayGetsSets.kt:15:3:15:7 | ...[...] | arrayGetsSets.kt:15:3:15:7 | ...=... |
8+
| arrayGetsSets.kt:15:11:15:15 | ...[...] | arrayGetsSets.kt:15:3:15:7 | ...=... |
9+
| arrayGetsSets.kt:16:3:16:7 | ...[...] | arrayGetsSets.kt:16:3:16:7 | ...=... |
10+
| arrayGetsSets.kt:16:11:16:15 | ...[...] | arrayGetsSets.kt:16:3:16:7 | ...=... |
11+
| arrayGetsSets.kt:17:3:17:7 | ...[...] | arrayGetsSets.kt:17:3:17:7 | ...=... |
12+
| arrayGetsSets.kt:17:11:17:15 | ...[...] | arrayGetsSets.kt:17:3:17:7 | ...=... |
13+
| arrayGetsSets.kt:18:3:18:7 | ...[...] | arrayGetsSets.kt:18:3:18:7 | ...=... |
14+
| arrayGetsSets.kt:18:11:18:15 | ...[...] | arrayGetsSets.kt:18:3:18:7 | ...=... |
15+
| arrayGetsSets.kt:19:3:19:7 | ...[...] | arrayGetsSets.kt:19:3:19:7 | ...=... |
16+
| arrayGetsSets.kt:19:11:19:15 | ...[...] | arrayGetsSets.kt:19:3:19:7 | ...=... |
17+
| arrayGetsSets.kt:20:3:20:7 | ...[...] | arrayGetsSets.kt:20:3:20:7 | ...=... |
18+
| arrayGetsSets.kt:20:11:20:15 | ...[...] | arrayGetsSets.kt:20:3:20:7 | ...=... |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java
2+
3+
from ArrayAccess aa
4+
select aa, aa.getParent()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fun arrayGetSet(
2+
a1: IntArray,
3+
a2: ShortArray,
4+
a3: ByteArray,
5+
a4: LongArray,
6+
a5: FloatArray,
7+
a6: DoubleArray,
8+
a7: BooleanArray,
9+
a8: CharArray,
10+
a9: Array<Any>) {
11+
12+
a1[0] = a1[0]
13+
a2[0] = a2[0]
14+
a3[0] = a3[0]
15+
a4[0] = a4[0]
16+
a5[0] = a5[0]
17+
a6[0] = a6[0]
18+
a7[0] = a7[0]
19+
a8[0] = a8[0]
20+
a9[0] = a9[0]
21+
22+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sourceSignatures
1717
| arrayCreations.kt:26:27:26:36 | invoke | invoke(int) |
1818
| arrayCreations.kt:27:24:27:38 | | |
1919
| arrayCreations.kt:27:24:27:38 | invoke | invoke(int) |
20+
| arrayGetsSets.kt:1:1:22:1 | arrayGetSet | arrayGetSet(int[],short[],byte[],long[],float[],double[],boolean[],char[],java.lang.Object[]) |
2021
| primitiveArrays.kt:3:1:7:1 | <obinit> | <obinit>() |
2122
| primitiveArrays.kt:3:1:7:1 | Test | Test() |
2223
| primitiveArrays.kt:5:3:5:123 | test | test(java.lang.Integer[],java.lang.Integer[],int[],java.lang.Integer[][],java.lang.Integer[][],int[][]) |

java/ql/test/kotlin/library-tests/vararg/args.expected

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ implicitVarargsArguments
2626
| intList.kt:3:14:3:31 | listOf(...) | 0 | intList.kt:3:21:3:22 | 10 |
2727
| intList.kt:3:14:3:31 | listOf(...) | 1 | intList.kt:3:25:3:26 | 11 |
2828
| intList.kt:3:14:3:31 | listOf(...) | 2 | intList.kt:3:29:3:30 | 12 |
29-
| test.kt:7:5:7:19 | sink(...) | 0 | test.kt:7:10:7:18 | get(...) |
30-
| test.kt:7:10:7:18 | get(...) | 0 | test.kt:7:13:7:17 | get(...) |
31-
| test.kt:7:13:7:17 | get(...) | 0 | test.kt:7:16:7:16 | 0 |
32-
| test.kt:11:5:11:19 | sink(...) | 0 | test.kt:11:10:11:18 | get(...) |
33-
| test.kt:11:10:11:18 | get(...) | 0 | test.kt:11:13:11:17 | get(...) |
34-
| test.kt:11:13:11:17 | get(...) | 0 | test.kt:11:16:11:16 | 0 |
35-
| test.kt:15:5:15:19 | sink(...) | 0 | test.kt:15:10:15:18 | get(...) |
36-
| test.kt:15:10:15:18 | get(...) | 0 | test.kt:15:13:15:17 | get(...) |
37-
| test.kt:15:13:15:17 | get(...) | 0 | test.kt:15:16:15:16 | 0 |
29+
| test.kt:7:5:7:19 | sink(...) | 0 | test.kt:7:10:7:18 | ...[...] |
30+
| test.kt:11:5:11:19 | sink(...) | 0 | test.kt:11:10:11:18 | ...[...] |
31+
| test.kt:15:5:15:19 | sink(...) | 0 | test.kt:15:10:15:18 | ...[...] |
3832
| test.kt:19:14:19:31 | listOf(...) | 0 | test.kt:19:21:19:22 | 10 |
3933
| test.kt:19:14:19:31 | listOf(...) | 1 | test.kt:19:25:19:26 | 11 |
4034
| test.kt:19:14:19:31 | listOf(...) | 2 | test.kt:19:29:19:30 | 12 |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| test.kt:20:28:20:30 | 100 | test.kt:7:10:7:18 | ...[...] |
2+
| test.kt:20:28:20:30 | 100 | test.kt:11:10:11:18 | ...[...] |
3+
| test.kt:20:28:20:30 | 100 | test.kt:15:10:15:18 | ...[...] |
4+
| test.kt:20:33:20:35 | 101 | test.kt:7:10:7:18 | ...[...] |
5+
| test.kt:20:33:20:35 | 101 | test.kt:11:10:11:18 | ...[...] |
6+
| test.kt:20:33:20:35 | 101 | test.kt:15:10:15:18 | ...[...] |
7+
| test.kt:20:38:20:40 | 102 | test.kt:7:10:7:18 | ...[...] |
8+
| test.kt:20:38:20:40 | 102 | test.kt:11:10:11:18 | ...[...] |
9+
| test.kt:20:38:20:40 | 102 | test.kt:15:10:15:18 | ...[...] |
10+
| test.kt:21:24:21:25 | 20 | test.kt:7:10:7:18 | ...[...] |
11+
| test.kt:21:28:21:29 | 21 | test.kt:7:10:7:18 | ...[...] |
12+
| test.kt:21:32:21:33 | 22 | test.kt:7:10:7:18 | ...[...] |
13+
| test.kt:22:40:22:41 | 30 | test.kt:11:10:11:18 | ...[...] |
14+
| test.kt:22:44:22:45 | 31 | test.kt:11:10:11:18 | ...[...] |
15+
| test.kt:22:48:22:49 | 32 | test.kt:11:10:11:18 | ...[...] |

0 commit comments

Comments
 (0)