Skip to content

Commit 635b722

Browse files
committed
Improve SSA tests for variables in closures
1 parent a3dbc5e commit 635b722

File tree

6 files changed

+107
-36
lines changed

6 files changed

+107
-36
lines changed

go/ql/test/library-tests/semmle/go/dataflow/SSA/DefUse.expected

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
| main.go:84:15:84:15 | x | main.go:83:2:83:2 | definition of x | main.go:83:2:83:2 | x |
2929
| main.go:97:2:97:8 | wrapper | main.go:95:22:95:28 | definition of wrapper | main.go:95:22:95:28 | wrapper |
3030
| main.go:100:9:100:9 | x | main.go:97:2:99:3 | capture variable x | main.go:96:2:96:2 | x |
31-
| main.go:117:2:117:2 | p | main.go:117:2:117:2 | p = phi(def@112:3, def@114:3) | main.go:110:6:110:6 | p |
32-
| main.go:119:12:119:12 | p | main.go:117:2:117:2 | p = phi(def@112:3, def@114:3) | main.go:110:6:110:6 | p |
33-
| main.go:119:17:119:17 | p | main.go:117:2:117:2 | p = phi(def@112:3, def@114:3) | main.go:110:6:110:6 | p |
34-
| main.go:119:24:119:24 | p | main.go:117:2:117:2 | p = phi(def@112:3, def@114:3) | main.go:110:6:110:6 | p |
31+
| main.go:105:2:105:8 | wrapper | main.go:103:20:103:26 | definition of wrapper | main.go:103:20:103:26 | wrapper |
32+
| main.go:106:8:106:8 | x | main.go:105:16:108:2 | capture variable x | main.go:104:2:104:2 | x |
33+
| main.go:107:7:107:7 | y | main.go:106:3:106:3 | definition of y | main.go:106:3:106:3 | y |
34+
| main.go:109:9:109:9 | x | main.go:104:2:104:2 | definition of x | main.go:104:2:104:2 | x |
35+
| main.go:114:2:114:8 | wrapper | main.go:112:29:112:35 | definition of wrapper | main.go:112:29:112:35 | wrapper |
36+
| main.go:115:8:115:8 | x | main.go:114:16:117:2 | capture variable x | main.go:113:2:113:2 | x |
37+
| main.go:116:7:116:7 | y | main.go:115:3:115:3 | definition of y | main.go:115:3:115:3 | y |
38+
| main.go:118:9:118:9 | x | main.go:114:2:117:3 | capture variable x | main.go:113:2:113:2 | x |
39+
| main.go:135:2:135:2 | p | main.go:135:2:135:2 | p = phi(def@130:3, def@132:3) | main.go:128:6:128:6 | p |
40+
| main.go:137:12:137:12 | p | main.go:135:2:135:2 | p = phi(def@130:3, def@132:3) | main.go:128:6:128:6 | p |
41+
| main.go:137:17:137:17 | p | main.go:135:2:135:2 | p = phi(def@130:3, def@132:3) | main.go:128:6:128:6 | p |
42+
| main.go:137:24:137:24 | p | main.go:135:2:135:2 | p = phi(def@130:3, def@132:3) | main.go:128:6:128:6 | p |

go/ql/test/library-tests/semmle/go/dataflow/SSA/SsaDefinition.expected

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
| main.go:96:2:96:2 | definition of x |
3737
| main.go:97:2:99:3 | capture variable x |
3838
| main.go:98:3:98:3 | definition of x |
39-
| main.go:112:3:112:3 | definition of p |
40-
| main.go:114:3:114:3 | definition of p |
41-
| main.go:117:2:117:2 | p = phi(def@112:3, def@114:3) |
39+
| main.go:103:20:103:26 | definition of wrapper |
40+
| main.go:104:2:104:2 | definition of x |
41+
| main.go:105:16:108:2 | capture variable x |
42+
| main.go:106:3:106:3 | definition of y |
43+
| main.go:112:29:112:35 | definition of wrapper |
44+
| main.go:113:2:113:2 | definition of x |
45+
| main.go:114:2:117:3 | capture variable x |
46+
| main.go:114:16:117:2 | capture variable x |
47+
| main.go:115:3:115:3 | definition of y |
48+
| main.go:116:3:116:3 | definition of x |
49+
| main.go:130:3:130:3 | definition of p |
50+
| main.go:132:3:132:3 | definition of p |
51+
| main.go:135:2:135:2 | p = phi(def@130:3, def@132:3) |

go/ql/test/library-tests/semmle/go/dataflow/SSA/SsaWithFields.expected

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,22 @@
3737
| main.go:96:2:96:2 | (def@96:2) | x |
3838
| main.go:97:2:99:3 | (capture@97:2) | x |
3939
| main.go:98:3:98:3 | (def@98:3) | x |
40-
| main.go:112:3:112:3 | (def@112:3) | p |
41-
| main.go:114:3:114:3 | (def@114:3) | p |
42-
| main.go:117:2:117:2 | (phi@117:2) | p |
43-
| main.go:117:2:117:2 | (phi@117:2).a | p.a |
44-
| main.go:117:2:117:2 | (phi@117:2).b | p.b |
45-
| main.go:117:2:117:2 | (phi@117:2).b.a | p.b.a |
46-
| main.go:117:2:117:2 | (phi@117:2).c | p.c |
40+
| main.go:103:20:103:26 | (def@103:20) | wrapper |
41+
| main.go:103:20:103:26 | (def@103:20).s | wrapper.s |
42+
| main.go:104:2:104:2 | (def@104:2) | x |
43+
| main.go:105:16:108:2 | (capture@105:16) | x |
44+
| main.go:106:3:106:3 | (def@106:3) | y |
45+
| main.go:112:29:112:35 | (def@112:29) | wrapper |
46+
| main.go:112:29:112:35 | (def@112:29).s | wrapper.s |
47+
| main.go:113:2:113:2 | (def@113:2) | x |
48+
| main.go:114:2:117:3 | (capture@114:2) | x |
49+
| main.go:114:16:117:2 | (capture@114:16) | x |
50+
| main.go:115:3:115:3 | (def@115:3) | y |
51+
| main.go:116:3:116:3 | (def@116:3) | x |
52+
| main.go:130:3:130:3 | (def@130:3) | p |
53+
| main.go:132:3:132:3 | (def@132:3) | p |
54+
| main.go:135:2:135:2 | (phi@135:2) | p |
55+
| main.go:135:2:135:2 | (phi@135:2).a | p.a |
56+
| main.go:135:2:135:2 | (phi@135:2).b | p.b |
57+
| main.go:135:2:135:2 | (phi@135:2).b.a | p.b.a |
58+
| main.go:135:2:135:2 | (phi@135:2).c | p.c |

go/ql/test/library-tests/semmle/go/dataflow/SSA/VarDefs.expected

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,23 @@
3232
| main.go:95:22:95:28 | initialization of wrapper | main.go:95:22:95:28 | wrapper | main.go:95:22:95:28 | argument corresponding to wrapper |
3333
| main.go:96:2:96:2 | assignment to x | main.go:96:2:96:2 | x | main.go:96:7:96:7 | 0 |
3434
| main.go:98:3:98:3 | assignment to x | main.go:96:2:96:2 | x | main.go:98:7:98:7 | 1 |
35-
| main.go:110:6:110:6 | assignment to p | main.go:110:6:110:6 | p | main.go:110:6:110:6 | zero value for p |
36-
| main.go:112:3:112:3 | assignment to p | main.go:110:6:110:6 | p | main.go:112:7:112:24 | struct literal |
37-
| main.go:112:9:112:9 | init of 2 | main.go:104:2:104:2 | a | main.go:112:9:112:9 | 2 |
38-
| main.go:112:12:112:18 | init of struct literal | main.go:105:2:105:2 | b | main.go:112:12:112:18 | struct literal |
39-
| main.go:112:14:112:14 | init of 1 | main.go:89:2:89:2 | a | main.go:112:14:112:14 | 1 |
40-
| main.go:112:17:112:17 | init of 5 | main.go:90:2:90:2 | b | main.go:112:17:112:17 | 5 |
41-
| main.go:112:21:112:23 | init of 'n' | main.go:106:2:106:2 | c | main.go:112:21:112:23 | 'n' |
42-
| main.go:114:3:114:3 | assignment to p | main.go:110:6:110:6 | p | main.go:114:7:114:24 | struct literal |
43-
| main.go:114:9:114:9 | init of 3 | main.go:104:2:104:2 | a | main.go:114:9:114:9 | 3 |
44-
| main.go:114:12:114:18 | init of struct literal | main.go:105:2:105:2 | b | main.go:114:12:114:18 | struct literal |
45-
| main.go:114:14:114:14 | init of 4 | main.go:89:2:89:2 | a | main.go:114:14:114:14 | 4 |
46-
| main.go:114:17:114:17 | init of 5 | main.go:90:2:90:2 | b | main.go:114:17:114:17 | 5 |
47-
| main.go:114:21:114:23 | init of '2' | main.go:106:2:106:2 | c | main.go:114:21:114:23 | '2' |
35+
| main.go:103:20:103:26 | initialization of wrapper | main.go:103:20:103:26 | wrapper | main.go:103:20:103:26 | argument corresponding to wrapper |
36+
| main.go:104:2:104:2 | assignment to x | main.go:104:2:104:2 | x | main.go:104:7:104:7 | 0 |
37+
| main.go:106:3:106:3 | assignment to y | main.go:106:3:106:3 | y | main.go:106:8:106:8 | x |
38+
| main.go:112:29:112:35 | initialization of wrapper | main.go:112:29:112:35 | wrapper | main.go:112:29:112:35 | argument corresponding to wrapper |
39+
| main.go:113:2:113:2 | assignment to x | main.go:113:2:113:2 | x | main.go:113:7:113:7 | 0 |
40+
| main.go:115:3:115:3 | assignment to y | main.go:115:3:115:3 | y | main.go:115:8:115:12 | ...+... |
41+
| main.go:116:3:116:3 | assignment to x | main.go:113:2:113:2 | x | main.go:116:7:116:7 | y |
42+
| main.go:128:6:128:6 | assignment to p | main.go:128:6:128:6 | p | main.go:128:6:128:6 | zero value for p |
43+
| main.go:130:3:130:3 | assignment to p | main.go:128:6:128:6 | p | main.go:130:7:130:24 | struct literal |
44+
| main.go:130:9:130:9 | init of 2 | main.go:122:2:122:2 | a | main.go:130:9:130:9 | 2 |
45+
| main.go:130:12:130:18 | init of struct literal | main.go:123:2:123:2 | b | main.go:130:12:130:18 | struct literal |
46+
| main.go:130:14:130:14 | init of 1 | main.go:89:2:89:2 | a | main.go:130:14:130:14 | 1 |
47+
| main.go:130:17:130:17 | init of 5 | main.go:90:2:90:2 | b | main.go:130:17:130:17 | 5 |
48+
| main.go:130:21:130:23 | init of 'n' | main.go:124:2:124:2 | c | main.go:130:21:130:23 | 'n' |
49+
| main.go:132:3:132:3 | assignment to p | main.go:128:6:128:6 | p | main.go:132:7:132:24 | struct literal |
50+
| main.go:132:9:132:9 | init of 3 | main.go:122:2:122:2 | a | main.go:132:9:132:9 | 3 |
51+
| main.go:132:12:132:18 | init of struct literal | main.go:123:2:123:2 | b | main.go:132:12:132:18 | struct literal |
52+
| main.go:132:14:132:14 | init of 4 | main.go:89:2:89:2 | a | main.go:132:14:132:14 | 4 |
53+
| main.go:132:17:132:17 | init of 5 | main.go:90:2:90:2 | b | main.go:132:17:132:17 | 5 |
54+
| main.go:132:21:132:23 | init of '2' | main.go:124:2:124:2 | c | main.go:132:21:132:23 | '2' |

go/ql/test/library-tests/semmle/go/dataflow/SSA/VarUses.expected

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,29 @@
2828
| main.go:84:15:84:15 | x | main.go:83:2:83:2 | x |
2929
| main.go:97:2:97:8 | wrapper | main.go:95:22:95:28 | wrapper |
3030
| main.go:97:2:97:10 | selection of s | main.go:95:38:95:38 | s |
31+
| main.go:97:2:97:10 | selection of s | main.go:103:36:103:36 | s |
32+
| main.go:97:2:97:10 | selection of s | main.go:112:45:112:45 | s |
3133
| main.go:100:9:100:9 | x | main.go:96:2:96:2 | x |
32-
| main.go:117:2:117:2 | p | main.go:110:6:110:6 | p |
33-
| main.go:117:2:117:4 | selection of b | main.go:105:2:105:2 | b |
34-
| main.go:119:12:119:12 | p | main.go:110:6:110:6 | p |
35-
| main.go:119:12:119:14 | selection of a | main.go:104:2:104:2 | a |
36-
| main.go:119:17:119:17 | p | main.go:110:6:110:6 | p |
37-
| main.go:119:17:119:19 | selection of b | main.go:105:2:105:2 | b |
38-
| main.go:119:17:119:21 | selection of a | main.go:89:2:89:2 | a |
39-
| main.go:119:24:119:24 | p | main.go:110:6:110:6 | p |
40-
| main.go:119:24:119:26 | selection of c | main.go:106:2:106:2 | c |
34+
| main.go:105:2:105:8 | wrapper | main.go:103:20:103:26 | wrapper |
35+
| main.go:105:2:105:10 | selection of s | main.go:95:38:95:38 | s |
36+
| main.go:105:2:105:10 | selection of s | main.go:103:36:103:36 | s |
37+
| main.go:105:2:105:10 | selection of s | main.go:112:45:112:45 | s |
38+
| main.go:106:8:106:8 | x | main.go:104:2:104:2 | x |
39+
| main.go:107:7:107:7 | y | main.go:106:3:106:3 | y |
40+
| main.go:109:9:109:9 | x | main.go:104:2:104:2 | x |
41+
| main.go:114:2:114:8 | wrapper | main.go:112:29:112:35 | wrapper |
42+
| main.go:114:2:114:10 | selection of s | main.go:95:38:95:38 | s |
43+
| main.go:114:2:114:10 | selection of s | main.go:103:36:103:36 | s |
44+
| main.go:114:2:114:10 | selection of s | main.go:112:45:112:45 | s |
45+
| main.go:115:8:115:8 | x | main.go:113:2:113:2 | x |
46+
| main.go:116:7:116:7 | y | main.go:115:3:115:3 | y |
47+
| main.go:118:9:118:9 | x | main.go:113:2:113:2 | x |
48+
| main.go:135:2:135:2 | p | main.go:128:6:128:6 | p |
49+
| main.go:135:2:135:4 | selection of b | main.go:123:2:123:2 | b |
50+
| main.go:137:12:137:12 | p | main.go:128:6:128:6 | p |
51+
| main.go:137:12:137:14 | selection of a | main.go:122:2:122:2 | a |
52+
| main.go:137:17:137:17 | p | main.go:128:6:128:6 | p |
53+
| main.go:137:17:137:19 | selection of b | main.go:123:2:123:2 | b |
54+
| main.go:137:17:137:21 | selection of a | main.go:89:2:89:2 | a |
55+
| main.go:137:24:137:24 | p | main.go:128:6:128:6 | p |
56+
| main.go:137:24:137:26 | selection of c | main.go:124:2:124:2 | c |

go/ql/test/library-tests/semmle/go/dataflow/SSA/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ func updateInClosure(wrapper struct{ s }) int {
100100
return x
101101
}
102102

103+
func readInClosure(wrapper struct{ s }) int {
104+
x := 0
105+
wrapper.s.foo(func() {
106+
y := x
107+
_ = y
108+
})
109+
return x
110+
}
111+
112+
func readAndUpdateInClosure(wrapper struct{ s }) int {
113+
x := 0
114+
wrapper.s.foo(func() {
115+
y := x + 1
116+
x = y
117+
})
118+
return x
119+
}
120+
103121
type t struct {
104122
a int
105123
b s

0 commit comments

Comments
 (0)