unified: Add debug query for local scoping graph

This commit is contained in:
Asger F
2026-07-01 21:51:41 +02:00
parent bced7bb6d2
commit 36b31a855c
2 changed files with 44 additions and 0 deletions

View File

@@ -296,6 +296,31 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
)
}
/** Holds if `node` should be included in the debug tree. */
private signature predicate relevantNodeSig(AstNode node);
module DebugScopeGraph<relevantNodeSig/1 relevantNode> {
private string getANodeAnnotation(AstNode node) {
result =
"[scope=" +
strictconcat(string name |
declInScope(_, name, node) or implicitDeclInScope(name, node)
|
name, ","
) + "]"
}
query predicate nodes(AstNode node, string key, string value) {
relevantNode(node) and
key = "semmle.label" and
value = node.toString() + concat(getANodeAnnotation(node))
}
query predicate edges(AstNode node1, AstNode node2) {
relevantNode(node2) and node2 = getParentForScoping(node1)
}
}
/** Gets the immediately enclosing variable scope of `n`. */
private Scope getEnclosingScope(AstNode n) {
result = getParentForScoping(n)

View File

@@ -0,0 +1,19 @@
/**
* @name Debug scope graph
* @description Renders the graph used to perform local variable lookups
* @kind graph
* @id unified/debug-scope-graph
*/
private import unified
private import codeql.unified.internal.Variables
/**
* Holds if `node` should be shown in the graph.
*/
predicate relevantNode(AstNode node) {
// Match an ancestor node by location so its whole subtree is shown.
node.getParent*().getLocation().toString().matches("%test.swift@227:%")
}
import LocalNameBindingOutput::DebugScopeGraph<relevantNode/1>