Skip to content

Commit c9788a7

Browse files
authored
Merge pull request github#3308 from jcreedcmu/jcreed/jump-to-def
Add queries for VS Code jump-to-definition
2 parents 761e318 + 62c128f commit c9788a7

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@
1212
- Critical/FileNeverClosed.ql
1313
- Critical/MemoryMayNotBeFreed.ql
1414
- Critical/MemoryNeverFreed.ql
15+
# These are only for IDE use.
16+
- exclude:
17+
tags contain:
18+
- ide-contextual-queries/local-definitions
19+
- ide-contextual-queries/local-references

cpp/ql/src/definitions.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
132132
* - `"X"` for macro accesses
133133
* - `"I"` for import / include directives
134134
*/
135+
cached
135136
Top definitionOf(Top e, string kind) {
136137
(
137138
// call -> function called
@@ -213,3 +214,11 @@ Top definitionOf(Top e, string kind) {
213214
// later on.
214215
strictcount(result.getLocation()) < 10
215216
}
217+
218+
/**
219+
* Returns an appropriately encoded version of a filename `name`
220+
* passed by the VS Code extension in order to coincide with the
221+
* output of `.getFile()` on locatable entities.
222+
*/
223+
cached
224+
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

cpp/ql/src/localDefinitions.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 cpp/ide-jump-to-definition
7+
* @tags ide-contextual-queries/local-definitions
8+
*/
9+
10+
import definitions
11+
12+
external string selectedSourceFile();
13+
14+
from Top e, Top def, string kind
15+
where def = definitionOf(e, kind) and e.getFile() = getEncodedFile(selectedSourceFile())
16+
select e, def, kind

cpp/ql/src/localReferences.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 cpp/ide-find-references
7+
* @tags ide-contextual-queries/local-references
8+
*/
9+
10+
import definitions
11+
12+
external string selectedSourceFile();
13+
14+
from Top e, Top def, string kind
15+
where def = definitionOf(e, kind) and def.getFile() = getEncodedFile(selectedSourceFile())
16+
select e, def, kind

0 commit comments

Comments
 (0)