mirror of
https://github.com/github/codeql.git
synced 2026-04-26 01:05:15 +02:00
Rust: Add ArithmeticOperation library.
This commit is contained in:
41
rust/ql/lib/codeql/rust/elements/ArithmeticOperation.qll
Normal file
41
rust/ql/lib/codeql/rust/elements/ArithmeticOperation.qll
Normal 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() = "-"
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user