Skip to content

Commit f23d6be

Browse files
committed
Cfg/Java: Move InstanceOfExpr CFG into shared lib.
1 parent 9fc8f61 commit f23d6be

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

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

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ private module Ast implements AstSig<Location> {
208208
class BooleanLiteral extends FinalBooleanLiteral {
209209
boolean getValue() { result = this.getBooleanValue() }
210210
}
211+
212+
final private class FinalInstanceOfExpr = J::InstanceOfExpr;
213+
214+
class PatternMatchExpr extends FinalInstanceOfExpr {
215+
PatternMatchExpr() { this.isPattern() }
216+
217+
AstNode getPattern() { result = super.getPattern() }
218+
}
211219
}
212220

213221
private module Exceptions {
@@ -544,14 +552,8 @@ private module Input implements InputSig1, InputSig2 {
544552

545553
private string assertThrowNodeTag() { result = "[assert-throw]" }
546554

547-
private string instanceofTrueNodeTag() { result = "[instanceof-true]" }
548-
549555
predicate additionalNode(Ast::AstNode n, string tag, NormalSuccessor t) {
550556
n instanceof AssertStmt and tag = assertThrowNodeTag() and t instanceof DirectSuccessor
551-
or
552-
n.(InstanceOfExpr).isPattern() and
553-
tag = instanceofTrueNodeTag() and
554-
t.(BooleanSuccessor).getValue() = true
555557
}
556558

557559
/**
@@ -593,34 +595,6 @@ private module Input implements InputSig1, InputSig2 {
593595

594596
/** Holds if there is a local non-abrupt step from `n1` to `n2`. */
595597
predicate step(PreControlFlowNode n1, PreControlFlowNode n2) {
596-
exists(InstanceOfExpr ioe |
597-
// common
598-
n1.isBefore(ioe) and
599-
n2.isBefore(ioe.getExpr())
600-
or
601-
n1.isAfter(ioe.getExpr()) and
602-
n2.isIn(ioe)
603-
or
604-
// std postorder:
605-
not ioe.isPattern() and
606-
n1.isIn(ioe) and
607-
n2.isAfter(ioe)
608-
or
609-
// pattern case:
610-
ioe.isPattern() and
611-
n1.isIn(ioe) and
612-
n2.isAfterValue(ioe, any(BooleanSuccessor s | s.getValue() = false))
613-
or
614-
n1.isIn(ioe) and
615-
n2.isAdditional(ioe, instanceofTrueNodeTag())
616-
or
617-
n1.isAdditional(ioe, instanceofTrueNodeTag()) and
618-
n2.isBefore(ioe.getPattern())
619-
or
620-
n1.isAfter(ioe.getPattern()) and
621-
n2.isAfterValue(ioe, any(BooleanSuccessor s | s.getValue() = true))
622-
)
623-
or
624598
exists(AssertStmt assertstmt |
625599
n1.isBefore(assertstmt) and
626600
n2.isBefore(assertstmt.getExpr())

shared/controlflow/codeql/controlflow/ControlFlowGraph.qll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,19 @@ signature module AstSig<LocationSig Location> {
341341
/** Gets the boolean value of this literal. */
342342
boolean getValue();
343343
}
344+
345+
/**
346+
* A pattern matching expression.
347+
*
348+
* In Java this is `x instanceof Pattern`, and in C# this is `x is Pattern`.
349+
*/
350+
class PatternMatchExpr extends Expr {
351+
/** Gets the expression being matched. */
352+
Expr getExpr();
353+
354+
/** Gets the pattern being matched against. */
355+
AstNode getPattern();
356+
}
344357
}
345358

346359
/**
@@ -575,6 +588,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
575588

576589
private string loopHeaderTag() { result = "[LoopHeader]" }
577590

591+
private string patternMatchTrueTag() { result = "[match-true]" }
592+
578593
/**
579594
* Holds if an additional node tagged with `tag` should be created for
580595
* `n`. Edges targeting such nodes are labeled with `t` and therefore `t`
@@ -586,6 +601,10 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
586601
n instanceof LoopStmt and
587602
tag = loopHeaderTag() and
588603
t instanceof DirectSuccessor
604+
or
605+
n instanceof PatternMatchExpr and
606+
tag = patternMatchTrueTag() and
607+
t.(BooleanSuccessor).getValue() = true
589608
}
590609

591610
/**
@@ -1309,6 +1328,26 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
13091328
n2.isAfterValue(boollit, any(BooleanSuccessor t | t.getValue() = boollit.getValue()))
13101329
)
13111330
or
1331+
exists(PatternMatchExpr pme |
1332+
n1.isBefore(pme) and
1333+
n2.isBefore(pme.getExpr())
1334+
or
1335+
n1.isAfter(pme.getExpr()) and
1336+
n2.isIn(pme)
1337+
or
1338+
n1.isIn(pme) and
1339+
n2.isAfterValue(pme, any(BooleanSuccessor s | s.getValue() = false))
1340+
or
1341+
n1.isIn(pme) and
1342+
n2.isAdditional(pme, patternMatchTrueTag())
1343+
or
1344+
n1.isAdditional(pme, patternMatchTrueTag()) and
1345+
n2.isBefore(pme.getPattern())
1346+
or
1347+
n1.isAfter(pme.getPattern()) and
1348+
n2.isAfterValue(pme, any(BooleanSuccessor s | s.getValue() = true))
1349+
)
1350+
or
13121351
exists(IfStmt ifstmt |
13131352
n1.isBefore(ifstmt) and
13141353
n2.isBefore(ifstmt.getCondition())

0 commit comments

Comments
 (0)