Rust: Add an Operation class above LogicalOperation, AssignmentOperation etc.

This commit is contained in:
Geoffrey White
2025-04-09 16:02:54 +01:00
parent f64e86fe2e
commit 060d5152c4
7 changed files with 60 additions and 35 deletions

View File

@@ -1,11 +1,12 @@
private import codeql.rust.elements.Expr
private import codeql.rust.elements.BinaryExpr
private import codeql.rust.elements.PrefixExpr
private import codeql.rust.elements.Operation
/**
* A logical operation, such as `&&`, `||` or `!`.
*/
abstract private class LogicalOperationImpl extends Expr {
abstract private class LogicalOperationImpl extends Operation {
abstract Expr getAnOperand();
}

View File

@@ -0,0 +1,19 @@
/**
* Provides classes for operations.
*/
private import rust
private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
/**
* INTERNAL: This module contains the customizable definition of `Operation` and should not
* be referenced directly.
*/
module OperationImpl {
/**
* An operation, for example `&&`, `+=`, `!` or `*`.
*/
abstract class Operation extends ExprImpl::Expr { }
}
final class Operation = OperationImpl::Operation;

View File

@@ -5,6 +5,7 @@
*/
private import codeql.rust.elements.internal.generated.BinaryExpr
private import codeql.rust.elements.Operation::OperationImpl as OperationImpl
/**
* INTERNAL: This module contains the customizable definition of `BinaryExpr` and should not
@@ -22,7 +23,7 @@ module Impl {
* x += y;
* ```
*/
class BinaryExpr extends Generated::BinaryExpr {
class BinaryExpr extends Generated::BinaryExpr, OperationImpl::Operation {
override string toStringImpl() { result = "... " + this.getOperatorName() + " ..." }
}
}

View File

@@ -5,6 +5,7 @@
*/
private import codeql.rust.elements.internal.generated.PrefixExpr
private import codeql.rust.elements.Operation::OperationImpl as OperationImpl
/**
* INTERNAL: This module contains the customizable definition of `PrefixExpr` and should not
@@ -20,7 +21,7 @@ module Impl {
* let z = *ptr;
* ```
*/
class PrefixExpr extends Generated::PrefixExpr {
class PrefixExpr extends Generated::PrefixExpr, OperationImpl::Operation {
override string toStringImpl() { result = this.getOperatorName() + " ..." }
}
}

View File

@@ -3,6 +3,7 @@
import codeql.rust.elements
import codeql.Locations
import codeql.files.FileSystem
import codeql.rust.elements.Operation
import codeql.rust.elements.AssignmentOperation
import codeql.rust.elements.LogicalOperation
import codeql.rust.elements.AsyncBlockExpr

View File

@@ -2,6 +2,8 @@ import rust
import utils.test.InlineExpectationsTest
string describe(Expr op) {
op instanceof Operation and result = "Operation"
or
op instanceof PrefixExpr and result = "PrefixExpr"
or
op instanceof BinaryExpr and result = "BinaryExpr"

View File

@@ -8,48 +8,48 @@ fn test_operations(
let mut x: i32;
// simple assignment
x = y; // $ AssignmentOperation BinaryExpr
x = y; // $ Operation AssignmentOperation BinaryExpr
// comparison operations
x == y; // $ BinaryExpr
x != y; // $ BinaryExpr
x < y; // $ BinaryExpr
x <= y; // $ BinaryExpr
x > y; // $ BinaryExpr
x >= y; // $ BinaryExpr
x == y; // $ Operation BinaryExpr
x != y; // $ Operation BinaryExpr
x < y; // $ Operation BinaryExpr
x <= y; // $ Operation BinaryExpr
x > y; // $ Operation BinaryExpr
x >= y; // $ Operation BinaryExpr
// arithmetic operations
x + y; // $ BinaryExpr
x - y; // $ BinaryExpr
x * y; // $ BinaryExpr
x / y; // $ BinaryExpr
x % y; // $ BinaryExpr
x += y; // $ AssignmentOperation BinaryExpr
x -= y; // $ AssignmentOperation BinaryExpr
x *= y; // $ AssignmentOperation BinaryExpr
x /= y; // $ AssignmentOperation BinaryExpr
x %= y; // $ AssignmentOperation BinaryExpr
-x; // $ PrefixExpr
x + y; // $ Operation BinaryExpr
x - y; // $ Operation BinaryExpr
x * y; // $ Operation BinaryExpr
x / y; // $ Operation BinaryExpr
x % y; // $ Operation BinaryExpr
x += y; // $ Operation AssignmentOperation BinaryExpr
x -= y; // $ Operation AssignmentOperation BinaryExpr
x *= y; // $ Operation AssignmentOperation BinaryExpr
x /= y; // $ Operation AssignmentOperation BinaryExpr
x %= y; // $ Operation AssignmentOperation BinaryExpr
-x; // $ Operation PrefixExpr
// logical operations
a && b; // $ BinaryExpr LogicalOperation
a || b; // $ BinaryExpr LogicalOperation
!a; // $ PrefixExpr LogicalOperation
a && b; // $ Operation BinaryExpr LogicalOperation
a || b; // $ Operation BinaryExpr LogicalOperation
!a; // $ Operation PrefixExpr LogicalOperation
// bitwise operations
x & y; // $ BinaryExpr
x | y; // $ BinaryExpr
x ^ y; // $ BinaryExpr
x << y; // $ BinaryExpr
x >> y; // $ BinaryExpr
x &= y; // $ AssignmentOperation BinaryExpr
x |= y; // $ AssignmentOperation BinaryExpr
x ^= y; // $ AssignmentOperation BinaryExpr
x <<= y; // $ AssignmentOperation BinaryExpr
x >>= y; // $ AssignmentOperation BinaryExpr
x & y; // $ Operation BinaryExpr
x | y; // $ Operation BinaryExpr
x ^ y; // $ Operation BinaryExpr
x << y; // $ Operation BinaryExpr
x >> y; // $ Operation BinaryExpr
x &= y; // $ Operation AssignmentOperation BinaryExpr
x |= y; // $ Operation AssignmentOperation BinaryExpr
x ^= y; // $ Operation AssignmentOperation BinaryExpr
x <<= y; // $ Operation AssignmentOperation BinaryExpr
x >>= y; // $ Operation AssignmentOperation BinaryExpr
// miscellaneous expressions that might be operations
*ptr; // $ PrefixExpr
*ptr; // $ Operation PrefixExpr
&x; // $ RefExpr
res?;