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.
This commit is contained in:
Andrew Eisenberg
2020-07-17 10:12:48 -07:00
parent 830f83f21a
commit 0ae1330c02
4 changed files with 37 additions and 6 deletions

9
ql/src/ideContextual.qll Normal file
View File

@@ -0,0 +1,9 @@
/**
* Provides classes and predicates related to contextual queries
* in the code viewer.
*/
import go
cached
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }

View File

@@ -8,12 +8,10 @@
*/
import go
import ideContextual
external string selectedSourceFile();
cached
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
from Ident def, Ident use, Entity e
where use.uses(e) and def.declares(e) and use.getFile() = getEncodedFile(selectedSourceFile())
select use, def, "V"

View File

@@ -8,12 +8,10 @@
*/
import go
import ideContextual
external string selectedSourceFile();
cached
File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name }
from Ident def, Ident use, Entity e
where use.uses(e) and def.declares(e) and def.getFile() = getEncodedFile(selectedSourceFile())
select use, def, "V"

26
ql/src/printAst.ql Normal file
View File

@@ -0,0 +1,26 @@
/**
* @name Print AST
* @description Outputs a representation of a file's Abstract Syntax Tree. This
* query is used by the VS Code extension.
* @id go/print-ast
* @kind graph
* @tags ide-contextual-queries/print-ast
*/
import go
import semmle.go.PrintAst
import ideContextual
/**
* The source file to generate an AST from.
*/
external string selectedSourceFile();
/**
* Hook to customize the functions printed by this query.
*/
class Cfg extends PrintAstConfiguration {
override predicate shouldPrintFunction(FuncDef func) {
func.getFile() = getEncodedFile(selectedSourceFile())
}
}