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

Skip to content

Commit 64531dd

Browse files
atorralbaigfoo
authored andcommitted
Fix AST representation of WhenExpr and WhenBranch
1 parent 4b22e1a commit 64531dd

8 files changed

Lines changed: 199 additions & 82 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2413,7 +2413,7 @@ open class KotlinFileExtractor(
24132413
val bLocId = tw.getLocation(b)
24142414
tw.writeStmts_whenbranch(bId, id, i, callable)
24152415
tw.writeHasLocation(bId, bLocId)
2416-
extractExpressionExpr(b.condition, callable, bId, 0, exprParent.enclosingStmt)
2416+
extractExpressionExpr(b.condition, callable, bId, 0, bId)
24172417
extractExpressionStmt(b.result, callable, bId, 1)
24182418
if(b is IrElseBranch) {
24192419
tw.writeWhen_branch_else(bId)

java/ql/consistency-queries/statementEnclosingExpr.ql

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import java
33
Element enclosingStmtOrOther(ExprParent ep) {
44
result = ep.(Stmt) or
55
result = enclosingStmtOrOther(ep.(Expr).getParent()) or
6-
result = enclosingStmtOrOther(ep.(WhenBranch).getWhenExpr()) or
76
result = ep.(Callable) or
87
result = enclosingStmtOrOther(ep.(Field).getDeclaration()) or
98
result = ep.(FieldDeclaration) or
@@ -14,47 +13,46 @@ Element enclosingStmtOrOther(ExprParent ep) {
1413
result = ep.(TypeVariable)
1514
}
1615

17-
predicate multipleSpecified(Expr e) {
18-
e.getEnclosingStmt() != e.getEnclosingStmt()
19-
}
16+
predicate multipleSpecified(Expr e) { e.getEnclosingStmt() != e.getEnclosingStmt() }
2017

21-
predicate multipleInferred(Expr e) {
22-
enclosingStmtOrOther(e) != enclosingStmtOrOther(e)
23-
}
18+
predicate multipleInferred(Expr e) { enclosingStmtOrOther(e) != enclosingStmtOrOther(e) }
2419

25-
predicate noneInferred(Expr e) {
26-
not exists(enclosingStmtOrOther(e))
27-
}
20+
predicate noneInferred(Expr e) { not exists(enclosingStmtOrOther(e)) }
2821

2922
predicate difference(Expr e) {
30-
e.getEnclosingStmt() != enclosingStmtOrOther(e)
23+
e.getEnclosingStmt() != enclosingStmtOrOther(e) and
3124
// Java #167
32-
and not enclosingStmtOrOther(e) instanceof LocalVariableDeclStmt
25+
not enclosingStmtOrOther(e) instanceof LocalVariableDeclStmt
3326
}
3427

3528
predicate notSpecified(Expr e) {
36-
enclosingStmtOrOther(e) instanceof Stmt and not exists(e.getEnclosingStmt())
29+
enclosingStmtOrOther(e) instanceof Stmt and
30+
not exists(e.getEnclosingStmt()) and
3731
// Java #168
38-
and not (e instanceof Annotation and e.getFile().isJavaSourceFile())
32+
not (e instanceof Annotation and e.getFile().isJavaSourceFile()) and
3933
// TODO: Kotlin bug
40-
and not (e instanceof ArrayCreationExpr and e.getFile().isKotlinSourceFile())
34+
not (e instanceof ArrayCreationExpr and e.getFile().isKotlinSourceFile()) and
4135
// TODO: Kotlin bug
42-
and not (e instanceof VarAccess and e.getFile().isKotlinSourceFile())
36+
not (e instanceof VarAccess and e.getFile().isKotlinSourceFile()) and
4337
// TODO: Kotlin bug
44-
and not (e instanceof TypeAccess and e.getFile().isKotlinSourceFile())
38+
not (e instanceof TypeAccess and e.getFile().isKotlinSourceFile())
4539
}
4640

4741
predicate problem(Expr e, string s) {
48-
multipleSpecified(e) and s = "multiple specified" or
49-
multipleInferred(e) and s = "multiple inferred" or
50-
noneInferred(e) and s = "none inferred" or
51-
difference(e) and s = "difference" or
42+
multipleSpecified(e) and s = "multiple specified"
43+
or
44+
multipleInferred(e) and s = "multiple inferred"
45+
or
46+
noneInferred(e) and s = "none inferred"
47+
or
48+
difference(e) and s = "difference"
49+
or
5250
notSpecified(e) and s = "not specified"
5351
}
5452

5553
from Expr e, string s
56-
where problem(e, s)
57-
and // TODO: bug in external deps?
58-
e.getCompilationUnit().fromSource()
54+
where
55+
problem(e, s) and
56+
// TODO: bug in external deps?
57+
e.getCompilationUnit().fromSource()
5958
select e, s, e.getPrimaryQlClasses()
60-

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ private predicate locationSortKeys(Element ast, string file, int line, int colum
117117
private newtype TPrintAstNode =
118118
TElementNode(Element el) { shouldPrint(el, _) } or
119119
TForInitNode(ForStmt fs) { shouldPrint(fs, _) and exists(fs.getAnInit()) } or
120-
TWhenBranchNode(WhenBranch wb) { shouldPrint(wb.getWhenExpr(), _) } or
121120
TLocalVarDeclNode(LocalVariableDeclExpr lvde) {
122121
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
123122
} or
@@ -357,18 +356,6 @@ final class ForStmtNode extends ExprStmtNode {
357356
}
358357
}
359358

360-
/**
361-
* A node representing a `WhenExpr`.
362-
*/
363-
final class WhenExprNode extends ExprStmtNode {
364-
WhenExprNode() { element instanceof WhenExpr }
365-
366-
override PrintAstNode getChild(int childIndex) {
367-
childIndex >= 0 and
368-
result.(WhenBranchNode).getWhenBranch() = element.(WhenExpr).getBranch(childIndex)
369-
}
370-
}
371-
372359
/**
373360
* An element that can be the parent of up to one `LocalVariableDeclExpr` for which we want
374361
* to use a synthetic node to hold the variable declaration and its `TypeAccess`.
@@ -585,30 +572,6 @@ final class ForInitNode extends PrintAstNode, TForInitNode {
585572
ForStmt getForStmt() { result = fs }
586573
}
587574

588-
/**
589-
* A node representing the synthetic node of a `when` expression branch.
590-
*/
591-
final class WhenBranchNode extends PrintAstNode, TWhenBranchNode {
592-
WhenBranch wb;
593-
594-
WhenBranchNode() { this = TWhenBranchNode(wb) }
595-
596-
override string toString() { result = "(branch)" }
597-
598-
override ElementNode getChild(int childIndex) {
599-
childIndex = 0 and
600-
result.getElement().(Expr).isNthChildOf(wb, childIndex)
601-
or
602-
childIndex = 1 and
603-
result.getElement().(Stmt).isNthChildOf(wb, childIndex)
604-
}
605-
606-
/**
607-
* Gets the underlying `WhenBranch`.
608-
*/
609-
WhenBranch getWhenBranch() { result = wb }
610-
}
611-
612575
/**
613576
* A synthetic node holding a `LocalVariableDeclExpr` and its type access.
614577
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ classes.kt:
150150
# 64| 5: [BlockStmt] { ... }
151151
# 65| 0: [ExprStmt] <Expr>;
152152
# 65| 0: [WhenExpr] when ...
153-
#-----| 0: (branch)
153+
# 65| 0: [WhenBranch] ... -> ...
154154
# 65| 0: [VarAccess] b
155155
# 66| 1: [ReturnStmt] return ...
156156
# 66| 0: [StmtExpr] <Stmt>
@@ -163,7 +163,7 @@ classes.kt:
163163
# 66| 1: [ExprStmt] <Expr>;
164164
# 66| 0: [ClassInstanceExpr] new (...)
165165
# 66| -3: [TypeAccess] Object
166-
#-----| 1: (branch)
166+
# 65| 1: [WhenBranch] ... -> ...
167167
# 65| 0: [BooleanLiteral] true
168168
# 68| 1: [ReturnStmt] return ...
169169
# 68| 0: [StmtExpr] <Stmt>

0 commit comments

Comments
 (0)