blob: 2a35d326a98834db189ef6238cceaa9dafbfcc13 [file] [log] [blame]
Ted Kremenek45021952009-02-14 17:08:39 +00001//== RangeConstraintManager.cpp - Manage range constraints.------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This file defines RangeConstraintManager, a class that tracks simple
Ted Kremenek45021952009-02-14 17:08:39 +000011// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SimpleConstraintManager.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRState.h"
17#include "clang/Checker/PathSensitive/GRStateTrait.h"
18#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
19#include "clang/Checker/ManagerRegistry.h"
Ted Kremenek45021952009-02-14 17:08:39 +000020#include "llvm/Support/Debug.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/ImmutableSet.h"
23#include "llvm/Support/raw_ostream.h"
24
25using namespace clang;
26
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000027namespace { class ConstraintRange {}; }
Ted Kremenek9beefec2009-02-17 19:28:04 +000028static int ConstraintRangeIndex = 0;
Ted Kremenek45021952009-02-14 17:08:39 +000029
Ted Kremenek9beefec2009-02-17 19:28:04 +000030/// A Range represents the closed range [from, to]. The caller must
31/// guarantee that from <= to. Note that Range is immutable, so as not
32/// to subvert RangeSet's immutability.
Ted Kremenekb103f012009-02-18 05:22:01 +000033namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000034class Range : public std::pair<const llvm::APSInt*,
Ted Kremenekb103f012009-02-18 05:22:01 +000035 const llvm::APSInt*> {
Ted Kremenek45021952009-02-14 17:08:39 +000036public:
37 Range(const llvm::APSInt &from, const llvm::APSInt &to)
Ted Kremenek9beefec2009-02-17 19:28:04 +000038 : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
Ted Kremenek45021952009-02-14 17:08:39 +000039 assert(from <= to);
40 }
41 bool Includes(const llvm::APSInt &v) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000042 return *first <= v && v <= *second;
Ted Kremenek45021952009-02-14 17:08:39 +000043 }
44 const llvm::APSInt &From() const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000045 return *first;
Ted Kremenek45021952009-02-14 17:08:39 +000046 }
47 const llvm::APSInt &To() const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000048 return *second;
Ted Kremenek45021952009-02-14 17:08:39 +000049 }
Ted Kremenek9beefec2009-02-17 19:28:04 +000050 const llvm::APSInt *getConcreteValue() const {
51 return &From() == &To() ? &From() : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +000052 }
53
54 void Profile(llvm::FoldingSetNodeID &ID) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000055 ID.AddPointer(&From());
56 ID.AddPointer(&To());
Ted Kremenek45021952009-02-14 17:08:39 +000057 }
58};
59
Ted Kremenekb103f012009-02-18 05:22:01 +000060
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000061class RangeTrait : public llvm::ImutContainerInfo<Range> {
Ted Kremenekb103f012009-02-18 05:22:01 +000062public:
63 // When comparing if one Range is less than another, we should compare
Ted Kremeneke53f8202009-02-18 17:42:44 +000064 // the actual APSInt values instead of their pointers. This keeps the order
65 // consistent (instead of comparing by pointer values) and can potentially
66 // be used to speed up some of the operations in RangeSet.
Ted Kremenekb103f012009-02-18 05:22:01 +000067 static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
Mike Stump1eb44332009-09-09 15:08:12 +000068 return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
Ted Kremenekb103f012009-02-18 05:22:01 +000069 *lhs.second < *rhs.second);
70 }
71};
72
Ted Kremenek9beefec2009-02-17 19:28:04 +000073/// RangeSet contains a set of ranges. If the set is empty, then
74/// there the value of a symbol is overly constrained and there are no
75/// possible values for that symbol.
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000076class RangeSet {
Ted Kremenekb103f012009-02-18 05:22:01 +000077 typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
Ted Kremenek45021952009-02-14 17:08:39 +000078 PrimRangeSet ranges; // no need to make const, since it is an
79 // ImmutableSet - this allows default operator=
Mike Stump1eb44332009-09-09 15:08:12 +000080 // to work.
Ted Kremenek45021952009-02-14 17:08:39 +000081public:
Ted Kremenek9beefec2009-02-17 19:28:04 +000082 typedef PrimRangeSet::Factory Factory;
83 typedef PrimRangeSet::iterator iterator;
84
85 RangeSet(PrimRangeSet RS) : ranges(RS) {}
86 RangeSet(Factory& F) : ranges(F.GetEmptySet()) {}
87
88 iterator begin() const { return ranges.begin(); }
89 iterator end() const { return ranges.end(); }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Ted Kremenek9beefec2009-02-17 19:28:04 +000091 bool isEmpty() const { return ranges.isEmpty(); }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek9beefec2009-02-17 19:28:04 +000093 /// Construct a new RangeSet representing '{ [from, to] }'.
94 RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
95 : ranges(F.Add(F.GetEmptySet(), Range(from, to))) {}
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremenek9beefec2009-02-17 19:28:04 +000097 /// Profile - Generates a hash profile of this RangeSet for use
98 /// by FoldingSet.
99 void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
100
101 /// getConcreteValue - If a symbol is contrained to equal a specific integer
102 /// constant then this method returns that value. Otherwise, it returns
103 /// NULL.
104 const llvm::APSInt* getConcreteValue() const {
105 return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
Ted Kremenek45021952009-02-14 17:08:39 +0000106 }
107
Jordy Roseba0f61c2010-06-18 22:49:11 +0000108private:
109 void IntersectInRange(BasicValueFactory &BV, Factory &F,
110 const llvm::APSInt &Lower,
111 const llvm::APSInt &Upper,
112 PrimRangeSet &newRanges,
113 PrimRangeSet::iterator &i,
114 PrimRangeSet::iterator &e) const {
115 // There are six cases for each range R in the set:
116 // 1. R is entirely before the intersection range.
117 // 2. R is entirely after the intersection range.
118 // 3. R contains the entire intersection range.
119 // 4. R starts before the intersection range and ends in the middle.
120 // 5. R starts in the middle of the intersection range and ends after it.
121 // 6. R is entirely contained in the intersection range.
122 // These correspond to each of the conditions below.
123 for (/* i = begin(), e = end() */; i != e; ++i) {
124 if (i->To() < Lower) {
125 continue;
126 }
127 if (i->From() > Upper) {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000128 break;
Ted Kremenek45021952009-02-14 17:08:39 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Jordy Roseba0f61c2010-06-18 22:49:11 +0000131 if (i->Includes(Lower)) {
132 if (i->Includes(Upper)) {
133 newRanges = F.Add(newRanges, Range(BV.getValue(Lower),
134 BV.getValue(Upper)));
135 break;
136 } else
137 newRanges = F.Add(newRanges, Range(BV.getValue(Lower), i->To()));
138 } else {
139 if (i->Includes(Upper)) {
140 newRanges = F.Add(newRanges, Range(i->From(), BV.getValue(Upper)));
141 break;
142 } else
143 newRanges = F.Add(newRanges, *i);
144 }
145 }
Ted Kremenek45021952009-02-14 17:08:39 +0000146 }
147
Jordy Roseba0f61c2010-06-18 22:49:11 +0000148public:
149 // Returns a set containing the values in the receiving set, intersected with
150 // the closed range [Lower, Upper]. Unlike the Range type, this range uses
151 // modular arithmetic, corresponding to the common treatment of C integer
152 // overflow. Thus, if the Lower bound is greater than the Upper bound, the
153 // range is taken to wrap around. This is equivalent to taking the
154 // intersection with the two ranges [Min, Upper] and [Lower, Max],
155 // or, alternatively, /removing/ all integers between Upper and Lower.
156 RangeSet Intersect(BasicValueFactory &BV, Factory &F,
157 const llvm::APSInt &Lower,
158 const llvm::APSInt &Upper) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000159 PrimRangeSet newRanges = F.GetEmptySet();
160
Jordy Roseba0f61c2010-06-18 22:49:11 +0000161 PrimRangeSet::iterator i = begin(), e = end();
162 if (Lower <= Upper)
163 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
164 else {
165 // The order of the next two statements is important!
166 // IntersectInRange() does not reset the iteration state for i and e.
167 // Therefore, the lower range most be handled first.
168 IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
169 IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
Ted Kremenek9beefec2009-02-17 19:28:04 +0000170 }
Ted Kremenek9beefec2009-02-17 19:28:04 +0000171 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000172 }
173
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000174 void print(llvm::raw_ostream &os) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000175 bool isFirst = true;
Ted Kremenek45021952009-02-14 17:08:39 +0000176 os << "{ ";
Ted Kremenek9beefec2009-02-17 19:28:04 +0000177 for (iterator i = begin(), e = end(); i != e; ++i) {
178 if (isFirst)
179 isFirst = false;
180 else
Ted Kremenek45021952009-02-14 17:08:39 +0000181 os << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek45021952009-02-14 17:08:39 +0000183 os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
184 << ']';
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186 os << " }";
Ted Kremenek9beefec2009-02-17 19:28:04 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek45021952009-02-14 17:08:39 +0000189 bool operator==(const RangeSet &other) const {
190 return ranges == other.ranges;
191 }
192};
Ted Kremenekb103f012009-02-18 05:22:01 +0000193} // end anonymous namespace
Ted Kremenek45021952009-02-14 17:08:39 +0000194
Ted Kremenek9beefec2009-02-17 19:28:04 +0000195typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy;
Ted Kremenek45021952009-02-14 17:08:39 +0000196
197namespace clang {
198template<>
Ted Kremenek9beefec2009-02-17 19:28:04 +0000199struct GRStateTrait<ConstraintRange>
200 : public GRStatePartialTrait<ConstraintRangeTy> {
Mike Stump1eb44332009-09-09 15:08:12 +0000201 static inline void* GDMIndex() { return &ConstraintRangeIndex; }
Ted Kremenek45021952009-02-14 17:08:39 +0000202};
Mike Stump1eb44332009-09-09 15:08:12 +0000203}
204
Ted Kremenek45021952009-02-14 17:08:39 +0000205namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000206class RangeConstraintManager : public SimpleConstraintManager{
Mike Stump1eb44332009-09-09 15:08:12 +0000207 RangeSet GetRange(const GRState *state, SymbolRef sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000208public:
Ted Kremenek32a58082010-01-05 00:15:18 +0000209 RangeConstraintManager(GRSubEngine &subengine)
210 : SimpleConstraintManager(subengine) {}
Ted Kremenek45021952009-02-14 17:08:39 +0000211
Jordy Roseba0f61c2010-06-18 22:49:11 +0000212 const GRState* AssumeSymNE(const GRState* state, SymbolRef sym,
213 const llvm::APSInt& Int,
214 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000215
Jordy Roseba0f61c2010-06-18 22:49:11 +0000216 const GRState* AssumeSymEQ(const GRState* state, SymbolRef sym,
217 const llvm::APSInt& Int,
218 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000219
Jordy Roseba0f61c2010-06-18 22:49:11 +0000220 const GRState* AssumeSymLT(const GRState* state, SymbolRef sym,
221 const llvm::APSInt& Int,
222 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000223
Jordy Roseba0f61c2010-06-18 22:49:11 +0000224 const GRState* AssumeSymGT(const GRState* state, SymbolRef sym,
225 const llvm::APSInt& Int,
226 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000227
Jordy Roseba0f61c2010-06-18 22:49:11 +0000228 const GRState* AssumeSymGE(const GRState* state, SymbolRef sym,
229 const llvm::APSInt& Int,
230 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000231
Jordy Roseba0f61c2010-06-18 22:49:11 +0000232 const GRState* AssumeSymLE(const GRState* state, SymbolRef sym,
233 const llvm::APSInt& Int,
234 const llvm::APSInt& Adjustment);
Ted Kremenek45021952009-02-14 17:08:39 +0000235
Ted Kremenek45021952009-02-14 17:08:39 +0000236 const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek9beefec2009-02-17 19:28:04 +0000238 // FIXME: Refactor into SimpleConstraintManager?
239 bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const {
240 const llvm::APSInt *i = getSymVal(St, sym);
241 return i ? *i == V : false;
242 }
Ted Kremenek45021952009-02-14 17:08:39 +0000243
Ted Kremenek45021952009-02-14 17:08:39 +0000244 const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper);
245
Mike Stump1eb44332009-09-09 15:08:12 +0000246 void print(const GRState* St, llvm::raw_ostream& Out,
Ted Kremenek45021952009-02-14 17:08:39 +0000247 const char* nl, const char *sep);
248
249private:
Ted Kremenek9beefec2009-02-17 19:28:04 +0000250 RangeSet::Factory F;
Ted Kremenek45021952009-02-14 17:08:39 +0000251};
252
253} // end anonymous namespace
254
Ted Kremenek32a58082010-01-05 00:15:18 +0000255ConstraintManager* clang::CreateRangeConstraintManager(GRStateManager&,
256 GRSubEngine &subeng) {
257 return new RangeConstraintManager(subeng);
Ted Kremenek45021952009-02-14 17:08:39 +0000258}
259
Ted Kremenek45021952009-02-14 17:08:39 +0000260const llvm::APSInt* RangeConstraintManager::getSymVal(const GRState* St,
261 SymbolRef sym) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000262 const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
263 return T ? T->getConcreteValue() : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000264}
265
266/// Scan all symbols referenced by the constraints. If the symbol is not alive
267/// as marked in LSymbols, mark it as dead in DSymbols.
268const GRState*
Ted Kremenekc8781382009-06-17 22:28:13 +0000269RangeConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek45021952009-02-14 17:08:39 +0000270 SymbolReaper& SymReaper) {
Ted Kremenek45021952009-02-14 17:08:39 +0000271
Ted Kremenekc8781382009-06-17 22:28:13 +0000272 ConstraintRangeTy CR = state->get<ConstraintRange>();
273 ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
Ted Kremenek45021952009-02-14 17:08:39 +0000274
Ted Kremenek9beefec2009-02-17 19:28:04 +0000275 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 SymbolRef sym = I.getKey();
Ted Kremenek45021952009-02-14 17:08:39 +0000277 if (SymReaper.maybeDead(sym))
278 CR = CRFactory.Remove(CR, sym);
279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenekc8781382009-06-17 22:28:13 +0000281 return state->set<ConstraintRange>(CR);
Ted Kremenek45021952009-02-14 17:08:39 +0000282}
283
Ted Kremenek9beefec2009-02-17 19:28:04 +0000284RangeSet
Ted Kremenekc8781382009-06-17 22:28:13 +0000285RangeConstraintManager::GetRange(const GRState *state, SymbolRef sym) {
286 if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
Ted Kremenek9beefec2009-02-17 19:28:04 +0000287 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenek9beefec2009-02-17 19:28:04 +0000289 // Lazily generate a new RangeSet representing all possible values for the
290 // given symbol type.
Ted Kremenekc8781382009-06-17 22:28:13 +0000291 QualType T = state->getSymbolManager().getType(sym);
Mike Stump1eb44332009-09-09 15:08:12 +0000292 BasicValueFactory& BV = state->getBasicVals();
Ted Kremenek9beefec2009-02-17 19:28:04 +0000293 return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T));
294}
295
296//===------------------------------------------------------------------------===
297// AssumeSymX methods: public interface for RangeConstraintManager.
298//===------------------------------------------------------------------------===/
299
Jordy Roseba0f61c2010-06-18 22:49:11 +0000300// The syntax for ranges below is mathematical, using [x, y] for closed ranges
301// and (x, y) for open ranges. These ranges are modular, corresponding with
302// a common treatment of C integer overflow. This means that these methods
303// do not have to worry about overflow; RangeSet::Intersect can handle such a
304// "wraparound" range.
305// As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
306// UINT_MAX, 0, 1, and 2.
307
308const GRState*
309RangeConstraintManager::AssumeSymNE(const GRState* state, SymbolRef sym,
310 const llvm::APSInt& Int,
311 const llvm::APSInt& Adjustment) {
312 BasicValueFactory &BV = state->getBasicVals();
313
314 llvm::APSInt Lower = Int-Adjustment;
315 llvm::APSInt Upper = Lower;
316 --Lower;
317 ++Upper;
318
319 // [Int-Adjustment+1, Int-Adjustment-1]
320 // Notice that the lower bound is greater than the upper bound.
321 RangeSet New = GetRange(state, sym).Intersect(BV, F, Upper, Lower);
322 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
Ted Kremenek9beefec2009-02-17 19:28:04 +0000323}
324
Jordy Roseba0f61c2010-06-18 22:49:11 +0000325const GRState*
326RangeConstraintManager::AssumeSymEQ(const GRState* state, SymbolRef sym,
327 const llvm::APSInt& Int,
328 const llvm::APSInt& Adjustment) {
329 // [Int-Adjustment, Int-Adjustment]
330 BasicValueFactory &BV = state->getBasicVals();
331 llvm::APSInt AdjInt = Int-Adjustment;
332 RangeSet New = GetRange(state, sym).Intersect(BV, F, AdjInt, AdjInt);
333 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
334}
335
336const GRState*
337RangeConstraintManager::AssumeSymLT(const GRState* state, SymbolRef sym,
338 const llvm::APSInt& Int,
339 const llvm::APSInt& Adjustment) {
340 BasicValueFactory &BV = state->getBasicVals();
341
342 QualType T = state->getSymbolManager().getType(sym);
343 const llvm::APSInt &Min = BV.getMinValue(T);
344
345 // Special case for Int == Min. This is always false.
346 if (Int == Min)
347 return NULL;
348
349 llvm::APSInt Lower = Min-Adjustment;
350 llvm::APSInt Upper = Int-Adjustment;
351 --Upper;
352
353 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
354 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
355}
356
357const GRState*
358RangeConstraintManager::AssumeSymGT(const GRState* state, SymbolRef sym,
359 const llvm::APSInt& Int,
360 const llvm::APSInt& Adjustment) {
361 BasicValueFactory &BV = state->getBasicVals();
362
363 QualType T = state->getSymbolManager().getType(sym);
364 const llvm::APSInt &Max = BV.getMaxValue(T);
365
366 // Special case for Int == Max. This is always false.
367 if (Int == Max)
368 return NULL;
369
370 llvm::APSInt Lower = Int-Adjustment;
371 llvm::APSInt Upper = Max-Adjustment;
372 ++Lower;
373
374 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
375 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
376}
377
378const GRState*
379RangeConstraintManager::AssumeSymGE(const GRState* state, SymbolRef sym,
380 const llvm::APSInt& Int,
381 const llvm::APSInt& Adjustment) {
382 BasicValueFactory &BV = state->getBasicVals();
383
384 QualType T = state->getSymbolManager().getType(sym);
385 const llvm::APSInt &Min = BV.getMinValue(T);
386
387 // Special case for Int == Min. This is always feasible.
388 if (Int == Min)
389 return state;
390
391 const llvm::APSInt &Max = BV.getMaxValue(T);
392
393 llvm::APSInt Lower = Int-Adjustment;
394 llvm::APSInt Upper = Max-Adjustment;
395
396 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
397 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
398}
399
400const GRState*
401RangeConstraintManager::AssumeSymLE(const GRState* state, SymbolRef sym,
402 const llvm::APSInt& Int,
403 const llvm::APSInt& Adjustment) {
404 BasicValueFactory &BV = state->getBasicVals();
405
406 QualType T = state->getSymbolManager().getType(sym);
407 const llvm::APSInt &Max = BV.getMaxValue(T);
408
409 // Special case for Int == Max. This is always feasible.
410 if (Int == Max)
411 return state;
412
413 const llvm::APSInt &Min = BV.getMinValue(T);
414
415 llvm::APSInt Lower = Min-Adjustment;
416 llvm::APSInt Upper = Int-Adjustment;
417
418 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
419 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
420}
Ted Kremenek9beefec2009-02-17 19:28:04 +0000421
422//===------------------------------------------------------------------------===
423// Pretty-printing.
424//===------------------------------------------------------------------------===/
425
Mike Stump1eb44332009-09-09 15:08:12 +0000426void RangeConstraintManager::print(const GRState* St, llvm::raw_ostream& Out,
Ted Kremenek45021952009-02-14 17:08:39 +0000427 const char* nl, const char *sep) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek9beefec2009-02-17 19:28:04 +0000429 ConstraintRangeTy Ranges = St->get<ConstraintRange>();
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenekdd28d002009-02-16 18:42:56 +0000431 if (Ranges.isEmpty())
432 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenekdd28d002009-02-16 18:42:56 +0000434 Out << nl << sep << "ranges of symbol values:";
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek9beefec2009-02-17 19:28:04 +0000436 for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
Ted Kremenekec751c42009-04-21 22:37:11 +0000437 Out << nl << ' ' << I.getKey() << " : ";
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000438 I.getData().print(Out);
Ted Kremenek45021952009-02-14 17:08:39 +0000439 }
Ted Kremenek45021952009-02-14 17:08:39 +0000440}