mirror of
https://github.com/github/codeql.git
synced 2026-02-20 08:53:49 +01:00
Add classes and tests for operations
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import codeql.Locations
|
||||
import ast.Expr
|
||||
import ast.Method
|
||||
import ast.Parameter
|
||||
import ast.Operation
|
||||
import ast.Pattern
|
||||
import ast.Variable
|
||||
private import ast.internal.TreeSitter
|
||||
|
||||
75
ql/src/codeql_ruby/ast/Expr.qll
Normal file
75
ql/src/codeql_ruby/ast/Expr.qll
Normal file
@@ -0,0 +1,75 @@
|
||||
import codeql_ruby.AST
|
||||
private import codeql_ruby.ast.internal.TreeSitter
|
||||
private import internal.Expr
|
||||
|
||||
/**
|
||||
* An expression.
|
||||
*
|
||||
* This is the root QL class for all expressions.
|
||||
*/
|
||||
class Expr extends AstNode {
|
||||
Expr::Range range;
|
||||
|
||||
Expr() { this = range }
|
||||
}
|
||||
|
||||
/**
|
||||
* A literal.
|
||||
*
|
||||
* This is the QL root class for all literals.
|
||||
*/
|
||||
class Literal extends Expr {
|
||||
override Literal::Range range;
|
||||
|
||||
override string toString() { result = this.getValueText() }
|
||||
|
||||
string getValueText() { result = range.getValueText() }
|
||||
}
|
||||
|
||||
/**
|
||||
* An integer literal.
|
||||
* ```rb
|
||||
* x = 123
|
||||
* y = 0xff
|
||||
* ```
|
||||
*/
|
||||
class IntegerLiteral extends Literal, @token_integer {
|
||||
final override IntegerLiteral::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "IntegerLiteral" }
|
||||
}
|
||||
|
||||
/** A `nil` literal. */
|
||||
class NilLiteral extends Literal, @token_nil {
|
||||
final override NilLiteral::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "NilLiteral" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A Boolean literal.
|
||||
* ```rb
|
||||
* true
|
||||
* false
|
||||
* TRUE
|
||||
* FALSE
|
||||
* ```
|
||||
*/
|
||||
class BooleanLiteral extends Literal, BooleanLiteral::DbUnion {
|
||||
final override BooleanLiteral::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "BooleanLiteral" }
|
||||
|
||||
/** Holds if the Boolean literal is `true` or `TRUE`. */
|
||||
predicate isTrue() { range.isTrue() }
|
||||
|
||||
/** Holds if the Boolean literal is `false` or `FALSE`. */
|
||||
predicate isFalse() { range.isFalse() }
|
||||
}
|
||||
|
||||
// TODO: expand this. It's a minimal placeholder so we can test `=~` and `!~`.
|
||||
class RegexLiteral extends Literal, @regex {
|
||||
final override RegexLiteral::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "RegexLiteral" }
|
||||
}
|
||||
655
ql/src/codeql_ruby/ast/Operation.qll
Normal file
655
ql/src/codeql_ruby/ast/Operation.qll
Normal file
@@ -0,0 +1,655 @@
|
||||
import codeql_ruby.AST
|
||||
private import internal.Operation
|
||||
|
||||
/**
|
||||
* An operation.
|
||||
*
|
||||
* This is the QL root class for all operations.
|
||||
*/
|
||||
class Operation extends Expr {
|
||||
override Operation::Range range;
|
||||
|
||||
/** Gets the operator of this operation. */
|
||||
string getOperator() { result = range.getOperator() }
|
||||
|
||||
/** Gets an operand of this operation. */
|
||||
Expr getAnOperand() { result = range.getAnOperand() }
|
||||
}
|
||||
|
||||
/** A unary operation. */
|
||||
class UnaryOperation extends Operation, @unary {
|
||||
override UnaryOperation::Range range;
|
||||
|
||||
/** Gets the operand of this unary operation. */
|
||||
Expr getOperand() { result = range.getOperand() }
|
||||
|
||||
override string toString() { result = this.getOperator() + " ..." }
|
||||
}
|
||||
|
||||
/** A unary logical operation. */
|
||||
class UnaryLogicalOperation extends UnaryOperation {
|
||||
override UnaryLogicalOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical NOT operation, using either `!` or `not`.
|
||||
* ```rb
|
||||
* !x.nil?
|
||||
* not params.empty?
|
||||
* ```
|
||||
*/
|
||||
class NotExpr extends UnaryLogicalOperation, NotExpr::DbUnion {
|
||||
final override NotExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "NotExpr" }
|
||||
}
|
||||
|
||||
/** A unary arithmetic operation. */
|
||||
class UnaryArithmeticOperation extends UnaryOperation {
|
||||
override UnaryArithmeticOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A unary plus expression.
|
||||
* ```rb
|
||||
* b = + a;
|
||||
* ```
|
||||
*/
|
||||
class UnaryPlusExpr extends UnaryArithmeticOperation, @unary_plus {
|
||||
final override UnaryPlusExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "UnaryPlusExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A unary minus expression.
|
||||
* ```rb
|
||||
* b = - a;
|
||||
* ```
|
||||
*/
|
||||
class UnaryMinusExpr extends UnaryArithmeticOperation, @unary_minus {
|
||||
final override UnaryMinusExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "UnaryMinusExpr" }
|
||||
}
|
||||
|
||||
/** A unary bitwise operation. */
|
||||
class UnaryBitwiseOperation extends UnaryOperation {
|
||||
override UnaryBitwiseOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A complement (bitwise NOT) expression.
|
||||
* ```rb
|
||||
* ~x
|
||||
* ```
|
||||
*/
|
||||
class ComplementExpr extends UnaryBitwiseOperation, @unary_tilde {
|
||||
final override ComplementExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "ComplementExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A call to the special `defined?` operator.
|
||||
* ```rb
|
||||
* defined? some_method
|
||||
* ```
|
||||
*/
|
||||
class DefinedExpr extends UnaryOperation, @unary_definedquestion {
|
||||
final override DefinedExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "DefinedExpr" }
|
||||
}
|
||||
|
||||
/** A binary operation. */
|
||||
class BinaryOperation extends Operation, @binary {
|
||||
override BinaryOperation::Range range;
|
||||
|
||||
override string toString() { result = "... " + this.getOperator() + " ..." }
|
||||
|
||||
/** Gets the left operand of this binary operation. */
|
||||
Expr getLeftOperand() { result = range.getLeftOperand() }
|
||||
|
||||
/** Gets the right operand of this binary operation. */
|
||||
Expr getRightOperand() { result = range.getRightOperand() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary arithmetic operation.
|
||||
*/
|
||||
class BinaryArithmeticOperation extends BinaryOperation {
|
||||
override BinaryArithmeticOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* An add expression.
|
||||
* ```rb
|
||||
* x + 1
|
||||
* ```
|
||||
*/
|
||||
class AddExpr extends BinaryArithmeticOperation, @binary_plus {
|
||||
final override AddExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AddExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A subtract expression.
|
||||
* ```rb
|
||||
* x - 3
|
||||
* ```
|
||||
*/
|
||||
class SubExpr extends BinaryArithmeticOperation, @binary_minus {
|
||||
final override SubExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "SubExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A multiply expression.
|
||||
* ```rb
|
||||
* x * 10
|
||||
* ```
|
||||
*/
|
||||
class MulExpr extends BinaryArithmeticOperation, @binary_star {
|
||||
final override MulExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "MulExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A divide expression.
|
||||
* ```rb
|
||||
* x / y
|
||||
* ```
|
||||
*/
|
||||
class DivExpr extends BinaryArithmeticOperation, @binary_slash {
|
||||
final override DivExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "DivExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A modulo expression.
|
||||
* ```rb
|
||||
* x % 2
|
||||
* ```
|
||||
*/
|
||||
class ModuloExpr extends BinaryArithmeticOperation, @binary_percent {
|
||||
final override ModuloExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "ModuloExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* An exponent expression.
|
||||
* ```rb
|
||||
* x ** 2
|
||||
* ```
|
||||
*/
|
||||
class ExponentExpr extends BinaryArithmeticOperation, @binary_starstar {
|
||||
final override ExponentExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "ExponentExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary logical operation.
|
||||
*/
|
||||
class BinaryLogicalOperation extends BinaryOperation {
|
||||
override BinaryLogicalOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical AND operation, using either `and` or `&&`.
|
||||
* ```rb
|
||||
* x and y
|
||||
* a && b
|
||||
* ```
|
||||
*/
|
||||
class LogicalAndExpr extends BinaryLogicalOperation, LogicalAndExpr::DbUnion {
|
||||
final override LogicalAndExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LogicalAndExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical OR operation, using either `or` or `||`.
|
||||
* ```rb
|
||||
* x or y
|
||||
* a || b
|
||||
* ```
|
||||
*/
|
||||
class LogicalOrExpr extends BinaryLogicalOperation, LogicalOrExpr::DbUnion {
|
||||
final override LogicalOrExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LogicalOrExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary bitwise operation.
|
||||
*/
|
||||
class BinaryBitwiseOperation extends BinaryOperation {
|
||||
override BinaryBitwiseOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A left-shift operation.
|
||||
* ```rb
|
||||
* x << n
|
||||
* ```
|
||||
*/
|
||||
class LShiftExpr extends BinaryBitwiseOperation, @binary_langlelangle {
|
||||
final override LShiftExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LShiftExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A right-shift operation.
|
||||
* ```rb
|
||||
* x >> n
|
||||
* ```
|
||||
*/
|
||||
class RShiftExpr extends BinaryBitwiseOperation, @binary_ranglerangle {
|
||||
final override RShiftExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "RShiftExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise AND operation.
|
||||
* ```rb
|
||||
* x & 0xff
|
||||
* ```
|
||||
*/
|
||||
class BitwiseAndExpr extends BinaryBitwiseOperation, @binary_ampersand {
|
||||
final override BitwiseAndExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "BitwiseAndExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise OR operation.
|
||||
* ```rb
|
||||
* x | 0x01
|
||||
* ```
|
||||
*/
|
||||
class BitwiseOrExpr extends BinaryBitwiseOperation, @binary_pipe {
|
||||
final override BitwiseOrExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "BitwiseOrExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* An XOR (exclusive OR) operation.
|
||||
* ```rb
|
||||
* x ^ y
|
||||
* ```
|
||||
*/
|
||||
class BitwiseXorExpr extends BinaryBitwiseOperation, @binary_caret {
|
||||
final override BitwiseXorExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "BitwiseXorExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A comparison operation. That is, either an equality operation or a
|
||||
* relational operation.
|
||||
*/
|
||||
class ComparisonOperation extends BinaryOperation {
|
||||
override ComparisonOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* An equality operation.
|
||||
*/
|
||||
class EqualityOperation extends ComparisonOperation {
|
||||
override EqualityOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* An equals expression.
|
||||
* ```rb
|
||||
* x == y
|
||||
* ```
|
||||
*/
|
||||
class EqExpr extends EqualityOperation, @binary_equalequal {
|
||||
final override EqExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "EqExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A not-equals expression.
|
||||
* ```rb
|
||||
* x != y
|
||||
* ```
|
||||
*/
|
||||
class NEExpr extends EqualityOperation, @binary_bangequal {
|
||||
final override NEExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "NEExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A case-equality (or 'threequals') expression.
|
||||
* ```rb
|
||||
* String === "foo"
|
||||
* ```
|
||||
*/
|
||||
class CaseEqExpr extends EqualityOperation, @binary_equalequalequal {
|
||||
final override CaseEqExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "CaseEqExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A relational operation, that is, one of `<=`, `<`, `>`, or `>=`.
|
||||
*/
|
||||
class RelationalOperation extends ComparisonOperation {
|
||||
override RelationalOperation::Range range;
|
||||
|
||||
Expr getGreaterOperand() { result = range.getGreaterOperand() }
|
||||
|
||||
Expr getLesserOperand() { result = range.getLesserOperand() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A greater-than expression.
|
||||
* ```rb
|
||||
* x > 0
|
||||
* ```
|
||||
*/
|
||||
class GTExpr extends RelationalOperation, @binary_rangle {
|
||||
final override GTExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "GTExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A greater-than-or-equal expression.
|
||||
* ```rb
|
||||
* x >= 0
|
||||
* ```
|
||||
*/
|
||||
class GEExpr extends RelationalOperation, @binary_rangleequal {
|
||||
final override GEExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "GEExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A less-than expression.
|
||||
* ```rb
|
||||
* x < 10
|
||||
* ```
|
||||
*/
|
||||
class LTExpr extends RelationalOperation, @binary_langle {
|
||||
final override LTExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LTExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A less-than-or-equal expression.
|
||||
* ```rb
|
||||
* x <= 10
|
||||
* ```
|
||||
*/
|
||||
class LEExpr extends RelationalOperation, @binary_langleequal {
|
||||
final override LEExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LEExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A three-way comparison ('spaceship') expression.
|
||||
* ```rb
|
||||
* a <=> b
|
||||
* ```
|
||||
*/
|
||||
class SpaceshipExpr extends BinaryOperation, @binary_langleequalrangle {
|
||||
final override SpaceshipExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "SpaceshipExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A regex match expression.
|
||||
* ```rb
|
||||
* input =~ /\d/
|
||||
* ```
|
||||
*/
|
||||
class RegexMatchExpr extends BinaryOperation, @binary_equaltilde {
|
||||
final override RegexMatchExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "RegexMatchExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A regex-doesn't-match expression.
|
||||
* ```rb
|
||||
* input !~ /\d/
|
||||
* ```
|
||||
*/
|
||||
class NoRegexMatchExpr extends BinaryOperation, @binary_bangtilde {
|
||||
final override NoRegexMatchExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "NoRegexMatchExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary assignment operation, including `=`, `+=`, `&=`, etc.
|
||||
*
|
||||
* This is a QL base class for all assignments.
|
||||
*/
|
||||
class Assignment extends Operation {
|
||||
override Assignment::Range range;
|
||||
|
||||
override string toString() { result = "... " + this.getOperator() + " ..." }
|
||||
|
||||
/** Gets the left hand side of this assignment. */
|
||||
Expr getLhs() { result = range.getLhs() }
|
||||
|
||||
/** Gets the right hand side of this assignment. */
|
||||
Expr getRhs() { result = range.getRhs() }
|
||||
}
|
||||
|
||||
/**
|
||||
* An assignment operation with the operator `=`.
|
||||
* ```rb
|
||||
* x = 123
|
||||
* ```
|
||||
*/
|
||||
class AssignExpr extends Assignment {
|
||||
override AssignExpr::Range range;
|
||||
|
||||
override string getAPrimaryQlClass() { result = "AssignExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary assignment operation other than `=`.
|
||||
*/
|
||||
class AssignOperation extends Assignment {
|
||||
override AssignOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* An arithmetic assignment operation: `+=`, `-=`, `*=`, `/=`, `**=`, and `%=`.
|
||||
*/
|
||||
class AssignArithmeticOperation extends AssignOperation {
|
||||
override AssignArithmeticOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A `+=` assignment expression.
|
||||
* ```rb
|
||||
* x += 1
|
||||
* ```
|
||||
*/
|
||||
class AssignAddExpr extends AssignArithmeticOperation, @operator_assignment_plusequal {
|
||||
final override AssignAddExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignAddExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `-=` assignment expression.
|
||||
* ```rb
|
||||
* x -= 3
|
||||
* ```
|
||||
*/
|
||||
class AssignSubExpr extends AssignArithmeticOperation, @operator_assignment_minusequal {
|
||||
final override AssignSubExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignSubExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `*=` assignment expression.
|
||||
* ```rb
|
||||
* x *= 10
|
||||
* ```
|
||||
*/
|
||||
class AssignMulExpr extends AssignArithmeticOperation, @operator_assignment_starequal {
|
||||
final override AssignMulExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignMulExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `/=` assignment expression.
|
||||
* ```rb
|
||||
* x /= y
|
||||
* ```
|
||||
*/
|
||||
class AssignDivExpr extends AssignArithmeticOperation, @operator_assignment_slashequal {
|
||||
final override AssignDivExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignDivExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `%=` assignment expression.
|
||||
* ```rb
|
||||
* x %= 4
|
||||
* ```
|
||||
*/
|
||||
class AssignModuloExpr extends AssignArithmeticOperation, @operator_assignment_percentequal {
|
||||
final override AssignModuloExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignModuloExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `**=` assignment expression.
|
||||
* ```rb
|
||||
* x **= 2
|
||||
* ```
|
||||
*/
|
||||
class AssignExponentExpr extends AssignArithmeticOperation, @operator_assignment_starstarequal {
|
||||
final override AssignExponentExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignExponentExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical assignment operation: `&&=` and `||=`.
|
||||
*/
|
||||
class AssignLogicalOperation extends AssignOperation {
|
||||
override AssignLogicalOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical AND assignment operation.
|
||||
* ```rb
|
||||
* x &&= y.even?
|
||||
* ```
|
||||
*/
|
||||
class AssignLogicalAndExpr extends AssignLogicalOperation,
|
||||
@operator_assignment_ampersandampersandequal {
|
||||
final override AssignLogicalAndExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignLogicalAndExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical OR assignment operation.
|
||||
* ```rb
|
||||
* x ||= y
|
||||
* ```
|
||||
*/
|
||||
class AssignLogicalOrExpr extends AssignLogicalOperation, @operator_assignment_pipepipeequal {
|
||||
final override AssignLogicalOrExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignLogicalOrExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise assignment operation: `<<=`, `>>=`, `&=`, `|=` and `^=`.
|
||||
*/
|
||||
class AssignBitwiseOperation extends AssignOperation {
|
||||
override AssignBitwiseOperation::Range range;
|
||||
}
|
||||
|
||||
/**
|
||||
* A left-shift assignment operation.
|
||||
* ```rb
|
||||
* x <<= 3
|
||||
* ```
|
||||
*/
|
||||
class AssignLShiftExpr extends AssignBitwiseOperation, @operator_assignment_langlelangleequal {
|
||||
final override AssignLShiftExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignLShiftExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A right-shift assignment operation.
|
||||
* ```rb
|
||||
* x >>= 3
|
||||
* ```
|
||||
*/
|
||||
class AssignRShiftExpr extends AssignBitwiseOperation, @operator_assignment_ranglerangleequal {
|
||||
final override AssignRShiftExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignRShiftExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise AND assignment operation.
|
||||
* ```rb
|
||||
* x &= 0xff
|
||||
* ```
|
||||
*/
|
||||
class AssignBitwiseAndExpr extends AssignBitwiseOperation, @operator_assignment_ampersandequal {
|
||||
final override AssignBitwiseAndExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignBitwiseAndExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise OR assignment operation.
|
||||
* ```rb
|
||||
* x |= 0x01
|
||||
* ```
|
||||
*/
|
||||
class AssignBitwiseOrExpr extends AssignBitwiseOperation, @operator_assignment_pipeequal {
|
||||
final override AssignBitwiseOrExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignBitwiseOrExpr" }
|
||||
}
|
||||
|
||||
/**
|
||||
* An XOR (exclusive OR) assignment operation.
|
||||
* ```rb
|
||||
* x ^= y
|
||||
* ```
|
||||
*/
|
||||
class AssignBitwiseXorExpr extends AssignBitwiseOperation, @operator_assignment_caretequal {
|
||||
final override AssignBitwiseXorExpr::Range range;
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "AssignBitwiseXorExpr" }
|
||||
}
|
||||
@@ -60,21 +60,23 @@ class LocalVariable extends Variable {
|
||||
}
|
||||
|
||||
/** An access to a variable. */
|
||||
class VariableAccess extends AstNode, @token_identifier {
|
||||
override Generated::Identifier generated;
|
||||
Variable variable;
|
||||
|
||||
VariableAccess() { access(this, variable) }
|
||||
class VariableAccess extends Expr, @token_identifier {
|
||||
override VariableAccess::Range range;
|
||||
|
||||
/** Gets the variable this identifier refers to. */
|
||||
Variable getVariable() { result = variable }
|
||||
Variable getVariable() { result = range.getVariable() }
|
||||
|
||||
final override string toString() { result = variable.getName() }
|
||||
final override string toString() { result = this.getVariable().getName() }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "VariableAccess" }
|
||||
}
|
||||
|
||||
/** An access to a local variable. */
|
||||
class LocalVariableAccess extends VariableAccess {
|
||||
override LocalVariable variable;
|
||||
final override LocalVariableAccess::Range range;
|
||||
|
||||
override LocalVariable getVariable() { result = variable }
|
||||
/** Gets the variable this identifier refers to. */
|
||||
override LocalVariable getVariable() { result = range.getVariable() }
|
||||
|
||||
final override string getAPrimaryQlClass() { result = "LocalVariableAccess" }
|
||||
}
|
||||
|
||||
61
ql/src/codeql_ruby/ast/internal/Expr.qll
Normal file
61
ql/src/codeql_ruby/ast/internal/Expr.qll
Normal file
@@ -0,0 +1,61 @@
|
||||
import codeql_ruby.AST
|
||||
private import codeql_ruby.ast.internal.TreeSitter
|
||||
|
||||
module Expr {
|
||||
abstract class Range extends AstNode { }
|
||||
}
|
||||
|
||||
module Literal {
|
||||
abstract class Range extends Expr::Range {
|
||||
abstract string getValueText();
|
||||
}
|
||||
}
|
||||
|
||||
module IntegerLiteral {
|
||||
class Range extends Literal::Range, @token_integer {
|
||||
final override Generated::Integer generated;
|
||||
|
||||
final override string getValueText() { result = generated.getValue() }
|
||||
}
|
||||
}
|
||||
|
||||
module NilLiteral {
|
||||
class Range extends Literal::Range, @token_nil {
|
||||
final override Generated::Nil generated;
|
||||
|
||||
final override string getValueText() { result = generated.getValue() }
|
||||
}
|
||||
}
|
||||
|
||||
module BooleanLiteral {
|
||||
class DbUnion = @token_true or @token_false;
|
||||
|
||||
class Range extends Literal::Range, DbUnion {
|
||||
final override Generated::Token generated;
|
||||
|
||||
final override string getValueText() { result = generated.getValue() }
|
||||
|
||||
predicate isTrue() { this instanceof @token_true }
|
||||
|
||||
predicate isFalse() { this instanceof @token_false }
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: expand this. It's a minimal placeholder so we can test `=~` and `!~`.
|
||||
module RegexLiteral {
|
||||
class Range extends Literal::Range, @regex {
|
||||
final override Generated::Regex generated;
|
||||
|
||||
final override string getValueText() {
|
||||
result =
|
||||
concat(AstNode c, int i, string s |
|
||||
c = generated.getChild(i) and
|
||||
if c instanceof Generated::Token
|
||||
then s = c.(Generated::Token).getValue()
|
||||
else s = "#{...}"
|
||||
|
|
||||
s order by i
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
314
ql/src/codeql_ruby/ast/internal/Operation.qll
Normal file
314
ql/src/codeql_ruby/ast/internal/Operation.qll
Normal file
@@ -0,0 +1,314 @@
|
||||
import codeql_ruby.AST
|
||||
private import codeql_ruby.ast.internal.TreeSitter
|
||||
private import codeql_ruby.ast.internal.Expr
|
||||
|
||||
module Operation {
|
||||
abstract class Range extends Expr::Range {
|
||||
abstract string getOperator();
|
||||
|
||||
abstract Expr getAnOperand();
|
||||
}
|
||||
}
|
||||
|
||||
module UnaryOperation {
|
||||
abstract class Range extends Operation::Range, @unary {
|
||||
final override Generated::Unary generated;
|
||||
|
||||
final override string getOperator() { result = generated.getOperator() }
|
||||
|
||||
Expr getOperand() { result = generated.getOperand() }
|
||||
|
||||
final override Expr getAnOperand() { result = this.getOperand() }
|
||||
}
|
||||
}
|
||||
|
||||
module UnaryLogicalOperation {
|
||||
abstract class Range extends UnaryOperation::Range { }
|
||||
}
|
||||
|
||||
module UnaryArithmeticOperation {
|
||||
abstract class Range extends UnaryOperation::Range { }
|
||||
}
|
||||
|
||||
module UnaryBitwiseOperation {
|
||||
abstract class Range extends UnaryOperation::Range { }
|
||||
}
|
||||
|
||||
module NotExpr {
|
||||
class DbUnion = @unary_bang or @unary_not;
|
||||
|
||||
class Range extends UnaryLogicalOperation::Range, DbUnion { }
|
||||
}
|
||||
|
||||
module UnaryPlusExpr {
|
||||
class Range extends UnaryArithmeticOperation::Range, @unary_plus { }
|
||||
}
|
||||
|
||||
module UnaryMinusExpr {
|
||||
class Range extends UnaryArithmeticOperation::Range, @unary_minus { }
|
||||
}
|
||||
|
||||
module ComplementExpr {
|
||||
class Range extends UnaryBitwiseOperation::Range, @unary_tilde { }
|
||||
}
|
||||
|
||||
module DefinedExpr {
|
||||
class Range extends UnaryOperation::Range, @unary_definedquestion { }
|
||||
}
|
||||
|
||||
module BinaryOperation {
|
||||
abstract class Range extends Operation::Range, @binary {
|
||||
final override Generated::Binary generated;
|
||||
|
||||
final override string getOperator() { result = generated.getOperator() }
|
||||
|
||||
final Expr getLeftOperand() { result = generated.getLeft() }
|
||||
|
||||
final Expr getRightOperand() { result = generated.getRight() }
|
||||
|
||||
final override Expr getAnOperand() {
|
||||
result = this.getLeftOperand() or result = this.getRightOperand()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module BinaryArithmeticOperation {
|
||||
abstract class Range extends BinaryOperation::Range { }
|
||||
}
|
||||
|
||||
module BinaryLogicalOperation {
|
||||
abstract class Range extends BinaryOperation::Range { }
|
||||
}
|
||||
|
||||
module BinaryBitwiseOperation {
|
||||
abstract class Range extends BinaryOperation::Range { }
|
||||
}
|
||||
|
||||
module ComparisonOperation {
|
||||
abstract class Range extends BinaryOperation::Range, @binary { }
|
||||
}
|
||||
|
||||
module AddExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_plus { }
|
||||
}
|
||||
|
||||
module SubExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_minus { }
|
||||
}
|
||||
|
||||
module MulExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_star { }
|
||||
}
|
||||
|
||||
module DivExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_slash { }
|
||||
}
|
||||
|
||||
module ModuloExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_percent { }
|
||||
}
|
||||
|
||||
module ExponentExpr {
|
||||
class Range extends BinaryArithmeticOperation::Range, @binary_starstar { }
|
||||
}
|
||||
|
||||
module LogicalAndExpr {
|
||||
class DbUnion = @binary_and or @binary_ampersandampersand;
|
||||
|
||||
class Range extends BinaryLogicalOperation::Range, DbUnion { }
|
||||
}
|
||||
|
||||
module LogicalOrExpr {
|
||||
class DbUnion = @binary_or or @binary_pipepipe;
|
||||
|
||||
class Range extends BinaryLogicalOperation::Range, DbUnion { }
|
||||
}
|
||||
|
||||
module LShiftExpr {
|
||||
class Range extends BinaryBitwiseOperation::Range, @binary_langlelangle { }
|
||||
}
|
||||
|
||||
module RShiftExpr {
|
||||
class Range extends BinaryBitwiseOperation::Range, @binary_ranglerangle { }
|
||||
}
|
||||
|
||||
module BitwiseAndExpr {
|
||||
class Range extends BinaryBitwiseOperation::Range, @binary_ampersand { }
|
||||
}
|
||||
|
||||
module BitwiseOrExpr {
|
||||
class Range extends BinaryBitwiseOperation::Range, @binary_pipe { }
|
||||
}
|
||||
|
||||
module BitwiseXorExpr {
|
||||
class Range extends BinaryBitwiseOperation::Range, @binary_caret { }
|
||||
}
|
||||
|
||||
module EqualityOperation {
|
||||
abstract class Range extends ComparisonOperation::Range { }
|
||||
}
|
||||
|
||||
module EqExpr {
|
||||
class Range extends EqualityOperation::Range, @binary_equalequal { }
|
||||
}
|
||||
|
||||
module NEExpr {
|
||||
class Range extends EqualityOperation::Range, @binary_bangequal { }
|
||||
}
|
||||
|
||||
module CaseEqExpr {
|
||||
class Range extends EqualityOperation::Range, @binary_equalequalequal { }
|
||||
}
|
||||
|
||||
module RelationalOperation {
|
||||
abstract class Range extends ComparisonOperation::Range {
|
||||
abstract Expr getGreaterOperand();
|
||||
|
||||
abstract Expr getLesserOperand();
|
||||
}
|
||||
}
|
||||
|
||||
module GTExpr {
|
||||
class Range extends RelationalOperation::Range, @binary_rangle {
|
||||
final override Expr getGreaterOperand() { result = this.getLeftOperand() }
|
||||
|
||||
final override Expr getLesserOperand() { result = this.getRightOperand() }
|
||||
}
|
||||
}
|
||||
|
||||
module GEExpr {
|
||||
class Range extends RelationalOperation::Range, @binary_rangleequal {
|
||||
final override Expr getGreaterOperand() { result = this.getLeftOperand() }
|
||||
|
||||
final override Expr getLesserOperand() { result = this.getRightOperand() }
|
||||
}
|
||||
}
|
||||
|
||||
module LTExpr {
|
||||
class Range extends RelationalOperation::Range, @binary_langle {
|
||||
final override Expr getGreaterOperand() { result = this.getRightOperand() }
|
||||
|
||||
final override Expr getLesserOperand() { result = this.getLeftOperand() }
|
||||
}
|
||||
}
|
||||
|
||||
module LEExpr {
|
||||
class Range extends RelationalOperation::Range, @binary_langleequal {
|
||||
final override Expr getGreaterOperand() { result = this.getRightOperand() }
|
||||
|
||||
final override Expr getLesserOperand() { result = this.getLeftOperand() }
|
||||
}
|
||||
}
|
||||
|
||||
module SpaceshipExpr {
|
||||
class Range extends BinaryOperation::Range, @binary_langleequalrangle { }
|
||||
}
|
||||
|
||||
module RegexMatchExpr {
|
||||
class Range extends BinaryOperation::Range, @binary_equaltilde { }
|
||||
}
|
||||
|
||||
module NoRegexMatchExpr {
|
||||
class Range extends BinaryOperation::Range, @binary_bangtilde { }
|
||||
}
|
||||
|
||||
module Assignment {
|
||||
class DbUnion = @operator_assignment or @assignment;
|
||||
|
||||
abstract class Range extends Operation::Range, DbUnion {
|
||||
abstract Expr getLhs();
|
||||
|
||||
abstract Expr getRhs();
|
||||
|
||||
final override Expr getAnOperand() { result = this.getLhs() or result = this.getRhs() }
|
||||
}
|
||||
}
|
||||
|
||||
module AssignExpr {
|
||||
class Range extends Assignment::Range, @assignment {
|
||||
final override Generated::Assignment generated;
|
||||
|
||||
final override Expr getLhs() { result = generated.getLeft() }
|
||||
|
||||
final override Expr getRhs() { result = generated.getRight() }
|
||||
|
||||
final override string getOperator() { result = "=" }
|
||||
}
|
||||
}
|
||||
|
||||
module AssignOperation {
|
||||
abstract class Range extends Assignment::Range, @operator_assignment {
|
||||
final override Generated::OperatorAssignment generated;
|
||||
|
||||
final override string getOperator() { result = generated.getOperator() }
|
||||
|
||||
final override Expr getLhs() { result = generated.getLeft() }
|
||||
|
||||
final override Expr getRhs() { result = generated.getRight() }
|
||||
}
|
||||
}
|
||||
|
||||
module AssignArithmeticOperation {
|
||||
abstract class Range extends AssignOperation::Range { }
|
||||
}
|
||||
|
||||
module AssignLogicalOperation {
|
||||
abstract class Range extends AssignOperation::Range { }
|
||||
}
|
||||
|
||||
module AssignBitwiseOperation {
|
||||
abstract class Range extends AssignOperation::Range { }
|
||||
}
|
||||
|
||||
module AssignAddExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_plusequal { }
|
||||
}
|
||||
|
||||
module AssignSubExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_minusequal { }
|
||||
}
|
||||
|
||||
module AssignMulExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_starequal { }
|
||||
}
|
||||
|
||||
module AssignDivExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_slashequal { }
|
||||
}
|
||||
|
||||
module AssignExponentExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_starstarequal { }
|
||||
}
|
||||
|
||||
module AssignModuloExpr {
|
||||
class Range extends AssignArithmeticOperation::Range, @operator_assignment_percentequal { }
|
||||
}
|
||||
|
||||
module AssignLogicalAndExpr {
|
||||
class Range extends AssignLogicalOperation::Range, @operator_assignment_ampersandampersandequal {
|
||||
}
|
||||
}
|
||||
|
||||
module AssignLogicalOrExpr {
|
||||
class Range extends AssignLogicalOperation::Range, @operator_assignment_pipepipeequal { }
|
||||
}
|
||||
|
||||
module AssignLShiftExpr {
|
||||
class Range extends AssignBitwiseOperation::Range, @operator_assignment_langlelangleequal { }
|
||||
}
|
||||
|
||||
module AssignRShiftExpr {
|
||||
class Range extends AssignBitwiseOperation::Range, @operator_assignment_ranglerangleequal { }
|
||||
}
|
||||
|
||||
module AssignBitwiseAndExpr {
|
||||
class Range extends AssignBitwiseOperation::Range, @operator_assignment_ampersandequal { }
|
||||
}
|
||||
|
||||
module AssignBitwiseOrExpr {
|
||||
class Range extends AssignBitwiseOperation::Range, @operator_assignment_pipeequal { }
|
||||
}
|
||||
|
||||
module AssignBitwiseXorExpr {
|
||||
class Range extends AssignBitwiseOperation::Range, @operator_assignment_caretequal { }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
private import codeql_ruby.AST
|
||||
private import TreeSitter
|
||||
private import codeql.Locations
|
||||
private import codeql_ruby.AST
|
||||
private import codeql_ruby.ast.internal.Expr
|
||||
private import codeql_ruby.ast.internal.Pattern
|
||||
|
||||
private Generated::AstNode parent(Generated::AstNode n) {
|
||||
@@ -224,3 +225,22 @@ module LocalVariable {
|
||||
final override VariableScope getDeclaringScope() { result = scope }
|
||||
}
|
||||
}
|
||||
|
||||
module VariableAccess {
|
||||
class Range extends Expr::Range, @token_identifier {
|
||||
override Generated::Identifier generated;
|
||||
Variable variable;
|
||||
|
||||
Range() { access(this, variable) }
|
||||
|
||||
Variable getVariable() { result = variable }
|
||||
}
|
||||
}
|
||||
|
||||
module LocalVariableAccess {
|
||||
class Range extends VariableAccess::Range {
|
||||
override LocalVariable variable;
|
||||
|
||||
override LocalVariable getVariable() { result = variable }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user