Add basic code navigation queries

This commit is contained in:
Arthur Baars
2020-12-03 17:11:22 +01:00
parent 1d502cb40d
commit d92d635103
4 changed files with 61 additions and 19 deletions

19
ql/src/codeql/IDE.qll Normal file
View File

@@ -0,0 +1,19 @@
private import codeql.files.FileSystem
/**
* Returns an appropriately encoded version of a filename `name`
* passed by the VS Code extension in order to coincide with the
* output of `.getFile()` on locatable entities.
*/
cached
File getFileBySourceArchiveName(string name) {
// The name provided for a file in the source archive by the VS Code extension
// has some differences from the absolute path in the database:
// 1. colons are replaced by underscores
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
// "/C_/foo/bar"
// 3. double slashes in UNC prefixes are replaced with a single slash
// We can handle 2 and 3 together by unconditionally adding a leading slash
// before replacing double slashes.
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
}

View File

@@ -0,0 +1,20 @@
/**
* @name Jump-to-definition links
* @description Generates use-definition pairs that provide the data
* for jump-to-definition in the code viewer.
* @kind definitions
* @id ruby/ide-jump-to-definition
* @tags ide-contextual-queries/local-definitions
*/
import codeql.IDE
import codeql_ruby.AST
external string selectedSourceFile();
from AstNode e, Variable def, string kind
where
e = def.getAnAccess() and
kind = "local variable" and
e.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
select e, def, kind

21
ql/src/localReferences.ql Normal file
View File

@@ -0,0 +1,21 @@
/**
* @name Find-references links
* @description Generates use-definition pairs that provide the data
* for find-references in the code viewer.
* @kind definitions
* @id ruby/ide-find-references
* @tags ide-contextual-queries/local-references
*/
import codeql.IDE
import codeql_ruby.AST
import codeql_ruby.ast.Variable
external string selectedSourceFile();
from AstNode e, Variable def, string kind
where
e = def.getAnAccess() and
kind = "local variable" and
def.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile())
select e, def, kind

View File

@@ -8,31 +8,13 @@
*/
import codeql_ruby.printAst
import codeql.files.FileSystem
import codeql.IDE
/**
* The source file to generate an AST from.
*/
external string selectedSourceFile();
/**
* Returns an appropriately encoded version of a filename `name`
* passed by the VS Code extension in order to coincide with the
* output of `.getFile()` on locatable entities.
*/
cached
File getFileBySourceArchiveName(string name) {
// The name provided for a file in the source archive by the VS Code extension
// has some differences from the absolute path in the database:
// 1. colons are replaced by underscores
// 2. there's a leading slash, even for Windows paths: "C:/foo/bar" ->
// "/C_/foo/bar"
// 3. double slashes in UNC prefixes are replaced with a single slash
// We can handle 2 and 3 together by unconditionally adding a leading slash
// before replacing double slashes.
name = ("/" + result.getAbsolutePath().replaceAll(":", "_")).replaceAll("//", "/")
}
/**
* Overrides the configuration to print only nodes in the selected source file.
*/