Adapt instanceof CFG and DFG to general patterns

This commit is contained in:
Chris Smowton
2023-11-02 13:45:58 +00:00
parent 20b97af02f
commit f037030c26
3 changed files with 16 additions and 11 deletions

View File

@@ -527,8 +527,7 @@ private module ControlFlowGraphImpl {
or
this instanceof NotInstanceOfExpr
or
this instanceof LocalVariableDeclExpr and
not this = any(InstanceOfExpr ioe).getLocalVariableDeclExpr()
this instanceof LocalVariableDeclExpr
or
this instanceof StringTemplateExpr
or
@@ -832,7 +831,7 @@ private module ControlFlowGraphImpl {
exists(InstanceOfExpr ioe | ioe.isPattern() and ioe = n |
last = n and completion = basicBooleanCompletion(false)
or
last = ioe.getLocalVariableDeclExpr() and completion = basicBooleanCompletion(true)
last(ioe.getPattern(), last, NormalCompletion()) and completion = basicBooleanCompletion(true)
)
or
// The last node of a node executed in post-order is the node itself.
@@ -1128,7 +1127,7 @@ private module ControlFlowGraphImpl {
last(ioe.getExpr(), n, completion) and completion = NormalCompletion() and result = ioe
or
n = ioe and
result = ioe.getLocalVariableDeclExpr() and
result = first(ioe.getPattern()) and
completion = basicBooleanCompletion(true)
)
or

View File

@@ -1559,20 +1559,26 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
class InstanceOfExpr extends Expr, @instanceofexpr {
/** Gets the expression on the left-hand side of the `instanceof` operator. */
Expr getExpr() {
if this.isPattern()
then result = this.getLocalVariableDeclExpr().getInit()
else result.isNthChildOf(this, 0)
result.isNthChildOf(this, 0)
}
/**
* Gets the pattern of an `x instanceof T pattern` expression, if any.
*/
Pattern getPattern() { result.isNthChildOf(this, 2) }
/**
* Holds if this `instanceof` expression uses pattern matching.
*/
predicate isPattern() { exists(this.getLocalVariableDeclExpr()) }
predicate isPattern() { exists(this.getPattern()) }
/**
* Gets the local variable declaration of this `instanceof` expression if pattern matching is used.
* Gets the local variable declaration of this `instanceof` expression if simple pattern matching is used.
*
* Note that this won't get anything when record pattern matching is used-- for more general patterns,
* use `getPattern`.
*/
LocalVariableDeclExpr getLocalVariableDeclExpr() { result.isNthChildOf(this, 0) }
LocalVariableDeclExpr getLocalVariableDeclExpr() { result = this.getPattern().asBindingPattern() }
/** Gets the access to the type on the right-hand side of the `instanceof` operator. */
Expr getTypeName() { result.isNthChildOf(this, 1) }

View File

@@ -200,7 +200,7 @@ predicate simpleAstFlowStep(Expr e1, Expr e2) {
)
or
exists(InstanceOfExpr ioe |
e1 = ioe.getExpr() and e2 = ioe.getLocalVariableDeclExpr()
e1 = ioe.getExpr() and e2 = ioe.getPattern()
)
}