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

Skip to content

Commit 613d81d

Browse files
tamasvajkigfoo
authored andcommitted
Extract static modifier and missing type access qualifier for static calls
1 parent 4cfda63 commit 613d81d

9 files changed

Lines changed: 129 additions & 6 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,21 @@ open class KotlinFileExtractor(
701701
}
702702

703703
extractVisibility(f, id, f.visibility)
704+
if (isStaticFunction(f)) {
705+
addModifiers(id, "static")
706+
}
704707

705708
return id
706709
}
707710
}
708711
}
709712

713+
private fun isStaticFunction(f: IrFunction): Boolean {
714+
return f.dispatchReceiverParameter == null // Has no dispatch receiver,
715+
&& !f.isLocalFunction() // not a local function. Local functions are extracted as instance methods with the local class instantiation as the qualifier
716+
&& f.symbol !is IrConstructorSymbol // not a constructor
717+
}
718+
710719
fun extractField(f: IrField, parentId: Label<out DbReftype>): Label<out DbField> {
711720
with("field", f) {
712721
DeclarationStackAdjuster(f).use {
@@ -1192,8 +1201,8 @@ open class KotlinFileExtractor(
11921201

11931202
if (dispatchReceiver != null) {
11941203
extractExpressionExpr(dispatchReceiver, enclosingCallable, id, -1, enclosingStmt)
1195-
} else if(callTarget.isStaticMethodOfClass) {
1196-
extractTypeAccessRecursive(callTarget.parentAsClass.toRawType(), locId, id, -1, enclosingCallable, enclosingStmt)
1204+
} else if (isStaticFunction(callTarget)) {
1205+
extractStaticCallTypeAccessQualifier(callTarget, id, locId, enclosingCallable, enclosingStmt)
11971206
}
11981207
}
11991208

@@ -1212,6 +1221,14 @@ open class KotlinFileExtractor(
12121221
extractCallValueArguments(argParent, valueArguments, enclosingStmt, enclosingCallable, idxOffset)
12131222
}
12141223

1224+
private fun extractStaticCallTypeAccessQualifier(target: IrFunction, parentExpr: Label<out DbExprparent>, locId: Label<DbLocation>, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) {
1225+
if (target.isStaticMethodOfClass) {
1226+
extractTypeAccessRecursive(target.parentAsClass.toRawType(), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
1227+
} else if (target is IrSimpleFunction && target.dispatchReceiverParameter == null && target.parent is IrFile) {
1228+
extractTypeAccess(useFileClassType(target.parent as IrFile), locId, parentExpr, -1, enclosingCallable, enclosingStmt)
1229+
}
1230+
}
1231+
12151232
private fun extractCallValueArguments(callId: Label<out DbExprparent>, call: IrFunctionAccessExpression, enclosingStmt: Label<out DbStmt>, enclosingCallable: Label<out DbCallable>, idxOffset: Int) =
12161233
extractCallValueArguments(callId, (0 until call.valueArgumentsCount).map { call.getValueArgument(it) }, enclosingStmt, enclosingCallable, idxOffset)
12171234

@@ -2932,6 +2949,10 @@ open class KotlinFileExtractor(
29322949
}
29332950
else {
29342951
useFirstArgAsDispatch = target.owner.dispatchReceiverParameter != null
2952+
2953+
if (isStaticFunction(target.owner)) {
2954+
extractStaticCallTypeAccessQualifier(target.owner, callId, locId, labels.methodId, retId)
2955+
}
29352956
}
29362957
}
29372958

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ open class KotlinUsesExtractor(
4343
return id
4444
}
4545

46+
fun useFileClassType(f: IrFile) = TypeResults(
47+
TypeResult(extractFileClass(f), "", ""),
48+
TypeResult(fakeKotlinType(), "", "")
49+
)
4650

4751
@OptIn(kotlin.ExperimentalStdlibApi::class) // Annotation required by kotlin versions < 1.5
4852
fun extractFileClass(f: IrFile): Label<out DbClass> {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,18 @@ classes.kt:
8686
# 35| 1: [BlockStmt] { ... }
8787
# 39| 0: [ExprStmt] <Expr>;
8888
# 39| 0: [MethodAccess] f(...)
89+
# 39| -1: [TypeAccess] ClassesKt
8990
# 39| 0: [StringLiteral] init1
9091
# 42| 1: [ExprStmt] <Expr>;
9192
# 42| 0: [KtInitializerAssignExpr] ...=...
9293
# 42| 0: [VarAccess] x
9394
# 45| 2: [ExprStmt] <Expr>;
9495
# 45| 0: [MethodAccess] f(...)
96+
# 45| -1: [TypeAccess] ClassesKt
9597
# 45| 0: [StringLiteral] init2
9698
# 36| 2: [ExprStmt] <Expr>;
9799
# 36| 0: [MethodAccess] f(...)
100+
# 36| -1: [TypeAccess] ClassesKt
98101
# 36| 0: [VarAccess] i
99102
# 42| 2: [Method] getX
100103
# 42| 5: [BlockStmt] { ... }

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

Lines changed: 41 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 41 additions & 0 deletions
Large diffs are not rendered by default.

java/ql/test/kotlin/library-tests/extensions_recursion/element.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
| test.kt:7:24:9:5 | { ... } | BlockStmt |
1818
| test.kt:8:9:8:20 | return ... | ReturnStmt |
1919
| test.kt:8:16:8:20 | T | TypeAccess |
20+
| test.kt:8:16:8:20 | TestKt | TypeAccess |
2021
| test.kt:8:16:8:20 | bar(...) | MethodAccess |
2122
| test.kt:8:16:8:20 | this | ThisAccess |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| methods.kt:10:9:10:25 | this | ThisAccess |
3232
| methods.kt:10:21:10:21 | a | VarAccess |
3333
| methods.kt:10:24:10:24 | 3 | IntegerLiteral |
34+
| methods.kt:11:9:11:28 | MethodsKt | TypeAccess |
3435
| methods.kt:11:9:11:28 | topLevelMethod(...) | MethodAccess |
3536
| methods.kt:11:24:11:24 | b | VarAccess |
3637
| methods.kt:11:27:11:27 | 4 | IntegerLiteral |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
methods
2-
| methods2.kt:0:0:0:0 | Methods2Kt | methods2.kt:4:1:5:1 | fooBarTopLevelMethod | fooBarTopLevelMethod(int,int) | public |
2+
| methods2.kt:0:0:0:0 | Methods2Kt | methods2.kt:4:1:5:1 | fooBarTopLevelMethod | fooBarTopLevelMethod(int,int) | public, static |
33
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:8:5:9:5 | fooBarClassMethod | fooBarClassMethod(int,int) | public |
4-
| methods3.kt:0:0:0:0 | Methods3Kt | methods3.kt:3:1:3:42 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) | public |
4+
| methods3.kt:0:0:0:0 | Methods3Kt | methods3.kt:3:1:3:42 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) | public, static |
55
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:6:5:6:46 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) | public |
66
| methods4.kt:5:3:9:3 | InsideNestedTest | methods4.kt:7:5:7:34 | m | m(foo.bar.NestedTest.InsideNestedTest) | public |
7-
| methods5.kt:0:0:0:0 | Methods5Kt | methods5.kt:3:1:11:1 | x | x() | public |
7+
| methods5.kt:0:0:0:0 | Methods5Kt | methods5.kt:3:1:11:1 | x | x() | public, static |
88
| methods5.kt:5:3:5:27 | | methods5.kt:5:3:5:27 | a | a(int) | public |
99
| methods5.kt:9:3:9:32 | | methods5.kt:9:3:9:32 | f1 | f1(foo.bar.C1,int) | public |
10-
| methods.kt:0:0:0:0 | MethodsKt | methods.kt:2:1:3:1 | topLevelMethod | topLevelMethod(int,int) | public |
10+
| methods.kt:0:0:0:0 | MethodsKt | methods.kt:2:1:3:1 | topLevelMethod | topLevelMethod(int,int) | public, static |
1111
| methods.kt:5:1:19:1 | Class | methods.kt:6:5:7:5 | classMethod | classMethod(int,int) | public |
1212
| methods.kt:5:1:19:1 | Class | methods.kt:9:5:12:5 | anotherClassMethod | anotherClassMethod(int,int) | public |
1313
| methods.kt:5:1:19:1 | Class | methods.kt:14:12:14:29 | publicFun | publicFun() | public |

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ reflection.kt:
2929
# 50| 5: [BlockStmt] { ... }
3030
# 50| 0: [ReturnStmt] return ...
3131
# 50| 0: [MethodAccess] getLastChar(...)
32+
# 50| -1: [TypeAccess] ReflectionKt
3233
# 50| 0: [VarAccess] a0
3334
# 50| 1: [Method] invoke
3435
#-----| 4: (Parameters)
@@ -64,6 +65,7 @@ reflection.kt:
6465
# 51| 5: [BlockStmt] { ... }
6566
# 51| 0: [ReturnStmt] return ...
6667
# 51| 0: [MethodAccess] getLastChar(...)
68+
# 51| -1: [TypeAccess] ReflectionKt
6769
# 51| 0: [VarAccess] this.<extensionReceiver>
6870
# 51| -1: [ThisAccess] this
6971
# 51| 1: [Method] invoke
@@ -103,6 +105,7 @@ reflection.kt:
103105
# 97| -3: [TypeAccess] Class2<String>
104106
# 97| 0: [TypeAccess] String
105107
# 97| -2: [TypeAccess] String
108+
# 97| -1: [TypeAccess] ReflectionKt
106109
# 97| 0: [StringLiteral]
107110
# 97| 1: [MemberRefExpr] ...::...
108111
# 97| -4: [AnonymousClass] new Function1<String,Class2<String>>(...) { ... }
@@ -126,6 +129,7 @@ reflection.kt:
126129
# 98| 0: [MethodAccess] fn11(...)
127130
# 98| -3: [TypeAccess] Unit
128131
# 98| -2: [TypeAccess] String
132+
# 98| -1: [TypeAccess] ReflectionKt
129133
# 98| 0: [StringLiteral]
130134
# 98| 1: [MemberRefExpr] ...::...
131135
# 98| -4: [AnonymousClass] new Function1<String,Unit>(...) { ... }
@@ -139,6 +143,7 @@ reflection.kt:
139143
# 98| 0: [ReturnStmt] return ...
140144
# 98| 0: [MethodAccess] fn(...)
141145
# 98| -2: [TypeAccess] String
146+
# 98| -1: [TypeAccess] ReflectionKt
142147
# 98| 0: [VarAccess] a0
143148
# 98| -3: [TypeAccess] Function1<String,Unit>
144149
# 98| 0: [TypeAccess] String
@@ -149,6 +154,7 @@ reflection.kt:
149154
# 99| 0: [TypeAccess] String
150155
# 99| 1: [TypeAccess] Integer
151156
# 99| -2: [TypeAccess] String
157+
# 99| -1: [TypeAccess] ReflectionKt
152158
# 99| 0: [StringLiteral]
153159
# 99| 1: [MemberRefExpr] ...::...
154160
# 99| -4: [AnonymousClass] new Function1<String,Inner<String>>(...) { ... }
@@ -640,6 +646,7 @@ reflection.kt:
640646
# 62| 0: [ReturnStmt] return ...
641647
# 62| 0: [MethodAccess] ext1(...)
642648
# 62| -2: [TypeAccess] Integer
649+
# 62| -1: [TypeAccess] ReflectionKt
643650
# 62| 0: [VarAccess] a0
644651
# 62| -3: [TypeAccess] Function1<Generic<Integer>,String>
645652
# 62| 0: [TypeAccess] Generic<Integer>
@@ -665,6 +672,7 @@ reflection.kt:
665672
# 63| 0: [ReturnStmt] return ...
666673
# 63| 0: [MethodAccess] ext1(...)
667674
# 63| -2: [TypeAccess] Integer
675+
# 63| -1: [TypeAccess] ReflectionKt
668676
# 63| 0: [VarAccess] this.<extensionReceiver>
669677
# 63| -1: [ThisAccess] this
670678
# 63| 1: [FieldDeclaration] Generic<Integer> <extensionReceiver>;
@@ -689,6 +697,7 @@ reflection.kt:
689697
# 64| 5: [BlockStmt] { ... }
690698
# 64| 0: [ReturnStmt] return ...
691699
# 64| 0: [MethodAccess] ext2(...)
700+
# 64| -1: [TypeAccess] ReflectionKt
692701
# 64| 0: [VarAccess] a0
693702
# 64| -3: [TypeAccess] Function1<Generic<Integer>,String>
694703
# 64| 0: [TypeAccess] Generic<Integer>
@@ -713,6 +722,7 @@ reflection.kt:
713722
# 65| 5: [BlockStmt] { ... }
714723
# 65| 0: [ReturnStmt] return ...
715724
# 65| 0: [MethodAccess] ext2(...)
725+
# 65| -1: [TypeAccess] ReflectionKt
716726
# 65| 0: [VarAccess] this.<extensionReceiver>
717727
# 65| -1: [ThisAccess] this
718728
# 65| 1: [FieldDeclaration] Generic<Integer> <extensionReceiver>;
@@ -955,6 +965,7 @@ reflection.kt:
955965
# 90| 0: [TypeAccess] String
956966
# 90| 1: [TypeAccess] T
957967
# 90| -2: [TypeAccess] String
968+
# 90| -1: [TypeAccess] ReflectionKt
958969
# 90| 0: [StringLiteral]
959970
# 90| 1: [MemberRefExpr] ...::...
960971
# 90| -4: [AnonymousClass] new Function1<String,Inner<String>>(...) { ... }

0 commit comments

Comments
 (0)