Skip to content

Commit 2d46e22

Browse files
committed
Add go/print-cfg
1 parent 0b769ab commit 2d46e22

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

go/ql/lib/printCfg.ql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @name Print CFG
3+
* @description Produces a representation of a file's Control Flow Graph.
4+
* This query is used by the VS Code extension.
5+
* @id go/print-cfg
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-cfg
8+
*/
9+
10+
import go
11+
import semmle.go.controlflow.ControlFlowGraph
12+
private import semmle.go.controlflow.ControlFlowGraphShared
13+
14+
external string selectedSourceFile();
15+
16+
private predicate selectedSourceFileAlias = selectedSourceFile/0;
17+
18+
external int selectedSourceLine();
19+
20+
private predicate selectedSourceLineAlias = selectedSourceLine/0;
21+
22+
external int selectedSourceColumn();
23+
24+
private predicate selectedSourceColumnAlias = selectedSourceColumn/0;
25+
26+
module ViewCfgQueryInput implements GoCfg::ControlFlow::ViewCfgQueryInputSig<File> {
27+
predicate selectedSourceFile = selectedSourceFileAlias/0;
28+
29+
predicate selectedSourceLine = selectedSourceLineAlias/0;
30+
31+
predicate selectedSourceColumn = selectedSourceColumnAlias/0;
32+
33+
predicate cfgScopeSpan(
34+
CfgScope scope, File file, int startLine, int startColumn, int endLine, int endColumn
35+
) {
36+
file = scope.getFile() and
37+
scope.getLocation().getStartLine() = startLine and
38+
scope.getLocation().getStartColumn() = startColumn and
39+
exists(Location loc |
40+
loc.getEndLine() = endLine and
41+
loc.getEndColumn() = endColumn and
42+
loc = scope.(FuncDef).getBody().getLocation()
43+
)
44+
or
45+
file = scope.(File) and
46+
startLine = 1 and
47+
startColumn = 1 and
48+
endLine = file.getNumberOfLines() and
49+
endColumn = 999999
50+
}
51+
}
52+
53+
import GoCfg::ControlFlow::ViewCfgQuery<File, ViewCfgQueryInput>

go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,6 @@ module ControlFlow {
366366

367367
class ControlFlowNode = ControlFlow::Node;
368368

369+
class CfgScope = GoCfg::CfgScope;
370+
369371
class Write = ControlFlow::WriteNode;

go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module GoCfg {
2626
private import Cfg2
2727
import Public
2828

29+
class CfgScope = Ast::Callable;
30+
2931
/** Holds if `e` has an implicit field selection at `index` for `implicitField`. */
3032
predicate implicitFieldSelection(Go::AstNode e, int index, Go::Field implicitField) {
3133
Input::implicitFieldSelection(e, index, implicitField)
@@ -206,7 +208,7 @@ module GoCfg {
206208
class Case extends AstNode {
207209
Case() { none() }
208210

209-
AstNode getAPattern() { none() }
211+
AstNode getPattern(int index) { none() }
210212

211213
Expr getGuard() { none() }
212214

0 commit comments

Comments
 (0)