Skip to content

Commit 086e0c6

Browse files
authored
Merge pull request github#17817 from hvitved/rust/cfg-scope-callable
Rust: Use `Callable` to define `CfgScope`
2 parents 24ae548 + f72af4f commit 086e0c6

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ private module CfgInput implements InputSig<Location> {
4040
predicate successorTypeIsCondition(SuccessorType t) { t instanceof Cfg::BooleanSuccessor }
4141

4242
/** Holds if `first` is first executed when entering `scope`. */
43-
predicate scopeFirst(CfgScope scope, AstNode first) { scope.scopeFirst(first) }
43+
predicate scopeFirst(CfgScope scope, AstNode first) {
44+
first(scope.(CfgScopeTree).getFirstChildNode(), first)
45+
}
4446

4547
/** Holds if `scope` is exited when `last` finishes with completion `c`. */
46-
predicate scopeLast(CfgScope scope, AstNode last, Completion c) { scope.scopeLast(last, c) }
48+
predicate scopeLast(CfgScope scope, AstNode last, Completion c) { last(scope.getBody(), last, c) }
4749
}
4850

4951
private module CfgSplittingInput implements SplittingInputSig<Location, CfgInput> {
@@ -65,7 +67,7 @@ private module CfgImpl =
6567

6668
import CfgImpl
6769

68-
class FunctionTree extends StandardTree, Function {
70+
class CfgScopeTree extends StandardTree, Scope::CfgScope {
6971
override predicate first(AstNode first) { first = this }
7072

7173
override predicate last(AstNode last, Completion c) {
@@ -319,24 +321,6 @@ module ExprTrees {
319321
override AstNode getChildNode(int i) { i = 0 and result = super.getExpr() }
320322
}
321323

322-
class ClosureExprTree extends StandardTree, ClosureExpr {
323-
override predicate first(AstNode first) { first = this }
324-
325-
override predicate last(AstNode last, Completion c) {
326-
last = this and
327-
completionIsValidFor(c, this)
328-
}
329-
330-
override predicate propagatesAbnormal(AstNode child) { none() }
331-
332-
override AstNode getChildNode(int i) {
333-
result = this.getParamList().getParam(i)
334-
or
335-
i = this.getParamList().getNumberOfParams() and
336-
result = this.getBody()
337-
}
338-
}
339-
340324
class ContinueExprTree extends LeafTree, ContinueExpr {
341325
override predicate last(AstNode last, Completion c) { none() }
342326

rust/ql/lib/codeql/rust/controlflow/internal/Scope.qll

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,26 @@ private import Completion
33
private import ControlFlowGraphImpl
44
private import codeql.rust.elements.internal.generated.ParentChild
55

6-
abstract class CfgScope extends AstNode {
7-
/** Holds if `first` is executed first when entering scope. */
8-
abstract predicate scopeFirst(AstNode first);
9-
10-
/** Holds if scope is exited when `last` finishes with completion `c`. */
11-
abstract predicate scopeLast(AstNode last, Completion c);
12-
}
13-
14-
final class FunctionScope extends CfgScope, Function {
15-
FunctionScope() {
6+
/**
7+
* A control-flow graph (CFG) scope.
8+
*
9+
* A CFG scope is a callable with a body.
10+
*/
11+
class CfgScope extends Callable {
12+
CfgScope() {
1613
// A function without a body corresponds to a trait method signature and
1714
// should not have a CFG scope.
18-
this.hasBody()
15+
this.(Function).hasBody()
16+
or
17+
this instanceof ClosureExpr
1918
}
2019

21-
override predicate scopeFirst(AstNode node) {
22-
first(this.(FunctionTree).getFirstChildNode(), node)
20+
/** Gets the body of this callable. */
21+
AstNode getBody() {
22+
result = this.(Function).getBody()
23+
or
24+
result = this.(ClosureExpr).getBody()
2325
}
24-
25-
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
26-
}
27-
28-
final class ClosureScope extends CfgScope, ClosureExpr {
29-
override predicate scopeFirst(AstNode node) {
30-
first(this.(ExprTrees::ClosureExprTree).getFirstChildNode(), node)
31-
}
32-
33-
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
3426
}
3527

3628
/**

0 commit comments

Comments
 (0)