Skip to content

Commit 299ccb6

Browse files
authored
Merge pull request #20230 from hvitved/cfg/standard-tree-skip-non-tree-children
Shared: Skip non-CFG children in `StandardTree`
2 parents 4eea443 + 7501e62 commit 299ccb6

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private predicate letElsePanic(BlockExpr be) {
5353
*/
5454
query predicate deadEnd(CfgImpl::Node node) {
5555
Consistency::deadEnd(node) and
56+
successfullyExtractedFile(node.getLocation().getFile()) and
5657
not letElsePanic(node.getAstNode())
5758
}
5859

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ module PatternTrees {
619619
(
620620
StandardPatTree.super.succ(pred, succ, c)
621621
or
622-
pred = this and first(this.getFirstChildNode(), succ) and completionIsValidFor(c, this)
622+
pred = this and first(this.getFirstChildTree(), succ) and completionIsValidFor(c, this)
623623
)
624624
}
625625

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ final class CfgScope = CfgScopeImpl;
1818

1919
final class AsyncBlockScope extends CfgScopeImpl, AsyncBlockExpr instanceof ExprTrees::AsyncBlockExprTree
2020
{
21-
override predicate scopeFirst(AstNode first) { first(super.getFirstChildNode(), first) }
21+
override predicate scopeFirst(AstNode first) { first(super.getFirstChildTree(), first) }
2222

2323
override predicate scopeLast(AstNode last, Completion c) {
24-
last(super.getLastChildElement(), last, c)
24+
last(super.getLastChildTree(), last, c)
2525
or
2626
last(super.getChildNode(_), last, c) and
2727
not c instanceof NormalCompletion
@@ -48,7 +48,7 @@ final class CallableScope extends CfgScopeImpl, Callable {
4848
}
4949

5050
override predicate scopeFirst(AstNode first) {
51-
first(this.(CallableScopeTree).getFirstChildNode(), first)
51+
first(this.(CallableScopeTree).getFirstChildTree(), first)
5252
}
5353

5454
/** Holds if `scope` is exited when `last` finishes with completion `c`. */

shared/controlflow/codeql/controlflow/Cfg.qll

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,40 +261,52 @@ module MakeWithSplitting<
261261
/** Gets the `i`th child element, in order of evaluation. */
262262
abstract AstNode getChildNode(int i);
263263

264-
private AstNode getChildNodeRanked(int i) {
265-
result = rank[i + 1](AstNode child, int j | child = this.getChildNode(j) | child order by j)
264+
private ControlFlowTree getChildTreeRanked(int i) {
265+
result =
266+
rank[i + 1](ControlFlowTree child, int j | child = this.getChildNode(j) | child order by j)
266267
}
267268

268269
/** Gets the first child node of this element. */
269-
final AstNode getFirstChildNode() { result = this.getChildNodeRanked(0) }
270+
deprecated final AstNode getFirstChildNode() { result = this.getChildTreeRanked(0) }
271+
272+
/** Gets the first child node of this element. */
273+
final ControlFlowTree getFirstChildTree() { result = this.getChildTreeRanked(0) }
274+
275+
/** Gets the last child node of this node. */
276+
deprecated final AstNode getLastChildElement() {
277+
exists(int last |
278+
result = this.getChildTreeRanked(last) and
279+
not exists(this.getChildTreeRanked(last + 1))
280+
)
281+
}
270282

271283
/** Gets the last child node of this node. */
272-
final AstNode getLastChildElement() {
284+
final ControlFlowTree getLastChildTree() {
273285
exists(int last |
274-
result = this.getChildNodeRanked(last) and
275-
not exists(this.getChildNodeRanked(last + 1))
286+
result = this.getChildTreeRanked(last) and
287+
not exists(this.getChildTreeRanked(last + 1))
276288
)
277289
}
278290

279291
/** Holds if this element has no children. */
280-
predicate isLeafElement() { not exists(this.getFirstChildNode()) }
292+
predicate isLeafElement() { not exists(this.getFirstChildTree()) }
281293

282294
override predicate propagatesAbnormal(AstNode child) { child = this.getChildNode(_) }
283295

284296
pragma[nomagic]
285297
override predicate succ(AstNode pred, AstNode succ, Completion c) {
286298
exists(int i |
287-
last(this.getChildNodeRanked(i), pred, c) and
299+
last(this.getChildTreeRanked(i), pred, c) and
288300
completionIsNormal(c) and
289-
first(this.getChildNodeRanked(i + 1), succ)
301+
first(this.getChildTreeRanked(i + 1), succ)
290302
)
291303
}
292304
}
293305

294306
/** A standard element that is executed in pre-order. */
295307
abstract class StandardPreOrderTree extends StandardTree, PreOrderTree {
296308
override predicate last(AstNode last, Completion c) {
297-
last(this.getLastChildElement(), last, c)
309+
last(this.getLastChildTree(), last, c)
298310
or
299311
this.isLeafElement() and
300312
completionIsValidFor(c, this) and
@@ -305,24 +317,24 @@ module MakeWithSplitting<
305317
StandardTree.super.succ(pred, succ, c)
306318
or
307319
pred = this and
308-
first(this.getFirstChildNode(), succ) and
320+
first(this.getFirstChildTree(), succ) and
309321
completionIsSimple(c)
310322
}
311323
}
312324

313325
/** A standard element that is executed in post-order. */
314326
abstract class StandardPostOrderTree extends StandardTree, PostOrderTree {
315327
override predicate first(AstNode first) {
316-
first(this.getFirstChildNode(), first)
328+
first(this.getFirstChildTree(), first)
317329
or
318-
not exists(this.getFirstChildNode()) and
330+
not exists(this.getFirstChildTree()) and
319331
first = this
320332
}
321333

322334
override predicate succ(AstNode pred, AstNode succ, Completion c) {
323335
StandardTree.super.succ(pred, succ, c)
324336
or
325-
last(this.getLastChildElement(), pred, c) and
337+
last(this.getLastChildTree(), pred, c) and
326338
succ = this and
327339
completionIsNormal(c)
328340
}

0 commit comments

Comments
 (0)