Skip to content

Commit e88dde7

Browse files
committed
unified: Make debug graph subset more configurable
1 parent 3999894 commit e88dde7

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ class NameBindingNode extends TNameBindingNode {
4242
/** Holds if this represents the root namespace in which all named modules are members. */
4343
predicate isModuleRoot() { this = TModuleRoot() }
4444

45+
/**
46+
* Gets an AST node wrapped by this name-binding node, if such a node exists.
47+
*
48+
* Mainly for debugging purposes.
49+
*/
50+
AstNode getWrappedAstNode() {
51+
this.isIdentifier(result)
52+
or
53+
this.isBulkImport(result)
54+
or
55+
this.isExportedNamespace(result)
56+
or
57+
this.isLocalNamespace(result)
58+
or
59+
this.isModuleScopeNode(result)
60+
}
61+
4562
string toString() {
4663
exists(Identifier n | this.isIdentifier(n) and result = "Identifier(" + n + ")")
4764
or
@@ -351,21 +368,32 @@ NameBindingNode trackNameDeclaration(NameDeclaration decl) {
351368
}
352369

353370
/** Holds if `node` should be included in the debug view. */
354-
private signature predicate relevantFileSig(File node);
371+
private signature predicate relevantNodeSig(AstNode node);
355372

356-
module DebugGraph<relevantFileSig/1 relevantFile> {
357-
private predicate relevantNode(NameBindingNode node) {
358-
relevantFile(node.getLocation().getFile())
373+
module DebugGraph<relevantNodeSig/1 relevantNode> {
374+
private predicate relevantNameBindingNode(NameBindingNode node) {
375+
relevantNode(node.getWrappedAstNode())
376+
or
377+
// Also consider LocalName to be relevant if any of its accesses are relevant
378+
exists(LocalName name |
379+
node.isLocalName(name) and
380+
relevantNode(any(PotentialLocalNameAccess ac | ac.getLocalName() = name))
381+
)
382+
or
383+
// Always include module root
384+
node.isModuleRoot()
359385
}
360386

361387
query predicate nodes(NameBindingNode node, string key, string value) {
362-
relevantNode(node) and
388+
relevantNameBindingNode(node) and
363389
key = "semmle.label" and
364390
value = node.toString()
365391
}
366392

367393
query predicate edges(NameBindingNode node1, NameBindingNode node2, string key, string value) {
368394
key = "semmle.label" and
395+
relevantNameBindingNode(node1) and
396+
relevantNameBindingNode(node2) and
369397
(
370398
valueStep(node1, node2) and value = ""
371399
or

unified/ql/lib/codeql/unified/internal/dev/debugStaticNameBindingGraph.ql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ private import unified
99
private import codeql.unified.internal.StaticNameBinding
1010

1111
/**
12-
* Holds if graphs related to `file` should be shown in the graph.
12+
* Holds if `node` should be shown in the graph.
1313
*/
14-
predicate relevantFile(File file) { file.getBaseName() = "test.swift" }
14+
predicate relevantNode(AstNode node) {
15+
// Match an ancestor node by location so its whole subtree is shown.
16+
node.getParent*().getLocation().toString().matches("%test.swift@13:%")
17+
}
1518

16-
import DebugGraph<relevantFile/1>
19+
import DebugGraph<relevantNode/1>

0 commit comments

Comments
 (0)