Skip to content

Commit 71813fa

Browse files
committed
Swift: Implement full tree of Assignment classes
1 parent aaa89f7 commit 71813fa

File tree

3 files changed

+129
-3
lines changed

3 files changed

+129
-3
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,117 @@
11
private import codeql.swift.generated.expr.AssignExpr
2+
private import codeql.swift.elements.expr.BinaryExpr
23

4+
/**
5+
* Any assignment expression. For example:
6+
* ```
7+
* x = 0
8+
* y += 1
9+
* z <<= 1
10+
* ```
11+
*/
12+
class Assignment extends Expr {
13+
Assignment() {
14+
this instanceof AssignExpr or
15+
this instanceof AssignArithmeticOperationEx or
16+
this instanceof AssignBitwiseOperationEx
17+
}
18+
19+
/**
20+
* Gets the destination of this assignment. For example `x` in:
21+
* ```
22+
* x = y
23+
* ```
24+
*/
25+
Expr getDest() {
26+
result = this.(AssignExpr).getDest() or
27+
result = this.(AssignOperation).getLeftOperand()
28+
}
29+
30+
/**
31+
* Gets the source of this assignment. For example `y` in:
32+
* ```
33+
* x = y
34+
* ```
35+
*/
36+
Expr getSource() {
37+
result = this.(AssignExpr).getSource() or
38+
result = this.(AssignOperation).getRightOperand()
39+
}
40+
}
41+
42+
/**
43+
* A simple assignment expression using the `=` operator:
44+
* ```
45+
* x = 0
46+
* ```
47+
*/
348
class AssignExpr extends Generated::AssignExpr {
449
override string toString() { result = " ... = ..." }
550
}
51+
52+
/**
53+
* Any assignment expression apart from `=`. For example:
54+
* ```
55+
* x += 1
56+
* y &= z
57+
* ```
58+
*/
59+
class AssignOperation extends Assignment, BinaryExpr {
60+
AssignOperation() {
61+
this instanceof AssignArithmeticOperationEx or
62+
this instanceof AssignBitwiseOperationEx
63+
}
64+
}
65+
66+
/**
67+
* An arithmetic assignment expression. For example:
68+
* ```
69+
* x += 1
70+
* y *= z
71+
* ```
72+
*/
73+
class AssignArithmeticOperation extends AssignOperation instanceof AssignArithmeticOperationEx { }
74+
75+
/**
76+
* Private abstract class, extended to define the scope of `AssignArithmeticOperation`.
77+
*/
78+
abstract private class AssignArithmeticOperationEx extends BinaryExpr { }
79+
80+
/**
81+
* A bitwise assignment expression. For example:
82+
* ```
83+
* x &= y
84+
* z <<= 1
85+
* ```
86+
*/
87+
class AssignBitwiseOperation extends AssignOperation instanceof AssignBitwiseOperationEx { }
88+
89+
/**
90+
* Private abstract class, extended to define the scope of `AssignBitwiseOperation`.
91+
*/
92+
abstract private class AssignBitwiseOperationEx extends BinaryExpr { }
93+
94+
/**
95+
* An addition assignment expression:
96+
* ```
97+
* a += b
98+
* ```
99+
*/
100+
class AssignAddExpr extends AssignArithmeticOperationEx {
101+
AssignAddExpr() { this.getOperator().getName() = "+=(_:_:)" }
102+
103+
override string toString() { result = "... += ..." }
104+
}
105+
106+
/**
107+
* A left-shift assignment expression:
108+
* ```
109+
* a <<= b
110+
* ```
111+
*/
112+
class AssignLShiftExpr extends AssignBitwiseOperationEx {
113+
AssignLShiftExpr() { this.getOperator().getName() = "<<=(_:_:)" }
114+
115+
override string toString() { result = "... <<= ..." }
116+
}
117+
// TODO ...
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
| assignment.swift:6:2:6:6 | ... = ... | AssignExpr | x | 1 |
2-
| assignment.swift:33:2:33:6 | ... = ... | AssignExpr | y | z |
1+
| assignment.swift:6:2:6:6 | ... = ... | AssignExpr, Assignment | x | 1 |
2+
| assignment.swift:9:2:9:7 | ... += ... | AssignAddExpr, AssignArithmeticOperation, AssignOperation, Assignment | &... | 1 |
3+
| assignment.swift:19:2:19:8 | ... <<= ... | AssignBitwiseOperation, AssignLShiftExpr, AssignOperation, Assignment | &... | 1 |
4+
| assignment.swift:33:2:33:6 | ... = ... | AssignExpr, Assignment | y | z |

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import swift
22

33
string describe(Expr e) {
4+
e instanceof Assignment and result = "Assignment"
5+
or
6+
e instanceof AssignOperation and result = "AssignOperation"
7+
or
8+
e instanceof AssignArithmeticOperation and result = "AssignArithmeticOperation"
9+
or
10+
e instanceof AssignBitwiseOperation and result = "AssignBitwiseOperation"
11+
or
412
e instanceof AssignExpr and result = "AssignExpr"
13+
or
14+
e instanceof AssignAddExpr and result = "AssignAddExpr"
15+
or
16+
e instanceof AssignLShiftExpr and result = "AssignLShiftExpr"
517
}
618

7-
from AssignExpr e
19+
from Assignment e
820
where
921
e.getLocation().getFile().getBaseName() != ""
1022
select

0 commit comments

Comments
 (0)