Skip to content

Commit c0a59ad

Browse files
Copilotowen-mc
andauthored
Add File as a CFG root to support file-level declarations
Agent-Logs-Url: https://github.com/github/codeql/sessions/2867f173-c3f1-4564-9885-3ff4517bbf30 Co-authored-by: owen-mc <62447351+owen-mc@users.noreply.github.com>
1 parent 1db6bac commit c0a59ad

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@ private import ControlFlowGraphShared
99

1010
/** Provides helper predicates for mapping between CFG nodes and the AST. */
1111
module ControlFlow {
12-
/** A function with which a CFG is associated. */
12+
/** A file or function with which a CFG is associated. */
1313
class Root extends AstNode {
14-
Root() { exists(this.(FuncDef).getBody()) }
14+
Root() {
15+
exists(this.(FuncDef).getBody())
16+
or
17+
exists(this.(File).getADecl())
18+
}
1519

16-
/** Holds if `nd` belongs to this function. */
17-
predicate isRootOf(AstNode nd) { this = nd.getEnclosingFunction() }
20+
/** Holds if `nd` belongs to this file or function. */
21+
predicate isRootOf(AstNode nd) {
22+
this = nd.getEnclosingFunction()
23+
or
24+
not exists(nd.getEnclosingFunction()) and
25+
this = nd.getFile()
26+
}
1827

19-
/** Gets the synthetic entry node of the CFG for this function. */
28+
/** Gets the synthetic entry node of the CFG for this file or function. */
2029
EntryNode getEntryNode() { result = ControlFlow::entryNode(this) }
2130

22-
/** Gets the synthetic exit node of the CFG for this function. */
31+
/** Gets the synthetic exit node of the CFG for this file or function. */
2332
ExitNode getExitNode() { result = ControlFlow::exitNode(this) }
2433
}
2534

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ module GoCfg {
6363
}
6464

6565
class Callable extends AstNode {
66-
Callable() { exists(this.(Go::FuncDef).getBody()) }
66+
Callable() {
67+
exists(this.(Go::FuncDef).getBody())
68+
or
69+
exists(this.(Go::File).getADecl())
70+
}
6771
}
6872

6973
AstNode callableGetBody(Callable c) { result = c }
@@ -72,6 +76,11 @@ module GoCfg {
7276
result = node and node instanceof Callable
7377
or
7478
not node instanceof Callable and result = node.getEnclosingFunction()
79+
or
80+
not node instanceof Callable and
81+
not exists(node.getEnclosingFunction()) and
82+
result = node.getFile() and
83+
result instanceof Callable
7584
}
7685

7786
class Stmt = Go::Stmt;

0 commit comments

Comments
 (0)