Skip to content

Commit ea1c52e

Browse files
committed
unified: Add tests with capture declarations
1 parent ba544c5 commit ea1c52e

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎unified/ql/test/library-tests/dataflow/capture.swift‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,83 @@ func t9() {
9999
return x
100100
}
101101
}
102+
103+
func t10() {
104+
let x = source("t10.1")
105+
let closure1 = { [x1 = x] in
106+
sink(x1) // $ MISSING: hasValueFlow=t10.1
107+
}
108+
let closure2 = { [x] in
109+
sink(x) // $ MISSING: hasValueFlow=t10.1
110+
}
111+
closure1()
112+
closure2()
113+
}
114+
115+
func t11() {
116+
let x = source("t11.1")
117+
let closure = { [x = x, blah = x] in
118+
sink(x) // $ MISSING: hasValueFlow=t11.1
119+
sink(blah) // $ MISSING: hasValueFlow=t11.1
120+
}
121+
closure()
122+
}
123+
124+
func t12() {
125+
class Box {
126+
var value: String
127+
init(_ x: String) { self.value = x }
128+
}
129+
let x = Box(source("t12.1"))
130+
let closure = { [weak x] in
131+
guard let x else { return }
132+
sink(x.value) // $ MISSING: hasValueFlow=t12.1
133+
}
134+
closure()
135+
}
136+
137+
func t13() {
138+
var x = "safe"
139+
let closure1 = { [x] in
140+
sink(x) // no flow
141+
}
142+
let closure2 = {
143+
sink(x) // $ hasValueFlow=t13.1
144+
}
145+
closure1()
146+
closure2()
147+
x = source("t13.1")
148+
closure1()
149+
closure2()
150+
}
151+
152+
class C {
153+
var x: String
154+
155+
func capture_self_by_ref() {
156+
x = source("C.1")
157+
let closure = {
158+
sink(self.x) // $ hasValueFlow=C.1
159+
}
160+
closure()
161+
}
162+
163+
func capture_self() {
164+
x = source("C.2")
165+
let closure = { [self] in
166+
sink(self.x) // $ MISSING: hasValueFlow=C.2
167+
sink(x) // $ MISSING: hasValueFlow=C.2
168+
}
169+
closure()
170+
}
171+
172+
func capture_weak_self() {
173+
x = source("C.3")
174+
let closure = { [weak self] in
175+
guard let self else { return }
176+
sink(self.x) // $ MISSING: hasValueFlow=C.3
177+
sink(x) // $ MISSING: hasValueFlow=C.3
178+
}
179+
closure()
180+
}
181+
}

‎unified/ql/test/library-tests/dataflow/test.expected‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ edges
8080
| capture.swift:94:13:94:17 | local [x] | capture.swift:98:14:98:14 | x | provenance | |
8181
| capture.swift:94:13:94:17 | local [x] | capture.swift:99:16:99:16 | x | provenance | |
8282
| capture.swift:94:13:94:19 | local(...) | capture.swift:94:9:94:9 | y | provenance | |
83+
| capture.swift:104:9:104:9 | x | capture.swift:105:28:105:28 | x | provenance | |
84+
| capture.swift:104:13:104:27 | source(...) | capture.swift:104:9:104:9 | x | provenance | |
85+
| capture.swift:105:23:105:24 | x1 | capture.swift:111:5:111:12 | closure1 [x1] | provenance | |
86+
| capture.swift:105:28:105:28 | x | capture.swift:105:23:105:24 | x1 | provenance | |
87+
| capture.swift:111:5:111:12 | closure1 [x1] | capture.swift:106:14:106:15 | x1 | provenance | |
88+
| capture.swift:147:5:147:5 | x | capture.swift:149:5:149:12 | closure2 [x] | provenance | |
89+
| capture.swift:147:9:147:23 | source(...) | capture.swift:147:5:147:5 | x | provenance | |
90+
| capture.swift:149:5:149:12 | closure2 [x] | capture.swift:143:14:143:14 | x | provenance | |
91+
| capture.swift:156:9:156:9 | x | capture.swift:160:9:160:15 | closure [self, x] | provenance | |
92+
| capture.swift:156:13:156:25 | source(...) | capture.swift:156:9:156:9 | x | provenance | |
93+
| capture.swift:158:18:158:21 | self [x] | capture.swift:158:18:158:23 | ... .x | provenance | |
94+
| capture.swift:160:9:160:15 | closure [self, x] | capture.swift:158:18:158:21 | self [x] | provenance | |
8395
| implicit-self.swift:16:9:16:12 | [post] self [x] | implicit-self.swift:17:14:17:17 | self [x] | provenance | |
8496
| implicit-self.swift:16:9:16:14 | ... .x | implicit-self.swift:16:9:16:12 | [post] self [x] | provenance | |
8597
| implicit-self.swift:16:18:16:31 | source(...) | implicit-self.swift:16:9:16:14 | ... .x | provenance | |
@@ -324,6 +336,21 @@ nodes
324336
| capture.swift:95:10:95:10 | y | semmle.label | y |
325337
| capture.swift:98:14:98:14 | x | semmle.label | x |
326338
| capture.swift:99:16:99:16 | x | semmle.label | x |
339+
| capture.swift:104:9:104:9 | x | semmle.label | x |
340+
| capture.swift:104:13:104:27 | source(...) | semmle.label | source(...) |
341+
| capture.swift:105:23:105:24 | x1 | semmle.label | x1 |
342+
| capture.swift:105:28:105:28 | x | semmle.label | x |
343+
| capture.swift:106:14:106:15 | x1 | semmle.label | x1 |
344+
| capture.swift:111:5:111:12 | closure1 [x1] | semmle.label | closure1 [x1] |
345+
| capture.swift:143:14:143:14 | x | semmle.label | x |
346+
| capture.swift:147:5:147:5 | x | semmle.label | x |
347+
| capture.swift:147:9:147:23 | source(...) | semmle.label | source(...) |
348+
| capture.swift:149:5:149:12 | closure2 [x] | semmle.label | closure2 [x] |
349+
| capture.swift:156:9:156:9 | x | semmle.label | x |
350+
| capture.swift:156:13:156:25 | source(...) | semmle.label | source(...) |
351+
| capture.swift:158:18:158:21 | self [x] | semmle.label | self [x] |
352+
| capture.swift:158:18:158:23 | ... .x | semmle.label | ... .x |
353+
| capture.swift:160:9:160:15 | closure [self, x] | semmle.label | closure [self, x] |
327354
| implicit-self.swift:16:9:16:12 | [post] self [x] | semmle.label | [post] self [x] |
328355
| implicit-self.swift:16:9:16:14 | ... .x | semmle.label | ... .x |
329356
| implicit-self.swift:16:18:16:31 | source(...) | semmle.label | source(...) |
@@ -519,6 +546,7 @@ subpaths
519546
| capture.swift:60:13:60:13 | x | capture.swift:56:22:56:24 | arg | capture.swift:57:9:57:9 | [post] y [0] | capture.swift:61:10:61:10 | y [0] |
520547
| capture.swift:94:13:94:17 | local [x] | capture.swift:99:16:99:16 | x | capture.swift:99:16:99:16 | x | capture.swift:94:13:94:19 | local(...) |
521548
testFailures
549+
| capture.swift:109:18:109:40 | // $ hasValueFlow=t10.1 | Missing result: hasValueFlow=t10.1 |
522550
#select
523551
| calls.swift:8:14:8:14 | x | calls.swift:10:12:10:25 | source(...) | calls.swift:8:14:8:14 | x | $@ | calls.swift:10:12:10:25 | source(...) | source(...) |
524552
| calls.swift:17:10:17:17 | target(...) | calls.swift:15:16:15:29 | source(...) | calls.swift:17:10:17:17 | target(...) | $@ | calls.swift:15:16:15:29 | source(...) | source(...) |
@@ -549,6 +577,9 @@ testFailures
549577
| capture.swift:88:10:88:10 | x | capture.swift:84:13:84:26 | source(...) | capture.swift:88:10:88:10 | x | $@ | capture.swift:84:13:84:26 | source(...) | source(...) |
550578
| capture.swift:95:10:95:10 | y | capture.swift:93:13:93:26 | source(...) | capture.swift:95:10:95:10 | y | $@ | capture.swift:93:13:93:26 | source(...) | source(...) |
551579
| capture.swift:98:14:98:14 | x | capture.swift:93:13:93:26 | source(...) | capture.swift:98:14:98:14 | x | $@ | capture.swift:93:13:93:26 | source(...) | source(...) |
580+
| capture.swift:106:14:106:15 | x1 | capture.swift:104:13:104:27 | source(...) | capture.swift:106:14:106:15 | x1 | $@ | capture.swift:104:13:104:27 | source(...) | source(...) |
581+
| capture.swift:143:14:143:14 | x | capture.swift:147:9:147:23 | source(...) | capture.swift:143:14:143:14 | x | $@ | capture.swift:147:9:147:23 | source(...) | source(...) |
582+
| capture.swift:158:18:158:23 | ... .x | capture.swift:156:13:156:25 | source(...) | capture.swift:158:18:158:23 | ... .x | $@ | capture.swift:156:13:156:25 | source(...) | source(...) |
552583
| implicit-self.swift:17:14:17:19 | ... .x | implicit-self.swift:16:18:16:31 | source(...) | implicit-self.swift:17:14:17:19 | ... .x | $@ | implicit-self.swift:16:18:16:31 | source(...) | source(...) |
553584
| implicit-self.swift:23:14:23:14 | x | implicit-self.swift:22:13:22:26 | source(...) | implicit-self.swift:23:14:23:14 | x | $@ | implicit-self.swift:22:13:22:26 | source(...) | source(...) |
554585
| implicit-self.swift:29:14:29:19 | ... .x | implicit-self.swift:28:13:28:26 | source(...) | implicit-self.swift:29:14:29:19 | ... .x | $@ | implicit-self.swift:28:13:28:26 | source(...) | source(...) |

0 commit comments

Comments
 (0)