Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 0ae1330

Browse files
committed
Add the printAst contextual query
This is similar to the cpp query for printing the AST in the context of VS Code. This PR also includes a small refactoring to extract the `getEncodedFile` predicate to a new `qll` file.
1 parent 830f83f commit 0ae1330

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

ql/src/ideContextual.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Provides classes and predicates related to contextual queries
3+
* in the code viewer.
4+
*/
5+
6+
import go
7+
8+
cached
9+
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

ql/src/localDefinitions.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
*/
99

1010
import go
11+
import ideContextual
1112

1213
external string selectedSourceFile();
1314

14-
cached
15-
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
16-
1715
from Ident def, Ident use, Entity e
1816
where use.uses(e) and def.declares(e) and use.getFile() = getEncodedFile(selectedSourceFile())
1917
select use, def, "V"

ql/src/localReferences.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
*/
99

1010
import go
11+
import ideContextual
1112

1213
external string selectedSourceFile();
1314

14-
cached
15-
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
16-
1715
from Ident def, Ident use, Entity e
1816
where use.uses(e) and def.declares(e) and def.getFile() = getEncodedFile(selectedSourceFile())
1917
select use, def, "V"

ql/src/printAst.ql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @name Print AST
3+
* @description Outputs a representation of a file's Abstract Syntax Tree. This
4+
* query is used by the VS Code extension.
5+
* @id go/print-ast
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-ast
8+
*/
9+
10+
import go
11+
import semmle.go.PrintAst
12+
import ideContextual
13+
14+
/**
15+
* The source file to generate an AST from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
/**
20+
* Hook to customize the functions printed by this query.
21+
*/
22+
class Cfg extends PrintAstConfiguration {
23+
override predicate shouldPrintFunction(FuncDef func) {
24+
func.getFile() = getEncodedFile(selectedSourceFile())
25+
}
26+
}

0 commit comments

Comments
 (0)