19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/ADT/ImmutableSet.h" 21 #include "llvm/Support/raw_ostream.h" 23 using namespace clang;
26 void RangeSet::IntersectInRange(BasicValueFactory &BV, Factory &F,
27 const llvm::APSInt &Lower,
const llvm::APSInt &Upper,
28 PrimRangeSet &newRanges, PrimRangeSet::iterator &i,
29 PrimRangeSet::iterator &e)
const {
39 if (i->To() < Lower) {
42 if (i->From() > Upper) {
46 if (i->Includes(Lower)) {
47 if (i->Includes(Upper)) {
49 F.add(newRanges, Range(BV.getValue(Lower), BV.getValue(Upper)));
52 newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
54 if (i->Includes(Upper)) {
55 newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
58 newRanges = F.add(newRanges, *i);
63 const llvm::APSInt &RangeSet::getMinValue()
const {
65 return ranges.begin()->From();
68 bool RangeSet::pin(llvm::APSInt &Lower, llvm::APSInt &Upper)
const {
74 APSIntType
Type(getMinValue());
88 Lower =
Type.getMinValue();
89 Upper =
Type.getMaxValue();
93 Lower =
Type.getMinValue();
98 Lower =
Type.getMinValue();
99 Upper =
Type.getMaxValue();
108 Upper =
Type.getMaxValue();
118 Upper =
Type.getMaxValue();
129 Lower =
Type.getMinValue();
139 Lower =
Type.getMinValue();
140 Upper =
Type.getMaxValue();
157 llvm::APSInt Lower, llvm::APSInt Upper)
const {
158 if (!pin(Lower, Upper))
159 return F.getEmptySet();
161 PrimRangeSet newRanges = F.getEmptySet();
163 PrimRangeSet::iterator i =
begin(), e =
end();
165 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
170 IntersectInRange(BV, F, BV.
getMinValue(Upper), Upper, newRanges, i, e);
171 IntersectInRange(BV, F, Lower, BV.
getMaxValue(Lower), newRanges, i, e);
181 PrimRangeSet newRanges = F.getEmptySet();
184 const llvm::APSInt &from = i->From(), &to = i->To();
185 const llvm::APSInt &newTo = (from.isMinSignedValue() ?
187 BV.getValue(- from));
188 if (to.isMaxSignedValue() && !newRanges.isEmpty() &&
189 newRanges.begin()->From().isMinSignedValue()) {
190 assert(newRanges.begin()->To().isMinSignedValue() &&
191 "Ranges should not overlap");
192 assert(!from.isMinSignedValue() &&
"Ranges should not overlap");
193 const llvm::APSInt &newFrom = newRanges.
begin()->From();
195 F.add(F.remove(newRanges, *newRanges.begin()),
Range(newFrom, newTo));
196 }
else if (!to.isMinSignedValue()) {
197 const llvm::APSInt &newFrom = BV.getValue(- to);
198 newRanges = F.add(newRanges,
Range(newFrom, newTo));
200 if (from.isMinSignedValue()) {
218 os <<
'[' << i->From().toString(10) <<
", " << i->To().toString(10)
234 bool canReasonAbout(
SVal X)
const override;
245 const char *sep)
override;
252 const llvm::APSInt &V,
253 const llvm::APSInt &Adjustment)
override;
256 const llvm::APSInt &V,
257 const llvm::APSInt &Adjustment)
override;
260 const llvm::APSInt &V,
261 const llvm::APSInt &Adjustment)
override;
264 const llvm::APSInt &V,
265 const llvm::APSInt &Adjustment)
override;
268 const llvm::APSInt &V,
269 const llvm::APSInt &Adjustment)
override;
272 const llvm::APSInt &V,
273 const llvm::APSInt &Adjustment)
override;
277 const llvm::APSInt &To,
const llvm::APSInt &Adjustment)
override;
281 const llvm::APSInt &To,
const llvm::APSInt &Adjustment)
override;
291 const llvm::APSInt &Int,
292 const llvm::APSInt &Adjustment);
294 const llvm::APSInt &Int,
295 const llvm::APSInt &Adjustment);
297 const llvm::APSInt &Int,
298 const llvm::APSInt &Adjustment);
300 const llvm::APSInt &Int,
301 const llvm::APSInt &Adjustment);
303 const llvm::APSInt &Int,
304 const llvm::APSInt &Adjustment);
310 std::unique_ptr<ConstraintManager>
312 return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.
getSValBuilder());
315 bool RangeConstraintManager::canReasonAbout(
SVal X)
const {
317 if (SymVal && SymVal->isExpression()) {
318 const SymExpr *SE = SymVal->getSymbol();
320 if (
const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
321 switch (SIE->getOpcode()) {
341 if (
const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
385 const llvm::APSInt *RangeConstraintManager::getSymVal(
ProgramStateRef St,
388 return T ? T->getConcreteValue() :
nullptr;
396 bool Changed =
false;
398 ConstraintRangeTy::Factory &CRFactory = State->get_context<
ConstraintRange>();
400 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
402 if (SymReaper.
isDead(Sym)) {
404 CR = CRFactory.remove(CR, Sym);
413 BasicValueFactory &BV,
417 APSIntType IntType = BV.getAPSIntType(Sym->getType());
418 return Domain.Intersect(BV, F, ++IntType.getZeroValue(),
419 --IntType.getZeroValue());
431 BasicValueFactory &BV,
434 const SymIntExpr* SIE) {
437 const llvm::APSInt &RHS = SIE->getRHS();
438 const llvm::APSInt &Zero = BV.getAPSIntType(T).getZeroValue();
442 if (Operator == BO_Or && IsUnsigned)
443 return Input.Intersect(BV, F, RHS, BV.getMaxValue(T));
446 if (Operator == BO_Or && RHS != Zero)
452 if (Operator == BO_And && (IsUnsigned || RHS >= Zero))
453 return Input.Intersect(BV, F, BV.getMinValue(T), RHS);
460 if (ConstraintRangeTy::data_type *V = State->get<ConstraintRange>(Sym))
463 BasicValueFactory &BV = getBasicVals();
467 if (
const RangeSet *R = getRangeForMinusSymbol(State, Sym))
468 return R->Negate(BV, F);
474 RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
481 if (
const SymIntExpr* SIE = dyn_cast<SymIntExpr>(Sym))
495 if (
const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
496 if (SSE->getOpcode() == BO_Sub) {
498 SymbolManager &SymMgr = State->getSymbolManager();
499 SymbolRef negSym = SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub,
501 if (
const RangeSet *negV = State->get<ConstraintRange>(negSym)) {
503 if ((negV->getConcreteValue() &&
504 (*negV->getConcreteValue() == 0)) ||
527 const llvm::APSInt &Int,
528 const llvm::APSInt &Adjustment) {
530 APSIntType AdjustmentType(Adjustment);
534 llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
535 llvm::APSInt Upper = Lower;
541 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
542 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
547 const llvm::APSInt &Int,
548 const llvm::APSInt &Adjustment) {
550 APSIntType AdjustmentType(Adjustment);
555 llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
556 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
557 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
562 const llvm::APSInt &Int,
563 const llvm::APSInt &Adjustment) {
565 APSIntType AdjustmentType(Adjustment);
566 switch (AdjustmentType.testInRange(Int,
true)) {
568 return F.getEmptySet();
572 return getRange(St, Sym);
576 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
577 llvm::APSInt Min = AdjustmentType.getMinValue();
578 if (ComparisonVal == Min)
579 return F.getEmptySet();
581 llvm::APSInt Lower = Min - Adjustment;
582 llvm::APSInt Upper = ComparisonVal - Adjustment;
585 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
590 const llvm::APSInt &Int,
591 const llvm::APSInt &Adjustment) {
592 RangeSet New = getSymLTRange(St, Sym, Int, Adjustment);
593 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
598 const llvm::APSInt &Int,
599 const llvm::APSInt &Adjustment) {
601 APSIntType AdjustmentType(Adjustment);
602 switch (AdjustmentType.testInRange(Int,
true)) {
604 return getRange(St, Sym);
608 return F.getEmptySet();
612 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
613 llvm::APSInt Max = AdjustmentType.getMaxValue();
614 if (ComparisonVal == Max)
615 return F.getEmptySet();
617 llvm::APSInt Lower = ComparisonVal - Adjustment;
618 llvm::APSInt Upper = Max - Adjustment;
621 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
626 const llvm::APSInt &Int,
627 const llvm::APSInt &Adjustment) {
628 RangeSet New = getSymGTRange(St, Sym, Int, Adjustment);
629 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
634 const llvm::APSInt &Int,
635 const llvm::APSInt &Adjustment) {
637 APSIntType AdjustmentType(Adjustment);
638 switch (AdjustmentType.testInRange(Int,
true)) {
640 return getRange(St, Sym);
644 return F.getEmptySet();
648 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
649 llvm::APSInt Min = AdjustmentType.getMinValue();
650 if (ComparisonVal == Min)
651 return getRange(St, Sym);
653 llvm::APSInt Max = AdjustmentType.getMaxValue();
654 llvm::APSInt Lower = ComparisonVal - Adjustment;
655 llvm::APSInt Upper = Max - Adjustment;
657 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
662 const llvm::APSInt &Int,
663 const llvm::APSInt &Adjustment) {
664 RangeSet New = getSymGERange(St, Sym, Int, Adjustment);
665 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
668 RangeSet RangeConstraintManager::getSymLERange(
670 const llvm::APSInt &Int,
671 const llvm::APSInt &Adjustment) {
673 APSIntType AdjustmentType(Adjustment);
674 switch (AdjustmentType.testInRange(Int,
true)) {
676 return F.getEmptySet();
684 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
685 llvm::APSInt Max = AdjustmentType.getMaxValue();
686 if (ComparisonVal == Max)
689 llvm::APSInt Min = AdjustmentType.getMinValue();
690 llvm::APSInt Lower = Min - Adjustment;
691 llvm::APSInt Upper = ComparisonVal - Adjustment;
693 return RS().Intersect(getBasicVals(), F, Lower, Upper);
698 const llvm::APSInt &Int,
699 const llvm::APSInt &Adjustment) {
700 return getSymLERange([&] {
return getRange(St, Sym); }, Int, Adjustment);
705 const llvm::APSInt &Int,
706 const llvm::APSInt &Adjustment) {
707 RangeSet New = getSymLERange(St, Sym, Int, Adjustment);
708 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
713 const llvm::APSInt &To,
const llvm::APSInt &Adjustment) {
714 RangeSet New = getSymGERange(State, Sym, From, Adjustment);
717 RangeSet Out = getSymLERange([&] {
return New; }, To, Adjustment);
718 return Out.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, Out);
721 ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange(
723 const llvm::APSInt &To,
const llvm::APSInt &Adjustment) {
724 RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment);
725 RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment);
726 RangeSet New(RangeLT.addRange(F, RangeGT));
727 return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New);
734 void RangeConstraintManager::print(
ProgramStateRef St, raw_ostream &Out,
735 const char *nl,
const char *sep) {
739 if (Ranges.isEmpty()) {
740 Out << nl << sep <<
"Ranges are empty." << nl;
744 Out << nl << sep <<
"Ranges of symbol values:";
745 for (ConstraintRangeTy::iterator I = Ranges.begin(), E = Ranges.end(); I != E;
747 Out << nl <<
' ' << I.getKey() <<
" : ";
748 I.getData().print(Out);
A (possibly-)qualified type.
Value is less than the minimum representable value.
bool isDead(SymbolRef sym)
Returns whether or not a symbol has been confirmed dead.
const SymExpr * SymbolRef
std::unique_ptr< ConstraintManager > CreateRangeConstraintManager(ProgramStateManager &statemgr, SubEngine *subengine)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The base class of the type hierarchy.
A Range represents the closed range [from, to].
llvm::ImmutableMap< SymbolRef, RangeSet > ConstraintRangeTy
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
RangeSet contains a set of ranges.
bool isEqualityOp() const
bool isReferenceType() const
SValBuilder & getSValBuilder()
RangeSet Negate(BasicValueFactory &BV, Factory &F) const
static bool isLocType(QualType T)
PrimRangeSet::Factory Factory
Value is representable using this type.
A record of the "type" of an APSInt, used for conversions.
Represents a symbolic expression like 'x' + 3.
bool isRelationalOp() const
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
virtual QualType getType() const =0
void print(raw_ostream &os) const
const llvm::APSInt * getConcreteValue() const
getConcreteValue - If a symbol is contrained to equal a specific integer constant then this method re...
static RangeSet assumeNonZero(BasicValueFactory &BV, RangeSet::Factory &F, SymbolRef Sym, RangeSet Domain)
Return a range set subtracting zero from Domain.
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
A class responsible for cleaning up unused symbols.
Value is greater than the maximum representable value.
RangeTestResultKind
Used to classify whether a value is representable using this type.
Dataflow Directional Tag Classes.
RangeSet(PrimRangeSet RS)
Represents symbolic expression that isn't a location.
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
RangeSet Intersect(BasicValueFactory &BV, Factory &F, llvm::APSInt Lower, llvm::APSInt Upper) const
PrimRangeSet::iterator iterator
static RangeSet applyBitwiseConstraints(BasicValueFactory &BV, RangeSet::Factory &F, RangeSet Input, const SymIntExpr *SIE)
Apply implicit constraints for bitwise OR- and AND-.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
Represents a symbolic expression like 'x' + 'y'.