Skip to content

Commit d9c4048

Browse files
committed
Swift: add ThenStmt to control flow
1 parent b8e3828 commit d9c4048

File tree

3 files changed

+158
-6
lines changed

3 files changed

+158
-6
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ module Stmts {
255255
}
256256
}
257257

258+
private class ThenStmtTree extends AstStandardPostOrderTree {
259+
override ThenStmt ast;
260+
261+
final override ControlFlowElement getChildNode(int i) {
262+
i = 0 and
263+
result.asAstNode() = ast.getResult()
264+
}
265+
}
266+
258267
private class FailTree extends AstLeafTree {
259268
override FailStmt ast;
260269
}

swift/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 142 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ cfg.swift:
340340
#-----| exception -> exit mightThrow(x:) (abnormal)
341341

342342
# 19| MyError.Type
343-
#-----| -> (Error) ...
343+
#-----| -> (any Error) ...
344344

345-
# 19| (Error) ...
345+
# 19| (any Error) ...
346346
#-----| -> throw ...
347347

348348
# 19| .error1
@@ -379,11 +379,11 @@ cfg.swift:
379379
# 22| .error3
380380
#-----| -> MyError.Type
381381

382-
# 22| (Error) ...
382+
# 22| (any Error) ...
383383
#-----| -> throw ...
384384

385385
# 22| call to ...
386-
#-----| -> (Error) ...
386+
#-----| -> (any Error) ...
387387

388388
# 22| x
389389
#-----| -> 1
@@ -6728,11 +6728,11 @@ cfg.swift:
67286728
# 568| MyProcotolImpl.init()
67296729
#-----| -> MyProcotolImpl.Type
67306730

6731-
# 568| (MyProtocol) ...
6731+
# 568| (any MyProtocol) ...
67326732
#-----| -> return ...
67336733

67346734
# 568| call to MyProcotolImpl.init()
6735-
#-----| -> (MyProtocol) ...
6735+
#-----| -> (any MyProtocol) ...
67366736

67376737
# 569| enter getMyProtocolImpl()
67386738
#-----| -> getMyProtocolImpl()
@@ -6868,3 +6868,139 @@ cfg.swift:
68686868

68696869
# 577| call to source()
68706870
#-----| -> call to sink(arg:)
6871+
6872+
# 580| enter singleStmtExpr(_:)
6873+
#-----| -> singleStmtExpr(_:)
6874+
6875+
# 580| exit singleStmtExpr(_:)
6876+
6877+
# 580| exit singleStmtExpr(_:) (normal)
6878+
#-----| -> exit singleStmtExpr(_:)
6879+
6880+
# 580| singleStmtExpr(_:)
6881+
#-----| -> x
6882+
6883+
# 580| x
6884+
#-----| -> a
6885+
6886+
# 581| var ... = ...
6887+
#-----| -> b
6888+
6889+
# 581| a
6890+
#-----| match -> switch x { ... }
6891+
6892+
# 581| SingleValueStmtExpr
6893+
#-----| -> var ... = ...
6894+
6895+
# 581| switch x { ... }
6896+
#-----| -> x
6897+
6898+
# 581| x
6899+
#-----| -> case ...
6900+
6901+
# 582| case ...
6902+
#-----| -> =~ ...
6903+
6904+
# 582| .~=(_:_:)
6905+
#-----| -> Range<Int>.Type
6906+
6907+
# 582| 0
6908+
#-----| -> 5
6909+
6910+
# 582| Range<Int>.Type
6911+
#-----| -> ...<(_:_:)
6912+
6913+
# 582| ... ...<(_:_:) ...
6914+
#-----| -> $match
6915+
6916+
# 582| ... .~=(_:_:) ...
6917+
#-----| -> =~ ...
6918+
6919+
# 582| =~ ...
6920+
#-----| -> .~=(_:_:)
6921+
6922+
# 582| =~ ...
6923+
#-----| match -> 1
6924+
#-----| no-match -> case ...
6925+
6926+
# 582| ...<(_:_:)
6927+
#-----| -> Int.Type
6928+
6929+
# 582| Int.Type
6930+
#-----| -> 0
6931+
6932+
# 582| $match
6933+
#-----| -> ... .~=(_:_:) ...
6934+
6935+
# 582| 5
6936+
#-----| -> ... ...<(_:_:) ...
6937+
6938+
# 582| 1
6939+
#-----| -> ThenStmt
6940+
6941+
# 582| ThenStmt
6942+
#-----| -> SingleValueStmtExpr
6943+
6944+
# 583| _
6945+
#-----| match -> 2
6946+
6947+
# 583| _
6948+
#-----| -> _
6949+
6950+
# 583| case ...
6951+
#-----| -> _
6952+
6953+
# 583| 2
6954+
#-----| -> ThenStmt
6955+
6956+
# 583| ThenStmt
6957+
#-----| -> SingleValueStmtExpr
6958+
6959+
# 585| var ... = ...
6960+
#-----| -> exit singleStmtExpr(_:) (normal)
6961+
6962+
# 585| b
6963+
#-----| match -> if ... then { ... } else { ... }
6964+
6965+
# 585| SingleValueStmtExpr
6966+
#-----| -> var ... = ...
6967+
6968+
# 585| if ... then { ... } else { ... }
6969+
#-----| -> StmtCondition
6970+
6971+
# 585| StmtCondition
6972+
#-----| -> .<(_:_:)
6973+
6974+
# 585| [false] (...)
6975+
#-----| false -> 2
6976+
6977+
# 585| [true] (...)
6978+
#-----| true -> 1
6979+
6980+
# 585| x
6981+
#-----| -> 42
6982+
6983+
# 585| ... .<(_:_:) ...
6984+
#-----| false -> [false] (...)
6985+
#-----| true -> [true] (...)
6986+
6987+
# 585| .<(_:_:)
6988+
#-----| -> Int.Type
6989+
6990+
# 585| Int.Type
6991+
#-----| -> x
6992+
6993+
# 585| 42
6994+
#-----| -> ... .<(_:_:) ...
6995+
6996+
# 585| 1
6997+
#-----| -> ThenStmt
6998+
6999+
# 585| ThenStmt
7000+
#-----| -> SingleValueStmtExpr
7001+
7002+
# 585| 2
7003+
#-----| -> ThenStmt
7004+
7005+
# 585| ThenStmt
7006+
#-----| -> SingleValueStmtExpr

swift/ql/test/library-tests/controlflow/graph/cfg.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,11 @@ func testOpenExistentialExpr(x: MyProtocol, y: MyProcotolImpl) {
577577
sink(arg: getMyProtocolImpl().source())
578578
}
579579

580+
func singleStmtExpr(_ x: Int) {
581+
let a = switch x {
582+
case 0..<5: 1
583+
default: 2
584+
}
585+
let b = if (x < 42) { 1 } else { 2 }
586+
}
580587
// ---

0 commit comments

Comments
 (0)