Rust: make printAst work

This commit is contained in:
Arthur Baars
2024-09-05 14:28:18 +02:00
parent d8eb5d463d
commit d93330bc4c
3 changed files with 20 additions and 23 deletions

View File

@@ -0,0 +1,16 @@
/**
* Provides shared predicates related to contextual queries in the code viewer.
*/
private import codeql.rust.elements.File
private import codeql.util.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) {
result = IdeContextual<File>::getFileBySourceArchiveName(name)
}

View File

@@ -1,5 +1,5 @@
/**
* Provides queries to pretty-print a Swift AST as a graph.
* Provides queries to pretty-print a Rust AST as a graph.
*/
import PrintAstNode

View File

@@ -1,5 +1,5 @@
/**
* Provides classes used to pretty-print a Swift AST as a graph.
* Provides classes used to pretty-print a Rust AST as a graph.
* This is factored out of `PrintAst.qll` for testing purposes.
*/
@@ -20,7 +20,7 @@ class PrintAstConfiguration extends TPrintAstConfiguration {
/**
* Holds if the AST for `e` should be printed. By default, holds for all.
*/
predicate shouldPrint(Locatable e) { not e instanceof Diagnostics and not e instanceof MacroRole }
predicate shouldPrint(Locatable e) { any() }
}
private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).shouldPrint(e) }
@@ -88,21 +88,7 @@ class PrintLocatable extends PrintAstNode, TPrintLocatable {
final override predicate shouldBePrinted() { shouldPrint(ast) }
override predicate hasChild(PrintAstNode child, int index, string label) {
exists(Locatable c, int i, string accessor |
c = getChildAndAccessor(ast, i, accessor) and
(
// use even indexes for normal children, leaving odd slots for conversions if any
child = TPrintLocatable(c) and index = 2 * i and label = accessor
or
child = TPrintLocatable(c.getFullyUnresolved().(Unresolved)) and
index = 2 * i + 1 and
(
if c instanceof Expr
then label = accessor + ".getFullyConverted()"
else label = accessor + ".getFullyUnresolved()"
)
)
)
child = TPrintLocatable(any(Locatable c | c = getChildAndAccessor(ast, index, label)))
}
final override Locatable getAstNode() { result = ast }
@@ -122,8 +108,3 @@ class PrintUnresolved extends PrintLocatable {
child = TPrintLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved))
}
}
private predicate hasPropertyWrapperElement(VarDecl d, Locatable a) {
a = [d.getPropertyWrapperBackingVar(), d.getPropertyWrapperProjectionVar()] or
a = [d.getPropertyWrapperBackingVarBinding(), d.getPropertyWrapperProjectionVarBinding()]
}