-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid quadratic switch case intermediate
- Loading branch information
Showing
3 changed files
with
64 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
java/ql/lib/semmle/code/java/controlflow/internal/SwitchCases.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** Provides utility predicates relating to switch cases. */ | ||
|
||
import java | ||
|
||
/** | ||
* Gets the `i`th `SwitchCase` defined on `switch`, if one exists. | ||
*/ | ||
SwitchCase getCase(StmtParent switch, int i) { | ||
result = switch.(SwitchExpr).getCase(i) or result = switch.(SwitchStmt).getCase(i) | ||
} | ||
|
||
/** | ||
* Gets the `i`th `PatternCase` defined on `switch`, if one exists. | ||
*/ | ||
PatternCase getPatternCase(StmtParent switch, int i) { | ||
result = | ||
rank[i + 1](PatternCase pc, int caseIdx | pc = getCase(switch, caseIdx) | pc order by caseIdx) | ||
} | ||
|
||
/** | ||
* Gets the PatternCase after pc, if one exists. | ||
*/ | ||
PatternCase getNextPatternCase(PatternCase pc) { | ||
exists(int idx, StmtParent switch | | ||
pc = getPatternCase(switch, idx) and result = getPatternCase(switch, idx + 1) | ||
) | ||
} | ||
|
||
int lastCaseIndex(StmtParent switch) { | ||
result = max(int i | any(SwitchCase c).isNthCaseOf(switch, i)) | ||
} |