Skip to content

Commit

Permalink
Merge pull request #18042 from hvitved/shared/dense-rank-refactor
Browse files Browse the repository at this point in the history
Util: Refactor `DenseRank` implementation
  • Loading branch information
hvitved authored Nov 20, 2024
2 parents 9aede5f + 42e0d7c commit 596cfcf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ module Impl {
}
}

private module DenseRankInput implements DenseRankInputSig3 {
private module DenseRankInput implements DenseRankInputSig2 {
class C1 = VariableScope;

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

/**
Expand Down
40 changes: 19 additions & 21 deletions shared/util/codeql/util/DenseRank.qll
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ module DenseRank<DenseRankInputSig Input> {
rnk = rank[result](int rnk0 | rnk0 = getRank(_) | rnk0)
}

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

/** Provides the input to `DenseRank2`. */
signature module DenseRankInputSig2 {
/** Provides the input to `DenseRank1`. */
signature module DenseRankInputSig1 {
/** A ranking context. */
bindingset[this]
class C;
Expand All @@ -77,25 +77,23 @@ signature module DenseRankInputSig2 {
}

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

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

/** Gets the dense rank of `r` in the context provided by `c`. */
int denseRank(C c, Ranked r) {
exists(int rnk |
result = rankRank(c, r, rnk) and
rnk = getRank(c, r)
)
}
/**
* Gets the `Ranked` value for which the dense rank in the context provided by
* `c` is `rnk`.
*/
Ranked denseRank(C c, int rnk) { rnk = rankRank(c, result, getRank(c, result)) }
}

/** Provides the input to `DenseRank3`. */
signature module DenseRankInputSig3 {
/** Provides the input to `DenseRank2`. */
signature module DenseRankInputSig2 {
/** A ranking context. */
bindingset[this]
class C1;
Expand All @@ -113,19 +111,19 @@ signature module DenseRankInputSig3 {
}

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

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

/** Gets the dense rank of `r` in the context provided by `c1` and `c2`. */
int denseRank(C1 c1, C2 c2, Ranked r) {
exists(int rnk |
result = rankRank(c1, c2, r, rnk) and
rnk = getRank(c1, c2, r)
)
/**
* Gets the `Ranked` value for which the dense rank in the context provided by
* `c1` and `c2` is `rnk`.
*/
Ranked denseRank(C1 c1, C2 c2, int rnk) {
rnk = rankRank(c1, c2, result, getRank(c1, c2, result))
}
}

0 comments on commit 596cfcf

Please sign in to comment.