Skip to content

Commit 3038543

Browse files
committed
Swift: Add UnaryPlusExpr.
1 parent 31967cc commit 3038543

File tree

8 files changed

+29
-2
lines changed

8 files changed

+29
-2
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ private module Cached {
153153
or
154154
nodeFrom.asExpr() = nodeTo.asExpr().(OptionalEvaluationExpr).getSubExpr()
155155
or
156+
// flow through unary `+` (which does nothing)
157+
nodeFrom.asExpr() = nodeTo.asExpr().(UnaryPlusExpr).getOperand()
158+
or
156159
// flow through nil-coalescing operator `??`
157160
exists(BinaryExpr nco |
158161
nco.getOperator().(FreeFunctionDecl).getName() = "??(_:_:)" and

swift/ql/lib/codeql/swift/elements/expr/ArithmeticOperation.qll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ class RemExpr extends BinaryExpr {
9797
* -a
9898
* ```
9999
*/
100-
class UnaryArithmeticOperation extends PrefixUnaryExpr instanceof UnaryMinusExpr { }
100+
class UnaryArithmeticOperation extends PrefixUnaryExpr {
101+
UnaryArithmeticOperation() {
102+
this instanceof UnaryMinusExpr or
103+
this instanceof UnaryPlusExpr
104+
}
105+
}
101106

102107
/**
103108
* A unary minus expression.
@@ -108,3 +113,13 @@ class UnaryArithmeticOperation extends PrefixUnaryExpr instanceof UnaryMinusExpr
108113
class UnaryMinusExpr extends PrefixUnaryExpr {
109114
UnaryMinusExpr() { this.getStaticTarget().getName() = "-(_:)" }
110115
}
116+
117+
/**
118+
* A unary plus expression.
119+
* ```
120+
* +a
121+
* ```
122+
*/
123+
class UnaryPlusExpr extends PrefixUnaryExpr {
124+
UnaryPlusExpr() { this.getStaticTarget().getName() = "+(_:)" }
125+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ edges
154154
| test.swift:472:20:472:20 | cx [x] : | test.swift:462:9:462:9 | self [x] : |
155155
| test.swift:472:20:472:20 | cx [x] : | test.swift:472:20:472:23 | .x : |
156156
| test.swift:472:20:472:23 | .x : | test.swift:473:15:473:15 | z1 |
157+
| test.swift:479:14:479:21 | call to source() : | test.swift:479:13:479:21 | call to +(_:) |
157158
nodes
158159
| file://:0:0:0:0 | .a [x] : | semmle.label | .a [x] : |
159160
| file://:0:0:0:0 | .x : | semmle.label | .x : |
@@ -324,6 +325,8 @@ nodes
324325
| test.swift:472:20:472:20 | cx [x] : | semmle.label | cx [x] : |
325326
| test.swift:472:20:472:23 | .x : | semmle.label | .x : |
326327
| test.swift:473:15:473:15 | z1 | semmle.label | z1 |
328+
| test.swift:479:13:479:21 | call to +(_:) | semmle.label | call to +(_:) |
329+
| test.swift:479:14:479:21 | call to source() : | semmle.label | call to source() : |
327330
| test.swift:480:14:480:21 | call to source() | semmle.label | call to source() |
328331
subpaths
329332
| test.swift:75:21:75:22 | &... : | test.swift:65:16:65:28 | arg1 : | test.swift:65:1:70:1 | arg2[return] : | test.swift:75:31:75:32 | [post] &... : |
@@ -409,4 +412,5 @@ subpaths
409412
| test.swift:361:15:361:18 | .1 | test.swift:351:31:351:38 | call to source() : | test.swift:361:15:361:18 | .1 | result |
410413
| test.swift:442:19:442:19 | a | test.swift:259:12:259:19 | call to source() : | test.swift:442:19:442:19 | a | result |
411414
| test.swift:473:15:473:15 | z1 | test.swift:259:12:259:19 | call to source() : | test.swift:473:15:473:15 | z1 | result |
415+
| test.swift:479:13:479:21 | call to +(_:) | test.swift:479:14:479:21 | call to source() : | test.swift:479:13:479:21 | call to +(_:) | result |
412416
| test.swift:480:14:480:21 | call to source() | test.swift:480:14:480:21 | call to source() | test.swift:480:14:480:21 | call to source() | result |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,4 @@
390390
| test.swift:472:20:472:23 | .x | test.swift:472:11:472:15 | SSA def(z1) |
391391
| test.swift:474:11:474:15 | SSA def(z2) | test.swift:475:15:475:15 | z2 |
392392
| test.swift:474:20:474:23 | .x | test.swift:474:11:474:15 | SSA def(z2) |
393+
| test.swift:479:14:479:21 | call to source() | test.swift:479:13:479:21 | call to +(_:) |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,6 @@ func testOptionalPropertyAccess(y: Int?) {
476476
}
477477

478478
func testIdentityArithmetic() {
479-
sink(arg: +source()) // $ MISSING: flow=479
479+
sink(arg: +source()) // $ flow=479
480480
sink(arg: (source())) // $ flow=480
481481
}

swift/ql/test/library-tests/elements/expr/arithmeticoperation/arithmeticoperation.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| arithmeticoperation.swift:9:6:9:10 | ... ./(_:_:) ... | BinaryArithmeticOperation, DivExpr |
55
| arithmeticoperation.swift:10:6:10:10 | ... .%(_:_:) ... | BinaryArithmeticOperation, RemExpr |
66
| arithmeticoperation.swift:11:6:11:7 | call to -(_:) | UnaryArithmeticOperation, UnaryMinusExpr |
7+
| arithmeticoperation.swift:12:6:12:7 | call to +(_:) | UnaryArithmeticOperation, UnaryPlusExpr |

swift/ql/test/library-tests/elements/expr/arithmeticoperation/arithmeticoperation.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ string describe(ArithmeticOperation e) {
1616
e instanceof UnaryArithmeticOperation and result = "UnaryArithmeticOperation"
1717
or
1818
e instanceof UnaryMinusExpr and result = "UnaryMinusExpr"
19+
or
20+
e instanceof UnaryPlusExpr and result = "UnaryPlusExpr"
1921
}
2022

2123
from ArithmeticOperation e

swift/ql/test/library-tests/elements/expr/arithmeticoperation/arithmeticoperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ func test(c: Bool, x: Int, y: Int, z: Int) {
99
v = 3 / 4;
1010
v = x % y;
1111
v = -x;
12+
v = +x;
1213
}

0 commit comments

Comments
 (0)