Skip to content

Commit 1d4a8d4

Browse files
committed
CFG: Use dense ranking
1 parent 71fa216 commit 1d4a8d4

1 file changed

Lines changed: 41 additions & 12 deletions

File tree

shared/controlflow/codeql/controlflow/ControlFlowGraph.qll

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
483483
* by subsequent instatiation of `Make2`.
484484
*/
485485
module Make1<InputSig1 Input1> {
486+
private import codeql.util.DenseRank
487+
486488
/**
487489
* Holds if `n` is executed in post-order or in-order. This means that an
488490
* additional node is created to represent `n` in the control flow graph.
@@ -687,15 +689,25 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
687689
n = callableGetBody(c) or n = Input1::callableGetBodyPart(c, _, _)
688690
}
689691

690-
private AstNode getRankedBodyPart(Callable c, Input1::CallableBodyPartContext ctx, int rnk) {
691-
result =
692-
rank[rnk](AstNode child, int ix |
693-
child = Input1::callableGetBodyPart(c, ctx, ix)
694-
|
695-
child order by ix
696-
)
692+
private module BodyPartDenseRankInput implements DenseRankInputSig2 {
693+
class C1 = Callable;
694+
695+
class C2 = Input1::CallableBodyPartContext;
696+
697+
class Ranked = AstNode;
698+
699+
int getRank(C1 c, C2 ctx, Ranked n) {
700+
n =
701+
rank[result](AstNode child, int ix |
702+
child = Input1::callableGetBodyPart(c, ctx, ix)
703+
|
704+
child order by ix
705+
)
706+
}
697707
}
698708

709+
private predicate getRankedBodyPart = DenseRank2<BodyPartDenseRankInput>::denseRank/3;
710+
699711
private AstNode getBodyEntry(Callable c) {
700712
result = callableGetBody(c) and
701713
not exists(getRankedBodyPart(c, _, _))
@@ -1247,10 +1259,19 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
12471259
)
12481260
}
12491261

1250-
private Case getRankedCaseCfgOrder(Switch s, int rnk) {
1251-
result = rank[rnk](Case c, int i | getCaseControlFlowOrder(s, c) = i | c order by i)
1262+
private module CaseDenseRankInput implements DenseRankInputSig1 {
1263+
class C = Switch;
1264+
1265+
class Ranked = Case;
1266+
1267+
int getRank(C parent, Ranked child) {
1268+
child =
1269+
rank[result](Case c, int i | getCaseControlFlowOrder(parent, c) = i | c order by i)
1270+
}
12521271
}
12531272

1273+
private predicate getRankedCaseCfgOrder = DenseRank1<CaseDenseRankInput>::denseRank/2;
1274+
12541275
private int numberOfStmts(Switch s) { result = strictcount(s.getStmt(_)) }
12551276

12561277
private predicate caseIndex(Switch s, Case c, int caseIdx, int caseStmtPos) {
@@ -1685,11 +1706,19 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
16851706
not explicitStep(any(PreControlFlowNode n | n.isBefore(ast)), _)
16861707
}
16871708

1688-
private AstNode getRankedChild(AstNode parent, int rnk) {
1689-
defaultCfg(parent) and
1690-
result = rank[rnk](AstNode c, int ix | c = getChild(parent, ix) | c order by ix)
1709+
private module ChildDenseRankInput implements DenseRankInputSig1 {
1710+
class C = AstNode;
1711+
1712+
class Ranked = AstNode;
1713+
1714+
int getRank(C parent, Ranked child) {
1715+
defaultCfg(parent) and
1716+
child = rank[result](AstNode c, int ix | c = getChild(parent, ix) | c order by ix)
1717+
}
16911718
}
16921719

1720+
private predicate getRankedChild = DenseRank1<ChildDenseRankInput>::denseRank/2;
1721+
16931722
/**
16941723
* Holds if `n1` to `n2` is a default left-to-right evaluation step for
16951724
* an `AstNode` that does not otherwise have explicitly defined control

0 commit comments

Comments
 (0)