Skip to content

Commit 8a9a69f

Browse files
committed
Swift: Add more dataflow tests for of optionals, patterns, enums.
1 parent 04f87a2 commit 8a9a69f

File tree

3 files changed

+123
-32
lines changed

3 files changed

+123
-32
lines changed

swift/ql/test/library-tests/dataflow/dataflow/DataFlow.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ edges
107107
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:275:15:275:27 | ... ??(_:_:) ... |
108108
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:279:15:279:31 | ... ? ... : ... |
109109
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:280:15:280:38 | ... ? ... : ... |
110+
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:303:15:303:16 | ...! : |
110111
| test.swift:270:15:270:22 | call to source() : | file://:0:0:0:0 | [summary param] this in signum() : |
111112
| test.swift:270:15:270:22 | call to source() : | test.swift:270:15:270:31 | call to signum() |
112113
| test.swift:271:15:271:16 | ...? : | file://:0:0:0:0 | [summary param] this in signum() : |
113114
| test.swift:271:15:271:16 | ...? : | test.swift:271:15:271:25 | call to signum() : |
114115
| test.swift:271:15:271:25 | call to signum() : | test.swift:271:15:271:25 | OptionalEvaluationExpr |
115116
| test.swift:280:31:280:38 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... |
116117
| test.swift:282:31:282:38 | call to source() : | test.swift:282:15:282:38 | ... ? ... : ... |
118+
| test.swift:303:15:303:16 | ...! : | file://:0:0:0:0 | [summary param] this in signum() : |
119+
| test.swift:303:15:303:16 | ...! : | test.swift:303:15:303:25 | call to signum() |
117120
| test.swift:331:14:331:26 | (...) [Tuple element at index 1] : | test.swift:335:15:335:15 | t1 [Tuple element at index 1] : |
118121
| test.swift:331:18:331:25 | call to source() : | test.swift:331:14:331:26 | (...) [Tuple element at index 1] : |
119122
| test.swift:335:15:335:15 | t1 [Tuple element at index 1] : | test.swift:335:15:335:18 | .1 |
@@ -258,6 +261,8 @@ nodes
258261
| test.swift:280:31:280:38 | call to source() : | semmle.label | call to source() : |
259262
| test.swift:282:15:282:38 | ... ? ... : ... | semmle.label | ... ? ... : ... |
260263
| test.swift:282:31:282:38 | call to source() : | semmle.label | call to source() : |
264+
| test.swift:303:15:303:16 | ...! : | semmle.label | ...! : |
265+
| test.swift:303:15:303:25 | call to signum() | semmle.label | call to signum() |
261266
| test.swift:331:14:331:26 | (...) [Tuple element at index 1] : | semmle.label | (...) [Tuple element at index 1] : |
262267
| test.swift:331:18:331:25 | call to source() : | semmle.label | call to source() : |
263268
| test.swift:335:15:335:15 | t1 [Tuple element at index 1] : | semmle.label | t1 [Tuple element at index 1] : |
@@ -306,6 +311,7 @@ subpaths
306311
| test.swift:219:13:219:15 | .a [x] : | test.swift:163:7:163:7 | self [x] : | file://:0:0:0:0 | .x : | test.swift:219:13:219:17 | .x |
307312
| test.swift:270:15:270:22 | call to source() : | file://:0:0:0:0 | [summary param] this in signum() : | file://:0:0:0:0 | [summary] to write: return (return) in signum() : | test.swift:270:15:270:31 | call to signum() |
308313
| test.swift:271:15:271:16 | ...? : | file://:0:0:0:0 | [summary param] this in signum() : | file://:0:0:0:0 | [summary] to write: return (return) in signum() : | test.swift:271:15:271:25 | call to signum() : |
314+
| test.swift:303:15:303:16 | ...! : | file://:0:0:0:0 | [summary param] this in signum() : | file://:0:0:0:0 | [summary] to write: return (return) in signum() : | test.swift:303:15:303:25 | call to signum() |
309315
#select
310316
| test.swift:7:15:7:15 | t1 | test.swift:6:19:6:26 | call to source() : | test.swift:7:15:7:15 | t1 | result |
311317
| test.swift:9:15:9:15 | t1 | test.swift:6:19:6:26 | call to source() : | test.swift:9:15:9:15 | t1 | result |
@@ -345,6 +351,7 @@ subpaths
345351
| test.swift:280:15:280:38 | ... ? ... : ... | test.swift:259:12:259:19 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... | result |
346352
| test.swift:280:15:280:38 | ... ? ... : ... | test.swift:280:31:280:38 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... | result |
347353
| test.swift:282:15:282:38 | ... ? ... : ... | test.swift:282:31:282:38 | call to source() : | test.swift:282:15:282:38 | ... ? ... : ... | result |
354+
| test.swift:303:15:303:25 | call to signum() | test.swift:259:12:259:19 | call to source() : | test.swift:303:15:303:25 | call to signum() | result |
348355
| test.swift:335:15:335:18 | .1 | test.swift:331:18:331:25 | call to source() : | test.swift:335:15:335:18 | .1 | result |
349356
| test.swift:346:15:346:18 | .0 | test.swift:343:12:343:19 | call to source() : | test.swift:346:15:346:18 | .0 | result |
350357
| test.swift:356:15:356:18 | .0 | test.swift:351:18:351:25 | call to source() : | test.swift:356:15:356:18 | .0 | result |

swift/ql/test/library-tests/dataflow/dataflow/LocalFlow.expected

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,22 @@
241241
| test.swift:282:26:282:26 | y | test.swift:287:16:287:16 | y |
242242
| test.swift:282:26:282:27 | ...! | test.swift:282:15:282:38 | ... ? ... : ... |
243243
| test.swift:282:31:282:38 | call to source() | test.swift:282:15:282:38 | ... ? ... : ... |
244-
| test.swift:284:16:284:16 | x | test.swift:290:16:290:16 | x |
245-
| test.swift:287:16:287:16 | y | test.swift:293:16:293:16 | y |
246-
| test.swift:290:16:290:16 | x | test.swift:290:16:290:17 | ...? |
247-
| test.swift:290:16:290:26 | call to signum() | test.swift:290:16:290:26 | OptionalEvaluationExpr |
248-
| test.swift:293:16:293:16 | y | test.swift:293:16:293:17 | ...? |
249-
| test.swift:293:16:293:26 | call to signum() | test.swift:293:16:293:26 | OptionalEvaluationExpr |
244+
| test.swift:284:16:284:16 | x | test.swift:291:16:291:16 | x |
245+
| test.swift:287:16:287:16 | y | test.swift:294:16:294:16 | y |
246+
| test.swift:291:16:291:16 | x | test.swift:291:16:291:17 | ...? |
247+
| test.swift:291:16:291:16 | x | test.swift:298:20:298:20 | x |
248+
| test.swift:291:16:291:26 | call to signum() | test.swift:291:16:291:26 | OptionalEvaluationExpr |
249+
| test.swift:294:16:294:16 | y | test.swift:294:16:294:17 | ...? |
250+
| test.swift:294:16:294:16 | y | test.swift:299:20:299:20 | y |
251+
| test.swift:294:16:294:26 | call to signum() | test.swift:294:16:294:26 | OptionalEvaluationExpr |
252+
| test.swift:298:20:298:20 | x | test.swift:303:15:303:15 | x |
253+
| test.swift:299:20:299:20 | y | test.swift:304:15:304:15 | y |
254+
| test.swift:303:15:303:15 | x | test.swift:303:15:303:16 | ...! |
255+
| test.swift:303:15:303:15 | x | test.swift:306:28:306:28 | x |
256+
| test.swift:304:15:304:15 | y | test.swift:304:15:304:16 | ...! |
257+
| test.swift:304:15:304:15 | y | test.swift:309:28:309:28 | y |
258+
| test.swift:306:28:306:28 | x | test.swift:313:12:313:12 | x |
259+
| test.swift:309:28:309:28 | y | test.swift:319:12:319:12 | y |
250260
| test.swift:331:9:331:9 | SSA def(t1) | test.swift:333:15:333:15 | t1 |
251261
| test.swift:331:14:331:26 | (...) | test.swift:331:9:331:9 | SSA def(t1) |
252262
| test.swift:333:15:333:15 | t1 | test.swift:334:15:334:15 | t1 |
@@ -288,3 +298,9 @@
288298
| test.swift:360:15:360:15 | t2 | test.swift:361:15:361:15 | t2 |
289299
| test.swift:361:15:361:15 | [post] t2 | test.swift:362:15:362:15 | t2 |
290300
| test.swift:361:15:361:15 | t2 | test.swift:362:15:362:15 | t2 |
301+
| test.swift:375:9:375:13 | SSA def(a) | test.swift:377:12:377:12 | a |
302+
| test.swift:375:22:375:23 | .myNone | test.swift:375:9:375:13 | SSA def(a) |
303+
| test.swift:387:9:387:13 | SSA def(b) | test.swift:389:12:389:12 | b |
304+
| test.swift:387:22:387:40 | call to ... | test.swift:387:9:387:13 | SSA def(b) |
305+
| test.swift:399:9:399:9 | SSA def(c) | test.swift:401:12:401:12 | c |
306+
| test.swift:399:13:399:38 | call to ... | test.swift:399:9:399:9 | SSA def(c) |

swift/ql/test/library-tests/dataflow/dataflow/test.swift

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -287,42 +287,42 @@ func test_optionals(y: Int?) {
287287
if let z = y {
288288
sink(arg: z)
289289
}
290+
290291
if let z = x?.signum() { // $ MISSING: flow=259
291292
sink(arg: z)
292293
}
293294
if let z = y?.signum() {
294295
sink(arg: z)
295296
}
296-
}
297-
298-
299-
300-
301-
302-
303-
304-
305-
306-
307-
308-
309-
310-
311-
312-
313-
314-
315-
316-
317-
318-
319-
320-
321-
322297

298+
guard let z1 = x else { return }
299+
guard let z2 = y else { return }
300+
sink(arg: z1) // $ MISSING: flow=259
301+
sink(arg: z2)
323302

303+
sink(arg: x!.signum()) // $ flow=259
304+
sink(arg: y!.signum())
324305

306+
if case .some(let z) = x {
307+
sink(arg: z) // $ MISSING: flow=259
308+
}
309+
if case .some(let z) = y {
310+
sink(arg: z)
311+
}
325312

313+
switch x {
314+
case .some(let z):
315+
sink(arg: z) // $ MISSING: flow=259
316+
case .none:
317+
()
318+
}
319+
switch y {
320+
case .some(let z):
321+
sink(arg: z)
322+
case .none:
323+
()
324+
}
325+
}
326326

327327
func sink(arg: (Int, Int)) {}
328328
func sink(arg: (Int, Int, Int)) {}
@@ -364,3 +364,71 @@ func testTuples2() {
364364
sink(arg: b) // $ MISSING: flow=351
365365
sink(arg: c)
366366
}
367+
368+
enum MyEnum {
369+
case myNone
370+
case mySingle(Int)
371+
case myPair(Int, Int)
372+
}
373+
374+
func testEnums() {
375+
let a : MyEnum = .myNone
376+
377+
switch a {
378+
case .myNone:
379+
()
380+
case .mySingle(let a):
381+
sink(arg: a)
382+
case .myPair(let a, let b):
383+
sink(arg: a)
384+
sink(arg: b)
385+
}
386+
387+
if case .mySingle(let x) = a {
388+
sink(arg: x)
389+
}
390+
if case .myPair(let x, let y) = a {
391+
sink(arg: x)
392+
sink(arg: y)
393+
}
394+
395+
let b : MyEnum = .mySingle(source())
396+
397+
switch b {
398+
case .myNone:
399+
()
400+
case .mySingle(let a):
401+
sink(arg: a) // $ MISSING: flow=395
402+
case .myPair(let a, let b):
403+
sink(arg: a)
404+
sink(arg: b)
405+
}
406+
407+
if case .mySingle(let x) = a {
408+
sink(arg: x) // $ MISSING: flow=395
409+
}
410+
if case .myPair(let x, let y) = a {
411+
sink(arg: x)
412+
sink(arg: y)
413+
}
414+
415+
let c = MyEnum.myPair(0, source())
416+
417+
switch c {
418+
case .myNone:
419+
()
420+
case .mySingle(let a):
421+
sink(arg: a)
422+
case .myPair(let a, let b):
423+
sink(arg: a)
424+
sink(arg: b) // $ MISSING: flow=415
425+
}
426+
427+
if case .mySingle(let x) = a {
428+
sink(arg: x)
429+
}
430+
if case .myPair(let x, let y) = a {
431+
sink(arg: x)
432+
sink(arg: y) // $ MISSING: flow=415
433+
}
434+
}

0 commit comments

Comments
 (0)