Skip to content

Commit 27fd756

Browse files
committed
Rust: Extend jump-to-def to include paths and mod file; imports
1 parent 6f71e3b commit 27fd756

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

rust/ql/lib/codeql/rust/internal/Definitions.qll

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* in the code viewer.
44
*/
55

6+
private import rust
67
private import codeql.rust.elements.Variable
78
private import codeql.rust.elements.Locatable
89
private import codeql.rust.elements.FormatArgsExpr
@@ -12,6 +13,7 @@ private import codeql.rust.elements.MacroCall
1213
private import codeql.rust.elements.NamedFormatArgument
1314
private import codeql.rust.elements.PositionalFormatArgument
1415
private import codeql.Locations
16+
private import codeql.rust.internal.PathResolution
1517

1618
/** An element with an associated definition. */
1719
abstract class Use extends Locatable {
@@ -30,7 +32,8 @@ private module Cached {
3032
newtype TDef =
3133
TVariable(Variable v) or
3234
TFormatArgsArgName(Name name) { name = any(FormatArgsArg a).getName() } or
33-
TFormatArgsArgIndex(Expr e) { e = any(FormatArgsArg a).getExpr() }
35+
TFormatArgsArgIndex(Expr e) { e = any(FormatArgsArg a).getExpr() } or
36+
TItemNode(ItemNode i)
3437

3538
/**
3639
* Gets an element, of kind `kind`, that element `use` uses, if any.
@@ -51,7 +54,8 @@ class Definition extends Cached::TDef {
5154
Location getLocation() {
5255
result = this.asVariable().getLocation() or
5356
result = this.asName().getLocation() or
54-
result = this.asExpr().getLocation()
57+
result = this.asExpr().getLocation() or
58+
result = this.asItemNode().getLocation()
5559
}
5660

5761
/** Gets this definition as a `Variable` */
@@ -63,11 +67,15 @@ class Definition extends Cached::TDef {
6367
/** Gets this definition as an `Expr` */
6468
Expr asExpr() { this = Cached::TFormatArgsArgIndex(result) }
6569

70+
/** Gets this definition as an `ItemNode` */
71+
ItemNode asItemNode() { this = Cached::TItemNode(result) }
72+
6673
/** Gets the string representation of this element. */
6774
string toString() {
6875
result = this.asExpr().toString() or
6976
result = this.asVariable().toString() or
70-
result = this.asName().getText()
77+
result = this.asName().getText() or
78+
result = this.asItemNode().toString()
7179
}
7280
}
7381

@@ -124,3 +132,20 @@ private class PositionalFormatArgumentUse extends Use instanceof PositionalForma
124132

125133
override string getUseType() { result = "format argument" }
126134
}
135+
136+
private class PathUse extends Use instanceof Path {
137+
override Definition getDefinition() { result.asItemNode() = resolvePath(this) }
138+
139+
override string getUseType() { result = "path" }
140+
}
141+
142+
private class FileUse extends Use instanceof Name {
143+
override Definition getDefinition() {
144+
exists(Module m |
145+
this = m.getName() and
146+
fileImport(m, result.asItemNode())
147+
)
148+
}
149+
150+
override string getUseType() { result = "file" }
151+
}

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ private predicate pathAttrImport(Folder f, Module m, string relativePath) {
10211021
private predicate shouldAppend(Folder f, string relativePath) { pathAttrImport(f, _, relativePath) }
10221022

10231023
/** Holds if `m` is a `mod name;` item importing file `f`. */
1024-
private predicate fileImport(Module m, SourceFile f) {
1024+
pragma[nomagic]
1025+
predicate fileImport(Module m, SourceFile f) {
10251026
exists(string name, Folder parent |
10261027
modImport0(m, name, _) and
10271028
fileModule(f, name, parent)

0 commit comments

Comments
 (0)