mirror of
https://github.com/github/codeql.git
synced 2025-12-20 10:46:30 +01:00
Python: Start scaffold for magic methods
This commit is contained in:
36
python/ql/src/semmle/python/Magic.qll
Normal file
36
python/ql/src/semmle/python/Magic.qll
Normal file
@@ -0,0 +1,36 @@
|
||||
import python
|
||||
|
||||
module MagicMethod {
|
||||
abstract class Potential extends ControlFlowNode {
|
||||
abstract string getMagicMethodName();
|
||||
abstract ControlFlowNode getArg(int n);
|
||||
ControlFlowNode getSelf() { result = this.getArg(1) }
|
||||
}
|
||||
|
||||
class Actual extends ControlFlowNode {
|
||||
Object resolvedMagicMethod;
|
||||
|
||||
Actual() {
|
||||
exists(Potential pot |
|
||||
this.(Potential) = pot and
|
||||
pot.getSelf().(ClassObject).lookupAttribute(pot.getMagicMethodName()) = resolvedMagicMethod
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MagicBinOp extends MagicMethod::Potential, BinaryExprNode {
|
||||
Operator operator;
|
||||
|
||||
MagicBinOp() { this.getOp() = operator}
|
||||
|
||||
override string getMagicMethodName() {
|
||||
result = operator.getSpecialMethodName()
|
||||
}
|
||||
|
||||
override ControlFlowNode getArg(int n) {
|
||||
n = 1 and result = this.getLeft()
|
||||
or
|
||||
n = 2 and result = this.getRight()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user