Skip to content

Commit 46fb986

Browse files
committed
Add lateinit test to print the extracted AST
1 parent 082544e commit 46fb986

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
test.kt:
2+
# 0| [CompilationUnit] test
3+
# 1| 1: [Class] LateInit
4+
# 1| 1: [Constructor] LateInit
5+
# 1| 5: [BlockStmt] { ... }
6+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
7+
# 1| 1: [BlockStmt] { ... }
8+
# 2| 2: [FieldDeclaration] LateInit test0;
9+
# 2| -1: [TypeAccess] LateInit
10+
# 2| 3: [Method] getTest0$private
11+
# 2| 3: [TypeAccess] LateInit
12+
# 2| 5: [BlockStmt] { ... }
13+
# 2| 0: [ReturnStmt] return ...
14+
# 2| 0: [VarAccess] this.test0
15+
# 2| -1: [ThisAccess] this
16+
# 2| 4: [Method] setTest0$private
17+
# 2| 3: [TypeAccess] Unit
18+
#-----| 4: (Parameters)
19+
# 2| 0: [Parameter] <set-?>
20+
# 2| 0: [TypeAccess] LateInit
21+
# 2| 5: [BlockStmt] { ... }
22+
# 2| 0: [ExprStmt] <Expr>;
23+
# 2| 0: [AssignExpr] ...=...
24+
# 2| 0: [VarAccess] this.test0
25+
# 2| -1: [ThisAccess] this
26+
# 2| 1: [VarAccess] <set-?>
27+
# 4| 5: [Method] f
28+
# 4| 3: [TypeAccess] Unit
29+
# 4| 5: [BlockStmt] { ... }
30+
# 4| 0: [ReturnStmt] return ...
31+
# 4| 0: [MethodAccess] println(...)
32+
# 4| -1: [TypeAccess] ConsoleKt
33+
# 4| 0: [StringLiteral] a
34+
# 6| 6: [Method] init
35+
# 6| 3: [TypeAccess] LateInit
36+
# 6| 5: [BlockStmt] { ... }
37+
# 6| 0: [ReturnStmt] return ...
38+
# 6| 0: [ClassInstanceExpr] new LateInit(...)
39+
# 6| -3: [TypeAccess] LateInit
40+
# 8| 7: [Method] fn
41+
# 8| 3: [TypeAccess] Unit
42+
# 8| 5: [BlockStmt] { ... }
43+
# 9| 0: [ExprStmt] <Expr>;
44+
# 9| 0: [MethodAccess] f(...)
45+
# 9| -1: [MethodAccess] getTest0$private(...)
46+
# 9| -1: [ThisAccess] this
47+
# 10| 1: [ExprStmt] <Expr>;
48+
# 10| 0: [WhenExpr] when ...
49+
# 10| 0: [WhenBranch] ... -> ...
50+
# 10| 0: [MethodAccess] isInitialized(...)
51+
# 10| -1: [TypeAccess] LateinitKt
52+
# 10| 0: [PropertyRefExpr] ...::...
53+
# 10| -4: [AnonymousClass] new KMutableProperty0<LateInit>(...) { ... }
54+
# 10| 1: [Constructor]
55+
#-----| 4: (Parameters)
56+
# 10| 0: [Parameter] <dispatchReceiver>
57+
# 10| 5: [BlockStmt] { ... }
58+
# 10| 0: [SuperConstructorInvocationStmt] super(...)
59+
# 10| 1: [ExprStmt] <Expr>;
60+
# 10| 0: [AssignExpr] ...=...
61+
# 10| 0: [VarAccess] this.<dispatchReceiver>
62+
# 10| -1: [ThisAccess] this
63+
# 10| 1: [VarAccess] <dispatchReceiver>
64+
# 10| 2: [FieldDeclaration] LateInit <dispatchReceiver>;
65+
# 10| -1: [TypeAccess] LateInit
66+
# 10| 3: [Method] get
67+
# 10| 5: [BlockStmt] { ... }
68+
# 10| 0: [ReturnStmt] return ...
69+
# 10| 0: [MethodAccess] getTest0$private(...)
70+
# 10| -1: [VarAccess] this.<dispatchReceiver>
71+
# 10| -1: [ThisAccess] this
72+
# 10| 4: [Method] invoke
73+
# 10| 5: [BlockStmt] { ... }
74+
# 10| 0: [ReturnStmt] return ...
75+
# 10| 0: [MethodAccess] get(...)
76+
# 10| -1: [ThisAccess] this
77+
# 10| 5: [Method] set
78+
#-----| 4: (Parameters)
79+
# 10| 0: [Parameter] a0
80+
# 10| 5: [BlockStmt] { ... }
81+
# 10| 0: [ReturnStmt] return ...
82+
# 10| 0: [MethodAccess] setTest0$private(...)
83+
# 10| -1: [VarAccess] this.<dispatchReceiver>
84+
# 10| -1: [ThisAccess] this
85+
# 10| 0: [VarAccess] a0
86+
# 10| -3: [TypeAccess] KMutableProperty0<LateInit>
87+
# 10| 0: [TypeAccess] LateInit
88+
# 10| 0: [ThisAccess] this
89+
# 10| 1: [BlockStmt] { ... }
90+
# 13| 2: [LocalVariableDeclStmt] var ...;
91+
# 13| 1: [LocalVariableDeclExpr] test1
92+
# 14| 3: [ExprStmt] <Expr>;
93+
# 14| 0: [MethodAccess] f(...)
94+
# 14| -1: [VarAccess] test1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| test.kt:4:15:4:26 | println(...) | file://<external>/ConsoleKt.class:0:0:0:0 | println |
2+
| test.kt:9:9:9:13 | getTest0$private(...) | test.kt:2:22:2:40 | getTest0$private |
3+
| test.kt:9:15:9:17 | f(...) | test.kt:4:5:4:26 | f |
4+
| test.kt:10:13:10:23 | get(...) | test.kt:10:13:10:23 | get |
5+
| test.kt:10:13:10:23 | getTest0$private(...) | test.kt:2:22:2:40 | getTest0$private |
6+
| test.kt:10:13:10:23 | setTest0$private(...) | test.kt:2:22:2:40 | setTest0$private |
7+
| test.kt:10:25:10:37 | isInitialized(...) | file://<external>/LateinitKt.class:0:0:0:0 | isInitialized |
8+
| test.kt:14:15:14:17 | f(...) | test.kt:4:5:4:26 | f |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class LateInit {
2+
private lateinit var test0: LateInit
3+
4+
fun f() = println("a")
5+
6+
fun init() = LateInit()
7+
8+
fun fn() {
9+
test0.f() // This is preceded by a null-check and a throw in bytecode, in IR it's a simple call
10+
if (this::test0.isInitialized) { // This is converted to a null-check in bytecode, in IR it's a call to `LateinitKt.isInitialized`
11+
}
12+
13+
lateinit var test1: LateInit
14+
test1.f() // This is replaced by `Intrinsics.throwUninitializedPropertyAccessException` in bytecode, in IR it's a simple call
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java
2+
3+
class MethodLocation extends Method {
4+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
5+
exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
6+
if exists(this.getFile().getRelativePath())
7+
then path = fullPath
8+
else path = fullPath.regexpReplaceAll(".*/", "<external>/")
9+
)
10+
}
11+
}
12+
13+
query predicate calls(MethodAccess ma, Method m) { ma.getMethod() = m }

0 commit comments

Comments
 (0)