|
| 1 | +private import RangeAnalysisStage |
| 2 | +private import RangeAnalysisSpecific |
| 3 | +private import experimental.semmle.code.cpp.semantic.analysis.FloatDelta |
| 4 | +private import RangeUtils |
| 5 | +private import experimental.semmle.code.cpp.semantic.SemanticBound as SemanticBound |
| 6 | +private import experimental.semmle.code.cpp.semantic.SemanticLocation |
| 7 | +private import experimental.semmle.code.cpp.semantic.SemanticSSA |
| 8 | + |
| 9 | +module ConstantBounds implements BoundSig<FloatDelta> { |
| 10 | + class SemBound instanceof SemanticBound::SemBound { |
| 11 | + SemBound() { |
| 12 | + this instanceof SemanticBound::SemZeroBound |
| 13 | + or |
| 14 | + this.(SemanticBound::SemSsaBound).getAVariable() instanceof SemSsaPhiNode |
| 15 | + } |
| 16 | + |
| 17 | + string toString() { result = super.toString() } |
| 18 | + |
| 19 | + SemLocation getLocation() { result = super.getLocation() } |
| 20 | + |
| 21 | + SemExpr getExpr(float delta) { result = super.getExpr(delta) } |
| 22 | + } |
| 23 | + |
| 24 | + class SemZeroBound extends SemBound instanceof SemanticBound::SemZeroBound { } |
| 25 | + |
| 26 | + class SemSsaBound extends SemBound instanceof SemanticBound::SemSsaBound { |
| 27 | + SemSsaVariable getAVariable() { result = this.(SemanticBound::SemSsaBound).getAVariable() } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +private module RelativeBounds implements BoundSig<FloatDelta> { |
| 32 | + class SemBound instanceof SemanticBound::SemBound { |
| 33 | + SemBound() { not this instanceof SemanticBound::SemZeroBound } |
| 34 | + |
| 35 | + string toString() { result = super.toString() } |
| 36 | + |
| 37 | + SemLocation getLocation() { result = super.getLocation() } |
| 38 | + |
| 39 | + SemExpr getExpr(float delta) { result = super.getExpr(delta) } |
| 40 | + } |
| 41 | + |
| 42 | + class SemZeroBound extends SemBound instanceof SemanticBound::SemZeroBound { } |
| 43 | + |
| 44 | + class SemSsaBound extends SemBound instanceof SemanticBound::SemSsaBound { |
| 45 | + SemSsaVariable getAVariable() { result = this.(SemanticBound::SemSsaBound).getAVariable() } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +private module ConstantStage = |
| 50 | + RangeStage<FloatDelta, ConstantBounds, CppLangImpl, RangeUtil<FloatDelta, CppLangImpl>>; |
| 51 | + |
| 52 | +private module RelativeStage = |
| 53 | + RangeStage<FloatDelta, RelativeBounds, CppLangImpl, RangeUtil<FloatDelta, CppLangImpl>>; |
| 54 | + |
| 55 | +private newtype TSemReason = |
| 56 | + TSemNoReason() or |
| 57 | + TSemCondReason(SemGuard guard) { |
| 58 | + guard = any(ConstantStage::SemCondReason reason).getCond() |
| 59 | + or |
| 60 | + guard = any(RelativeStage::SemCondReason reason).getCond() |
| 61 | + } |
| 62 | + |
| 63 | +/** |
| 64 | + * A reason for an inferred bound. This can either be `CondReason` if the bound |
| 65 | + * is due to a specific condition, or `NoReason` if the bound is inferred |
| 66 | + * without going through a bounding condition. |
| 67 | + */ |
| 68 | +abstract class SemReason extends TSemReason { |
| 69 | + /** Gets a textual representation of this reason. */ |
| 70 | + abstract string toString(); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * A reason for an inferred bound that indicates that the bound is inferred |
| 75 | + * without going through a bounding condition. |
| 76 | + */ |
| 77 | +class SemNoReason extends SemReason, TSemNoReason { |
| 78 | + override string toString() { result = "NoReason" } |
| 79 | +} |
| 80 | + |
| 81 | +/** A reason for an inferred bound pointing to a condition. */ |
| 82 | +class SemCondReason extends SemReason, TSemCondReason { |
| 83 | + /** Gets the condition that is the reason for the bound. */ |
| 84 | + SemGuard getCond() { this = TSemCondReason(result) } |
| 85 | + |
| 86 | + override string toString() { result = getCond().toString() } |
| 87 | +} |
| 88 | + |
| 89 | +private ConstantStage::SemReason constantReason(SemReason reason) { |
| 90 | + result instanceof ConstantStage::SemNoReason and reason instanceof SemNoReason |
| 91 | + or |
| 92 | + result.(ConstantStage::SemCondReason).getCond() = reason.(SemCondReason).getCond() |
| 93 | +} |
| 94 | + |
| 95 | +private RelativeStage::SemReason relativeReason(SemReason reason) { |
| 96 | + result instanceof RelativeStage::SemNoReason and reason instanceof SemNoReason |
| 97 | + or |
| 98 | + result.(RelativeStage::SemCondReason).getCond() = reason.(SemCondReason).getCond() |
| 99 | +} |
| 100 | + |
| 101 | +predicate semBounded( |
| 102 | + SemExpr e, SemanticBound::SemBound b, float delta, boolean upper, SemReason reason |
| 103 | +) { |
| 104 | + ConstantStage::semBounded(e, b, delta, upper, constantReason(reason)) |
| 105 | + or |
| 106 | + RelativeStage::semBounded(e, b, delta, upper, relativeReason(reason)) |
| 107 | +} |
0 commit comments