|
| 1 | +import ql |
| 2 | + |
| 3 | +/** |
| 4 | + * Gets a class where the charpred has an `this instanceof type` expression. |
| 5 | + */ |
| 6 | +predicate instanceofThisInCharPred(Class c, Type type) { |
| 7 | + exists(InstanceOf instanceOf | |
| 8 | + instanceOf = c.getCharPred().getBody() |
| 9 | + or |
| 10 | + exists(Conjunction conj | |
| 11 | + conj = c.getCharPred().getBody() and |
| 12 | + instanceOf = conj.getAnOperand() |
| 13 | + ) |
| 14 | + | |
| 15 | + instanceOf.getExpr() instanceof ThisAccess and |
| 16 | + type = instanceOf.getType().getResolvedType() |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Holds if `c` uses the casting based range pattern, which could be replaced with `instanceof type`. |
| 22 | + */ |
| 23 | +predicate usesCastingBasedInstanceof(Class c, Type type) { |
| 24 | + instanceofThisInCharPred(c, type) and |
| 25 | + // require that there is a call to the range class that matches the name of the enclosing predicate |
| 26 | + exists(InlineCast cast, MemberCall call | |
| 27 | + cast = getAThisCast(c, type) and |
| 28 | + call.getBase() = cast and |
| 29 | + cast.getEnclosingPredicate().getName() = call.getMemberName() |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +/** Gets an inline cast that cases `this` to `type` inside a class predicate for `c`. */ |
| 34 | +InlineCast getAThisCast(Class c, Type type) { |
| 35 | + exists(MemberCall call | |
| 36 | + call.getEnclosingPredicate() = c.getAClassPredicate() and |
| 37 | + result = call.getBase() and |
| 38 | + result.getBase() instanceof ThisAccess and |
| 39 | + result.getTypeExpr().getResolvedType() = type |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +predicate usesFieldBasedInstanceof(Class c, TypeExpr type, VarDecl field, ComparisonFormula comp) { |
| 44 | + exists(FieldAccess fieldAccess | |
| 45 | + c.getCharPred().getBody() = comp or |
| 46 | + c.getCharPred().getBody().(Conjunction).getAnOperand() = comp |
| 47 | + | |
| 48 | + comp.getOperator() = "=" and |
| 49 | + comp.getEnclosingPredicate() = c.getCharPred() and |
| 50 | + comp.getAnOperand() instanceof ThisAccess and |
| 51 | + comp.getAnOperand() = fieldAccess and |
| 52 | + fieldAccess.getDeclaration() = field and |
| 53 | + field.getTypeExpr() = type |
| 54 | + ) and |
| 55 | + // require that there is a call to the range field that matches the name of the enclosing predicate |
| 56 | + exists(FieldAccess access, MemberCall call | |
| 57 | + access = getARangeFieldAccess(c, field, _) and |
| 58 | + call.getBase() = access and |
| 59 | + access.getEnclosingPredicate().getName() = call.getMemberName() |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +FieldAccess getARangeFieldAccess(Class c, VarDecl field, string name) { |
| 64 | + exists(MemberCall call | |
| 65 | + result = call.getBase() and |
| 66 | + result.getDeclaration() = field and |
| 67 | + name = call.getMemberName() and |
| 68 | + call.getEnclosingPredicate().(ClassPredicate).getParent() = c |
| 69 | + ) |
| 70 | +} |
0 commit comments