CFG changes for non-null operator + some tests

This commit is contained in:
Tamas Vajk
2021-11-24 11:28:48 +01:00
committed by Ian Lynagh
parent de137415b8
commit 21af31f3ab
9 changed files with 185 additions and 10 deletions

View File

@@ -163,7 +163,8 @@ private module ControlFlowGraphImpl {
* Bind `t` to an exception type that may be thrown during execution of `n`,
* either because `n` is a `throw` statement, or because it is a call
* that may throw an exception, or because it is a cast and a
* `ClassCastException` is expected.
* `ClassCastException` is expected, or because it is a Kotlin not-null check
* and a `NullPointerException` is expected.
*/
private predicate mayThrow(ControlFlowNode n, ThrowableType t) {
t = n.(ThrowStmt).getThrownExceptionType()
@@ -179,6 +180,11 @@ private module ControlFlowGraphImpl {
t instanceof TypeClassCastException and
uncheckedExceptionFromCatch(n, t)
)
or
exists(NotNullExpr nn | nn = n |
t instanceof TypeNullPointerException and
uncheckedExceptionFromCatch(n, t)
)
}
/**
@@ -304,12 +310,12 @@ private module ControlFlowGraphImpl {
or
exists(ConditionalStmt condstmt | condstmt.getCondition() = b)
or
exists(WhenBranch whenbranch |
whenbranch.getCondition() = b)
exists(WhenBranch whenbranch | whenbranch.getCondition() = b)
or
exists(WhenExpr whenexpr |
inBooleanContext(whenexpr) and
whenexpr.getBranch(_).getAResult() = b)
whenexpr.getBranch(_).getAResult() = b
)
}
/**
@@ -419,7 +425,9 @@ private module ControlFlowGraphImpl {
exists(WhenExpr whenexpr | whenexpr = result |
whenexpr.getBranch(_).isElseBranch() and
forex(WhenBranch whenbranch | whenbranch = whenexpr.getBranch(_) |
whenbranch.getRhs() = nonReturningStmt()))
whenbranch.getRhs() = nonReturningStmt()
)
)
}
/**
@@ -947,9 +955,10 @@ private module ControlFlowGraphImpl {
or
// If our last branch condition is false then we are done
exists(int i |
last(whenexpr.getBranch(i), last, BooleanCompletion(false, _)) and
completion = NormalCompletion() and
not exists(whenexpr.getBranch(i + 1)))
last(whenexpr.getBranch(i), last, BooleanCompletion(false, _)) and
completion = NormalCompletion() and
not exists(whenexpr.getBranch(i + 1))
)
or
// Any branch getting an abnormal completion is propogated
last(whenexpr.getBranch(_), last, completion) and
@@ -1249,13 +1258,15 @@ private module ControlFlowGraphImpl {
or
// When expressions:
exists(WhenExpr whenexpr | n = whenexpr |
n = whenexpr and result = first(whenexpr.getBranch(0)) and
n = whenexpr and
result = first(whenexpr.getBranch(0)) and
completion = NormalCompletion()
or
exists(int i |
last(whenexpr.getBranch(i), n, completion) and
completion = BooleanCompletion(false, _) and
result = first(whenexpr.getBranch(i + 1)))
result = first(whenexpr.getBranch(i + 1))
)
)
or
// When branches:

View File

@@ -104,6 +104,11 @@ class TypeClassCastException extends Class {
TypeClassCastException() { this.hasQualifiedName("java.lang", "ClassCastException") }
}
/** The class `java.lang.NullPointerException`. */
class TypeNullPointerException extends Class {
TypeNullPointerException() { this.hasQualifiedName("java.lang", "NullPointerException") }
}
/**
* The class `java.lang.Class`.
*

View File

@@ -78,3 +78,21 @@ TODO
return
}
}
fun t1(o: Any): Int {
try {
val x = o as Int
return 1
} catch (e: ClassCastException) {
return 2
}
}
fun t2(o: Any?): Int {
try {
val x = o!!
return 1
} catch (e: NullPointerException) {
return 2
}
}

View File

@@ -105,3 +105,23 @@
| Test.kt:43:3:43:3 | <Expr>; | 9 | Test.kt:78:3:78:8 | INSTANCE |
| Test.kt:43:3:43:3 | <Expr>; | 10 | Test.kt:78:3:78:8 | return ... |
| Test.kt:43:3:43:3 | <Expr>; | 11 | Test.kt:4:2:79:2 | test |
| Test.kt:82:1:89:1 | t1 | 0 | Test.kt:82:1:89:1 | t1 |
| Test.kt:82:21:89:1 | { ... } | 0 | Test.kt:82:21:89:1 | { ... } |
| Test.kt:82:21:89:1 | { ... } | 1 | Test.kt:83:2:88:2 | try ... |
| Test.kt:82:21:89:1 | { ... } | 2 | Test.kt:83:6:86:2 | { ... } |
| Test.kt:82:21:89:1 | { ... } | 3 | Test.kt:84:3:84:18 | var ...; |
| Test.kt:82:21:89:1 | { ... } | 4 | Test.kt:84:11:84:11 | o |
| Test.kt:82:21:89:1 | { ... } | 5 | Test.kt:84:11:84:18 | (...)... |
| Test.kt:84:3:84:18 | x | 0 | Test.kt:84:3:84:18 | x |
| Test.kt:84:3:84:18 | x | 1 | Test.kt:85:10:85:10 | 1 |
| Test.kt:84:3:84:18 | x | 2 | Test.kt:85:3:85:10 | return ... |
| Test.kt:91:1:98:1 | t2 | 0 | Test.kt:91:1:98:1 | t2 |
| Test.kt:91:22:98:1 | { ... } | 0 | Test.kt:91:22:98:1 | { ... } |
| Test.kt:91:22:98:1 | { ... } | 1 | Test.kt:92:2:97:2 | try ... |
| Test.kt:91:22:98:1 | { ... } | 2 | Test.kt:92:6:95:2 | { ... } |
| Test.kt:91:22:98:1 | { ... } | 3 | Test.kt:93:3:93:13 | var ...; |
| Test.kt:91:22:98:1 | { ... } | 4 | Test.kt:93:11:93:11 | o |
| Test.kt:91:22:98:1 | { ... } | 5 | Test.kt:93:12:93:13 | ...!! |
| Test.kt:93:3:93:13 | x | 0 | Test.kt:93:3:93:13 | x |
| Test.kt:93:3:93:13 | x | 1 | Test.kt:94:10:94:10 | 1 |
| Test.kt:93:3:93:13 | x | 2 | Test.kt:94:3:94:10 | return ... |

View File

@@ -14,3 +14,9 @@
| Test.kt:35:3:35:3 | <Expr>; | Test.kt:43:3:43:3 | <Expr>; |
| Test.kt:38:9:38:9 | x | Test.kt:38:16:41:3 | { ... } |
| Test.kt:38:9:38:9 | x | Test.kt:43:3:43:3 | <Expr>; |
| Test.kt:82:21:89:1 | { ... } | Test.kt:82:1:89:1 | t1 |
| Test.kt:82:21:89:1 | { ... } | Test.kt:84:3:84:18 | x |
| Test.kt:82:21:89:1 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:91:22:98:1 | { ... } | Test.kt:91:1:98:1 | t2 |
| Test.kt:91:22:98:1 | { ... } | Test.kt:93:3:93:13 | x |
| Test.kt:91:22:98:1 | { ... } | file://:0:0:0:0 | catch (...) |

View File

@@ -7,3 +7,11 @@
| Test.kt:38:9:38:9 | x | Test.kt:38:16:41:3 | { ... } |
| Test.kt:38:9:38:9 | x | Test.kt:43:3:43:3 | <Expr>; |
| Test.kt:38:16:41:3 | { ... } | Test.kt:38:9:38:9 | x |
| Test.kt:82:21:89:1 | { ... } | Test.kt:84:3:84:18 | x |
| Test.kt:82:21:89:1 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:84:3:84:18 | x | Test.kt:82:1:89:1 | t1 |
| Test.kt:91:22:98:1 | { ... } | Test.kt:93:3:93:13 | x |
| Test.kt:91:22:98:1 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:93:3:93:13 | x | Test.kt:91:1:98:1 | t2 |
| file://:0:0:0:0 | catch (...) | Test.kt:82:1:89:1 | t1 |
| file://:0:0:0:0 | catch (...) | Test.kt:91:1:98:1 | t2 |

View File

@@ -130,3 +130,42 @@
| Test.kt:77:7:77:8 | 40 | IntegerLiteral | Test.kt:77:3:77:3 | ...=... | AssignExpr |
| Test.kt:78:3:78:8 | INSTANCE | VarAccess | Test.kt:78:3:78:8 | return ... | ReturnStmt |
| Test.kt:78:3:78:8 | return ... | ReturnStmt | Test.kt:4:2:79:2 | test | Method |
| Test.kt:82:1:89:1 | t1 | Method | file://:0:0:0:0 | <none> | <none> |
| Test.kt:82:8:82:13 | o | Parameter | file://:0:0:0:0 | <none> | <none> |
| Test.kt:82:21:89:1 | { ... } | BlockStmt | Test.kt:83:2:88:2 | try ... | TryStmt |
| Test.kt:83:2:88:2 | try ... | TryStmt | Test.kt:83:6:86:2 | { ... } | BlockStmt |
| Test.kt:83:6:86:2 | { ... } | BlockStmt | Test.kt:84:3:84:18 | var ...; | LocalVariableDeclStmt |
| Test.kt:84:3:84:18 | int x | LocalVariableDecl | file://:0:0:0:0 | <none> | <none> |
| Test.kt:84:3:84:18 | var ...; | LocalVariableDeclStmt | Test.kt:84:11:84:11 | o | VarAccess |
| Test.kt:84:3:84:18 | x | LocalVariableDeclExpr | Test.kt:85:10:85:10 | 1 | IntegerLiteral |
| Test.kt:84:11:84:11 | o | VarAccess | Test.kt:84:11:84:18 | (...)... | CastExpr |
| Test.kt:84:11:84:18 | (...)... | CastExpr | Test.kt:84:3:84:18 | x | LocalVariableDeclExpr |
| Test.kt:84:11:84:18 | (...)... | CastExpr | file://:0:0:0:0 | catch (...) | CatchClause |
| Test.kt:84:11:84:18 | int | TypeAccess | file://:0:0:0:0 | <none> | <none> |
| Test.kt:85:3:85:10 | return ... | ReturnStmt | Test.kt:82:1:89:1 | t1 | Method |
| Test.kt:85:10:85:10 | 1 | IntegerLiteral | Test.kt:85:3:85:10 | return ... | ReturnStmt |
| Test.kt:86:11:86:31 | ClassCastException | TypeAccess | file://:0:0:0:0 | <none> | <none> |
| Test.kt:86:11:86:31 | ClassCastException e | LocalVariableDecl | file://:0:0:0:0 | <none> | <none> |
| Test.kt:86:11:86:31 | e | LocalVariableDeclExpr | Test.kt:86:34:88:2 | { ... } | BlockStmt |
| Test.kt:86:34:88:2 | { ... } | BlockStmt | Test.kt:87:10:87:10 | 2 | IntegerLiteral |
| Test.kt:87:3:87:10 | return ... | ReturnStmt | Test.kt:82:1:89:1 | t1 | Method |
| Test.kt:87:10:87:10 | 2 | IntegerLiteral | Test.kt:87:3:87:10 | return ... | ReturnStmt |
| Test.kt:91:1:98:1 | t2 | Method | file://:0:0:0:0 | <none> | <none> |
| Test.kt:91:8:91:14 | o | Parameter | file://:0:0:0:0 | <none> | <none> |
| Test.kt:91:22:98:1 | { ... } | BlockStmt | Test.kt:92:2:97:2 | try ... | TryStmt |
| Test.kt:92:2:97:2 | try ... | TryStmt | Test.kt:92:6:95:2 | { ... } | BlockStmt |
| Test.kt:92:6:95:2 | { ... } | BlockStmt | Test.kt:93:3:93:13 | var ...; | LocalVariableDeclStmt |
| Test.kt:93:3:93:13 | Object x | LocalVariableDecl | file://:0:0:0:0 | <none> | <none> |
| Test.kt:93:3:93:13 | var ...; | LocalVariableDeclStmt | Test.kt:93:11:93:11 | o | VarAccess |
| Test.kt:93:3:93:13 | x | LocalVariableDeclExpr | Test.kt:94:10:94:10 | 1 | IntegerLiteral |
| Test.kt:93:11:93:11 | o | VarAccess | Test.kt:93:12:93:13 | ...!! | NotNullExpr |
| Test.kt:93:12:93:13 | ...!! | NotNullExpr | Test.kt:93:3:93:13 | x | LocalVariableDeclExpr |
| Test.kt:93:12:93:13 | ...!! | NotNullExpr | file://:0:0:0:0 | catch (...) | CatchClause |
| Test.kt:94:3:94:10 | return ... | ReturnStmt | Test.kt:91:1:98:1 | t2 | Method |
| Test.kt:94:10:94:10 | 1 | IntegerLiteral | Test.kt:94:3:94:10 | return ... | ReturnStmt |
| Test.kt:95:11:95:33 | NullPointerException | TypeAccess | file://:0:0:0:0 | <none> | <none> |
| Test.kt:95:11:95:33 | NullPointerException e | LocalVariableDecl | file://:0:0:0:0 | <none> | <none> |
| Test.kt:95:11:95:33 | e | LocalVariableDeclExpr | Test.kt:95:36:97:2 | { ... } | BlockStmt |
| Test.kt:95:36:97:2 | { ... } | BlockStmt | Test.kt:96:10:96:10 | 2 | IntegerLiteral |
| Test.kt:96:3:96:10 | return ... | ReturnStmt | Test.kt:91:1:98:1 | t2 | Method |
| Test.kt:96:10:96:10 | 2 | IntegerLiteral | Test.kt:96:3:96:10 | return ... | ReturnStmt |

View File

@@ -368,5 +368,55 @@
| Test.kt:73:3:73:3 | <Expr>; | Test.kt:77:3:77:3 | <Expr>; |
| Test.kt:73:3:73:3 | <Expr>; | Test.kt:78:3:78:8 | return ... |
| Test.kt:77:3:77:3 | <Expr>; | Test.kt:78:3:78:8 | return ... |
| Test.kt:82:21:89:1 | { ... } | Test.kt:83:2:88:2 | try ... |
| Test.kt:82:21:89:1 | { ... } | Test.kt:83:6:86:2 | { ... } |
| Test.kt:82:21:89:1 | { ... } | Test.kt:84:3:84:18 | var ...; |
| Test.kt:82:21:89:1 | { ... } | Test.kt:85:3:85:10 | return ... |
| Test.kt:82:21:89:1 | { ... } | Test.kt:86:34:88:2 | { ... } |
| Test.kt:82:21:89:1 | { ... } | Test.kt:87:3:87:10 | return ... |
| Test.kt:82:21:89:1 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:83:2:88:2 | try ... | Test.kt:83:6:86:2 | { ... } |
| Test.kt:83:2:88:2 | try ... | Test.kt:84:3:84:18 | var ...; |
| Test.kt:83:2:88:2 | try ... | Test.kt:85:3:85:10 | return ... |
| Test.kt:83:2:88:2 | try ... | Test.kt:86:34:88:2 | { ... } |
| Test.kt:83:2:88:2 | try ... | Test.kt:87:3:87:10 | return ... |
| Test.kt:83:2:88:2 | try ... | file://:0:0:0:0 | catch (...) |
| Test.kt:83:6:86:2 | { ... } | Test.kt:84:3:84:18 | var ...; |
| Test.kt:83:6:86:2 | { ... } | Test.kt:85:3:85:10 | return ... |
| Test.kt:83:6:86:2 | { ... } | Test.kt:86:34:88:2 | { ... } |
| Test.kt:83:6:86:2 | { ... } | Test.kt:87:3:87:10 | return ... |
| Test.kt:83:6:86:2 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:84:3:84:18 | var ...; | Test.kt:85:3:85:10 | return ... |
| Test.kt:84:3:84:18 | var ...; | Test.kt:86:34:88:2 | { ... } |
| Test.kt:84:3:84:18 | var ...; | Test.kt:87:3:87:10 | return ... |
| Test.kt:84:3:84:18 | var ...; | file://:0:0:0:0 | catch (...) |
| Test.kt:86:34:88:2 | { ... } | Test.kt:87:3:87:10 | return ... |
| Test.kt:91:22:98:1 | { ... } | Test.kt:92:2:97:2 | try ... |
| Test.kt:91:22:98:1 | { ... } | Test.kt:92:6:95:2 | { ... } |
| Test.kt:91:22:98:1 | { ... } | Test.kt:93:3:93:13 | var ...; |
| Test.kt:91:22:98:1 | { ... } | Test.kt:94:3:94:10 | return ... |
| Test.kt:91:22:98:1 | { ... } | Test.kt:95:36:97:2 | { ... } |
| Test.kt:91:22:98:1 | { ... } | Test.kt:96:3:96:10 | return ... |
| Test.kt:91:22:98:1 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:92:2:97:2 | try ... | Test.kt:92:6:95:2 | { ... } |
| Test.kt:92:2:97:2 | try ... | Test.kt:93:3:93:13 | var ...; |
| Test.kt:92:2:97:2 | try ... | Test.kt:94:3:94:10 | return ... |
| Test.kt:92:2:97:2 | try ... | Test.kt:95:36:97:2 | { ... } |
| Test.kt:92:2:97:2 | try ... | Test.kt:96:3:96:10 | return ... |
| Test.kt:92:2:97:2 | try ... | file://:0:0:0:0 | catch (...) |
| Test.kt:92:6:95:2 | { ... } | Test.kt:93:3:93:13 | var ...; |
| Test.kt:92:6:95:2 | { ... } | Test.kt:94:3:94:10 | return ... |
| Test.kt:92:6:95:2 | { ... } | Test.kt:95:36:97:2 | { ... } |
| Test.kt:92:6:95:2 | { ... } | Test.kt:96:3:96:10 | return ... |
| Test.kt:92:6:95:2 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:93:3:93:13 | var ...; | Test.kt:94:3:94:10 | return ... |
| Test.kt:93:3:93:13 | var ...; | Test.kt:95:36:97:2 | { ... } |
| Test.kt:93:3:93:13 | var ...; | Test.kt:96:3:96:10 | return ... |
| Test.kt:93:3:93:13 | var ...; | file://:0:0:0:0 | catch (...) |
| Test.kt:95:36:97:2 | { ... } | Test.kt:96:3:96:10 | return ... |
| file://:0:0:0:0 | catch (...) | Test.kt:86:34:88:2 | { ... } |
| file://:0:0:0:0 | catch (...) | Test.kt:87:3:87:10 | return ... |
| file://:0:0:0:0 | catch (...) | Test.kt:95:36:97:2 | { ... } |
| file://:0:0:0:0 | catch (...) | Test.kt:96:3:96:10 | return ... |
| file://:0:0:0:0 | var ...; | Test.kt:40:4:40:4 | <Expr>; |
| file://:0:0:0:0 | var ...; | Test.kt:40:4:40:6 | <Expr>; |

View File

@@ -280,6 +280,24 @@
| Test.kt:78:3:78:8 | return ... | Test.kt:73:3:73:3 | <Expr>; |
| Test.kt:78:3:78:8 | return ... | Test.kt:77:3:77:3 | <Expr>; |
| Test.kt:78:3:78:8 | return ... | file://:0:0:0:0 | var ...; |
| Test.kt:83:2:88:2 | try ... | Test.kt:82:21:89:1 | { ... } |
| Test.kt:83:6:86:2 | { ... } | Test.kt:82:21:89:1 | { ... } |
| Test.kt:83:6:86:2 | { ... } | Test.kt:83:2:88:2 | try ... |
| Test.kt:84:3:84:18 | var ...; | Test.kt:82:21:89:1 | { ... } |
| Test.kt:84:3:84:18 | var ...; | Test.kt:83:2:88:2 | try ... |
| Test.kt:84:3:84:18 | var ...; | Test.kt:83:6:86:2 | { ... } |
| Test.kt:86:34:88:2 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:87:3:87:10 | return ... | Test.kt:86:34:88:2 | { ... } |
| Test.kt:87:3:87:10 | return ... | file://:0:0:0:0 | catch (...) |
| Test.kt:92:2:97:2 | try ... | Test.kt:91:22:98:1 | { ... } |
| Test.kt:92:6:95:2 | { ... } | Test.kt:91:22:98:1 | { ... } |
| Test.kt:92:6:95:2 | { ... } | Test.kt:92:2:97:2 | try ... |
| Test.kt:93:3:93:13 | var ...; | Test.kt:91:22:98:1 | { ... } |
| Test.kt:93:3:93:13 | var ...; | Test.kt:92:2:97:2 | try ... |
| Test.kt:93:3:93:13 | var ...; | Test.kt:92:6:95:2 | { ... } |
| Test.kt:95:36:97:2 | { ... } | file://:0:0:0:0 | catch (...) |
| Test.kt:96:3:96:10 | return ... | Test.kt:95:36:97:2 | { ... } |
| Test.kt:96:3:96:10 | return ... | file://:0:0:0:0 | catch (...) |
| file://:0:0:0:0 | var ...; | Test.kt:38:16:41:3 | { ... } |
| file://:0:0:0:0 | var ...; | Test.kt:39:4:39:4 | <Expr>; |
| file://:0:0:0:0 | var ...; | Test.kt:40:4:40:6 | <Expr>; |