Skip to content

Commit 060d515

Browse files
committed
Rust: Add an Operation class above LogicalOperation, AssignmentOperation etc.
1 parent f64e86f commit 060d515

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

rust/ql/lib/codeql/rust/elements/LogicalOperation.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
private import codeql.rust.elements.Expr
22
private import codeql.rust.elements.BinaryExpr
33
private import codeql.rust.elements.PrefixExpr
4+
private import codeql.rust.elements.Operation
45

56
/**
67
* A logical operation, such as `&&`, `||` or `!`.
78
*/
8-
abstract private class LogicalOperationImpl extends Expr {
9+
abstract private class LogicalOperationImpl extends Operation {
910
abstract Expr getAnOperand();
1011
}
1112

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Provides classes for operations.
3+
*/
4+
5+
private import rust
6+
private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
7+
8+
/**
9+
* INTERNAL: This module contains the customizable definition of `Operation` and should not
10+
* be referenced directly.
11+
*/
12+
module OperationImpl {
13+
/**
14+
* An operation, for example `&&`, `+=`, `!` or `*`.
15+
*/
16+
abstract class Operation extends ExprImpl::Expr { }
17+
}
18+
19+
final class Operation = OperationImpl::Operation;

rust/ql/lib/codeql/rust/elements/internal/BinaryExprImpl.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.BinaryExpr
8+
private import codeql.rust.elements.Operation::OperationImpl as OperationImpl
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `BinaryExpr` and should not
@@ -22,7 +23,7 @@ module Impl {
2223
* x += y;
2324
* ```
2425
*/
25-
class BinaryExpr extends Generated::BinaryExpr {
26+
class BinaryExpr extends Generated::BinaryExpr, OperationImpl::Operation {
2627
override string toStringImpl() { result = "... " + this.getOperatorName() + " ..." }
2728
}
2829
}

rust/ql/lib/codeql/rust/elements/internal/PrefixExprImpl.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.PrefixExpr
8+
private import codeql.rust.elements.Operation::OperationImpl as OperationImpl
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `PrefixExpr` and should not
@@ -20,7 +21,7 @@ module Impl {
2021
* let z = *ptr;
2122
* ```
2223
*/
23-
class PrefixExpr extends Generated::PrefixExpr {
24+
class PrefixExpr extends Generated::PrefixExpr, OperationImpl::Operation {
2425
override string toStringImpl() { result = this.getOperatorName() + " ..." }
2526
}
2627
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import codeql.rust.elements
44
import codeql.Locations
55
import codeql.files.FileSystem
6+
import codeql.rust.elements.Operation
67
import codeql.rust.elements.AssignmentOperation
78
import codeql.rust.elements.LogicalOperation
89
import codeql.rust.elements.AsyncBlockExpr

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import rust
22
import utils.test.InlineExpectationsTest
33

44
string describe(Expr op) {
5+
op instanceof Operation and result = "Operation"
6+
or
57
op instanceof PrefixExpr and result = "PrefixExpr"
68
or
79
op instanceof BinaryExpr and result = "BinaryExpr"

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,48 @@ fn test_operations(
88
let mut x: i32;
99

1010
// simple assignment
11-
x = y; // $ AssignmentOperation BinaryExpr
11+
x = y; // $ Operation AssignmentOperation BinaryExpr
1212

1313
// comparison operations
14-
x == y; // $ BinaryExpr
15-
x != y; // $ BinaryExpr
16-
x < y; // $ BinaryExpr
17-
x <= y; // $ BinaryExpr
18-
x > y; // $ BinaryExpr
19-
x >= y; // $ BinaryExpr
14+
x == y; // $ Operation BinaryExpr
15+
x != y; // $ Operation BinaryExpr
16+
x < y; // $ Operation BinaryExpr
17+
x <= y; // $ Operation BinaryExpr
18+
x > y; // $ Operation BinaryExpr
19+
x >= y; // $ Operation BinaryExpr
2020

2121
// arithmetic operations
22-
x + y; // $ BinaryExpr
23-
x - y; // $ BinaryExpr
24-
x * y; // $ BinaryExpr
25-
x / y; // $ BinaryExpr
26-
x % y; // $ BinaryExpr
27-
x += y; // $ AssignmentOperation BinaryExpr
28-
x -= y; // $ AssignmentOperation BinaryExpr
29-
x *= y; // $ AssignmentOperation BinaryExpr
30-
x /= y; // $ AssignmentOperation BinaryExpr
31-
x %= y; // $ AssignmentOperation BinaryExpr
32-
-x; // $ PrefixExpr
22+
x + y; // $ Operation BinaryExpr
23+
x - y; // $ Operation BinaryExpr
24+
x * y; // $ Operation BinaryExpr
25+
x / y; // $ Operation BinaryExpr
26+
x % y; // $ Operation BinaryExpr
27+
x += y; // $ Operation AssignmentOperation BinaryExpr
28+
x -= y; // $ Operation AssignmentOperation BinaryExpr
29+
x *= y; // $ Operation AssignmentOperation BinaryExpr
30+
x /= y; // $ Operation AssignmentOperation BinaryExpr
31+
x %= y; // $ Operation AssignmentOperation BinaryExpr
32+
-x; // $ Operation PrefixExpr
3333

3434
// logical operations
35-
a && b; // $ BinaryExpr LogicalOperation
36-
a || b; // $ BinaryExpr LogicalOperation
37-
!a; // $ PrefixExpr LogicalOperation
35+
a && b; // $ Operation BinaryExpr LogicalOperation
36+
a || b; // $ Operation BinaryExpr LogicalOperation
37+
!a; // $ Operation PrefixExpr LogicalOperation
3838

3939
// bitwise operations
40-
x & y; // $ BinaryExpr
41-
x | y; // $ BinaryExpr
42-
x ^ y; // $ BinaryExpr
43-
x << y; // $ BinaryExpr
44-
x >> y; // $ BinaryExpr
45-
x &= y; // $ AssignmentOperation BinaryExpr
46-
x |= y; // $ AssignmentOperation BinaryExpr
47-
x ^= y; // $ AssignmentOperation BinaryExpr
48-
x <<= y; // $ AssignmentOperation BinaryExpr
49-
x >>= y; // $ AssignmentOperation BinaryExpr
40+
x & y; // $ Operation BinaryExpr
41+
x | y; // $ Operation BinaryExpr
42+
x ^ y; // $ Operation BinaryExpr
43+
x << y; // $ Operation BinaryExpr
44+
x >> y; // $ Operation BinaryExpr
45+
x &= y; // $ Operation AssignmentOperation BinaryExpr
46+
x |= y; // $ Operation AssignmentOperation BinaryExpr
47+
x ^= y; // $ Operation AssignmentOperation BinaryExpr
48+
x <<= y; // $ Operation AssignmentOperation BinaryExpr
49+
x >>= y; // $ Operation AssignmentOperation BinaryExpr
5050

5151
// miscellaneous expressions that might be operations
52-
*ptr; // $ PrefixExpr
52+
*ptr; // $ Operation PrefixExpr
5353
&x; // $ RefExpr
5454
res?;
5555

0 commit comments

Comments
 (0)