Skip to content

Commit 61aad2e

Browse files
committed
Rust: Sort CFG trees and add scope for closures
1 parent 1a85dfd commit 61aad2e

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

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

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ module CfgImpl = Make<Location, CfgInput>;
5959

6060
import CfgImpl
6161

62-
class FunctionTree extends LeafTree instanceof Function { }
63-
64-
class BlockExprTree extends StandardPostOrderTree instanceof BlockExpr {
65-
override ControlFlowTree getChildNode(int i) {
66-
result = super.getStatement(i)
67-
or
68-
not exists(super.getStatement(i)) and
69-
(exists(super.getStatement(i - 1)) or i = 0) and
70-
result = super.getTail()
71-
}
72-
}
73-
74-
class CallExprTree extends StandardPostOrderTree instanceof CallExpr {
75-
override ControlFlowTree getChildNode(int i) { result = super.getArg(i) }
76-
}
77-
7862
class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryOpExpr {
7963
BinaryOpExprTree() { super.getOp() != "&&" and super.getOp() != "||" }
8064

@@ -127,6 +111,44 @@ class LogicalAndBinaryOpExprTree extends PostOrderTree instanceof BinaryOpExpr {
127111
}
128112
}
129113

114+
class BlockExprTree extends StandardPostOrderTree instanceof BlockExpr {
115+
override ControlFlowTree getChildNode(int i) {
116+
result = super.getStatement(i)
117+
or
118+
not exists(super.getStatement(i)) and
119+
(exists(super.getStatement(i - 1)) or i = 0) and
120+
result = super.getTail()
121+
}
122+
}
123+
124+
class BreakExprTree extends PostOrderTree instanceof BreakExpr {
125+
override predicate propagatesAbnormal(AstNode child) { child = super.getExpr() }
126+
127+
override predicate first(AstNode node) {
128+
first(super.getExpr(), node)
129+
or
130+
not super.hasExpr() and node = this
131+
}
132+
133+
override predicate succ(AstNode pred, AstNode succ, Completion c) {
134+
last(super.getExpr(), pred, c) and succ = this
135+
}
136+
}
137+
138+
class CallExprTree extends StandardPostOrderTree instanceof CallExpr {
139+
override ControlFlowTree getChildNode(int i) { result = super.getArg(i) }
140+
}
141+
142+
class ClosureExprTree extends LeafTree instanceof ClosureExpr { }
143+
144+
class ContinueExprTree extends LeafTree instanceof ContinueExpr { }
145+
146+
class ExprStmtTree extends StandardPostOrderTree instanceof ExprStmt {
147+
override ControlFlowTree getChildNode(int i) { i = 0 and result = super.getExpr() }
148+
}
149+
150+
class FunctionTree extends LeafTree instanceof Function { }
151+
130152
class IfExprTree extends PostOrderTree instanceof IfExpr {
131153
override predicate first(AstNode node) { first(super.getCondition(), node) }
132154

@@ -157,10 +179,6 @@ class IfExprTree extends PostOrderTree instanceof IfExpr {
157179
}
158180
}
159181

160-
class ExprStmtTree extends StandardPostOrderTree instanceof ExprStmt {
161-
override ControlFlowTree getChildNode(int i) { i = 0 and result = super.getExpr() }
162-
}
163-
164182
class LetExprTree extends StandardPostOrderTree instanceof LetExpr {
165183
override ControlFlowTree getChildNode(int i) { i = 0 and result = super.getExpr() }
166184
}
@@ -173,6 +191,8 @@ class LetStmtTree extends StandardPostOrderTree instanceof LetStmt {
173191
}
174192
}
175193

194+
class LiteralExprTree extends LeafTree instanceof LiteralExpr { }
195+
176196
class LoopExprTree extends PostOrderTree instanceof LoopExpr {
177197
override predicate propagatesAbnormal(AstNode child) { none() }
178198

@@ -199,21 +219,9 @@ class LoopExprTree extends PostOrderTree instanceof LoopExpr {
199219
}
200220
}
201221

202-
class ReturnExprTree extends PostOrderTree instanceof ReturnExpr {
203-
override predicate propagatesAbnormal(AstNode child) { child = super.getExpr() }
204-
205-
override predicate first(AstNode node) {
206-
first(super.getExpr(), node)
207-
or
208-
not super.hasExpr() and node = this
209-
}
210-
211-
override predicate succ(AstNode pred, AstNode succ, Completion c) {
212-
last(super.getExpr(), pred, c) and succ = this
213-
}
214-
}
222+
class PathExprTree extends LeafTree instanceof PathExpr { }
215223

216-
class BreakExprTree extends PostOrderTree instanceof BreakExpr {
224+
class ReturnExprTree extends PostOrderTree instanceof ReturnExpr {
217225
override predicate propagatesAbnormal(AstNode child) { child = super.getExpr() }
218226

219227
override predicate first(AstNode node) {
@@ -227,13 +235,5 @@ class BreakExprTree extends PostOrderTree instanceof BreakExpr {
227235
}
228236
}
229237

230-
class ClosureExprTree extends LeafTree instanceof ClosureExpr { }
231-
232-
class ContinueExprTree extends LeafTree instanceof ContinueExpr { }
233-
234-
class LiteralExprTree extends LeafTree instanceof LiteralExpr { }
235-
236-
class PathExprTree extends LeafTree instanceof PathExpr { }
237-
238238
// A leaf tree for unimplemented nodes in the AST.
239239
class UnimplementedTree extends LeafTree instanceof Unimplemented { }

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ final class FunctionScope extends CfgScope, Function {
1515
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
1616
}
1717

18+
final class LambdaScope extends CfgScope, ClosureExpr {
19+
override predicate scopeFirst(AstNode node) { first(this.getBody(), node) }
20+
21+
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
22+
}
23+
1824
/**
1925
* Gets the immediate parent of a non-`AstNode` element `e`.
2026
*

0 commit comments

Comments
 (0)