Skip to content

Commit 0e4a06b

Browse files
committed
Unified: Support swift GuardIfStmt in CFG.
1 parent b781737 commit 0e4a06b

4 files changed

Lines changed: 21 additions & 14 deletions

File tree

unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,12 @@ private module Ast implements AstSig<Location> {
7575
Expr getExpr() { none() }
7676
}
7777

78-
class IfStmt extends Stmt {
79-
IfStmt() { none() }
80-
81-
Expr getCondition() { none() }
78+
class IfStmt extends Stmt instanceof U::GuardIfStmt {
79+
Expr getCondition() { result = super.getCondition() }
8280

8381
Stmt getThen() { none() }
8482

85-
Stmt getElse() { none() }
83+
Stmt getElse() { result = super.getElse() }
8684
}
8785

8886
abstract class LoopStmt extends Stmt {

unified/ql/test/library-tests/controlflow/basicblock-slices.expected

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
| 16 | cfg.swift:16:10:16:10 | x | 'x -> 0 -^ BinaryExpr -^ ReturnExpr' |
1515
| 19 | cfg.swift:19:1:26:1 | FunctionDeclaration | 'FunctionDeclaration' |
1616
| 19 | cfg.swift:19:17:19:17 | x | 'x -^ Block' |
17-
| 20 | cfg.swift:20:3:22:3 | GuardIfStmt | 'GuardIfStmt -V x -> 0 -^ BinaryExpr -> Block' |
17+
| 20 | cfg.swift:20:3:22:3 | GuardIfStmt | 'GuardIfStmt -V x -> 0 -^ BinaryExpr' |
18+
| 20 | cfg.swift:20:21:22:3 | Block | 'Block' |
1819
| 21 | cfg.swift:21:11:21:17 | MyError | 'MyError -^ MemberAccessExpr -^ ThrowExpr' |
20+
| 23 | cfg.swift:23:3:25:3 | GuardIfStmt | 'GuardIfStmt -V x -> 0 -^ BinaryExpr' |
21+
| 23 | cfg.swift:23:21:25:3 | Block | 'Block' |
22+
| 24 | cfg.swift:24:11:24:17 | MyError | 'MyError -^ MemberAccessExpr -> Argument -V x -> 1 -^ BinaryExpr -^ CallExpr -^ ThrowExpr' |
1923
| 28 | cfg.swift:28:1:45:1 | FunctionDeclaration | 'FunctionDeclaration' |
2024
| 28 | cfg.swift:28:15:28:15 | x | 'x -^ Block' |
2125
| 29 | cfg.swift:29:3:43:3 | TryExpr | 'TryExpr -V Block' |
@@ -393,7 +397,8 @@
393397
| 508 | cfg.swift:508:3:510:3 | IfExpr | 'IfExpr -V ' |
394398
| 508 | cfg.swift:508:49:510:3 | Block | 'Block' |
395399
| 509 | cfg.swift:509:5:509:5 | x | 'x -> 1 -^ BinaryExpr' |
396-
| 512 | cfg.swift:512:3:514:3 | GuardIfStmt | 'GuardIfStmt -V -> Block' |
400+
| 512 | cfg.swift:512:3:514:3 | GuardIfStmt | 'GuardIfStmt -V ' |
401+
| 512 | cfg.swift:512:38:514:3 | Block | 'Block' |
397402
| 513 | cfg.swift:513:5:513:5 | x | 'x -> 1 -^ BinaryExpr' |
398403
| 516 | cfg.swift:516:3:519:3 | IfExpr | 'IfExpr -V BinaryExpr -V ' |
399404
| 517 | cfg.swift:517:7:517:27 | | '' |

unified/ql/test/library-tests/controlflow/cfg.expected

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ bbContinues
44
| cfg.swift:525:78:525:78 | Block | 'Block goto Task(+2)' |
55
| cfg.swift:526:9:526:20 | continuation | 'continuation goto Block(-1)' |
66
bbStep
7+
| cfg.swift:20:9:20:14 | BinaryExpr | 'BinaryExpr : false -> Block(+0)' |
8+
| cfg.swift:20:9:20:14 | BinaryExpr | 'BinaryExpr : true -> GuardIfStmt(+3)' |
9+
| cfg.swift:23:9:23:14 | BinaryExpr | 'BinaryExpr : false -> Block(+0)' |
710
| cfg.swift:30:9:30:24 | CallExpr | 'CallExpr : exception -> CatchClause(+5)' |
811
| cfg.swift:30:9:30:24 | CallExpr | 'CallExpr : successor -> UnaryExpr(+0)' |
912
| cfg.swift:33:5:33:33 | CallExpr | 'CallExpr : successor -> 0(+11)' |
@@ -101,6 +104,9 @@ bbStep
101104
| cfg.swift:508:6:508:47 | | ' : false -> GuardIfStmt(+4)' |
102105
| cfg.swift:508:6:508:47 | | ' : true -> Block(+0)' |
103106
| cfg.swift:509:5:509:10 | BinaryExpr | 'BinaryExpr : successor -> GuardIfStmt(+3)' |
107+
| cfg.swift:512:9:512:31 | | ' : false -> Block(+0)' |
108+
| cfg.swift:512:9:512:31 | | ' : true -> IfExpr(+4)' |
109+
| cfg.swift:513:5:513:10 | BinaryExpr | 'BinaryExpr : successor -> IfExpr(+3)' |
104110
| cfg.swift:516:6:516:28 | | ' : false,false -> x(+5)' |
105111
| cfg.swift:516:6:516:28 | | ' : true -> (+1)' |
106112
| cfg.swift:517:7:517:27 | | ' : false,false -> x(+4)' |
@@ -126,8 +132,6 @@ bbStep
126132
| cfg.swift:591:13:591:18 | BinaryExpr | 'BinaryExpr : false -> Block(+1)' |
127133
| cfg.swift:591:13:591:18 | BinaryExpr | 'BinaryExpr : true -> Block(+0)' |
128134
noCfg
129-
| cfg.swift:23:9:23:9 | x |
130-
| cfg.swift:24:5:24:42 | ThrowExpr |
131135
| cfg.swift:47:42:47:47 | String |
132136
| cfg.swift:53:34:53:34 | _ |
133137
| cfg.swift:60:34:60:34 | _ |

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ func isZero(x : Int) -> Bool {
1717
}
1818

1919
func mightThrow(x : Int) throws -> Void {
20-
guard x >= 0 else {
20+
guard x >= 0 else { // $ bbStep='BinaryExpr : false -> Block(+0)' bbStep='BinaryExpr : true -> GuardIfStmt(+3)'
2121
throw MyError.error1
2222
}
23-
guard x <= 0 else { // $ noCfg
24-
throw MyError.error3(withParam: x + 1) // $ noCfg
23+
guard x <= 0 else { // $ bbStep='BinaryExpr : false -> Block(+0)'
24+
throw MyError.error3(withParam: x + 1)
2525
}
2626
}
2727

@@ -509,8 +509,8 @@ func testAvailable() -> Int { // $ noCfg
509509
x += 1 // $ bbStep='BinaryExpr : successor -> GuardIfStmt(+3)'
510510
}
511511

512-
guard #available(macOS 12, *) else {
513-
x += 1
512+
guard #available(macOS 12, *) else { // $ bbStep=' : false -> Block(+0)' bbStep=' : true -> IfExpr(+4)'
513+
x += 1 // $ bbStep='BinaryExpr : successor -> IfExpr(+3)'
514514
}
515515

516516
if #available(macOS 12, *), // $ bbStep=' : true -> (+1)' bbStep=' : false,false -> x(+5)'

0 commit comments

Comments
 (0)