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

Commit f35343e

Browse files
authored
Merge pull request #262 from aeisenberg/aeisenberg/print-ast
Add the printAst contextual query
2 parents 830f83f + 0ae1330 commit f35343e

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)