Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/ql/lib/change-notes/2026-03-28-switch-stmt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: feature
---
* A new predicate `getSwitchCase` was added to the `SwitchStmt` class, which yields the `n`th `case` statement from a `switch` statement.
79 changes: 49 additions & 30 deletions cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1412,9 +1412,9 @@ private int indexOfSwitchCaseRank(BlockStmt b, int rnk) {
* switch (i)
* {
* case 5:
* ...
* ...
* default:
* ...
* ...
* }
* ```
*/
Expand Down Expand Up @@ -1516,8 +1516,10 @@ class SwitchCase extends Stmt, @stmt_switch_case {
* which has result `default:`, which has no result.
*/
SwitchCase getNextSwitchCase() {
result.getSwitchStmt() = this.getSwitchStmt() and
result.getChildNum() = this.getChildNum() + 1
exists(SwitchStmt s, int n |
this = s.getSwitchCase(n) and
result = s.getSwitchCase(n + 1)
)
}

/**
Expand Down Expand Up @@ -1707,9 +1709,9 @@ class SwitchCase extends Stmt, @stmt_switch_case {
* switch (i)
* {
* case 5:
* ...
* ...
* default:
* ...
* ...
* }
* ```
*/
Expand All @@ -1731,9 +1733,9 @@ class DefaultCase extends SwitchCase {
* switch (i)
* {
* case 5:
* ...
* ...
* default:
* ...
* ...
* }
* ```
*/
Expand Down Expand Up @@ -1768,10 +1770,10 @@ class SwitchStmt extends ConditionalStmt, @stmt_switch {
* For example, for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
Expand All @@ -1790,20 +1792,20 @@ class SwitchStmt extends ConditionalStmt, @stmt_switch {
* For example, for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
* the result is
* ```
* {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
Expand All @@ -1816,36 +1818,53 @@ class SwitchStmt extends ConditionalStmt, @stmt_switch {
* For example, for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
* the results are `case 1:`, `case 2:` and `default:`.
*/
SwitchCase getASwitchCase() { switch_case(underlyingElement(this), _, unresolveElement(result)) }

/**
* Gets the `n`th 'switch case' statement of this 'switch' statement, where
* `n` is 0-based.
*
* For example, for
* ```
* switch(i) {
* case 5:
* case 6:
* default:
* } * ```
* 0 yields `case 5:`, 1 yields `case 6:`, and 2 yields `default:`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 0 yields `case 5:`, 1 yields `case 6:`, and 2 yields `default:`.
* 0 yields `case 1:`, 1 yields `case 2:`, and 2 yields `default:`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by updating the example instead.

*/
SwitchCase getSwitchCase(int n) {
switch_case(underlyingElement(this), n, unresolveElement(result))
}

/**
* Gets the 'default case' statement of this 'switch' statement,
* if any.
*
* For example, for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
* the result is `default:`, but there is no result for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* }
* ```
Expand All @@ -1858,18 +1877,18 @@ class SwitchStmt extends ConditionalStmt, @stmt_switch {
* For example, this holds for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* default:
* default:
* break;
* }
* ```
* but not for
* ```
* switch(i) {
* case 1:
* case 2:
* case 1:
* case 2:
* break;
* }
* ```
Expand Down
Loading