Skip to content

Commit dc1b4fc

Browse files
committed
Rust: Unify getOperatorName() methods into Operation.
1 parent be20176 commit dc1b4fc

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ module OperationImpl {
1414
* An operation, for example `&&`, `+=`, `!` or `*`.
1515
*/
1616
abstract class Operation extends ExprImpl::Expr {
17+
/**
18+
* Gets the operator name of this operation, if it exists.
19+
*/
20+
abstract string getOperatorName();
21+
1722
/**
1823
* Gets an operand of this operation.
1924
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Impl {
2626
class BinaryExpr extends Generated::BinaryExpr, OperationImpl::Operation {
2727
override string toStringImpl() { result = "... " + this.getOperatorName() + " ..." }
2828

29+
override string getOperatorName() { result = Generated::BinaryExpr.super.getOperatorName() }
30+
2931
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
3032
}
3133
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Impl {
2424
class PrefixExpr extends Generated::PrefixExpr, OperationImpl::Operation {
2525
override string toStringImpl() { result = this.getOperatorName() + " ..." }
2626

27+
override string getOperatorName() { result = Generated::PrefixExpr.super.getOperatorName() }
28+
2729
override Expr getAnOperand() { result = this.getExpr() }
2830
}
2931
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ string describe(Expr op) {
1616
}
1717

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

2121
predicate hasActualResult(Location location, string element, string tag, string value) {
2222
exists(Expr op |
@@ -27,6 +27,9 @@ module OperationsTest implements TestSig {
2727
tag = describe(op) and
2828
value = ""
2929
or
30+
tag = "Op" and
31+
value = op.(Operation).getOperatorName()
32+
or
3033
op instanceof Operation and
3134
tag = "Operands" and
3235
value = count(op.(Operation).getAnOperand()).toString()

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

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

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

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

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

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

0 commit comments

Comments
 (0)