Skip to content

Commit 633b92d

Browse files
committed
Introduce and use SwitchBlock instead of StmtParent for switch-statement-or-expression
1 parent e50a0ee commit 633b92d

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ private module ControlFlowGraphImpl {
440440
// Join order engineering -- first determine the switch block and the case indices required, then retrieve them.
441441
bindingset[switch, i]
442442
pragma[inline_late]
443-
private predicate isNthCaseOf(StmtParent switch, SwitchCase c, int i) { c.isNthCaseOf(switch, i) }
443+
private predicate isNthCaseOf(SwitchBlock switch, SwitchCase c, int i) { c.isNthCaseOf(switch, i) }
444444

445445
/**
446446
* Gets a `SwitchCase` that may be `pred`'s direct successor, where `pred` is declared in block `switch`.
@@ -450,7 +450,7 @@ private module ControlFlowGraphImpl {
450450
* Because we know the switch block contains at least one pattern, we know by https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11
451451
* that any default case comes after the last pattern case.
452452
*/
453-
private SwitchCase getASuccessorSwitchCase(PatternCase pred, StmtParent switch) {
453+
private SwitchCase getASuccessorSwitchCase(PatternCase pred, SwitchBlock switch) {
454454
// Note we do include `case null, default` (as well as plain old `default`) here.
455455
not result.(ConstCase).getValue(_) instanceof NullLiteral and
456456
exists(int maxCaseIndex |
@@ -471,7 +471,7 @@ private module ControlFlowGraphImpl {
471471
*
472472
* Otherwise it is any case in the switch block.
473473
*/
474-
private SwitchCase getAFirstSwitchCase(StmtParent switch) {
474+
private SwitchCase getAFirstSwitchCase(SwitchBlock switch) {
475475
result.getParent() = switch and
476476
(
477477
result.(ConstCase).getValue(_) instanceof NullLiteral
@@ -484,7 +484,7 @@ private module ControlFlowGraphImpl {
484484
)
485485
}
486486

487-
private Stmt getSwitchStatement(StmtParent switch, int i) { result.isNthChildOf(switch, i) }
487+
private Stmt getSwitchStatement(SwitchBlock switch, int i) { result.isNthChildOf(switch, i) }
488488

489489
/**
490490
* Holds if `last` is the last node in a pattern case `pc`'s succeeding bind-and-test operation,
@@ -1296,7 +1296,7 @@ private module ControlFlowGraphImpl {
12961296
)
12971297
or
12981298
// Switch statements and expressions
1299-
exists(StmtParent switch | switch instanceof SwitchStmt or switch instanceof SwitchExpr |
1299+
exists(SwitchBlock switch |
13001300
exists(Expr switchExpr |
13011301
switchExpr = switch.(SwitchStmt).getExpr() or switchExpr = switch.(SwitchExpr).getExpr()
13021302
|

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,13 @@ class SwitchStmt extends Stmt, @switchstmt {
428428
override string getAPrimaryQlClass() { result = "SwitchStmt" }
429429
}
430430

431+
/**
432+
* A `switch` statement or expression.
433+
*/
434+
class SwitchBlock extends StmtParent {
435+
SwitchBlock() { this instanceof SwitchStmt or this instanceof SwitchExpr }
436+
}
437+
431438
/**
432439
* A case of a `switch` statement or expression.
433440
*

java/ql/lib/semmle/code/java/controlflow/Guards.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class ConditionBlock extends BasicBlock {
7676
// Join order engineering -- first determine the switch block and the case indices required, then retrieve them.
7777
bindingset[switch, i]
7878
pragma[inline_late]
79-
private predicate isNthCaseOf(StmtParent switch, SwitchCase c, int i) { c.isNthCaseOf(switch, i) }
79+
private predicate isNthCaseOf(SwitchBlock switch, SwitchCase c, int i) { c.isNthCaseOf(switch, i) }
8080

8181
/**
8282
* Gets a switch case >= pred, up to but not including `pred`'s successor pattern case,
8383
* where `pred` is declared on `switch`.
8484
*/
85-
private SwitchCase getACaseUpToNextPattern(PatternCase pred, StmtParent switch) {
85+
private SwitchCase getACaseUpToNextPattern(PatternCase pred, SwitchBlock switch) {
8686
// Note we do include `case null, default` (as well as plain old `default`) here.
8787
not result.(ConstCase).getValue(_) instanceof NullLiteral and
8888
exists(int maxCaseIndex |

java/ql/lib/semmle/code/java/controlflow/internal/SwitchCases.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ import java
55
/**
66
* Gets the `i`th `PatternCase` defined on `switch`, if one exists.
77
*/
8-
private PatternCase getPatternCase(StmtParent switch, int i) {
8+
private PatternCase getPatternCase(SwitchBlock switch, int i) {
99
result =
1010
rank[i + 1](PatternCase pc, int caseIdx | pc.isNthCaseOf(switch, caseIdx) | pc order by caseIdx)
1111
}
1212

1313
/**
1414
* Gets the first `PatternCase` defined on `switch`, if one exists.
1515
*/
16-
PatternCase getFirstPatternCase(StmtParent switch) {
16+
PatternCase getFirstPatternCase(SwitchBlock switch) {
1717
result = getPatternCase(switch, 0)
1818
}
1919

2020
/**
2121
* Gets the PatternCase after pc, if one exists.
2222
*/
2323
PatternCase getNextPatternCase(PatternCase pc) {
24-
exists(int idx, StmtParent switch |
24+
exists(int idx, SwitchBlock switch |
2525
pc = getPatternCase(switch, idx) and result = getPatternCase(switch, idx + 1)
2626
)
2727
}
2828

29-
int lastCaseIndex(StmtParent switch) {
29+
int lastCaseIndex(SwitchBlock switch) {
3030
result = max(int i | any(SwitchCase c).isNthCaseOf(switch, i))
3131
}

0 commit comments

Comments
 (0)