Skip to content

Commit be20176

Browse files
committed
Rust: Unify getAnOperand() methods into Operation.
1 parent 060d515 commit be20176

File tree

6 files changed

+52
-41
lines changed

6 files changed

+52
-41
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ private import codeql.rust.elements.Operation
77
* A logical operation, such as `&&`, `||` or `!`.
88
*/
99
abstract private class LogicalOperationImpl extends Operation {
10-
abstract Expr getAnOperand();
1110
}
1211

1312
final class LogicalOperation = LogicalOperationImpl;
@@ -16,7 +15,6 @@ final class LogicalOperation = LogicalOperationImpl;
1615
* A binary logical operation, such as `&&` or `||`.
1716
*/
1817
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl {
19-
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
2018
}
2119

2220
final class BinaryLogicalOperation = BinaryLogicalOperationImpl;
@@ -47,6 +45,4 @@ final class UnaryLogicalOperation = UnaryLogicalOperationImpl;
4745
*/
4846
final class LogicalNotExpr extends UnaryLogicalOperationImpl {
4947
LogicalNotExpr() { this.getOperatorName() = "!" }
50-
51-
override Expr getAnOperand() { result = this.getExpr() }
5248
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
1010
* be referenced directly.
1111
*/
1212
module OperationImpl {
13+
/**
14+
* An operation, for example `&&`, `+=`, `!` or `*`.
15+
*/
16+
abstract class Operation extends ExprImpl::Expr {
1317
/**
14-
* An operation, for example `&&`, `+=`, `!` or `*`.
18+
* Gets an operand of this operation.
1519
*/
16-
abstract class Operation extends ExprImpl::Expr { }
20+
abstract Expr getAnOperand();
21+
}
1722
}
1823

1924
final class Operation = OperationImpl::Operation;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ module Impl {
2525
*/
2626
class BinaryExpr extends Generated::BinaryExpr, OperationImpl::Operation {
2727
override string toStringImpl() { result = "... " + this.getOperatorName() + " ..." }
28+
29+
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
2830
}
2931
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ module Impl {
2323
*/
2424
class PrefixExpr extends Generated::PrefixExpr, OperationImpl::Operation {
2525
override string toStringImpl() { result = this.getOperatorName() + " ..." }
26+
27+
override Expr getAnOperand() { result = this.getExpr() }
2628
}
2729
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ string describe(Expr op) {
1616
}
1717

1818
module OperationsTest implements TestSig {
19-
string getARelevantTag() { result = describe(_) }
19+
string getARelevantTag() { result = describe(_) or result = "Operands" }
2020

2121
predicate hasActualResult(Location location, string element, string tag, string value) {
2222
exists(Expr op |
2323
location = op.getLocation() and
2424
location.getFile().getBaseName() != "" and
2525
element = op.toString() and
26-
tag = describe(op) and
27-
value = ""
26+
(
27+
tag = describe(op) and
28+
value = ""
29+
or
30+
op instanceof Operation and
31+
tag = "Operands" and
32+
value = count(op.(Operation).getAnOperand()).toString()
33+
)
2834
)
2935
}
3036
}

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; // $ Operation AssignmentOperation BinaryExpr
11+
x = y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
1212

1313
// comparison operations
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
14+
x == y; // $ Operation Operands=2 BinaryExpr
15+
x != y; // $ Operation Operands=2 BinaryExpr
16+
x < y; // $ Operation Operands=2 BinaryExpr
17+
x <= y; // $ Operation Operands=2 BinaryExpr
18+
x > y; // $ Operation Operands=2 BinaryExpr
19+
x >= y; // $ Operation Operands=2 BinaryExpr
2020

2121
// arithmetic operations
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
22+
x + y; // $ Operation Operands=2 BinaryExpr
23+
x - y; // $ Operation Operands=2 BinaryExpr
24+
x * y; // $ Operation Operands=2 BinaryExpr
25+
x / y; // $ Operation Operands=2 BinaryExpr
26+
x % y; // $ Operation Operands=2 BinaryExpr
27+
x += y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
28+
x -= y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
29+
x *= y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
30+
x /= y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
31+
x %= y; // $ Operation Operands=2 AssignmentOperation BinaryExpr
32+
-x; // $ Operation Operands=1 PrefixExpr
3333

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

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

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

0 commit comments

Comments
 (0)