Skip to content

Commit fa8c1d6

Browse files
committed
C++: Add a getSwitchCase predicate to SwitchStmt
1 parent 1ba9601 commit fa8c1d6

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* A new predicate `getSwitchCase` was added to the `SwitchStmt` class, which yields the `n`th `case` statement from a `switch` statement.

cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,26 @@ class SwitchStmt extends ConditionalStmt, @stmt_switch {
18271827
*/
18281828
SwitchCase getASwitchCase() { switch_case(underlyingElement(this), _, unresolveElement(result)) }
18291829

1830+
/**
1831+
* Gets the `n`th 'switch case' statement of this 'switch' statement, where
1832+
* `n` is 0-based.
1833+
*
1834+
* For example, for
1835+
* ```
1836+
* switch(i) {
1837+
* case 1:
1838+
* case 2:
1839+
* break;
1840+
* default:
1841+
* break;
1842+
* }
1843+
* ```
1844+
* 0 yields `case 5:`, 1 yields `case 6:`, and 2 yields `default:`.
1845+
*/
1846+
SwitchCase getSwitchCase(int n) {
1847+
switch_case(underlyingElement(this), n, unresolveElement(result))
1848+
}
1849+
18301850
/**
18311851
* Gets the 'default case' statement of this 'switch' statement,
18321852
* if any.

0 commit comments

Comments
 (0)