Skip to content

Commit 596cfcf

Browse files
authored
Merge pull request #18042 from hvitved/shared/dense-rank-refactor
Util: Refactor `DenseRank` implementation
2 parents 9aede5f + 42e0d7c commit 596cfcf

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ module Impl {
378378
}
379379
}
380380

381-
private module DenseRankInput implements DenseRankInputSig3 {
381+
private module DenseRankInput implements DenseRankInputSig2 {
382382
class C1 = VariableScope;
383383

384384
class C2 = string;
@@ -401,7 +401,7 @@ module Impl {
401401
* to a variable named `name` in the variable scope `scope`.
402402
*/
403403
private int rankVariableOrAccess(VariableScope scope, string name, VariableOrAccessCand v) {
404-
result = DenseRank3<DenseRankInput>::denseRank(scope, name, v) - 1
404+
v = DenseRank2<DenseRankInput>::denseRank(scope, name, result + 1)
405405
}
406406

407407
/**

shared/util/codeql/util/DenseRank.qll

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ module DenseRank<DenseRankInputSig Input> {
5858
rnk = rank[result](int rnk0 | rnk0 = getRank(_) | rnk0)
5959
}
6060

61-
/** Gets the dense rank of `r`. */
62-
int denseRank(Ranked r) { result = rankRank(r, getRank(r)) }
61+
/** Gets the `Ranked` value for which the dense rank is `rnk`. */
62+
Ranked denseRank(int rnk) { rnk = rankRank(result, getRank(result)) }
6363
}
6464

65-
/** Provides the input to `DenseRank2`. */
66-
signature module DenseRankInputSig2 {
65+
/** Provides the input to `DenseRank1`. */
66+
signature module DenseRankInputSig1 {
6767
/** A ranking context. */
6868
bindingset[this]
6969
class C;
@@ -77,25 +77,23 @@ signature module DenseRankInputSig2 {
7777
}
7878

7979
/** Same as `DenseRank`, but allows for a context consisting of one element. */
80-
module DenseRank2<DenseRankInputSig2 Input> {
80+
module DenseRank1<DenseRankInputSig1 Input> {
8181
private import Input
8282

8383
private int rankRank(C c, Ranked r, int rnk) {
8484
rnk = getRank(c, r) and
8585
rnk = rank[result](int rnk0 | rnk0 = getRank(c, _) | rnk0)
8686
}
8787

88-
/** Gets the dense rank of `r` in the context provided by `c`. */
89-
int denseRank(C c, Ranked r) {
90-
exists(int rnk |
91-
result = rankRank(c, r, rnk) and
92-
rnk = getRank(c, r)
93-
)
94-
}
88+
/**
89+
* Gets the `Ranked` value for which the dense rank in the context provided by
90+
* `c` is `rnk`.
91+
*/
92+
Ranked denseRank(C c, int rnk) { rnk = rankRank(c, result, getRank(c, result)) }
9593
}
9694

97-
/** Provides the input to `DenseRank3`. */
98-
signature module DenseRankInputSig3 {
95+
/** Provides the input to `DenseRank2`. */
96+
signature module DenseRankInputSig2 {
9997
/** A ranking context. */
10098
bindingset[this]
10199
class C1;
@@ -113,19 +111,19 @@ signature module DenseRankInputSig3 {
113111
}
114112

115113
/** Same as `DenseRank`, but allows for a context consisting of two elements. */
116-
module DenseRank3<DenseRankInputSig3 Input> {
114+
module DenseRank2<DenseRankInputSig2 Input> {
117115
private import Input
118116

119117
private int rankRank(C1 c1, C2 c2, Ranked r, int rnk) {
120118
rnk = getRank(c1, c2, r) and
121119
rnk = rank[result](int rnk0 | rnk0 = getRank(c1, c2, _) | rnk0)
122120
}
123121

124-
/** Gets the dense rank of `r` in the context provided by `c1` and `c2`. */
125-
int denseRank(C1 c1, C2 c2, Ranked r) {
126-
exists(int rnk |
127-
result = rankRank(c1, c2, r, rnk) and
128-
rnk = getRank(c1, c2, r)
129-
)
122+
/**
123+
* Gets the `Ranked` value for which the dense rank in the context provided by
124+
* `c1` and `c2` is `rnk`.
125+
*/
126+
Ranked denseRank(C1 c1, C2 c2, int rnk) {
127+
rnk = rankRank(c1, c2, result, getRank(c1, c2, result))
130128
}
131129
}

0 commit comments

Comments
 (0)