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

Skip to content

Commit e5003e4

Browse files
tamasvajkigfoo
authored andcommitted
Adjust PrintAST query to handle kotlin constructs
1 parent 73cd497 commit e5003e4

3 files changed

Lines changed: 1468 additions & 3 deletions

File tree

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ private predicate isNotNeeded(Element el) {
6868
el.(ExprOrStmt).getEnclosingCallable() = c
6969
|
7070
el.getLocation().hasLocationInfo(_, sline, eline, scol, ecol) and
71-
c.getLocation().hasLocationInfo(_, sline, eline, scol, ecol)
71+
c.getLocation().hasLocationInfo(_, sline, eline, scol, ecol) and
72+
not c.getFile().isKotlinSourceFile() // Koltin constructor bodies have the same location as the constructor
7273
// simply comparing their getLocation() doesn't work as they have distinct but equivalent locations
7374
)
7475
or
76+
exists(Callable c | el = c and c.getSourceDeclaration() != c)
77+
or
7578
isNotNeeded(el.(Expr).getParent*().(Annotation).getAnnotatedElement())
7679
or
7780
isNotNeeded(el.(Parameter).getCallable())
@@ -114,6 +117,7 @@ private predicate locationSortKeys(Element ast, string file, int line, int colum
114117
private newtype TPrintAstNode =
115118
TElementNode(Element el) { shouldPrint(el, _) } or
116119
TForInitNode(ForStmt fs) { shouldPrint(fs, _) and exists(fs.getAnInit()) } or
120+
TWhenBranchNode(WhenBranch wb) { shouldPrint(wb.getWhenExpr(), _) } or
117121
TLocalVarDeclNode(LocalVariableDeclExpr lvde) {
118122
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
119123
} or
@@ -298,7 +302,8 @@ final class ClassInstanceExprNode extends ExprStmtNode {
298302
result = super.getChild(childIndex)
299303
or
300304
childIndex = -4 and
301-
result.getElement() = element.(ClassInstanceExpr).getAnonymousClass()
305+
result.getElement() = element.(ClassInstanceExpr).getAnonymousClass() and
306+
not result.getElement() instanceof LocalClassOrInterface // Kotlin anonymous classes are extracted as local classes too.
302307
}
303308
}
304309

@@ -337,6 +342,18 @@ final class ForStmtNode extends ExprStmtNode {
337342
}
338343
}
339344

345+
/**
346+
* A node representing a `WhenExpr`.
347+
*/
348+
final class WhenExprNode extends ExprStmtNode {
349+
WhenExprNode() { element instanceof WhenExpr }
350+
351+
override PrintAstNode getChild(int childIndex) {
352+
childIndex >= 0 and
353+
result.(WhenBranchNode).getWhenBranch() = element.(WhenExpr).getBranch(childIndex)
354+
}
355+
}
356+
340357
/**
341358
* An element that can be the parent of up to one `LocalVariableDeclExpr` for which we want
342359
* to use a synthetic node to hold the variable declaration and its `TypeAccess`.
@@ -553,6 +570,30 @@ final class ForInitNode extends PrintAstNode, TForInitNode {
553570
ForStmt getForStmt() { result = fs }
554571
}
555572

573+
/**
574+
* A node representing the synthetic node of a `when` expression branch.
575+
*/
576+
final class WhenBranchNode extends PrintAstNode, TWhenBranchNode {
577+
WhenBranch wb;
578+
579+
WhenBranchNode() { this = TWhenBranchNode(wb) }
580+
581+
override string toString() { result = "(branch)" }
582+
583+
override ElementNode getChild(int childIndex) {
584+
childIndex = 0 and
585+
result.getElement().(Expr).isNthChildOf(wb, childIndex)
586+
or
587+
childIndex = 1 and
588+
result.getElement().(Stmt).isNthChildOf(wb, childIndex)
589+
}
590+
591+
/**
592+
* Gets the underlying `WhenBranch`.
593+
*/
594+
WhenBranch getWhenBranch() { result = wb }
595+
}
596+
556597
/**
557598
* A synthetic node holding a `LocalVariableDeclExpr` and its type access.
558599
*/
@@ -681,7 +722,7 @@ final class GenericTypeNode extends PrintAstNode, TGenericTypeNode {
681722
final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
682723
GenericCallable c;
683724

684-
GenericCallableNode() { this = TGenericCallableNode(c) }
725+
GenericCallableNode() { this = TGenericCallableNode(c) and not isNotNeeded(c) }
685726

686727
override string toString() { result = "(Generic Parameters)" }
687728

0 commit comments

Comments
 (0)