-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Add Operation class #19454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geoffw0
wants to merge
6
commits into
github:main
Choose a base branch
from
geoffw0:deref
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−16
Open
Rust: Add Operation class #19454
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93f8cea
Rust: Add + clean up some QLDoc.
geoffw0 f64e86f
Rust: Add a library test for Operations.
geoffw0 060d515
Rust: Add an Operation class above LogicalOperation, AssignmentOperat…
geoffw0 be20176
Rust: Unify getAnOperand() methods into Operation.
geoffw0 dc1b4fc
Rust: Unify getOperatorName() methods into Operation.
geoffw0 09dc7fc
Rust: Autoformat.
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,46 @@ | ||
private import codeql.rust.elements.Expr | ||
private import codeql.rust.elements.BinaryExpr | ||
private import codeql.rust.elements.PrefixExpr | ||
private import codeql.rust.elements.Operation | ||
|
||
abstract private class LogicalOperationImpl extends Expr { | ||
abstract Expr getAnOperand(); | ||
} | ||
/** | ||
* A logical operation, such as `&&`, `||` or `!`. | ||
*/ | ||
abstract private class LogicalOperationImpl extends Operation { } | ||
|
||
final class LogicalOperation = LogicalOperationImpl; | ||
|
||
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl { | ||
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] } | ||
} | ||
/** | ||
* A binary logical operation, such as `&&` or `||`. | ||
*/ | ||
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl { } | ||
|
||
final class BinaryLogicalOperation = BinaryLogicalOperationImpl; | ||
|
||
/** | ||
* The logical and operation, `&&`. | ||
*/ | ||
final class LogicalAndExpr extends BinaryLogicalOperationImpl, BinaryExpr { | ||
LogicalAndExpr() { this.getOperatorName() = "&&" } | ||
} | ||
|
||
/** | ||
* The logical or operation, `||`. | ||
*/ | ||
final class LogicalOrExpr extends BinaryLogicalOperationImpl { | ||
LogicalOrExpr() { this.getOperatorName() = "||" } | ||
} | ||
|
||
/** | ||
* A unary logical operation, such as `!`. | ||
*/ | ||
abstract private class UnaryLogicalOperationImpl extends PrefixExpr, LogicalOperationImpl { } | ||
|
||
final class UnaryLogicalOperation = UnaryLogicalOperationImpl; | ||
|
||
/** | ||
* A logical not operation, `!`. | ||
*/ | ||
final class LogicalNotExpr extends UnaryLogicalOperationImpl { | ||
LogicalNotExpr() { this.getOperatorName() = "!" } | ||
|
||
override Expr getAnOperand() { result = this.getExpr() } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to place this in |
||
/** | ||
* An operation, for example `&&`, `+=`, `!` or `*`. | ||
*/ | ||
abstract class Operation extends ExprImpl::Expr { | ||
/** | ||
* Gets the operator name of this operation, if it exists. | ||
*/ | ||
abstract string getOperatorName(); | ||
|
||
/** | ||
* Gets an operand of this operation. | ||
*/ | ||
abstract Expr getAnOperand(); | ||
} | ||
} | ||
|
||
final class Operation = OperationImpl::Operation; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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" | ||
or | ||
op instanceof AssignmentOperation and result = "AssignmentOperation" | ||
or | ||
op instanceof LogicalOperation and result = "LogicalOperation" | ||
or | ||
op instanceof RefExpr and result = "RefExpr" | ||
} | ||
|
||
module OperationsTest implements TestSig { | ||
string getARelevantTag() { result = describe(_) or result = ["Op", "Operands"] } | ||
|
||
predicate hasActualResult(Location location, string element, string tag, string value) { | ||
exists(Expr op | | ||
location = op.getLocation() and | ||
location.getFile().getBaseName() != "" and | ||
element = op.toString() and | ||
( | ||
tag = describe(op) and | ||
value = "" | ||
or | ||
tag = "Op" and | ||
value = op.(Operation).getOperatorName() | ||
or | ||
op instanceof Operation and | ||
tag = "Operands" and | ||
value = count(op.(Operation).getAnOperand()).toString() | ||
) | ||
) | ||
} | ||
} | ||
|
||
import MakeTest<OperationsTest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
// --- tests --- | ||
|
||
fn test_operations( | ||
y: i32, a: bool, b: bool, | ||
ptr: &i32, res: Result<i32, ()>) -> Result<(), ()> | ||
{ | ||
let mut x: i32; | ||
|
||
// simple assignment | ||
x = y; // $ Operation Op== Operands=2 AssignmentOperation BinaryExpr | ||
|
||
// comparison 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 BinaryExpr | ||
|
||
// 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 | ||
|
||
// logical operations | ||
a && b; // $ Operation Op=&& Operands=2 BinaryExpr LogicalOperation | ||
a || b; // $ Operation Op=|| Operands=2 BinaryExpr LogicalOperation | ||
!a; // $ Operation Op=! Operands=1 PrefixExpr LogicalOperation | ||
|
||
// bitwise 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 | ||
|
||
// miscellaneous expressions that might be operations | ||
*ptr; // $ Operation Op=* Operands=1 PrefixExpr | ||
&x; // $ RefExpr | ||
res?; | ||
|
||
return Ok(()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make the abstract classes private?