Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 8c27e16

Browse files
authored
Merge pull request #226 from smowton/smowton/fix/remove-spurious-cfg-edge-from-expressionless-switch
Remove spurious control-flow edge around switch block without a test
2 parents 95011ce + 4882f27 commit 8c27e16

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

ql/src/semmle/go/Stmt.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,9 @@ class TypeSwitchStmt extends @typeswitchstmt, SwitchStmt {
882882
/** Gets the assign statement of this type-switch statement. */
883883
SimpleAssignStmt getAssign() { result = getChildStmt(1) }
884884

885+
/** Gets the test statement of this type-switch statement. This is a `SimpleAssignStmt` or `ExprStmt`. */
886+
Stmt getTest() { result = getChildStmt(1) }
887+
885888
/** Gets the expression whose type is examined by this `switch` statement. */
886889
Expr getExpr() { result = getAssign().getRhs() or result = getChildStmt(1).(ExprStmt).getExpr() }
887890

ql/src/semmle/go/controlflow/ControlFlowGraphImpl.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ module CFG {
17611761
or
17621762
first = MkImplicitTrue(this)
17631763
or
1764-
firstNode(this.(TypeSwitchStmt).getChildStmt(1), first)
1764+
firstNode(this.(TypeSwitchStmt).getTest(), first)
17651765
)
17661766
}
17671767

@@ -1772,17 +1772,18 @@ module CFG {
17721772
(
17731773
lastNode(this.(ExpressionSwitchStmt).getExpr(), last, cmpl)
17741774
or
1775-
last = MkImplicitTrue(this) and
1776-
cmpl = Bool(true)
1777-
or
1778-
lastNode(this.(TypeSwitchStmt).getChildStmt(1), last, cmpl)
1775+
lastNode(this.(TypeSwitchStmt).getTest(), last, cmpl)
17791776
) and
17801777
(
17811778
not cmpl.isNormal()
17821779
or
17831780
not exists(this.getDefault())
17841781
)
17851782
or
1783+
last = MkImplicitTrue(this) and
1784+
cmpl = Bool(true) and
1785+
this.getNumCase() = 0
1786+
or
17861787
exists(CaseClause cc, int i, Completion inner |
17871788
cc = this.getCase(i) and lastNode(cc, last, inner)
17881789
|
@@ -1811,13 +1812,13 @@ module CFG {
18111812
(
18121813
firstNode(this.(ExpressionSwitchStmt).getExpr(), succ) or
18131814
succ = MkImplicitTrue(this) or
1814-
firstNode(this.(TypeSwitchStmt).getChildStmt(1), succ)
1815+
firstNode(this.(TypeSwitchStmt).getTest(), succ)
18151816
)
18161817
or
18171818
(
18181819
lastNode(this.(ExpressionSwitchStmt).getExpr(), pred, normalCompletion()) or
18191820
pred = MkImplicitTrue(this) or
1820-
lastNode(this.(TypeSwitchStmt).getChildStmt(1), pred, normalCompletion())
1821+
lastNode(this.(TypeSwitchStmt).getTest(), pred, normalCompletion())
18211822
) and
18221823
(
18231824
firstNode(getNonDefaultCase(0), succ)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@
12061206
| tst.go:3:12:3:12 | argument corresponding to x | tst.go:3:12:3:12 | initialization of x |
12071207
| tst.go:3:12:3:12 | initialization of x | tst.go:4:2:4:2 | true |
12081208
| tst.go:4:2:4:2 | true | tst.go:5:7:5:7 | x |
1209-
| tst.go:4:2:4:2 | true | tst.go:12:1:12:1 | exit |
12101209
| tst.go:5:2:5:13 | skip | tst.go:12:1:12:1 | exit |
12111210
| tst.go:5:7:5:7 | x | tst.go:5:11:5:12 | 23 |
12121211
| tst.go:5:7:5:12 | ...<... | tst.go:5:7:5:12 | case ...<... |
@@ -1232,12 +1231,11 @@
12321231
| tst.go:9:12:9:12 | ...<... is false | tst.go:12:1:12:1 | exit |
12331232
| tst.go:9:12:9:12 | ...<... is true | tst.go:9:2:9:13 | skip |
12341233
| tst.go:14:1:14:1 | entry | tst.go:14:13:14:17 | argument corresponding to value |
1235-
| tst.go:14:1:21:1 | function declaration | tst.go:0:0:0:0 | exit |
1234+
| tst.go:14:1:21:1 | function declaration | tst.go:23:6:23:11 | skip |
12361235
| tst.go:14:6:14:11 | skip | tst.go:14:1:21:1 | function declaration |
12371236
| tst.go:14:13:14:17 | argument corresponding to value | tst.go:14:13:14:17 | initialization of value |
12381237
| tst.go:14:13:14:17 | initialization of value | tst.go:15:2:15:2 | true |
12391238
| tst.go:15:2:15:2 | true | tst.go:16:7:16:11 | value |
1240-
| tst.go:15:2:15:2 | true | tst.go:21:1:21:1 | exit |
12411239
| tst.go:16:2:16:34 | skip | tst.go:21:1:21:1 | exit |
12421240
| tst.go:16:7:16:11 | value | tst.go:16:15:16:33 | ...*... |
12431241
| tst.go:16:7:16:33 | ...<... | tst.go:16:7:16:33 | case ...<... |
@@ -1254,3 +1252,12 @@
12541252
| tst.go:18:15:18:38 | ...*... | tst.go:18:7:18:38 | ...<... |
12551253
| tst.go:18:38:18:38 | ...<... is false | tst.go:21:1:21:1 | exit |
12561254
| tst.go:18:38:18:38 | ...<... is true | tst.go:18:2:18:39 | skip |
1255+
| tst.go:23:1:23:1 | entry | tst.go:24:2:24:2 | true |
1256+
| tst.go:23:1:25:1 | function declaration | tst.go:27:6:27:11 | skip |
1257+
| tst.go:23:6:23:11 | skip | tst.go:23:1:25:1 | function declaration |
1258+
| tst.go:24:2:24:2 | true | tst.go:25:1:25:1 | exit |
1259+
| tst.go:27:1:27:1 | entry | tst.go:28:2:28:2 | true |
1260+
| tst.go:27:1:31:1 | function declaration | tst.go:0:0:0:0 | exit |
1261+
| tst.go:27:6:27:11 | skip | tst.go:27:1:31:1 | function declaration |
1262+
| tst.go:28:2:28:2 | true | tst.go:29:2:29:9 | skip |
1263+
| tst.go:29:2:29:9 | skip | tst.go:31:1:31:1 | exit |

ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/tst.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ func check2(value int64) {
1919

2020
}
2121
}
22+
23+
func check3() {
24+
switch { }
25+
}
26+
27+
func check4() {
28+
switch {
29+
default:
30+
}
31+
}

0 commit comments

Comments
 (0)