Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cached
newtype TControlFlowElement =
TAstElement(AstNode n) or
TFuncDeclElement(AbstractFunctionDecl func) { func.hasBody() } or
TClosureElement(ClosureExpr clos) or
TPropertyGetterElement(Decl accessor, Expr ref) { isPropertyGetterElement(accessor, ref) } or
TPropertySetterElement(AccessorDecl accessor, AssignExpr assign) {
isPropertySetterElement(accessor, assign)
Expand Down Expand Up @@ -187,3 +188,15 @@ class KeyPathElement extends ControlFlowElement, TKeyPathElement {

override string toString() { result = expr.toString() }
}

class ClosureElement extends ControlFlowElement, TClosureElement {
ClosureExpr expr;

ClosureElement() { this = TClosureElement(expr) }

override Location getLocation() { result = expr.getLocation() }

ClosureExpr getAst() { result = expr }

override string toString() { result = expr.toString() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ module CfgScope {

final override predicate exit(ControlFlowElement last, Completion c) { last(tree, last, c) }
}

private class ClosureExprScope extends Range_ instanceof ClosureExpr {
Exprs::ClosureExprTree tree;

ClosureExprScope() { tree.getAst() = this }

final override predicate entry(ControlFlowElement first) { first(tree, first) }

final override predicate exit(ControlFlowElement last, Completion c) { last(tree, last, c) }
}
}

/** Holds if `first` is first executed when entering `scope`. */
Expand Down Expand Up @@ -1046,6 +1056,21 @@ module Exprs {
}
}

class ClosureExprTree extends StandardPreOrderTree, TClosureElement {
ClosureExpr expr;

ClosureExprTree() { this = TClosureElement(expr) }

ClosureExpr getAst() { result = expr }

final override ControlFlowElement getChildElement(int i) {
result.asAstNode() = expr.getParam(i)
or
result.asAstNode() = expr.getBody() and
i = expr.getNumberOfParams()
}
}

private class BindOptionalTree extends AstStandardPostOrderTree {
override BindOptionalExpr ast;

Expand Down
2 changes: 1 addition & 1 deletion swift/ql/lib/codeql/swift/controlflow/internal/Scope.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ private import swift
private import codeql.swift.generated.GetImmediateParent

module CallableBase {
class TypeRange = @abstract_function_decl or @key_path_expr;
class TypeRange = @abstract_function_decl or @key_path_expr or @closure_expr;

class Range extends Scope::Range, TypeRange { }
}
Expand Down
84 changes: 84 additions & 0 deletions swift/ql/test/library-tests/controlflow/graph/Cfg.expected
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,41 @@ cfg.swift:
# 46| return ...
#-----| return -> exit createClosure1 (normal)

# 46| enter { ... }
#-----| -> { ... }

# 46| exit { ... }

# 46| exit { ... } (normal)
#-----| -> exit { ... }

# 46| { ... }
#-----| -> return ...

# 46| { ... }
#-----| -> +

# 47| return ...
#-----| return -> exit { ... } (normal)

# 47| s
#-----| ->

# 47| ... call to + ...
#-----| -> return ...

# 47| +
#-----| -> String.Type

# 47| String.Type
#-----| -> call to +

# 47| call to +
#-----| -> s

# 47|
#-----| -> ... call to + ...

# 51| createClosure2
#-----| -> x

Expand Down Expand Up @@ -436,9 +468,44 @@ cfg.swift:
# 59| return ...
#-----| return -> exit createClosure3 (normal)

# 59| enter { ... }
#-----| -> { ... }

# 59| exit { ... }

# 59| exit { ... } (normal)
#-----| -> exit { ... }

# 59| { ... }
#-----| -> return ...

# 59| { ... }
#-----| -> y

# 60| y
#-----| -> +

# 60| x
#-----| -> y

# 60| ... call to + ...
#-----| -> return ...

# 60| return ...
#-----| return -> exit { ... } (normal)

# 60| +
#-----| -> Int.Type

# 60| Int.Type
#-----| -> call to +

# 60| call to +
#-----| -> x

# 60| y
#-----| -> ... call to + ...

# 64| callClosures
#-----| -> x1

Expand Down Expand Up @@ -4633,12 +4700,23 @@ cfg.swift:
# 368| return ...
#-----| return -> exit testCapture (normal)

# 368| enter { ... }
#-----| -> { ... }

# 368| exit { ... }

# 368| exit { ... } (normal)
#-----| -> exit { ... }

# 368| { ... }
#-----| -> return ...

# 368| { ... }
#-----| -> { ... }

# 368| { ... }
#-----| -> z

# 368| z
#-----| match -> +

Expand Down Expand Up @@ -4672,6 +4750,12 @@ cfg.swift:
# 368| literal
#-----| -> var ... = ...

# 369| return ...
#-----| return -> exit { ... } (normal)

# 369| z
#-----| -> return ...

# 373| enter testTupleElement
#-----| -> testTupleElement

Expand Down