Skip to content

Commit 11480d2

Browse files
committed
Rust: Add ArithmeticOperation library.
1 parent d27596a commit 11480d2

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Provides classes for arithmetic operations.
3+
*/
4+
5+
private import codeql.rust.elements.BinaryExpr
6+
private import codeql.rust.elements.PrefixExpr
7+
private import codeql.rust.elements.Operation
8+
9+
/**
10+
* An arithmetic operation, such as `+`, `*=`, or `-`.
11+
*/
12+
abstract private class ArithmeticOperationImpl extends Operation { }
13+
14+
final class ArithmeticOperation = ArithmeticOperationImpl;
15+
16+
/**
17+
* A binary arithmetic operation, such as `+` or `*`.
18+
*/
19+
final class BinaryArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
20+
BinaryArithmeticOperation() {
21+
this.getOperatorName() = ["+", "-", "*", "/", "%"]
22+
}
23+
}
24+
25+
/**
26+
* An arithmetic assignment operation, such as `+=` or `*=`.
27+
*/
28+
final class AssignArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
29+
AssignArithmeticOperation() {
30+
this.getOperatorName() = ["+=", "-=", "*=", "/=", "%="]
31+
}
32+
}
33+
34+
/**
35+
* A prefix arithmetic operation, such as `-`.
36+
*/
37+
final class PrefixArithmeticOperation extends PrefixExpr, ArithmeticOperationImpl {
38+
PrefixArithmeticOperation() {
39+
this.getOperatorName() = "-"
40+
}
41+
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import codeql.rust.elements
44
import codeql.Locations
55
import codeql.files.FileSystem
66
import codeql.rust.elements.Operation
7+
import codeql.rust.elements.ArithmeticOperation
78
import codeql.rust.elements.AssignmentOperation
89
import codeql.rust.elements.ComparisonOperation
910
import codeql.rust.elements.LiteralExprExt

rust/ql/test/library-tests/operations/Operations.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ string describe(Expr op) {
3131
op instanceof LessOrEqualsOperation and result = "LessOrEqualsOperation"
3232
or
3333
op instanceof GreaterOrEqualsOperation and result = "GreaterOrEqualsOperation"
34+
or
35+
op instanceof ArithmeticOperation and result = "ArithmeticOperation"
36+
or
37+
op instanceof BinaryArithmeticOperation and result = "BinaryArithmeticOperation"
38+
or
39+
op instanceof AssignArithmeticOperation and result = "AssignArithmeticOperation"
40+
or
41+
op instanceof PrefixArithmeticOperation and result = "PrefixArithmeticOperation"
3442
}
3543

3644
module OperationsTest implements TestSig {

rust/ql/test/library-tests/operations/test.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ fn test_operations(
1919
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualsOperation Greater=x Lesser=y
2020

2121
// arithmetic operations
22-
x + y; // $ Operation Op=+ Operands=2 BinaryExpr
23-
x - y; // $ Operation Op=- Operands=2 BinaryExpr
24-
x * y; // $ Operation Op=* Operands=2 BinaryExpr
25-
x / y; // $ Operation Op=/ Operands=2 BinaryExpr
26-
x % y; // $ Operation Op=% Operands=2 BinaryExpr
27-
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr
28-
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr
29-
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr
30-
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr
31-
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr
32-
-x; // $ Operation Op=- Operands=1 PrefixExpr
22+
x + y; // $ Operation Op=+ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
23+
x - y; // $ Operation Op=- Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
24+
x * y; // $ Operation Op=* Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
25+
x / y; // $ Operation Op=/ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
26+
x % y; // $ Operation Op=% Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
27+
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
28+
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
29+
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
30+
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
31+
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
32+
-x; // $ Operation Op=- Operands=1 PrefixExpr ArithmeticOperation PrefixArithmeticOperation
3333

3434
// logical operations
3535
a && b; // $ Operation Op=&& Operands=2 BinaryExpr LogicalOperation

0 commit comments

Comments
 (0)