Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util: Refactor DenseRank implementation #18042

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading