Skip to content

Commit

Permalink
break
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Nov 7, 2024
1 parent 2578e06 commit 2cd7d38
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,40 @@ final class BlockExprCfgNode extends Nodes::BlockExprCfgNode {
*/
ExprCfgNode getTailExpr() { node.hasCfgChild(node.getStmtList().getTailExpr(), this, result) }
}

/**
* A break expression. For example:
* ```rust
* loop {
* if not_ready() {
* break;
* }
* }
* ```
* ```rust
* let x = 'label: loop {
* if done() {
* break 'label 42;
* }
* };
* ```
* ```rust
* let x = 'label: {
* if exit() {
* break 'label 42;
* }
* 0;
* };
* ```
*/
final class BreakExprCfgNode extends Nodes::BreakExprCfgNode {
/**
* Gets the target of this `break` expression.
*
* The target is either a `LoopExpr`, a `ForExpr`, a `WhileExpr`, or a
* `BlockExpr`.
*/
ExprCfgNode getTarget() {
any(BreakExprTargetChildMapping node).hasCfgChild(this.getBreakExpr(), result, this)
}
}
4 changes: 4 additions & 0 deletions rust/ql/lib/codeql/rust/controlflow/internal/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class BlockExprChildMapping extends ChildMapping, BlockExpr {
override predicate relevantChild(AstNode child) { child = this.getStmtList().getTailExpr() }
}

class BreakExprTargetChildMapping extends ChildMapping, Expr {
override predicate relevantChild(AstNode child) { child.(BreakExpr).getTarget() = this }
}

private class ChildMappingImpl extends ChildMapping {
ChildMappingImpl() { exists(this) }

Expand Down

0 comments on commit 2cd7d38

Please sign in to comment.