Skip to content

Commit 3767ce5

Browse files
authored
Merge pull request github#13763 from aibaars/swift-print-cfg
Swift: improve print-cfg query
2 parents 27c12de + 99d8ae7 commit 3767ce5

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

swift/ql/lib/codeql/swift/controlflow/internal/PrintCFG.ql

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,52 @@
77
* @tags ide-contextual-queries/print-cfg
88
*/
99

10+
private import codeql.IDEContextual
1011
private import codeql.swift.controlflow.ControlFlowGraph
1112
private import codeql.swift.controlflow.internal.ControlFlowGraphImpl::TestOutput
1213

14+
/**
15+
* Gets the source file to generate a CFG from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
/**
20+
* Gets the source line to generate a CFG from.
21+
*/
22+
external string selectedSourceLine();
23+
24+
/**
25+
* Gets the source column to generate a CFG from.
26+
*/
27+
external string selectedSourceColumn();
28+
29+
bindingset[file, line, column]
30+
private CfgScope smallestEnclosingScope(File file, int line, int column) {
31+
result =
32+
min(Location loc, CfgScope scope |
33+
loc = scope.getLocation() and
34+
(
35+
loc.getStartLine() < line
36+
or
37+
loc.getStartLine() = line and loc.getStartColumn() <= column
38+
) and
39+
(
40+
loc.getEndLine() > line
41+
or
42+
loc.getEndLine() = line and loc.getEndColumn() >= column
43+
) and
44+
loc.getFile() = file
45+
|
46+
scope
47+
order by
48+
loc.getStartLine() desc, loc.getStartColumn() desc, loc.getEndLine(), loc.getEndColumn()
49+
)
50+
}
51+
1352
class MyRelevantNode extends RelevantNode {
14-
MyRelevantNode() { any() }
53+
MyRelevantNode() {
54+
this.getScope() =
55+
smallestEnclosingScope(getFileBySourceArchiveName(selectedSourceFile()),
56+
selectedSourceLine().toInt(), selectedSourceColumn().toInt())
57+
}
1558
}

swift/ql/src/queries/ide-contextual-queries/printAst.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import swift
1111
import codeql.swift.printast.PrintAst
12-
import IDEContextual
12+
import codeql.IDEContextual
1313
import codeql.swift.generated.ParentChild
1414

1515
/**

0 commit comments

Comments
 (0)