Skip to content

Commit 9a66ef1

Browse files
authored
Merge pull request #23157 from github/unified/swift-fallthroug
Shared, Unified: Add shared CFG `fallthrough` handling and support Swift's `fallthrough`
2 parents a47aa3f + f79c3fe commit 9a66ef1

20 files changed

Lines changed: 340 additions & 31 deletions

File tree

‎actions/ql/lib/codeql/actions/controlflow/internal/Cfg.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ module CfgImpl {
204204
ContinueStmt() { none() }
205205
}
206206

207+
class FallthroughStmt extends Stmt {
208+
FallthroughStmt() { none() }
209+
}
210+
207211
class GotoStmt extends Stmt {
208212
GotoStmt() { none() }
209213
}

‎csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ module Ast implements AstSig<Location> {
204204

205205
class ContinueStmt = CS::ContinueStmt;
206206

207+
class FallthroughStmt extends Stmt {
208+
FallthroughStmt() { none() }
209+
}
210+
207211
class GotoStmt = CS::GotoStmt;
208212

209213
class ReturnStmt = CS::ReturnStmt;

‎go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ module CfgImpl {
256256

257257
class ContinueStmt = Go::ContinueStmt;
258258

259+
class FallthroughStmt = Go::FallthroughStmt;
260+
259261
class GotoStmt = Go::GotoStmt;
260262

261263
class ReturnStmt = Go::ReturnStmt;
@@ -324,12 +326,7 @@ module CfgImpl {
324326

325327
AstNode getSwitchInit(Switch switch) { result = switch.(Go::SwitchStmt).getInit() }
326328

327-
predicate fallsThrough(Case c) {
328-
// Go has no implicit fall-through between case clauses; an explicit
329-
// `fallthrough` statement is required.
330-
c.(Go::CaseClause).getStmt(max(int i | exists(c.(Go::CaseClause).getStmt(i)))) instanceof
331-
Go::FallthroughStmt
332-
}
329+
predicate fallsThrough(Case c) { none() }
333330

334331
/**
335332
* Holds if `s` is the flattened body element at position (`caseIdx`,

‎go/ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/ControlFlowNode_getASuccessor.expected‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,83 @@
11941194
| exprs.go:94:15:94:15 | y | exprs.go:94:15:94:15 | After y [false] |
11951195
| exprs.go:94:15:94:15 | y | exprs.go:94:15:94:15 | After y [true] |
11961196
| exprs.go:94:21:94:21 | z | exprs.go:94:9:94:21 | After ...\|\|... |
1197+
| fallthrough.go:0:0:0:0 | After fallthrough.go | fallthrough.go:0:0:0:0 | Normal Exit |
1198+
| fallthrough.go:0:0:0:0 | Entry | fallthrough.go:0:0:0:0 | fallthrough.go |
1199+
| fallthrough.go:0:0:0:0 | Normal Exit | fallthrough.go:0:0:0:0 | Exit |
1200+
| fallthrough.go:0:0:0:0 | fallthrough.go | fallthrough.go:3:1:12:1 | function declaration |
1201+
| fallthrough.go:3:1:12:1 | Entry | fallthrough.go:3:29:3:29 | x |
1202+
| fallthrough.go:3:1:12:1 | Exceptional Exit | fallthrough.go:3:1:12:1 | Exit |
1203+
| fallthrough.go:3:1:12:1 | Normal Exit | fallthrough.go:3:1:12:1 | Exit |
1204+
| fallthrough.go:3:1:12:1 | function declaration | fallthrough.go:14:1:21:1 | function declaration |
1205+
| fallthrough.go:3:29:3:29 | x | fallthrough.go:3:36:12:1 | block statement |
1206+
| fallthrough.go:3:36:12:1 | After block statement | fallthrough.go:3:1:12:1 | Normal Exit |
1207+
| fallthrough.go:3:36:12:1 | block statement | fallthrough.go:4:2:10:2 | expression-switch statement |
1208+
| fallthrough.go:4:2:10:2 | After expression-switch statement | fallthrough.go:11:2:11:13 | expression statement |
1209+
| fallthrough.go:4:2:10:2 | expression-switch statement | fallthrough.go:4:9:4:9 | x |
1210+
| fallthrough.go:4:9:4:9 | x | fallthrough.go:5:2:6:13 | case clause |
1211+
| fallthrough.go:5:2:6:13 | After case clause [match] | fallthrough.go:6:3:6:13 | Before fallthrough statement |
1212+
| fallthrough.go:5:2:6:13 | After case clause [no-match] | fallthrough.go:7:2:7:8 | case clause |
1213+
| fallthrough.go:5:2:6:13 | case clause | fallthrough.go:5:7:5:7 | 0 |
1214+
| fallthrough.go:5:7:5:7 | 0 | fallthrough.go:5:7:5:7 | After 0 [match] |
1215+
| fallthrough.go:5:7:5:7 | 0 | fallthrough.go:5:7:5:7 | After 0 [no-match] |
1216+
| fallthrough.go:5:7:5:7 | After 0 [match] | fallthrough.go:5:2:6:13 | After case clause [match] |
1217+
| fallthrough.go:5:7:5:7 | After 0 [no-match] | fallthrough.go:5:2:6:13 | After case clause [no-match] |
1218+
| fallthrough.go:6:3:6:13 | Before fallthrough statement | fallthrough.go:6:3:6:13 | fallthrough statement |
1219+
| fallthrough.go:6:3:6:13 | fallthrough statement | fallthrough.go:4:2:10:2 | After expression-switch statement |
1220+
| fallthrough.go:7:2:7:8 | After case clause [match] | fallthrough.go:4:2:10:2 | After expression-switch statement |
1221+
| fallthrough.go:7:2:7:8 | After case clause [no-match] | fallthrough.go:8:2:9:13 | case clause |
1222+
| fallthrough.go:7:2:7:8 | case clause | fallthrough.go:7:7:7:7 | 1 |
1223+
| fallthrough.go:7:7:7:7 | 1 | fallthrough.go:7:7:7:7 | After 1 [match] |
1224+
| fallthrough.go:7:7:7:7 | 1 | fallthrough.go:7:7:7:7 | After 1 [no-match] |
1225+
| fallthrough.go:7:7:7:7 | After 1 [match] | fallthrough.go:7:2:7:8 | After case clause [match] |
1226+
| fallthrough.go:7:7:7:7 | After 1 [no-match] | fallthrough.go:7:2:7:8 | After case clause [no-match] |
1227+
| fallthrough.go:8:2:9:13 | After case clause [match] | fallthrough.go:9:3:9:13 | expression statement |
1228+
| fallthrough.go:8:2:9:13 | case clause | fallthrough.go:8:2:9:13 | After case clause [match] |
1229+
| fallthrough.go:9:3:9:7 | test5 | fallthrough.go:9:9:9:12 | true |
1230+
| fallthrough.go:9:3:9:13 | After call to test5 | fallthrough.go:9:3:9:13 | After expression statement |
1231+
| fallthrough.go:9:3:9:13 | After expression statement | fallthrough.go:4:2:10:2 | After expression-switch statement |
1232+
| fallthrough.go:9:3:9:13 | Before call to test5 | fallthrough.go:9:3:9:7 | test5 |
1233+
| fallthrough.go:9:3:9:13 | call to test5 | fallthrough.go:3:1:12:1 | Exceptional Exit |
1234+
| fallthrough.go:9:3:9:13 | call to test5 | fallthrough.go:9:3:9:13 | After call to test5 |
1235+
| fallthrough.go:9:3:9:13 | expression statement | fallthrough.go:9:3:9:13 | Before call to test5 |
1236+
| fallthrough.go:9:9:9:12 | true | fallthrough.go:9:3:9:13 | call to test5 |
1237+
| fallthrough.go:11:2:11:6 | test5 | fallthrough.go:11:8:11:12 | false |
1238+
| fallthrough.go:11:2:11:13 | After call to test5 | fallthrough.go:11:2:11:13 | After expression statement |
1239+
| fallthrough.go:11:2:11:13 | After expression statement | fallthrough.go:3:36:12:1 | After block statement |
1240+
| fallthrough.go:11:2:11:13 | Before call to test5 | fallthrough.go:11:2:11:6 | test5 |
1241+
| fallthrough.go:11:2:11:13 | call to test5 | fallthrough.go:3:1:12:1 | Exceptional Exit |
1242+
| fallthrough.go:11:2:11:13 | call to test5 | fallthrough.go:11:2:11:13 | After call to test5 |
1243+
| fallthrough.go:11:2:11:13 | expression statement | fallthrough.go:11:2:11:13 | Before call to test5 |
1244+
| fallthrough.go:11:8:11:12 | false | fallthrough.go:11:2:11:13 | call to test5 |
1245+
| fallthrough.go:14:1:21:1 | Entry | fallthrough.go:14:32:14:32 | x |
1246+
| fallthrough.go:14:1:21:1 | Exceptional Exit | fallthrough.go:14:1:21:1 | Exit |
1247+
| fallthrough.go:14:1:21:1 | Normal Exit | fallthrough.go:14:1:21:1 | Exit |
1248+
| fallthrough.go:14:1:21:1 | function declaration | fallthrough.go:0:0:0:0 | After fallthrough.go |
1249+
| fallthrough.go:14:32:14:32 | x | fallthrough.go:14:39:21:1 | block statement |
1250+
| fallthrough.go:14:39:21:1 | After block statement | fallthrough.go:14:1:21:1 | Normal Exit |
1251+
| fallthrough.go:14:39:21:1 | block statement | fallthrough.go:15:2:19:2 | expression-switch statement |
1252+
| fallthrough.go:15:2:19:2 | After expression-switch statement | fallthrough.go:20:2:20:13 | expression statement |
1253+
| fallthrough.go:15:2:19:2 | expression-switch statement | fallthrough.go:15:9:15:9 | x |
1254+
| fallthrough.go:15:9:15:9 | x | fallthrough.go:16:2:17:13 | case clause |
1255+
| fallthrough.go:16:2:17:13 | After case clause [match] | fallthrough.go:17:3:17:13 | Before fallthrough statement |
1256+
| fallthrough.go:16:2:17:13 | After case clause [no-match] | fallthrough.go:18:2:18:9 | case clause |
1257+
| fallthrough.go:16:2:17:13 | case clause | fallthrough.go:16:7:16:7 | 0 |
1258+
| fallthrough.go:16:7:16:7 | 0 | fallthrough.go:16:7:16:7 | After 0 [match] |
1259+
| fallthrough.go:16:7:16:7 | 0 | fallthrough.go:16:7:16:7 | After 0 [no-match] |
1260+
| fallthrough.go:16:7:16:7 | After 0 [match] | fallthrough.go:16:2:17:13 | After case clause [match] |
1261+
| fallthrough.go:16:7:16:7 | After 0 [no-match] | fallthrough.go:16:2:17:13 | After case clause [no-match] |
1262+
| fallthrough.go:17:3:17:13 | Before fallthrough statement | fallthrough.go:17:3:17:13 | fallthrough statement |
1263+
| fallthrough.go:17:3:17:13 | fallthrough statement | fallthrough.go:15:2:19:2 | After expression-switch statement |
1264+
| fallthrough.go:18:2:18:9 | After case clause [match] | fallthrough.go:15:2:19:2 | After expression-switch statement |
1265+
| fallthrough.go:18:2:18:9 | case clause | fallthrough.go:18:2:18:9 | After case clause [match] |
1266+
| fallthrough.go:20:2:20:6 | test5 | fallthrough.go:20:8:20:12 | false |
1267+
| fallthrough.go:20:2:20:13 | After call to test5 | fallthrough.go:20:2:20:13 | After expression statement |
1268+
| fallthrough.go:20:2:20:13 | After expression statement | fallthrough.go:14:39:21:1 | After block statement |
1269+
| fallthrough.go:20:2:20:13 | Before call to test5 | fallthrough.go:20:2:20:6 | test5 |
1270+
| fallthrough.go:20:2:20:13 | call to test5 | fallthrough.go:14:1:21:1 | Exceptional Exit |
1271+
| fallthrough.go:20:2:20:13 | call to test5 | fallthrough.go:20:2:20:13 | After call to test5 |
1272+
| fallthrough.go:20:2:20:13 | expression statement | fallthrough.go:20:2:20:13 | Before call to test5 |
1273+
| fallthrough.go:20:8:20:12 | false | fallthrough.go:20:2:20:13 | call to test5 |
11971274
| generic.go:0:0:0:0 | After generic.go | generic.go:0:0:0:0 | Normal Exit |
11981275
| generic.go:0:0:0:0 | Entry | generic.go:0:0:0:0 | generic.go |
11991276
| generic.go:0:0:0:0 | Normal Exit | generic.go:0:0:0:0 | Exit |
@@ -2929,12 +3006,13 @@
29293006
| stmts.go:91:7:91:7 | After 1 [no-match] | stmts.go:91:2:93:13 | After case clause [no-match] |
29303007
| stmts.go:92:3:92:7 | test5 | stmts.go:92:9:92:13 | false |
29313008
| stmts.go:92:3:92:14 | After call to test5 | stmts.go:92:3:92:14 | After expression statement |
2932-
| stmts.go:92:3:92:14 | After expression statement | stmts.go:93:3:93:13 | fallthrough statement |
3009+
| stmts.go:92:3:92:14 | After expression statement | stmts.go:93:3:93:13 | Before fallthrough statement |
29333010
| stmts.go:92:3:92:14 | Before call to test5 | stmts.go:92:3:92:7 | test5 |
29343011
| stmts.go:92:3:92:14 | call to test5 | stmts.go:75:1:109:1 | Exceptional Exit |
29353012
| stmts.go:92:3:92:14 | call to test5 | stmts.go:92:3:92:14 | After call to test5 |
29363013
| stmts.go:92:3:92:14 | expression statement | stmts.go:92:3:92:14 | Before call to test5 |
29373014
| stmts.go:92:9:92:13 | false | stmts.go:92:3:92:14 | call to test5 |
3015+
| stmts.go:93:3:93:13 | Before fallthrough statement | stmts.go:93:3:93:13 | fallthrough statement |
29383016
| stmts.go:93:3:93:13 | fallthrough statement | stmts.go:95:3:95:13 | expression statement |
29393017
| stmts.go:94:2:95:13 | After case clause [match] | stmts.go:95:3:95:13 | expression statement |
29403018
| stmts.go:94:2:95:13 | After case clause [no-match] | stmts.go:90:2:96:2 | After expression-switch statement |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func fallthroughToEmptyCase(x int) {
4+
switch x {
5+
case 0:
6+
fallthrough
7+
case 1:
8+
default:
9+
test5(true)
10+
}
11+
test5(false)
12+
}
13+
14+
func fallthroughToEmptyDefault(x int) {
15+
switch x {
16+
case 0:
17+
fallthrough
18+
default:
19+
}
20+
test5(false)
21+
}

‎java/ql/lib/semmle/code/java/ControlFlowGraph.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ private module Ast implements AstSig<Location> {
114114

115115
class ContinueStmt = J::ContinueStmt;
116116

117+
class FallthroughStmt extends Stmt {
118+
FallthroughStmt() { none() }
119+
}
120+
117121
class GotoStmt extends Stmt {
118122
GotoStmt() { none() }
119123
}

‎python/ql/lib/semmle/python/controlflow/internal/AstNodeImpl.qll‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,11 @@ module Ast implements AstSig<Py::Location> {
700700
ContinueStmt() { this = TPyStmt(any(Py::Continue c)) }
701701
}
702702

703+
/** A `fallthrough` statement. Python has no fallthrough. */
704+
class FallthroughStmt extends Stmt {
705+
FallthroughStmt() { none() }
706+
}
707+
703708
/** A `goto` statement. Python has no goto. */
704709
class GotoStmt extends Stmt {
705710
GotoStmt() { none() }

‎ruby/ql/lib/codeql/ruby/controlflow/ControlFlowGraph.qll‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ private module Ast implements AstSig<Location> {
308308

309309
class ContinueStmt extends Stmt instanceof R::Ast::NextStmt { }
310310

311+
class FallthroughStmt extends Stmt {
312+
FallthroughStmt() { none() }
313+
}
314+
311315
class LabeledStmt extends Stmt {
312316
LabeledStmt() { none() }
313317

‎shared/controlflow/codeql/controlflow/ControlFlowGraph.qll‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ signature module AstSig<LocationSig Location> {
179179
*/
180180
class ContinueStmt extends Stmt;
181181

182+
/**
183+
* A `fallthrough` statement.
184+
*
185+
* Fallthrough statements complete abruptly and continue at the next switch
186+
* case's body, without evaluating its pattern or guard.
187+
*/
188+
class FallthroughStmt extends Stmt;
189+
182190
/**
183191
* A `goto` statement.
184192
*
@@ -579,6 +587,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
579587
or
580588
n instanceof ContinueStmt
581589
or
590+
n instanceof FallthroughStmt
591+
or
582592
n instanceof GotoStmt
583593
or
584594
n instanceof Expr and
@@ -743,6 +753,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
743753
or
744754
n instanceof ContinueStmt
745755
or
756+
n instanceof FallthroughStmt
757+
or
746758
n instanceof GotoStmt
747759
or
748760
n instanceof ReturnStmt
@@ -1288,6 +1300,9 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
12881300
ast instanceof ContinueStmt and
12891301
st instanceof ContinueSuccessor
12901302
or
1303+
ast instanceof FallthroughStmt and
1304+
st instanceof FallthroughSuccessor
1305+
or
12911306
ast instanceof GotoStmt and
12921307
st instanceof GotoSuccessor
12931308
|
@@ -1396,6 +1411,17 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
13961411
)
13971412
)
13981413
or
1414+
exists(Switch switch, int index, Case nextCase |
1415+
ast = getCaseBodyElement(switch.getCase(index), _) and
1416+
nextCase = switch.getCase(index + 1) and
1417+
c.asSimpleAbruptCompletion() instanceof FallthroughSuccessor
1418+
|
1419+
n.isBefore(getFirstCaseBodyElement(nextCase))
1420+
or
1421+
not exists(getFirstCaseBodyElement(nextCase)) and
1422+
n.isAfter(switch)
1423+
)
1424+
or
13991425
exists(LabeledStmt target, Input1::Label l |
14001426
ast = target.getStmt() and
14011427
Input1::hasLabel(target, l) and

‎shared/controlflow/codeql/controlflow/SuccessorType.qll‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* \- JumpSuccessor
1919
* |- BreakSuccessor
2020
* |- ContinueSuccessor
21+
* |- FallthroughSuccessor
2122
* |- GotoSuccessor
2223
* |- RedoSuccessor // rare, used in Ruby
2324
* \- RetrySuccessor // rare, used in Ruby
@@ -71,6 +72,7 @@ private newtype TSuccessorType =
7172
TExitSuccessor() or
7273
TBreakSuccessor() or
7374
TContinueSuccessor() or
75+
TFallthroughSuccessor() or
7476
TGotoSuccessor() or
7577
TRedoSuccessor() or
7678
TRetrySuccessor()
@@ -357,13 +359,14 @@ class ExitSuccessor extends AbruptSuccessorImpl, TExitSuccessor {
357359
}
358360

359361
private class TJumpSuccessor =
360-
TBreakSuccessor or TContinueSuccessor or TGotoSuccessor or TRedoSuccessor or TRetrySuccessor;
362+
TBreakSuccessor or TContinueSuccessor or TFallthroughSuccessor or TGotoSuccessor or
363+
TRedoSuccessor or TRetrySuccessor;
361364

362365
/**
363366
* A jump control flow successor.
364367
*
365368
* This covers non-exceptional, non-local control flow, such as `break`,
366-
* `continue`, and `goto`.
369+
* `continue`, `fallthrough`, and `goto`.
367370
*/
368371
abstract private class JumpSuccessorImpl extends AbruptSuccessorImpl, TJumpSuccessor { }
369372

@@ -379,6 +382,11 @@ class ContinueSuccessor extends JumpSuccessorImpl, TContinueSuccessor {
379382
override string toString() { result = "continue" }
380383
}
381384

385+
/** A `fallthrough` control flow successor. */
386+
class FallthroughSuccessor extends JumpSuccessorImpl, TFallthroughSuccessor {
387+
override string toString() { result = "fallthrough" }
388+
}
389+
382390
/** A `goto` control flow successor. */
383391
class GotoSuccessor extends JumpSuccessorImpl, TGotoSuccessor {
384392
override string toString() { result = "goto" }

0 commit comments

Comments
 (0)