Skip to content

Commit 25c8b81

Browse files
authored
Swift: add params to CFG
1 parent aa77ea6 commit 25c8b81

File tree

3 files changed

+402
-51
lines changed

3 files changed

+402
-51
lines changed

swift/ql/lib/codeql/swift/controlflow/internal/ControlFlowElements.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ private import swift
33
cached
44
newtype TControlFlowElement =
55
TAstElement(AstNode n) or
6+
TFuncDeclElement(AbstractFunctionDecl func) { func.hasBody() } or
67
TPropertyGetterElement(Decl accessor, Expr ref) { isPropertyGetterElement(accessor, ref) } or
78
TPropertySetterElement(AccessorDecl accessor, AssignExpr assign) {
89
isPropertySetterElement(accessor, assign)
@@ -161,3 +162,13 @@ class PropertyObserverElement extends ControlFlowElement, TPropertyObserverEleme
161162

162163
AssignExpr getAssignExpr() { result = assign }
163164
}
165+
166+
167+
class FuncDeclElement extends ControlFlowElement, TFuncDeclElement {
168+
AbstractFunctionDecl func;
169+
FuncDeclElement() { this = TFuncDeclElement(func) }
170+
171+
override string toString() { result = func.toString() }
172+
173+
override Location getLocation() { result = func.getLocation() }
174+
}

swift/ql/lib/codeql/swift/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ module CfgScope {
4848

4949
private class BodyStmtCallableScope extends Range_ instanceof AbstractFunctionDecl {
5050
final override predicate entry(ControlFlowElement first) {
51-
exists(Stmts::BraceStmtTree tree |
52-
tree.getAst() = super.getBody() and
53-
tree.firstInner(first)
51+
exists(Decls::FuncDeclTree tree |
52+
tree.getAst() = this and
53+
first = tree
5454
)
5555
}
5656

5757
final override predicate exit(ControlFlowElement last, Completion c) {
58-
exists(Stmts::BraceStmtTree tree |
59-
tree.getAst() = super.getBody() and
58+
exists(Decls::FuncDeclTree tree |
59+
tree.getAst() = this and
6060
tree.last(last, c)
6161
)
6262
}
@@ -884,6 +884,21 @@ module Decls {
884884
)
885885
}
886886
}
887+
888+
class FuncDeclTree extends StandardPreOrderTree, TFuncDeclElement {
889+
AbstractFunctionDecl ast;
890+
891+
FuncDeclTree() { this = TFuncDeclElement(ast) }
892+
893+
AbstractFunctionDecl getAst() { result = ast }
894+
895+
final override ControlFlowElement getChildElement(int i) {
896+
result.asAstNode() = ast.getParam(i)
897+
or
898+
result.asAstNode() = ast.getBody() and
899+
i = ast.getNumberOfParams()
900+
}
901+
}
887902
}
888903

889904
module Exprs {

0 commit comments

Comments
 (0)