|
| 1 | +/** |
| 2 | + * Provides classes for representing abstract bounds for use in, for example, range analysis. |
| 3 | + */ |
| 4 | + |
| 5 | +private import internal.rangeanalysis.BoundSpecific |
| 6 | + |
| 7 | +private newtype TBound = |
| 8 | + TBoundZero() or |
| 9 | + TBoundSsa(SsaVariable v) { v.getSourceVariable().getType() instanceof IntegralType } or |
| 10 | + TBoundExpr(Expr e) { |
| 11 | + interestingExprBound(e) and |
| 12 | + not exists(SsaVariable v | e = v.getAUse()) |
| 13 | + } |
| 14 | + |
| 15 | +/** |
| 16 | + * A bound that may be inferred for an expression plus/minus an integer delta. |
| 17 | + */ |
| 18 | +abstract class Bound extends TBound { |
| 19 | + /** Gets a textual representation of this bound. */ |
| 20 | + abstract string toString(); |
| 21 | + |
| 22 | + /** Gets an expression that equals this bound plus `delta`. */ |
| 23 | + abstract Expr getExpr(int delta); |
| 24 | + |
| 25 | + /** Gets an expression that equals this bound. */ |
| 26 | + Expr getExpr() { result = getExpr(0) } |
| 27 | + |
| 28 | + /** |
| 29 | + * Holds if this element is at the specified location. |
| 30 | + * The location spans column `sc` of line `sl` to |
| 31 | + * column `ec` of line `el` in file `path`. |
| 32 | + * For more information, see |
| 33 | + * [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html). |
| 34 | + */ |
| 35 | + predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { |
| 36 | + path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0 |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * The bound that corresponds to the integer 0. This is used to represent all |
| 42 | + * integer bounds as bounds are always accompanied by an added integer delta. |
| 43 | + */ |
| 44 | +class ZeroBound extends Bound, TBoundZero { |
| 45 | + override string toString() { result = "0" } |
| 46 | + |
| 47 | + override Expr getExpr(int delta) { result.(ConstantIntegerExpr).getIntValue() = delta } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A bound corresponding to the value of an SSA variable. |
| 52 | + */ |
| 53 | +class SsaBound extends Bound, TBoundSsa { |
| 54 | + /** Gets the SSA variable that equals this bound. */ |
| 55 | + SsaVariable getSsa() { this = TBoundSsa(result) } |
| 56 | + |
| 57 | + override string toString() { result = getSsa().toString() } |
| 58 | + |
| 59 | + override Expr getExpr(int delta) { result = getSsa().getAUse() and delta = 0 } |
| 60 | + |
| 61 | + override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { |
| 62 | + getSsa().getLocation().hasLocationInfo(path, sl, sc, el, ec) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * A bound that corresponds to the value of a specific expression that might be |
| 68 | + * interesting, but isn't otherwise represented by the value of an SSA variable. |
| 69 | + */ |
| 70 | +class ExprBound extends Bound, TBoundExpr { |
| 71 | + override string toString() { result = getExpr().toString() } |
| 72 | + |
| 73 | + override Expr getExpr(int delta) { this = TBoundExpr(result) and delta = 0 } |
| 74 | + |
| 75 | + override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { |
| 76 | + getExpr().getLocation().hasLocationInfo(path, sl, sc, el, ec) |
| 77 | + } |
| 78 | +} |
0 commit comments