Skip to content

Commit c81a1c2

Browse files
committed
Rust: Jump-to-def for operations and indexing
1 parent ee06364 commit c81a1c2

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ abstract class Use extends Locatable {
2626
* Gets the type of use.
2727
*/
2828
abstract string getUseType();
29+
30+
/**
31+
* Holds if this element is at the specified location.
32+
* The location spans column `startcolumn` of line `startline` to
33+
* column `endcolumn` of line `endline` in file `filepath`.
34+
* For more information, see
35+
* [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
36+
*/
37+
predicate hasLocationInfo(
38+
string filepath, int startline, int startcolumn, int endline, int endcolumn
39+
) {
40+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
41+
}
2942
}
3043

3144
cached
@@ -166,6 +179,43 @@ private class MethodUse extends Use instanceof NameRef {
166179
override string getUseType() { result = "method" }
167180
}
168181

182+
private class OperationUse extends Use instanceof Operation {
183+
override Definition getDefinition() { result.asItemNode() = this.(Call).getStaticTarget() }
184+
185+
override string getUseType() { result = "method" }
186+
187+
override predicate hasLocationInfo(
188+
string filepath, int startline, int startcolumn, int endline, int endcolumn
189+
) {
190+
// We don't have entities for the operator symbols, so approximate a location
191+
this =
192+
any(PrefixExpr pe |
193+
pe.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and
194+
pe.getExpr().getLocation().hasLocationInfo(_, endline, endcolumn + 2, _, _)
195+
)
196+
or
197+
this =
198+
any(BinaryExpr be |
199+
be.getLhs().getLocation().hasLocationInfo(filepath, _, _, startline, startcolumn - 2) and
200+
be.getRhs().getLocation().hasLocationInfo(filepath, endline, endcolumn + 2, _, _)
201+
)
202+
}
203+
}
204+
205+
private class IndexExprUse extends Use instanceof IndexExpr {
206+
override Definition getDefinition() { result.asItemNode() = this.(Call).getStaticTarget() }
207+
208+
override string getUseType() { result = "method" }
209+
210+
override predicate hasLocationInfo(
211+
string filepath, int startline, int startcolumn, int endline, int endcolumn
212+
) {
213+
// We don't have entities for the brackets, so approximate a location
214+
super.getIndex().getLocation().hasLocationInfo(filepath, _, _, startline, startcolumn - 2) and
215+
this.getLocation().hasLocationInfo(_, _, _, endline, endcolumn)
216+
}
217+
}
218+
169219
private class FileUse extends Use instanceof Name {
170220
override Definition getDefinition() {
171221
exists(Module m |

rust/ql/test/library-tests/definitions/Definitions.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@
6868
| main.rs:66:14:66:14 | S | main.rs:3:1:4:9 | struct S | path |
6969
| main.rs:66:18:66:20 | new | main.rs:46:9:48:9 | fn new | path |
7070
| main.rs:66:22:66:22 | S | main.rs:3:1:4:9 | struct S | path |
71+
| main.rs:67:5:67:4 | - ... | main.rs:11:5:13:5 | fn neg | method |
7172
| main.rs:67:6:67:6 | S | main.rs:3:1:4:9 | struct S | path |
7273
| main.rs:68:5:68:5 | S | main.rs:3:1:4:9 | struct S | path |
74+
| main.rs:68:7:68:7 | ... + ... | main.rs:19:5:21:5 | fn add | method |
7375
| main.rs:68:9:68:9 | S | main.rs:3:1:4:9 | struct S | path |
7476
| main.rs:69:5:69:5 | S | main.rs:3:1:4:9 | struct S | path |
77+
| main.rs:69:9:69:8 | S[0] | main.rs:27:5:29:5 | fn index | method |

0 commit comments

Comments
 (0)