Rust: Add ArithmeticOperation library.

This commit is contained in:
Geoffrey White
2025-05-21 11:26:55 +01:00
parent d27596a0b2
commit 11480d29b7
4 changed files with 61 additions and 11 deletions

View File

@@ -0,0 +1,41 @@
/**
* Provides classes for arithmetic operations.
*/
private import codeql.rust.elements.BinaryExpr
private import codeql.rust.elements.PrefixExpr
private import codeql.rust.elements.Operation
/**
* An arithmetic operation, such as `+`, `*=`, or `-`.
*/
abstract private class ArithmeticOperationImpl extends Operation { }
final class ArithmeticOperation = ArithmeticOperationImpl;
/**
* A binary arithmetic operation, such as `+` or `*`.
*/
final class BinaryArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
BinaryArithmeticOperation() {
this.getOperatorName() = ["+", "-", "*", "/", "%"]
}
}
/**
* An arithmetic assignment operation, such as `+=` or `*=`.
*/
final class AssignArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
AssignArithmeticOperation() {
this.getOperatorName() = ["+=", "-=", "*=", "/=", "%="]
}
}
/**
* A prefix arithmetic operation, such as `-`.
*/
final class PrefixArithmeticOperation extends PrefixExpr, ArithmeticOperationImpl {
PrefixArithmeticOperation() {
this.getOperatorName() = "-"
}
}

View File

@@ -4,6 +4,7 @@ import codeql.rust.elements
import codeql.Locations
import codeql.files.FileSystem
import codeql.rust.elements.Operation
import codeql.rust.elements.ArithmeticOperation
import codeql.rust.elements.AssignmentOperation
import codeql.rust.elements.ComparisonOperation
import codeql.rust.elements.LiteralExprExt

View File

@@ -31,6 +31,14 @@ string describe(Expr op) {
op instanceof LessOrEqualsOperation and result = "LessOrEqualsOperation"
or
op instanceof GreaterOrEqualsOperation and result = "GreaterOrEqualsOperation"
or
op instanceof ArithmeticOperation and result = "ArithmeticOperation"
or
op instanceof BinaryArithmeticOperation and result = "BinaryArithmeticOperation"
or
op instanceof AssignArithmeticOperation and result = "AssignArithmeticOperation"
or
op instanceof PrefixArithmeticOperation and result = "PrefixArithmeticOperation"
}
module OperationsTest implements TestSig {

View File

@@ -19,17 +19,17 @@ fn test_operations(
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualsOperation Greater=x Lesser=y
// arithmetic operations
x + y; // $ Operation Op=+ Operands=2 BinaryExpr
x - y; // $ Operation Op=- Operands=2 BinaryExpr
x * y; // $ Operation Op=* Operands=2 BinaryExpr
x / y; // $ Operation Op=/ Operands=2 BinaryExpr
x % y; // $ Operation Op=% Operands=2 BinaryExpr
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr
-x; // $ Operation Op=- Operands=1 PrefixExpr
x + y; // $ Operation Op=+ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x - y; // $ Operation Op=- Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x * y; // $ Operation Op=* Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x / y; // $ Operation Op=/ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x % y; // $ Operation Op=% Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
-x; // $ Operation Op=- Operands=1 PrefixExpr ArithmeticOperation PrefixArithmeticOperation
// logical operations
a && b; // $ Operation Op=&& Operands=2 BinaryExpr LogicalOperation