From d92d63510321ea92cb56a37e736ca7a9ce9a3722 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 3 Dec 2020 17:11:22 +0100 Subject: [PATCH 1/2] Add basic code navigation queries --- ql/src/codeql/IDE.qll | 19 +++++++++++++++++++ ql/src/localDefinitions.ql | 20 ++++++++++++++++++++ ql/src/localReferences.ql | 21 +++++++++++++++++++++ ql/src/printAst.ql | 20 +------------------- 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 ql/src/codeql/IDE.qll create mode 100644 ql/src/localDefinitions.ql create mode 100644 ql/src/localReferences.ql diff --git a/ql/src/codeql/IDE.qll b/ql/src/codeql/IDE.qll new file mode 100644 index 00000000000..0e58b1d878b --- /dev/null +++ b/ql/src/codeql/IDE.qll @@ -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("//", "/") +} diff --git a/ql/src/localDefinitions.ql b/ql/src/localDefinitions.ql new file mode 100644 index 00000000000..3054a6e4ca0 --- /dev/null +++ b/ql/src/localDefinitions.ql @@ -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 diff --git a/ql/src/localReferences.ql b/ql/src/localReferences.ql new file mode 100644 index 00000000000..42a7168ace6 --- /dev/null +++ b/ql/src/localReferences.ql @@ -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 diff --git a/ql/src/printAst.ql b/ql/src/printAst.ql index 92898a25228..c69ca543f6b 100644 --- a/ql/src/printAst.ql +++ b/ql/src/printAst.ql @@ -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. */ From 0a38d6801c701991408d8548a13a110bb1fa6970 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Mon, 7 Dec 2020 12:53:45 +0100 Subject: [PATCH 2/2] Address review comments --- ql/src/codeql/{IDE.qll => IDEContextual.qll} | 0 ql/src/{ => ide-contextual-queries}/localDefinitions.ql | 2 +- ql/src/{ => ide-contextual-queries}/localReferences.ql | 2 +- ql/src/{ => ide-contextual-queries}/printAst.ql | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename ql/src/codeql/{IDE.qll => IDEContextual.qll} (100%) rename ql/src/{ => ide-contextual-queries}/localDefinitions.ql (95%) rename ql/src/{ => ide-contextual-queries}/localReferences.ql (95%) rename ql/src/{ => ide-contextual-queries}/printAst.ql (95%) diff --git a/ql/src/codeql/IDE.qll b/ql/src/codeql/IDEContextual.qll similarity index 100% rename from ql/src/codeql/IDE.qll rename to ql/src/codeql/IDEContextual.qll diff --git a/ql/src/localDefinitions.ql b/ql/src/ide-contextual-queries/localDefinitions.ql similarity index 95% rename from ql/src/localDefinitions.ql rename to ql/src/ide-contextual-queries/localDefinitions.ql index 3054a6e4ca0..de4ec0ec29b 100644 --- a/ql/src/localDefinitions.ql +++ b/ql/src/ide-contextual-queries/localDefinitions.ql @@ -7,7 +7,7 @@ * @tags ide-contextual-queries/local-definitions */ -import codeql.IDE +import codeql.IDEContextual import codeql_ruby.AST external string selectedSourceFile(); diff --git a/ql/src/localReferences.ql b/ql/src/ide-contextual-queries/localReferences.ql similarity index 95% rename from ql/src/localReferences.ql rename to ql/src/ide-contextual-queries/localReferences.ql index 42a7168ace6..a5820983e26 100644 --- a/ql/src/localReferences.ql +++ b/ql/src/ide-contextual-queries/localReferences.ql @@ -7,7 +7,7 @@ * @tags ide-contextual-queries/local-references */ -import codeql.IDE +import codeql.IDEContextual import codeql_ruby.AST import codeql_ruby.ast.Variable diff --git a/ql/src/printAst.ql b/ql/src/ide-contextual-queries/printAst.ql similarity index 95% rename from ql/src/printAst.ql rename to ql/src/ide-contextual-queries/printAst.ql index c69ca543f6b..2e9acbc43d4 100644 --- a/ql/src/printAst.ql +++ b/ql/src/ide-contextual-queries/printAst.ql @@ -8,7 +8,7 @@ */ import codeql_ruby.printAst -import codeql.IDE +import codeql.IDEContextual /** * The source file to generate an AST from.