Skip to content

Commit 964b847

Browse files
authored
Merge pull request github#3405 from jcreedcmu/jcreed/jump-to-def-python
Python: Refactor definitions query, add queries for ide search
2 parents b2f1008 + 5934345 commit 964b847

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

python/ql/src/analysis/DefinitionTracking.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,25 @@ class NiceLocationExpr extends @py_expr {
490490
)
491491
}
492492
}
493+
494+
/**
495+
* Gets the definition (of kind `kind`) for the expression `use`, if one can be found.
496+
*/
497+
cached
498+
Definition definitionOf(NiceLocationExpr use, string kind) {
499+
exists(string f, int l |
500+
result = getUniqueDefinition(use) and
501+
kind = "Definition" and
502+
use.hasLocationInfo(f, l, _, _, _) and
503+
// Ignore if the definition is on the same line as the use
504+
not result.getLocation().hasLocationInfo(f, l, _, _, _)
505+
)
506+
}
507+
508+
/**
509+
* Returns an appropriately encoded version of a filename `name`
510+
* passed by the VS Code extension in order to coincide with the
511+
* output of `.getFile()` on locatable entities.
512+
*/
513+
cached
514+
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

python/ql/src/analysis/Definitions.ql

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
import python
99
import DefinitionTracking
1010

11-
from NiceLocationExpr use, Definition defn, string kind, string f, int l
12-
where
13-
defn = getUniqueDefinition(use) and
14-
kind = "Definition" and
15-
use.hasLocationInfo(f, l, _, _, _) and
16-
// Ignore if the definition is on the same line as the use
17-
not defn.getLocation().hasLocationInfo(f, l, _, _, _)
18-
select use, defn, kind
11+
from NiceLocationExpr use, Definition defn, string kind
12+
where defn = definitionOf(use, kind)
13+
select use, defn, kind
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Jump-to-definition links
3+
* @description Generates use-definition pairs that provide the data
4+
* for jump-to-definition in the code viewer.
5+
* @kind definitions
6+
* @id python/ide-jump-to-definition
7+
* @tags ide-contextual-queries/local-definitions
8+
*/
9+
10+
import python
11+
import DefinitionTracking
12+
13+
external string selectedSourceFile();
14+
15+
from NiceLocationExpr use, Definition defn, string kind, string f
16+
where defn = definitionOf(use, kind)
17+
and use.hasLocationInfo(f, _, _, _, _)
18+
and getEncodedFile(selectedSourceFile()).getAbsolutePath() = f
19+
select use, defn, kind
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Find-references links
3+
* @description Generates use-definition pairs that provide the data
4+
* for find-references in the code viewer.
5+
* @kind definitions
6+
* @id python/ide-find-references
7+
* @tags ide-contextual-queries/local-references
8+
*/
9+
10+
import python
11+
import DefinitionTracking
12+
13+
external string selectedSourceFile();
14+
15+
from NiceLocationExpr use, Definition defn, string kind
16+
where defn = definitionOf(use, kind)
17+
and defn.getLocation().getFile() = getEncodedFile(selectedSourceFile())
18+
select use, defn, kind

python/ql/src/codeql-suites/python-lgtm-full.qls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
- qlpack: codeql-python
33
- apply: lgtm-selectors.yml
44
from: codeql-suite-helpers
5+
# These are only for IDE use.
6+
- exclude:
7+
tags contain:
8+
- ide-contextual-queries/local-definitions
9+
- ide-contextual-queries/local-references

0 commit comments

Comments
 (0)