Skip to content

Commit a5fff9a

Browse files
committed
Swift: Create ArithmeticOperation.qll.
1 parent 9e0cf62 commit a5fff9a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
private import codeql.swift.elements.expr.Expr
2+
private import codeql.swift.elements.expr.BinaryExpr
3+
private import codeql.swift.elements.expr.PrefixUnaryExpr
4+
private import codeql.swift.elements.expr.DotSyntaxCallExpr
5+
6+
/**
7+
* An arithmetic operation, such as:
8+
* ```
9+
* a + b
10+
* ```
11+
*/
12+
class ArithmeticOperation extends Expr {
13+
ArithmeticOperation() {
14+
this instanceof BinaryArithmeticOperation or
15+
this instanceof UnaryArithmeticOperation
16+
}
17+
18+
Expr getAnOperand() {
19+
result = this.(BinaryArithmeticOperation).getAnOperand()
20+
or
21+
result = this.(UnaryArithmeticOperation).getOperand()
22+
}
23+
}
24+
25+
/**
26+
* A binary arithmetic operation, such as:
27+
* ```
28+
* a + b
29+
* ```
30+
*/
31+
abstract class BinaryArithmeticOperation extends BinaryExpr { }
32+
33+
/**
34+
* An add expression.
35+
* ```
36+
* a + b
37+
* ```
38+
*/
39+
class AddExpr extends BinaryArithmeticOperation {
40+
AddExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "+(_:_:)" }
41+
}
42+
43+
/**
44+
* A subtract expression.
45+
* ```
46+
* a - b
47+
* ```
48+
*/
49+
class SubExpr extends BinaryArithmeticOperation {
50+
SubExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "-(_:_:)" }
51+
}
52+
53+
/**
54+
* A multiply expression.
55+
* ```
56+
* a * b
57+
* ```
58+
*/
59+
class MulExpr extends BinaryArithmeticOperation {
60+
MulExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "*(_:_:)" }
61+
}
62+
63+
/**
64+
* A divide expression.
65+
* ```
66+
* a / b
67+
* ```
68+
*/
69+
class DivExpr extends BinaryArithmeticOperation {
70+
DivExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "/(_:_:)" }
71+
}
72+
73+
/**
74+
* A remainder expression.
75+
* ```
76+
* a % b
77+
* ```
78+
*/
79+
class RemExpr extends BinaryArithmeticOperation {
80+
RemExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "%(_:_:)" }
81+
}
82+
83+
/**
84+
* A unary arithmetic operation, such as:
85+
* ```
86+
* -a
87+
* ```
88+
*/
89+
abstract class UnaryArithmeticOperation extends PrefixUnaryExpr { }
90+
91+
/**
92+
* A unary minus expression.
93+
* ```
94+
* -a
95+
* ```
96+
*/
97+
class UnaryMinusExpr extends UnaryArithmeticOperation {
98+
UnaryMinusExpr() { this.getFunction().(DotSyntaxCallExpr).getStaticTarget().getName() = "-(_:)" }
99+
}

swift/ql/lib/swift.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Top-level import for the Swift language pack */
22

33
import codeql.swift.elements
4+
import codeql.swift.elements.expr.ArithmeticOperation
45
import codeql.swift.elements.expr.LogicalOperation
56
import codeql.swift.elements.decl.MethodDecl

0 commit comments

Comments
 (0)