Skip to content

Commit 06746e5

Browse files
authored
Merge pull request github#18140 from paldepind/rust-get-target
Rust: Add `getStaticTarget` to `CallExprBase`
2 parents bcb7901 + e8ddb6b commit 06746e5

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -538,20 +538,9 @@ module RustDataFlow implements InputSig<Location> {
538538

539539
final class ReturnKind = ReturnKindAlias;
540540

541-
pragma[nomagic]
542-
private Resolvable getCallResolvable(CallExprBase call) {
543-
result = call.(MethodCallExpr)
544-
or
545-
result = call.(CallExpr).getFunction().(PathExpr).getPath()
546-
}
547-
548541
/** Gets a viable implementation of the target of the given `Call`. */
549542
DataFlowCallable viableCallable(DataFlowCall call) {
550-
exists(Resolvable r, string path, CrateOriginOption crate |
551-
hasExtendedCanonicalPath(result.asCfgScope(), crate, path) and
552-
r = getCallResolvable(call.asCallBaseExprCfgNode().getExpr()) and
553-
resolveExtendedCanonicalPath(r, crate, path)
554-
)
543+
result.asCfgScope() = call.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget()
555544
}
556545

557546
/**

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `CallExprBase`.
43
*
@@ -12,8 +11,27 @@ private import codeql.rust.elements.internal.generated.CallExprBase
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
private import codeql.rust.elements.internal.CallableImpl::Impl
15+
private import codeql.rust.elements.internal.MethodCallExprImpl::Impl
16+
private import codeql.rust.elements.internal.CallExprImpl::Impl
17+
private import codeql.rust.elements.internal.PathExprImpl::Impl
18+
19+
pragma[nomagic]
20+
private Resolvable getCallResolvable(CallExprBase call) {
21+
result = call.(MethodCallExpr)
22+
or
23+
result = call.(CallExpr).getFunction().(PathExpr).getPath()
24+
}
25+
26+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1527
/**
1628
* A function or method call expression. See `CallExpr` and `MethodCallExpr` for further details.
1729
*/
18-
class CallExprBase extends Generated::CallExprBase { }
30+
class CallExprBase extends Generated::CallExprBase {
31+
/**
32+
* Gets the target callable of this call, if a unique such target can
33+
* be statically resolved.
34+
*/
35+
Callable getStaticTarget() { getCallResolvable(this).resolvesAsItem(result) }
36+
}
1937
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Resolvable`.
43
*
@@ -12,8 +11,25 @@ private import codeql.rust.elements.internal.generated.Resolvable
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
private import codeql.rust.elements.internal.ItemImpl::Impl
15+
16+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1517
/**
1618
* Either a `Path`, or a `MethodCallExpr`.
1719
*/
18-
class Resolvable extends Generated::Resolvable { }
20+
class Resolvable extends Generated::Resolvable {
21+
/**
22+
* Holds if this resolvable and the item `i` resolves to the same canonical
23+
* path in the same crate
24+
*/
25+
pragma[nomagic]
26+
predicate resolvesAsItem(Item i) {
27+
this.getResolvedPath() = i.getExtendedCanonicalPath() and
28+
(
29+
this.getResolvedCrateOrigin() = i.getCrateOrigin()
30+
or
31+
not this.hasResolvedCrateOrigin() and not i.hasCrateOrigin()
32+
)
33+
}
34+
}
1935
}

0 commit comments

Comments
 (0)