Skip to content

Commit

Permalink
Merge pull request #18579 from hvitved/rust/path-resolution
Browse files Browse the repository at this point in the history
Rust: Implement path resolution in QL
  • Loading branch information
hvitved authored Jan 31, 2025
2 parents 384c040 + 1cb524f commit 180782d
Show file tree
Hide file tree
Showing 19 changed files with 999 additions and 59 deletions.
11 changes: 11 additions & 0 deletions rust/ql/lib/codeql/rust/AstConsistency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ query predicate multiplePositions(Element parent, int pos1, int pos2, string acc
pos1 != pos2
}

private import codeql.rust.elements.internal.PathResolution

/** Holds if `p` may resolve to multiple items including `i`. */
query predicate multiplePathResolutions(Path p, ItemNode i) {
i = resolvePath(p) and
strictcount(resolvePath(p)) > 1
}

/**
* Gets counts of abstract syntax tree inconsistencies of each type.
*/
Expand All @@ -98,4 +106,7 @@ int getAstInconsistencyCounts(string type) {
or
type = "Multiple positions" and
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
or
type = "Multiple path resolutions" and
result = count(Path p | multiplePathResolutions(p, _) | p)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Impl {
private import codeql.rust.elements.internal.MethodCallExprImpl::Impl
private import codeql.rust.elements.internal.CallExprImpl::Impl
private import codeql.rust.elements.internal.PathExprImpl::Impl
private import codeql.rust.elements.internal.PathResolution

pragma[nomagic]
Resolvable getCallResolvable(CallExprBase call) {
Expand All @@ -33,6 +34,10 @@ module Impl {
* Gets the target callable of this call, if a unique such target can
* be statically resolved.
*/
Callable getStaticTarget() { getCallResolvable(this).resolvesAsItem(result) }
Callable getStaticTarget() {
getCallResolvable(this).resolvesAsItem(result)
or
result = resolvePath(this.(CallExpr).getFunction().(PathExpr).getPath())
}
}
}
6 changes: 6 additions & 0 deletions rust/ql/lib/codeql/rust/elements/internal/PathImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ module Impl {
then result = "...::" + this.getPart().toAbbreviatedString()
else result = this.getPart().toAbbreviatedString()
}

/**
* Gets the text of this path, if it exists.
*/
pragma[nomagic]
string getText() { result = this.getPart().getNameRef().getText() }
}
}
Loading

0 comments on commit 180782d

Please sign in to comment.