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

Skip to content

Commit dc64b53

Browse files
smowtonigfoo
authored andcommitted
Ensure that initializers are only printed once in a PrintAst run
Otherwise the output becomes a DAG not a tree. Java achieves the same by omitting all PrintAst of `<obinit>` routines.
1 parent 13cd145 commit dc64b53

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

java/ql/lib/semmle/code/java/PrintAst.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ class ExprStmtNode extends ElementNode {
244244
}
245245
}
246246

247+
/**
248+
* A node representing a `KtInitializerAssignExpr`.
249+
*/
250+
class KtInitializerNode extends ExprStmtNode {
251+
KtInitializerNode() { element instanceof KtInitializerAssignExpr }
252+
253+
override PrintAstNode getChild(int childIndex) {
254+
// Remove the RHS of the initializer, because otherwise
255+
// it appears as both the initializer's child and the
256+
// initialize of the related field, producing a DAG not
257+
// a tree and consequently unreadable output.
258+
result = super.getChild(childIndex) and childIndex = 0
259+
}
260+
}
261+
247262
/**
248263
* Holds if the given expression is part of an annotation.
249264
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ generics.kt:
8686
# 13| 0: [SuperConstructorInvocationStmt] super(...)
8787
# 13| 1: [BlockStmt] { ... }
8888
# 13| 0: [ExprStmt] <Expr>;
89-
# 13| 0: [AssignExpr] ...=...
89+
# 13| 0: [KtInitializerAssignExpr] ...=...
9090
# 13| 0: [VarAccess] t
91-
# 13| 1: [VarAccess] t
9291
# 13| 2: [Method] getT
9392
# 13| 5: [BlockStmt] { ... }
9493
# 13| 0: [ReturnStmt] return ...
9594
# 13| 0: [VarAccess] this.t
9695
# 13| -1: [ThisAccess] this
9796
# 13| 2: [FieldDeclaration] T t;
9897
# 13| -1: [TypeAccess] T
98+
# 13| 0: [VarAccess] t
9999
# 14| 4: [Method] f1
100100
#-----| 4: (Parameters)
101101
# 14| 0: [Parameter] t

0 commit comments

Comments
 (0)