|
| 1 | +/** |
| 2 | + * Provides support for special methods. |
| 3 | + * This is done in two steps: |
| 4 | + * - A subset of `ControlFlowNode`s are labelled as potentially corresponding to |
| 5 | + * a special method call (by being an instance of `SpecialMethod::Potential`). |
| 6 | + * - A subset of the potential special method calls are labelled as being actual |
| 7 | + * special method calls (`SpecialMethodCallNode`) if the appropriate method is defined. |
| 8 | + * Extend `SpecialMethod::Potential` to capture more cases. |
| 9 | + */ |
| 10 | + |
| 11 | +import python |
| 12 | + |
| 13 | + |
| 14 | +/** A control flow node which might correpsond to a special method call. */ |
| 15 | +class PotentialSpecialMethodCallNode extends ControlFlowNode { |
| 16 | + PotentialSpecialMethodCallNode() { this instanceof SpecialMethod::Potential} |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Machinery for detecting special method calls. |
| 21 | + * Extend `SpecialMethod::Potential` to capture more cases. |
| 22 | + */ |
| 23 | +module SpecialMethod { |
| 24 | + /** A control flow node which might correpsond to a special method call. */ |
| 25 | + abstract class Potential extends ControlFlowNode { |
| 26 | + /** Gets the name of the method that would be called. */ |
| 27 | + abstract string getSpecialMethodName(); |
| 28 | + |
| 29 | + /** Gets the controlflow node that would be passed as the specified argument. */ |
| 30 | + abstract ControlFlowNode getArg(int n); |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets the control flow node corresponding to the instance |
| 34 | + * that would define the special method. |
| 35 | + */ |
| 36 | + ControlFlowNode getSelf() { result = this.getArg(0) } |
| 37 | + } |
| 38 | + |
| 39 | + /** A binary expression node that might correspond to a special method call. */ |
| 40 | + class SpecialBinOp extends Potential, BinaryExprNode { |
| 41 | + Operator operator; |
| 42 | + |
| 43 | + SpecialBinOp() { this.getOp() = operator } |
| 44 | + |
| 45 | + override string getSpecialMethodName() { result = operator.getSpecialMethodName() } |
| 46 | + |
| 47 | + override ControlFlowNode getArg(int n) { |
| 48 | + n = 0 and result = this.getLeft() |
| 49 | + or |
| 50 | + n = 1 and result = this.getRight() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** A subscript expression node that might correspond to a special method call. */ |
| 55 | + abstract class SpecialSubscript extends Potential, SubscriptNode { |
| 56 | + override ControlFlowNode getArg(int n) { |
| 57 | + n = 0 and result = this.getObject() |
| 58 | + or |
| 59 | + n = 1 and result = this.getIndex() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** A subscript expression node that might correspond to a call to __getitem__. */ |
| 64 | + class SpecialGetItem extends SpecialSubscript { |
| 65 | + SpecialGetItem() { this.isLoad() } |
| 66 | + |
| 67 | + override string getSpecialMethodName() { result = "__getitem__" } |
| 68 | + } |
| 69 | + |
| 70 | + /** A subscript expression node that might correspond to a call to __setitem__. */ |
| 71 | + class SpecialSetItem extends SpecialSubscript { |
| 72 | + SpecialSetItem() { this.isStore() } |
| 73 | + |
| 74 | + override string getSpecialMethodName() { result = "__setitem__" } |
| 75 | + |
| 76 | + override ControlFlowNode getArg(int n) { |
| 77 | + n = 0 and result = this.getObject() |
| 78 | + or |
| 79 | + n = 1 and result = this.getIndex() |
| 80 | + or |
| 81 | + n = 2 and result = this.getValueNode() |
| 82 | + } |
| 83 | + |
| 84 | + private ControlFlowNode getValueNode() { |
| 85 | + exists(AssignStmt a | |
| 86 | + a.getATarget() = this.getNode() and |
| 87 | + result.getNode() = a.getValue() |
| 88 | + ) |
| 89 | + or |
| 90 | + exists(AugAssign a | |
| 91 | + a.getTarget() = this.getNode() and |
| 92 | + result.getNode() = a.getValue() |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** A subscript expression node that might correspond to a call to __delitem__. */ |
| 98 | + class SpecialDelItem extends SpecialSubscript { |
| 99 | + SpecialDelItem() { this.isDelete() } |
| 100 | + |
| 101 | + override string getSpecialMethodName() { result = "__delitem__" } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class SpecialMethodCallNode extends PotentialSpecialMethodCallNode { |
| 106 | + Value resolvedSpecialMethod; |
| 107 | + |
| 108 | + SpecialMethodCallNode() { |
| 109 | + exists(SpecialMethod::Potential pot | |
| 110 | + this.(SpecialMethod::Potential) = pot and |
| 111 | + pot.getSelf().pointsTo().getClass().lookup(pot.getSpecialMethodName()) = resolvedSpecialMethod |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + /** The method that is called. */ |
| 116 | + Value getResolvedSpecialMethod() { result = resolvedSpecialMethod } |
| 117 | +} |
0 commit comments