Skip to content

Commit 53ad559

Browse files
committed
Swift: fix for-try-await CFG
1 parent 988a871 commit 53ad559

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,10 @@ module Stmts {
523523
// Flow from last element of iterator expression to first element of iterator call
524524
astLast(ast.getIteratorVar(), pred, c) and
525525
c instanceof NormalCompletion and
526-
astFirst(ast.getNextCall(), succ)
526+
astFirst(ast.getNextCall().getFullyConverted(), succ)
527527
or
528528
// Flow from iterator call to emptiness test
529-
astLast(ast.getNextCall(), pred, c) and
529+
astLast(ast.getNextCall().getFullyConverted(), pred, c) and
530530
c instanceof NormalCompletion and
531531
succ.asAstNode() = ast
532532
or
@@ -554,12 +554,12 @@ module Stmts {
554554
c instanceof TrueCompletion and
555555
astFirst(ast.getBody(), succ)
556556
or
557-
// or to the emptiness test if the condition is false.
557+
// or to the getNextCall if the condition is false.
558558
c instanceof FalseCompletion and
559-
succ.asAstNode() = ast
559+
astFirst(ast.getNextCall(), succ)
560560
)
561561
or
562-
// Flow from last element of loop body back to emptiness test.
562+
// Flow from last element of loop body back to getNextCall
563563
astLast(ast.getBody(), pred, c) and
564564
c.continuesLoop(ast) and
565565
astFirst(ast.getNextCall(), succ)

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

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,19 @@ cfg.swift:
14221422
#-----| -> x
14231423

14241424
# 137| x
1425-
#-----| -> .makeIterator()
1425+
#-----| -> $generator
1426+
1427+
# 138| $generator
1428+
#-----| -> &...
1429+
1430+
# 138| &...
1431+
#-----| -> call to next()
1432+
1433+
# 138| .next()
1434+
#-----| -> $generator
1435+
1436+
# 138| call to next()
1437+
#-----| -> for ... in ... { ... }
14261438

14271439
# 138| for ... in ... { ... }
14281440
#-----| non-empty -> _
@@ -1431,6 +1443,9 @@ cfg.swift:
14311443
# 138| _
14321444
#-----| match -> { ... }
14331445

1446+
# 138| $generator
1447+
#-----| match -> .makeIterator()
1448+
14341449
# 138| 0
14351450
#-----| -> 10
14361451

@@ -1441,7 +1456,6 @@ cfg.swift:
14411456
#-----| -> ....(_:_:)
14421457

14431458
# 138| call to makeIterator()
1444-
#-----| -> for ... in ... { ... }
14451459

14461460
# 138| ....(_:_:)
14471461
#-----| -> Int.Type
@@ -1453,7 +1467,7 @@ cfg.swift:
14531467
#-----| -> ... ....(_:_:) ...
14541468

14551469
# 138| { ... }
1456-
#-----| -> for ... in ... { ... }
1470+
#-----| -> .next()
14571471

14581472
# 140| switch x { ... }
14591473
#-----| -> x
@@ -6011,7 +6025,7 @@ cfg.swift:
60116025
#-----| -> stream
60126026

60136027
# 523| var ... = ...
6014-
#-----| -> .makeAsyncIterator()
6028+
#-----| -> $i$generator
60156029

60166030
# 523| stream
60176031
#-----| match -> AsyncStream<Element>.init(_:bufferingPolicy:_:)
@@ -6084,18 +6098,33 @@ cfg.swift:
60846098
#-----| -> exit { ... }
60856099

60866100
# 525| { ... }
6087-
#-----| -> .makeIterator()
6101+
#-----| -> $i$generator
60886102

60896103
# 525| { ... }
60906104
#-----| -> call to detached(priority:operation:)
60916105

6106+
# 526| $i$generator
6107+
#-----| -> &...
6108+
6109+
# 526| &...
6110+
#-----| -> call to next()
6111+
6112+
# 526| .next()
6113+
#-----| -> $i$generator
6114+
6115+
# 526| call to next()
6116+
#-----| -> for ... in ... { ... }
6117+
60926118
# 526| for ... in ... { ... }
60936119
#-----| non-empty -> i
60946120
#-----| empty -> .finish()
60956121

60966122
# 526| i
60976123
#-----| match -> .yield(_:)
60986124

6125+
# 526| $i$generator
6126+
#-----| match -> .makeIterator()
6127+
60996128
# 526| 1
61006129
#-----| -> 100
61016130

@@ -6106,7 +6135,6 @@ cfg.swift:
61066135
#-----| -> ....(_:_:)
61076136

61086137
# 526| call to makeIterator()
6109-
#-----| -> for ... in ... { ... }
61106138

61116139
# 526| ....(_:_:)
61126140
#-----| -> Int.Type
@@ -6124,7 +6152,7 @@ cfg.swift:
61246152
#-----| -> continuation
61256153

61266154
# 527| call to yield(_:)
6127-
#-----| -> for ... in ... { ... }
6155+
#-----| -> .next()
61286156

61296157
# 527| i
61306158
#-----| -> call to yield(_:)
@@ -6138,21 +6166,34 @@ cfg.swift:
61386166
# 529| call to finish()
61396167
#-----| -> exit { ... } (normal)
61406168

6169+
# 533| $i$generator
6170+
#-----| -> &...
6171+
6172+
# 533| &...
6173+
#-----| -> call to next()
6174+
6175+
# 533| .next()
6176+
#-----| -> $i$generator
6177+
6178+
# 533| call to next()
6179+
61416180
# 533| for ... in ... { ... }
61426181
#-----| empty -> exit testAsyncFor() (normal)
61436182
#-----| non-empty -> i
61446183

61456184
# 533| i
61466185
#-----| match -> print(_:separator:terminator:)
61476186

6187+
# 533| $i$generator
6188+
#-----| match -> .makeAsyncIterator()
6189+
61486190
# 533| (AsyncStream<Int>) ...
61496191
#-----| -> call to makeAsyncIterator()
61506192

61516193
# 533| .makeAsyncIterator()
61526194
#-----| -> stream
61536195

61546196
# 533| call to makeAsyncIterator()
6155-
#-----| -> for ... in ... { ... }
61566197

61576198
# 533| stream
61586199
#-----| -> (AsyncStream<Int>) ...
@@ -6161,7 +6202,7 @@ cfg.swift:
61616202
#-----| -> i
61626203

61636204
# 534| call to print(_:separator:terminator:)
6164-
#-----| -> for ... in ... { ... }
6205+
#-----| -> .next()
61656206

61666207
# 534| default separator
61676208
#-----| -> default terminator

0 commit comments

Comments
 (0)