Skip to content

Commit 5017b21

Browse files
authored
Merge pull request #10617 from tamasvajk/kotlin-op-calls
Kotlin: extract operator expression when operator is in method call form
2 parents d54a305 + 121a564 commit 5017b21

File tree

11 files changed

+116
-31
lines changed

11 files changed

+116
-31
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,9 +1901,8 @@ open class KotlinFileExtractor(
19011901

19021902
val dr = c.dispatchReceiver
19031903
when {
1904-
c.origin == IrStatementOrigin.PLUS &&
1905-
(isNumericFunction(target, "plus")
1906-
|| isFunction(target, "kotlin", "String", "plus", null)) -> {
1904+
isNumericFunction(target, "plus")
1905+
|| isFunction(target, "kotlin", "String", "plus", false) -> {
19071906
val id = tw.getFreshIdLabel<DbAddexpr>()
19081907
val type = useType(c.type)
19091908
tw.writeExprs_addexpr(id, type.javaResult.id, parent, idx)
@@ -1918,28 +1917,28 @@ open class KotlinFileExtractor(
19181917
extractRawMethodAccess(stringPlusFn, c, callable, parent, idx, enclosingStmt, listOf(c.extensionReceiver, c.getValueArgument(0)), null, null)
19191918
}
19201919
}
1921-
c.origin == IrStatementOrigin.MINUS && isNumericFunction(target, "minus") -> {
1920+
isNumericFunction(target, "minus") -> {
19221921
val id = tw.getFreshIdLabel<DbSubexpr>()
19231922
val type = useType(c.type)
19241923
tw.writeExprs_subexpr(id, type.javaResult.id, parent, idx)
19251924
tw.writeExprsKotlinType(id, type.kotlinResult.id)
19261925
binopDisp(id)
19271926
}
1928-
c.origin == IrStatementOrigin.MUL && isNumericFunction(target, "times") -> {
1927+
isNumericFunction(target, "times") -> {
19291928
val id = tw.getFreshIdLabel<DbMulexpr>()
19301929
val type = useType(c.type)
19311930
tw.writeExprs_mulexpr(id, type.javaResult.id, parent, idx)
19321931
tw.writeExprsKotlinType(id, type.kotlinResult.id)
19331932
binopDisp(id)
19341933
}
1935-
c.origin == IrStatementOrigin.DIV && isNumericFunction(target, "div") -> {
1934+
isNumericFunction(target, "div") -> {
19361935
val id = tw.getFreshIdLabel<DbDivexpr>()
19371936
val type = useType(c.type)
19381937
tw.writeExprs_divexpr(id, type.javaResult.id, parent, idx)
19391938
tw.writeExprsKotlinType(id, type.kotlinResult.id)
19401939
binopDisp(id)
19411940
}
1942-
c.origin == IrStatementOrigin.PERC && isNumericFunction(target, "rem") -> {
1941+
isNumericFunction(target, "rem") -> {
19431942
val id = tw.getFreshIdLabel<DbRemexpr>()
19441943
val type = useType(c.type)
19451944
tw.writeExprs_remexpr(id, type.javaResult.id, parent, idx)

java/ql/integration-tests/posix-only/kotlin/gradle_kotlinx_serialization/PrintAst.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ app/src/main/kotlin/testProject/App.kt:
125125
# 0| 1: [ExprStmt] <Expr>;
126126
# 0| 0: [AssignExpr] ...=...
127127
# 0| 0: [VarAccess] result
128-
# 0| 1: [MethodAccess] plus(...)
129-
# 0| -1: [MethodAccess] times(...)
130-
# 0| -1: [VarAccess] result
131-
# 0| 0: [IntegerLiteral] 31
132-
# 0| 0: [MethodAccess] hashCode(...)
128+
# 0| 1: [AddExpr] ... + ...
129+
# 0| 0: [MulExpr] ... * ...
130+
# 0| 0: [VarAccess] result
131+
# 0| 1: [IntegerLiteral] 31
132+
# 0| 1: [MethodAccess] hashCode(...)
133133
# 0| -1: [VarAccess] this.language
134134
# 0| -1: [ThisAccess] this
135135
# 0| 2: [ReturnStmt] return ...

java/ql/test/kotlin/library-tests/data-classes/PrintAst.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ dc.kt:
8989
# 0| 1: [ExprStmt] <Expr>;
9090
# 0| 0: [AssignExpr] ...=...
9191
# 0| 0: [VarAccess] result
92-
# 0| 1: [MethodAccess] plus(...)
93-
# 0| -1: [MethodAccess] times(...)
94-
# 0| -1: [VarAccess] result
95-
# 0| 0: [IntegerLiteral] 31
96-
# 0| 0: [MethodAccess] hashCode(...)
92+
# 0| 1: [AddExpr] ... + ...
93+
# 0| 0: [MulExpr] ... * ...
94+
# 0| 0: [VarAccess] result
95+
# 0| 1: [IntegerLiteral] 31
96+
# 0| 1: [MethodAccess] hashCode(...)
9797
# 0| -1: [TypeAccess] Arrays
9898
# 0| 0: [VarAccess] this.strs
9999
# 0| -1: [ThisAccess] this
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
| dc.kt:0:0:0:0 | hashCode(...) | java.util.Arrays.hashCode |
22
| dc.kt:0:0:0:0 | hashCode(...) | java.util.Arrays.hashCode |
33
| dc.kt:0:0:0:0 | new ProtoMapValue(...) | ProtoMapValue.ProtoMapValue |
4-
| dc.kt:0:0:0:0 | plus(...) | kotlin.Int.plus |
5-
| dc.kt:0:0:0:0 | times(...) | kotlin.Int.times |
64
| dc.kt:0:0:0:0 | toString(...) | java.util.Arrays.toString |
75
| dc.kt:0:0:0:0 | toString(...) | java.util.Arrays.toString |
86
| dc.kt:1:1:1:71 | super(...) | java.lang.Object.Object |

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,8 @@ exprs.kt:
24732473
# 127| 0: [VarAccess] str1
24742474
# 127| 1: [VarAccess] str2
24752475
# 127| 2: [StringLiteral] bar
2476-
# 127| 3: [AddExpr] ... + ...
2476+
# 127| 3: [MethodAccess] stringPlus(...)
2477+
# 127| -1: [TypeAccess] Intrinsics
24772478
# 127| 0: [VarAccess] str2
24782479
# 127| 1: [VarAccess] str1
24792480
# 127| 4: [StringLiteral] baz
@@ -3052,15 +3053,16 @@ exprs.kt:
30523053
# 203| 1: [IntegerLiteral] 5
30533054
# 204| 2: [LocalVariableDeclStmt] var ...;
30543055
# 204| 1: [LocalVariableDeclExpr] b1
3055-
# 204| 0: [AddExpr] ... + ...
3056+
# 204| 0: [MethodAccess] stringPlus(...)
3057+
# 204| -1: [TypeAccess] Intrinsics
30563058
# 204| 0: [VarAccess] s
30573059
# 204| 1: [IntegerLiteral] 5
30583060
# 205| 3: [LocalVariableDeclStmt] var ...;
30593061
# 205| 1: [LocalVariableDeclExpr] b2
3060-
# 205| 0: [MethodAccess] plus(...)
3061-
# 205| -1: [NotNullExpr] ...!!
3062+
# 205| 0: [AddExpr] ... + ...
3063+
# 205| 0: [NotNullExpr] ...!!
30623064
# 205| 0: [VarAccess] s
3063-
# 205| 0: [IntegerLiteral] 5
3065+
# 205| 1: [IntegerLiteral] 5
30643066
# 206| 4: [LocalVariableDeclStmt] var ...;
30653067
# 206| 1: [LocalVariableDeclExpr] b3
30663068
# 206| 0: [AddExpr] ... + ...

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@
7373
| exprs.kt:113:14:113:21 | ... && ... | exprs.kt:113:14:113:15 | b1 | exprs.kt:113:20:113:21 | b2 |
7474
| exprs.kt:114:14:114:21 | ... \|\| ... | exprs.kt:114:14:114:15 | b1 | exprs.kt:114:20:114:21 | b2 |
7575
| exprs.kt:127:31:127:41 | ... + ... | exprs.kt:127:31:127:34 | str1 | exprs.kt:127:38:127:41 | str2 |
76-
| exprs.kt:127:50:127:60 | ... + ... | exprs.kt:127:50:127:53 | str2 | exprs.kt:127:57:127:60 | str1 |
7776
| exprs.kt:128:16:128:26 | ... + ... | exprs.kt:128:16:128:19 | str1 | exprs.kt:128:23:128:26 | str2 |
7877
| exprs.kt:131:12:131:23 | ... > ... | exprs.kt:131:12:131:19 | variable | exprs.kt:131:23:131:23 | 0 |
7978
| exprs.kt:135:12:135:20 | ... + ... | exprs.kt:135:12:135:14 | 123 | exprs.kt:135:18:135:20 | 456 |
8079
| exprs.kt:161:8:161:16 | ... (value not-equals) ... | exprs.kt:161:8:161:8 | r | exprs.kt:161:13:161:16 | null |
8180
| exprs.kt:190:31:190:37 | ... + ... | exprs.kt:190:31:190:32 | getA1(...) | exprs.kt:190:36:190:37 | a2 |
82-
| exprs.kt:204:19:204:23 | ... + ... | exprs.kt:204:19:204:19 | s | exprs.kt:204:23:204:23 | 5 |
81+
| exprs.kt:205:23:205:29 | ... + ... | exprs.kt:205:20:205:21 | ...!! | exprs.kt:205:28:205:28 | 5 |
8382
| exprs.kt:206:19:206:25 | ... + ... | exprs.kt:206:20:206:21 | ...!! | exprs.kt:206:25:206:25 | 5 |
8483
| exprs.kt:224:12:224:47 | ... (value equals) ... | exprs.kt:224:12:224:27 | notNullPrimitive | exprs.kt:224:32:224:47 | notNullPrimitive |
8584
| exprs.kt:225:12:225:48 | ... (value equals) ... | exprs.kt:225:12:225:27 | notNullPrimitive | exprs.kt:225:32:225:48 | nullablePrimitive |

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,8 @@
13491349
| exprs.kt:127:38:127:41 | str2 | exprs.kt:4:1:136:1 | topLevelMethod | VarAccess |
13501350
| exprs.kt:127:43:127:47 | bar | exprs.kt:4:1:136:1 | topLevelMethod | StringLiteral |
13511351
| exprs.kt:127:50:127:53 | str2 | exprs.kt:4:1:136:1 | topLevelMethod | VarAccess |
1352-
| exprs.kt:127:50:127:60 | ... + ... | exprs.kt:4:1:136:1 | topLevelMethod | AddExpr |
1352+
| exprs.kt:127:50:127:60 | Intrinsics | exprs.kt:4:1:136:1 | topLevelMethod | TypeAccess |
1353+
| exprs.kt:127:50:127:60 | stringPlus(...) | exprs.kt:4:1:136:1 | topLevelMethod | MethodAccess |
13531354
| exprs.kt:127:57:127:60 | str1 | exprs.kt:4:1:136:1 | topLevelMethod | VarAccess |
13541355
| exprs.kt:127:62:127:65 | baz | exprs.kt:4:1:136:1 | topLevelMethod | StringLiteral |
13551356
| exprs.kt:128:5:128:26 | str6 | exprs.kt:4:1:136:1 | topLevelMethod | LocalVariableDeclExpr |
@@ -1554,12 +1555,13 @@
15541555
| exprs.kt:203:26:203:26 | 5 | exprs.kt:200:5:211:5 | x | IntegerLiteral |
15551556
| exprs.kt:204:9:204:23 | b1 | exprs.kt:200:5:211:5 | x | LocalVariableDeclExpr |
15561557
| exprs.kt:204:19:204:19 | s | exprs.kt:200:5:211:5 | x | VarAccess |
1557-
| exprs.kt:204:19:204:23 | ... + ... | exprs.kt:200:5:211:5 | x | AddExpr |
1558+
| exprs.kt:204:19:204:23 | Intrinsics | exprs.kt:200:5:211:5 | x | TypeAccess |
1559+
| exprs.kt:204:19:204:23 | stringPlus(...) | exprs.kt:200:5:211:5 | x | MethodAccess |
15581560
| exprs.kt:204:23:204:23 | 5 | exprs.kt:200:5:211:5 | x | IntegerLiteral |
15591561
| exprs.kt:205:9:205:29 | b2 | exprs.kt:200:5:211:5 | x | LocalVariableDeclExpr |
15601562
| exprs.kt:205:19:205:19 | s | exprs.kt:200:5:211:5 | x | VarAccess |
15611563
| exprs.kt:205:20:205:21 | ...!! | exprs.kt:200:5:211:5 | x | NotNullExpr |
1562-
| exprs.kt:205:23:205:29 | plus(...) | exprs.kt:200:5:211:5 | x | MethodAccess |
1564+
| exprs.kt:205:23:205:29 | ... + ... | exprs.kt:200:5:211:5 | x | AddExpr |
15631565
| exprs.kt:205:28:205:28 | 5 | exprs.kt:200:5:211:5 | x | IntegerLiteral |
15641566
| exprs.kt:206:9:206:25 | b3 | exprs.kt:200:5:211:5 | x | LocalVariableDeclExpr |
15651567
| exprs.kt:206:19:206:19 | s | exprs.kt:200:5:211:5 | x | VarAccess |
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
test.kt:
2+
# 0| [CompilationUnit] test
3+
# 0| 1: [Class] TestKt
4+
# 1| 1: [Method] test
5+
# 1| 3: [TypeAccess] int
6+
#-----| 4: (Parameters)
7+
# 1| 0: [Parameter] m
8+
# 1| 0: [TypeAccess] Map<Integer,Integer>
9+
# 1| 0: [TypeAccess] Integer
10+
# 1| 1: [TypeAccess] Integer
11+
# 1| 5: [BlockStmt] { ... }
12+
# 1| 0: [ReturnStmt] return ...
13+
# 1| 0: [MethodAccess] getOrDefault(...)
14+
# 1| -1: [VarAccess] m
15+
# 1| 0: [IntegerLiteral] 1
16+
# 1| 1: [IntegerLiteral] 2
17+
# 3| 2: [Method] test2
18+
# 3| 3: [TypeAccess] int
19+
#-----| 4: (Parameters)
20+
# 3| 0: [Parameter] s
21+
# 3| 0: [TypeAccess] String
22+
# 3| 5: [BlockStmt] { ... }
23+
# 3| 0: [ReturnStmt] return ...
24+
# 3| 0: [MethodAccess] length(...)
25+
# 3| -1: [VarAccess] s
26+
# 5| 3: [Method] remove
27+
# 5| 3: [TypeAccess] Unit
28+
#-----| 4: (Parameters)
29+
# 5| 0: [Parameter] l
30+
# 5| 0: [TypeAccess] List<Integer>
31+
# 5| 0: [TypeAccess] Integer
32+
# 5| 5: [BlockStmt] { ... }
33+
# 6| 0: [ExprStmt] <Expr>;
34+
# 6| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
35+
# 6| 0: [TypeAccess] Unit
36+
# 6| 1: [MethodAccess] remove(...)
37+
# 6| -1: [VarAccess] l
38+
# 6| 0: [IntegerLiteral] 5
39+
# 9| 4: [Method] fn1
40+
# 9| 3: [TypeAccess] String
41+
#-----| 4: (Parameters)
42+
# 9| 0: [Parameter] s
43+
# 9| 0: [TypeAccess] String
44+
# 9| 5: [BlockStmt] { ... }
45+
# 9| 0: [ReturnStmt] return ...
46+
# 9| 0: [AddExpr] ... + ...
47+
# 9| 0: [VarAccess] s
48+
# 9| 1: [StringLiteral]
49+
# 10| 5: [Method] fn2
50+
# 10| 3: [TypeAccess] String
51+
#-----| 4: (Parameters)
52+
# 10| 0: [Parameter] s
53+
# 10| 0: [TypeAccess] String
54+
# 10| 5: [BlockStmt] { ... }
55+
# 10| 0: [ReturnStmt] return ...
56+
# 10| 0: [AddExpr] ... + ...
57+
# 10| 0: [VarAccess] s
58+
# 10| 1: [StringLiteral]
59+
# 12| 6: [Method] fn1
60+
# 12| 3: [TypeAccess] int
61+
#-----| 4: (Parameters)
62+
# 12| 0: [Parameter] i
63+
# 12| 0: [TypeAccess] int
64+
# 12| 5: [BlockStmt] { ... }
65+
# 12| 0: [ReturnStmt] return ...
66+
# 12| 0: [SubExpr] ... - ...
67+
# 12| 0: [VarAccess] i
68+
# 12| 1: [IntegerLiteral] 10
69+
# 13| 7: [Method] fn2
70+
# 13| 3: [TypeAccess] int
71+
#-----| 4: (Parameters)
72+
# 13| 0: [Parameter] i
73+
# 13| 0: [TypeAccess] int
74+
# 13| 5: [BlockStmt] { ... }
75+
# 13| 0: [ReturnStmt] return ...
76+
# 13| 0: [SubExpr] ... - ...
77+
# 13| 0: [VarAccess] i
78+
# 13| 1: [IntegerLiteral] 10
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql

java/ql/test/kotlin/library-tests/java-map-methods/test.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ fun test2(s: String) = s.length
55
fun remove(l: MutableList<Int>) {
66
l.remove(5)
77
}
8+
9+
fun fn1(s: String) = s.plus(other = "")
10+
fun fn2(s: String) = s + ""
11+
12+
fun fn1(i: Int) = i.minus(10)
13+
fun fn2(i: Int) = i - 10

0 commit comments

Comments
 (0)