Skip to content

Commit 6568eb8

Browse files
committed
Rust: Refactor CFG pattern tree implementation
1 parent 7aa28a0 commit 6568eb8

File tree

1 file changed

+24
-62
lines changed

1 file changed

+24
-62
lines changed

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

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -605,94 +605,48 @@ class YeetExprTree extends StandardPostOrderTree instanceof YeetExpr {
605605
* `OrPat`s and `IdentPat`s.
606606
*/
607607
module PatternTrees {
608-
abstract class PreOrderPatTree extends PreOrderTree {
608+
abstract class StandardPatTree extends StandardTree {
609609
abstract Pat getPat(int i);
610610

611-
private Pat getPatRanked(int i) {
611+
Pat getPatRanked(int i) {
612612
result = rank[i + 1](Pat pat, int j | pat = this.getPat(j) | pat order by j)
613613
}
614614

615-
predicate isEmpty() { not any(Pat p) = this.getPatRanked(0) }
616-
617-
override predicate propagatesAbnormal(AstNode child) { child = this.getPatRanked(_) }
615+
override AstNode getChildNode(int i) { result = this.getPat(i) }
616+
}
618617

618+
abstract class PreOrderPatTree extends StandardPatTree, StandardPreOrderTree {
619619
override predicate succ(AstNode pred, AstNode succ, Completion c) {
620-
pred = this and
621-
completionIsValidFor(c, this) and
622620
c.(MatchCompletion).succeeded() and
623-
first(this.getPatRanked(0), succ)
624-
or
625-
exists(int i | last(this.getPatRanked(i), pred, c) |
626-
// Edge from successful pattern to the next
627-
c.(MatchCompletion).succeeded() and
628-
first(this.getPatRanked(i + 1), succ)
629-
)
630-
}
631-
632-
override predicate last(AstNode node, Completion c) {
633-
node = this and
634621
(
635-
completionIsValidFor(c, this) and c.(MatchCompletion).failed()
622+
StandardPatTree.super.succ(pred, succ, c)
636623
or
637-
this.isEmpty() and node = this and c.(MatchCompletion).succeeded()
624+
pred = this and first(this.getFirstChildNode(), succ) and completionIsValidFor(c, this)
638625
)
639-
or
640-
exists(int i | last(this.getPatRanked(i), node, c) |
641-
c.(MatchCompletion).failed()
642-
or
643-
not exists(this.getPatRanked(i + 1)) and
644-
completionIsNormal(c)
645-
)
646-
}
647-
}
648-
649-
abstract class PostOrderPatTree extends PostOrderTree {
650-
abstract Pat getPat(int i);
651-
652-
private Pat getPatRanked(int i) {
653-
result = rank[i + 1](Pat pat, int j | pat = this.getPat(j) | pat order by j)
654626
}
655627

656-
override predicate propagatesAbnormal(AstNode child) { child = this.getPatRanked(_) }
657-
658-
override predicate first(AstNode node) {
659-
first(this.getPat(0), node)
628+
override predicate last(AstNode node, Completion c) {
629+
super.last(node, c)
660630
or
661-
not exists(this.getPat(_)) and
662-
node = this
663-
}
664-
665-
override predicate succ(AstNode pred, AstNode succ, Completion c) {
666-
exists(int i | last(this.getPat(i), pred, c) |
667-
// Edge from unsuccessful pattern to the next
668-
c.(MatchCompletion).failed() and
669-
first(this.getPat(i + 1), succ)
670-
or
671-
// Edge from successful pattern to this
672-
c.(MatchCompletion).succeeded() and
673-
succ = this
674-
or
675-
// Edge from last pattern to this
676-
not exists(this.getPat(i + 1)) and
677-
succ = this and
678-
completionIsNormal(c)
679-
)
631+
c.(MatchCompletion).failed() and
632+
completionIsValidFor(c, this) and
633+
(node = this or last(this.getPatRanked(_), node, c))
680634
}
681635
}
682636

637+
abstract class PostOrderPatTree extends StandardPatTree, StandardPostOrderTree { }
638+
683639
class IdentPatTree extends PostOrderPatTree, IdentPat {
684640
override Pat getPat(int i) { i = 0 and result = this.getPat() }
685641

686642
override predicate last(AstNode node, Completion c) {
687643
super.last(node, c)
688644
or
689-
last(this.getPat(), node, c) and
690-
c.(MatchCompletion).failed()
645+
last(this.getPat(), node, c) and c.(MatchCompletion).failed()
691646
}
692647

693648
override predicate succ(AstNode pred, AstNode succ, Completion c) {
694-
super.succ(pred, succ, c) and
695-
not (succ = this and c.(MatchCompletion).failed())
649+
super.succ(pred, succ, c) and c.(MatchCompletion).succeeded()
696650
}
697651
}
698652

@@ -710,6 +664,14 @@ module PatternTrees {
710664

711665
class OrPatTree extends PostOrderPatTree instanceof OrPat {
712666
override Pat getPat(int i) { result = OrPat.super.getPat(i) }
667+
668+
override predicate succ(AstNode pred, AstNode succ, Completion c) {
669+
// Failed patterns advance normally between children
670+
c.(MatchCompletion).failed() and super.succ(pred, succ, c)
671+
or
672+
// Successful pattern step to this
673+
c.(MatchCompletion).succeeded() and succ = this and last(this.getPat(_), pred, c)
674+
}
713675
}
714676

715677
class ParenPatTree extends ControlFlowTree, ParenPat {

0 commit comments

Comments
 (0)