mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #11156 from atorralba/atorralba/swift/bitwise-operation
Swift: Add `BitwiseOperation.qll`
This commit is contained in:
108
swift/ql/lib/codeql/swift/elements/expr/BitwiseOperation.qll
Normal file
108
swift/ql/lib/codeql/swift/elements/expr/BitwiseOperation.qll
Normal file
@@ -0,0 +1,108 @@
|
||||
private import codeql.swift.elements.expr.BinaryExpr
|
||||
private import codeql.swift.elements.expr.Expr
|
||||
private import codeql.swift.elements.expr.PrefixUnaryExpr
|
||||
|
||||
/**
|
||||
* A bitwise operation, such as:
|
||||
* ```
|
||||
* a & b
|
||||
* ```
|
||||
*/
|
||||
class BitwiseOperation extends Expr {
|
||||
BitwiseOperation() {
|
||||
this instanceof BinaryBitwiseOperation or
|
||||
this instanceof UnaryBitwiseOperation
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an operand of this bitwise operation.
|
||||
*/
|
||||
Expr getAnOperand() {
|
||||
result =
|
||||
[this.(BinaryBitwiseOperation).getAnOperand(), this.(UnaryBitwiseOperation).getOperand()]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A binary bitwise operation, such as:
|
||||
* ```
|
||||
* a & b
|
||||
* ```
|
||||
*/
|
||||
class BinaryBitwiseOperation extends BinaryExpr {
|
||||
BinaryBitwiseOperation() {
|
||||
this instanceof AndBitwiseExpr or
|
||||
this instanceof OrBitwiseExpr or
|
||||
this instanceof XorBitwiseExpr or
|
||||
this instanceof ShiftLeftBitwiseExpr or
|
||||
this instanceof ShiftRightBitwiseExpr
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise AND expression.
|
||||
* ```
|
||||
* a & b
|
||||
* ```
|
||||
*/
|
||||
class AndBitwiseExpr extends BinaryExpr {
|
||||
AndBitwiseExpr() { this.getStaticTarget().getName() = "&(_:_:)" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise OR expression.
|
||||
* ```
|
||||
* a | b
|
||||
* ```
|
||||
*/
|
||||
class OrBitwiseExpr extends BinaryExpr {
|
||||
OrBitwiseExpr() { this.getStaticTarget().getName() = "|(_:_:)" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise XOR expression.
|
||||
* ```
|
||||
* a ^ b
|
||||
* ```
|
||||
*/
|
||||
class XorBitwiseExpr extends BinaryExpr {
|
||||
XorBitwiseExpr() { this.getStaticTarget().getName() = "^(_:_:)" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise shift left expression.
|
||||
* ```
|
||||
* a << b
|
||||
* ```
|
||||
*/
|
||||
class ShiftLeftBitwiseExpr extends BinaryExpr {
|
||||
ShiftLeftBitwiseExpr() { this.getStaticTarget().getName() = "<<(_:_:)" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A bitwise shift right expression.
|
||||
* ```
|
||||
* a >> b
|
||||
* ```
|
||||
*/
|
||||
class ShiftRightBitwiseExpr extends BinaryExpr {
|
||||
ShiftRightBitwiseExpr() { this.getStaticTarget().getName() = ">>(_:_:)" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A unary bitwise operation, such as:
|
||||
* ```
|
||||
* ~a
|
||||
* ```
|
||||
*/
|
||||
class UnaryBitwiseOperation extends PrefixUnaryExpr instanceof NotBitwiseExpr { }
|
||||
|
||||
/**
|
||||
* A bitwise NOT expression.
|
||||
* ```
|
||||
* ~a
|
||||
* ```
|
||||
*/
|
||||
class NotBitwiseExpr extends PrefixUnaryExpr {
|
||||
NotBitwiseExpr() { this.getStaticTarget().getName() = "~(_:)" }
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import codeql.swift.elements
|
||||
import codeql.swift.elements.expr.ArithmeticOperation
|
||||
import codeql.swift.elements.expr.BitwiseOperation
|
||||
import codeql.swift.elements.expr.LogicalOperation
|
||||
import codeql.swift.elements.decl.MethodDecl
|
||||
import codeql.swift.elements.decl.ClassOrStructDecl
|
||||
|
||||
@@ -256,3 +256,17 @@
|
||||
| expressions.swift:161:9:161:9 | .^(_:_:) | MethodRefExpr |
|
||||
| expressions.swift:161:9:161:9 | Int.Type | TypeExpr |
|
||||
| expressions.swift:161:11:161:11 | 2 | IntegerLiteralExpr |
|
||||
| expressions.swift:162:3:162:3 | _ | DiscardAssignmentExpr |
|
||||
| expressions.swift:162:3:162:12 | ... = ... | AssignExpr |
|
||||
| expressions.swift:162:7:162:7 | 1 | IntegerLiteralExpr |
|
||||
| expressions.swift:162:7:162:12 | ... .<<(_:_:) ... | BinaryExpr |
|
||||
| expressions.swift:162:9:162:9 | .<<(_:_:) | MethodRefExpr |
|
||||
| expressions.swift:162:9:162:9 | Int.Type | TypeExpr |
|
||||
| expressions.swift:162:12:162:12 | 0 | IntegerLiteralExpr |
|
||||
| expressions.swift:163:3:163:3 | _ | DiscardAssignmentExpr |
|
||||
| expressions.swift:163:3:163:12 | ... = ... | AssignExpr |
|
||||
| expressions.swift:163:7:163:7 | 1 | IntegerLiteralExpr |
|
||||
| expressions.swift:163:7:163:12 | ... .>>(_:_:) ... | BinaryExpr |
|
||||
| expressions.swift:163:9:163:9 | .>>(_:_:) | MethodRefExpr |
|
||||
| expressions.swift:163:9:163:9 | Int.Type | TypeExpr |
|
||||
| expressions.swift:163:12:163:12 | 0 | IntegerLiteralExpr |
|
||||
|
||||
@@ -159,4 +159,6 @@ func bitwise() {
|
||||
_ = 1 & 2
|
||||
_ = 1 | 2
|
||||
_ = 1 ^ 2
|
||||
_ = 1 << 0
|
||||
_ = 1 >> 0
|
||||
}
|
||||
|
||||
@@ -4379,6 +4379,26 @@ expressions.swift:
|
||||
# 161| getExpr(): [IntegerLiteralExpr] 1
|
||||
# 161| getArgument(1): [Argument] : 2
|
||||
# 161| getExpr(): [IntegerLiteralExpr] 2
|
||||
# 162| getElement(4): [AssignExpr] ... = ...
|
||||
# 162| getDest(): [DiscardAssignmentExpr] _
|
||||
# 162| getSource(): [BinaryExpr] ... .<<(_:_:) ...
|
||||
# 162| getFunction(): [MethodRefExpr] .<<(_:_:)
|
||||
# 162| getBase(): [TypeExpr] Int.Type
|
||||
# 162| getTypeRepr(): [TypeRepr] Int
|
||||
# 162| getArgument(0): [Argument] : 1
|
||||
# 162| getExpr(): [IntegerLiteralExpr] 1
|
||||
# 162| getArgument(1): [Argument] : 0
|
||||
# 162| getExpr(): [IntegerLiteralExpr] 0
|
||||
# 163| getElement(5): [AssignExpr] ... = ...
|
||||
# 163| getDest(): [DiscardAssignmentExpr] _
|
||||
# 163| getSource(): [BinaryExpr] ... .>>(_:_:) ...
|
||||
# 163| getFunction(): [MethodRefExpr] .>>(_:_:)
|
||||
# 163| getBase(): [TypeExpr] Int.Type
|
||||
# 163| getTypeRepr(): [TypeRepr] Int
|
||||
# 163| getArgument(0): [Argument] : 1
|
||||
# 163| getExpr(): [IntegerLiteralExpr] 1
|
||||
# 163| getArgument(1): [Argument] : 0
|
||||
# 163| getExpr(): [IntegerLiteralExpr] 0
|
||||
patterns.swift:
|
||||
# 1| [ConcreteFuncDecl] basic_patterns()
|
||||
# 1| InterfaceType = () -> ()
|
||||
|
||||
@@ -159,4 +159,6 @@ func bitwise() {
|
||||
_ = 1 & 2
|
||||
_ = 1 | 2
|
||||
_ = 1 ^ 2
|
||||
_ = 1 << 0
|
||||
_ = 1 >> 0
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
| bitwiseoperation.swift:2:7:2:8 | call to ~(_:) | NotBitwiseExpr, UnaryBitwiseOperation |
|
||||
| bitwiseoperation.swift:3:7:3:11 | ... .&(_:_:) ... | AndBitwiseExpr, BinaryBitwiseOperation |
|
||||
| bitwiseoperation.swift:4:7:4:11 | ... .\|(_:_:) ... | BinaryBitwiseOperation, OrBitwiseExpr |
|
||||
| bitwiseoperation.swift:5:7:5:11 | ... .^(_:_:) ... | BinaryBitwiseOperation, XorBitwiseExpr |
|
||||
| bitwiseoperation.swift:6:7:6:12 | ... .<<(_:_:) ... | BinaryBitwiseOperation, ShiftLeftBitwiseExpr |
|
||||
| bitwiseoperation.swift:7:7:7:12 | ... .>>(_:_:) ... | BinaryBitwiseOperation, ShiftRightBitwiseExpr |
|
||||
@@ -0,0 +1,22 @@
|
||||
import swift
|
||||
|
||||
string describe(BitwiseOperation e) {
|
||||
e instanceof BinaryBitwiseOperation and result = "BinaryBitwiseOperation"
|
||||
or
|
||||
e instanceof AndBitwiseExpr and result = "AndBitwiseExpr"
|
||||
or
|
||||
e instanceof OrBitwiseExpr and result = "OrBitwiseExpr"
|
||||
or
|
||||
e instanceof XorBitwiseExpr and result = "XorBitwiseExpr"
|
||||
or
|
||||
e instanceof ShiftLeftBitwiseExpr and result = "ShiftLeftBitwiseExpr"
|
||||
or
|
||||
e instanceof ShiftRightBitwiseExpr and result = "ShiftRightBitwiseExpr"
|
||||
or
|
||||
e instanceof UnaryBitwiseOperation and result = "UnaryBitwiseOperation"
|
||||
or
|
||||
e instanceof NotBitwiseExpr and result = "NotBitwiseExpr"
|
||||
}
|
||||
|
||||
from BitwiseOperation e
|
||||
select e, concat(describe(e), ", ")
|
||||
@@ -0,0 +1,8 @@
|
||||
func bitwise() {
|
||||
_ = ~1
|
||||
_ = 1 & 2
|
||||
_ = 1 | 2
|
||||
_ = 1 ^ 2
|
||||
_ = 1 << 0
|
||||
_ = 1 >> 0
|
||||
}
|
||||
Reference in New Issue
Block a user