Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 1 | //== 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file defines RangeConstraintManager, a class that tracks simple |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 11 | // equality and inequality constraints on symbolic values of GRState. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SimpleConstraintManager.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 17 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Chandler Carruth | f465e85 | 2009-11-11 19:10:59 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/ManagerRegistry.h" |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/ADT/FoldingSet.h" |
| 23 | #include "llvm/ADT/ImmutableSet.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 28 | namespace { class VISIBILITY_HIDDEN ConstraintRange {}; } |
| 29 | static int ConstraintRangeIndex = 0; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 31 | /// A Range represents the closed range [from, to]. The caller must |
| 32 | /// guarantee that from <= to. Note that Range is immutable, so as not |
| 33 | /// to subvert RangeSet's immutability. |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN Range : public std::pair<const llvm::APSInt*, |
| 36 | const llvm::APSInt*> { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 37 | public: |
| 38 | Range(const llvm::APSInt &from, const llvm::APSInt &to) |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 39 | : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 40 | assert(from <= to); |
| 41 | } |
| 42 | bool Includes(const llvm::APSInt &v) const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 43 | return *first <= v && v <= *second; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 44 | } |
| 45 | const llvm::APSInt &From() const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 46 | return *first; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 47 | } |
| 48 | const llvm::APSInt &To() const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 49 | return *second; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 50 | } |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 51 | const llvm::APSInt *getConcreteValue() const { |
| 52 | return &From() == &To() ? &From() : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void Profile(llvm::FoldingSetNodeID &ID) const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 56 | ID.AddPointer(&From()); |
| 57 | ID.AddPointer(&To()); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 61 | |
| 62 | class VISIBILITY_HIDDEN RangeTrait : public llvm::ImutContainerInfo<Range> { |
| 63 | public: |
| 64 | // When comparing if one Range is less than another, we should compare |
Ted Kremenek | e53f820 | 2009-02-18 17:42:44 +0000 | [diff] [blame] | 65 | // the actual APSInt values instead of their pointers. This keeps the order |
| 66 | // consistent (instead of comparing by pointer values) and can potentially |
| 67 | // be used to speed up some of the operations in RangeSet. |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 68 | static inline bool isLess(key_type_ref lhs, key_type_ref rhs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) && |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 70 | *lhs.second < *rhs.second); |
| 71 | } |
| 72 | }; |
| 73 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 74 | /// RangeSet contains a set of ranges. If the set is empty, then |
| 75 | /// there the value of a symbol is overly constrained and there are no |
| 76 | /// possible values for that symbol. |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 77 | class VISIBILITY_HIDDEN RangeSet { |
| 78 | typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 79 | PrimRangeSet ranges; // no need to make const, since it is an |
| 80 | // ImmutableSet - this allows default operator= |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | // to work. |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 82 | public: |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 83 | typedef PrimRangeSet::Factory Factory; |
| 84 | typedef PrimRangeSet::iterator iterator; |
| 85 | |
| 86 | RangeSet(PrimRangeSet RS) : ranges(RS) {} |
| 87 | RangeSet(Factory& F) : ranges(F.GetEmptySet()) {} |
| 88 | |
| 89 | iterator begin() const { return ranges.begin(); } |
| 90 | iterator end() const { return ranges.end(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 92 | bool isEmpty() const { return ranges.isEmpty(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 94 | /// Construct a new RangeSet representing '{ [from, to] }'. |
| 95 | RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to) |
| 96 | : ranges(F.Add(F.GetEmptySet(), Range(from, to))) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 98 | /// Profile - Generates a hash profile of this RangeSet for use |
| 99 | /// by FoldingSet. |
| 100 | void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); } |
| 101 | |
| 102 | /// getConcreteValue - If a symbol is contrained to equal a specific integer |
| 103 | /// constant then this method returns that value. Otherwise, it returns |
| 104 | /// NULL. |
| 105 | const llvm::APSInt* getConcreteValue() const { |
| 106 | return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 109 | /// AddEQ - Create a new RangeSet with the additional constraint that the |
| 110 | /// value be equal to V. |
| 111 | RangeSet AddEQ(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 112 | // Search for a range that includes 'V'. If so, return a new RangeSet |
| 113 | // representing { [V, V] }. |
| 114 | for (PrimRangeSet::iterator i = begin(), e = end(); i!=e; ++i) |
| 115 | if (i->Includes(V)) |
| 116 | return RangeSet(F, V, V); |
| 117 | |
| 118 | return RangeSet(F); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 121 | /// AddNE - Create a new RangeSet with the additional constraint that the |
| 122 | /// value be not be equal to V. |
| 123 | RangeSet AddNE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 124 | PrimRangeSet newRanges = ranges; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 126 | // FIXME: We can perhaps enhance ImmutableSet to do this search for us |
| 127 | // in log(N) time using the sorted property of the internal AVL tree. |
| 128 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 129 | if (i->Includes(V)) { |
| 130 | // Remove the old range. |
| 131 | newRanges = F.Remove(newRanges, *i); |
| 132 | // Split the old range into possibly one or two ranges. |
| 133 | if (V != i->From()) |
| 134 | newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V))); |
| 135 | if (V != i->To()) |
| 136 | newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | // All of the ranges are non-overlapping, so we can stop. |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 138 | break; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 142 | return newRanges; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 145 | /// AddNE - Create a new RangeSet with the additional constraint that the |
| 146 | /// value be less than V. |
| 147 | RangeSet AddLT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 148 | PrimRangeSet newRanges = F.GetEmptySet(); |
| 149 | |
| 150 | for (iterator i = begin(), e = end() ; i != e ; ++i) { |
| 151 | if (i->Includes(V) && i->From() < V) |
| 152 | newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V))); |
| 153 | else if (i->To() < V) |
| 154 | newRanges = F.Add(newRanges, *i); |
| 155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 157 | return newRanges; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 160 | RangeSet AddLE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 161 | PrimRangeSet newRanges = F.GetEmptySet(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 163 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 164 | // Strictly we should test for includes *V + 1, but no harm is |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 165 | // done by this formulation |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 166 | if (i->Includes(V)) |
| 167 | newRanges = F.Add(newRanges, Range(i->From(), V)); |
| 168 | else if (i->To() <= V) |
| 169 | newRanges = F.Add(newRanges, *i); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 172 | return newRanges; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 175 | RangeSet AddGT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 176 | PrimRangeSet newRanges = F.GetEmptySet(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 178 | for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) { |
| 179 | if (i->Includes(V) && i->To() > V) |
| 180 | newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To())); |
| 181 | else if (i->From() > V) |
| 182 | newRanges = F.Add(newRanges, *i); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 185 | return newRanges; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 188 | RangeSet AddGE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) { |
| 189 | PrimRangeSet newRanges = F.GetEmptySet(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 191 | for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) { |
| 192 | // Strictly we should test for includes *V - 1, but no harm is |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 193 | // done by this formulation |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 194 | if (i->Includes(V)) |
| 195 | newRanges = F.Add(newRanges, Range(V, i->To())); |
| 196 | else if (i->From() >= V) |
| 197 | newRanges = F.Add(newRanges, *i); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 200 | return newRanges; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 203 | void print(llvm::raw_ostream &os) const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 204 | bool isFirst = true; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 205 | os << "{ "; |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 206 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 207 | if (isFirst) |
| 208 | isFirst = false; |
| 209 | else |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 210 | os << ", "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 212 | os << '[' << i->From().toString(10) << ", " << i->To().toString(10) |
| 213 | << ']'; |
| 214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | os << " }"; |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 218 | bool operator==(const RangeSet &other) const { |
| 219 | return ranges == other.ranges; |
| 220 | } |
| 221 | }; |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 222 | } // end anonymous namespace |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 224 | typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 225 | |
| 226 | namespace clang { |
| 227 | template<> |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 228 | struct GRStateTrait<ConstraintRange> |
| 229 | : public GRStatePartialTrait<ConstraintRangeTy> { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | static inline void* GDMIndex() { return &ConstraintRangeIndex; } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 231 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 234 | namespace { |
Ted Kremenek | b103f01 | 2009-02-18 05:22:01 +0000 | [diff] [blame] | 235 | class VISIBILITY_HIDDEN RangeConstraintManager : public SimpleConstraintManager{ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | RangeSet GetRange(const GRState *state, SymbolRef sym); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 237 | public: |
Ted Kremenek | f1b8227 | 2009-06-18 23:20:05 +0000 | [diff] [blame] | 238 | RangeConstraintManager() {} |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 239 | |
| 240 | const GRState* AssumeSymNE(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 241 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 242 | |
| 243 | const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 244 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 245 | |
| 246 | const GRState* AssumeSymLT(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 247 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 248 | |
| 249 | const GRState* AssumeSymGT(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 250 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 251 | |
| 252 | const GRState* AssumeSymGE(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 253 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 254 | |
| 255 | const GRState* AssumeSymLE(const GRState* St, SymbolRef sym, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 256 | const llvm::APSInt& V); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 258 | const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 260 | // FIXME: Refactor into SimpleConstraintManager? |
| 261 | bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const { |
| 262 | const llvm::APSInt *i = getSymVal(St, sym); |
| 263 | return i ? *i == V : false; |
| 264 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 266 | const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper); |
| 267 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | void print(const GRState* St, llvm::raw_ostream& Out, |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 269 | const char* nl, const char *sep); |
| 270 | |
| 271 | private: |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 272 | RangeSet::Factory F; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | } // end anonymous namespace |
| 276 | |
Ted Kremenek | f1b8227 | 2009-06-18 23:20:05 +0000 | [diff] [blame] | 277 | ConstraintManager* clang::CreateRangeConstraintManager(GRStateManager&) { |
| 278 | return new RangeConstraintManager(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 281 | const llvm::APSInt* RangeConstraintManager::getSymVal(const GRState* St, |
| 282 | SymbolRef sym) const { |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 283 | const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym); |
| 284 | return T ? T->getConcreteValue() : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /// Scan all symbols referenced by the constraints. If the symbol is not alive |
| 288 | /// as marked in LSymbols, mark it as dead in DSymbols. |
| 289 | const GRState* |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 290 | RangeConstraintManager::RemoveDeadBindings(const GRState* state, |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 291 | SymbolReaper& SymReaper) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 293 | ConstraintRangeTy CR = state->get<ConstraintRange>(); |
| 294 | ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 296 | for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | SymbolRef sym = I.getKey(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 298 | if (SymReaper.maybeDead(sym)) |
| 299 | CR = CRFactory.Remove(CR, sym); |
| 300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 302 | return state->set<ConstraintRange>(CR); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 305 | //===------------------------------------------------------------------------=== |
| 306 | // AssumeSymX methods: public interface for RangeConstraintManager. |
| 307 | //===------------------------------------------------------------------------===/ |
| 308 | |
| 309 | RangeSet |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 310 | RangeConstraintManager::GetRange(const GRState *state, SymbolRef sym) { |
| 311 | if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym)) |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 312 | return *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 314 | // Lazily generate a new RangeSet representing all possible values for the |
| 315 | // given symbol type. |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 316 | QualType T = state->getSymbolManager().getType(sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | BasicValueFactory& BV = state->getBasicVals(); |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 318 | return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T)); |
| 319 | } |
| 320 | |
| 321 | //===------------------------------------------------------------------------=== |
| 322 | // AssumeSymX methods: public interface for RangeConstraintManager. |
| 323 | //===------------------------------------------------------------------------===/ |
| 324 | |
| 325 | #define AssumeX(OP)\ |
| 326 | const GRState*\ |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 327 | RangeConstraintManager::AssumeSym ## OP(const GRState* state, SymbolRef sym,\ |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 328 | const llvm::APSInt& V){\ |
Ted Kremenek | c878138 | 2009-06-17 22:28:13 +0000 | [diff] [blame] | 329 | const RangeSet& R = GetRange(state, sym).Add##OP(state->getBasicVals(), F, V);\ |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 330 | return !R.isEmpty() ? state->set<ConstraintRange>(sym, R) : NULL;\ |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | AssumeX(EQ) |
| 334 | AssumeX(NE) |
| 335 | AssumeX(LT) |
| 336 | AssumeX(GT) |
| 337 | AssumeX(LE) |
| 338 | AssumeX(GE) |
| 339 | |
| 340 | //===------------------------------------------------------------------------=== |
| 341 | // Pretty-printing. |
| 342 | //===------------------------------------------------------------------------===/ |
| 343 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | void RangeConstraintManager::print(const GRState* St, llvm::raw_ostream& Out, |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 345 | const char* nl, const char *sep) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 347 | ConstraintRangeTy Ranges = St->get<ConstraintRange>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | dd28d00 | 2009-02-16 18:42:56 +0000 | [diff] [blame] | 349 | if (Ranges.isEmpty()) |
| 350 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | dd28d00 | 2009-02-16 18:42:56 +0000 | [diff] [blame] | 352 | Out << nl << sep << "ranges of symbol values:"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | 9beefec | 2009-02-17 19:28:04 +0000 | [diff] [blame] | 354 | for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){ |
Ted Kremenek | ec751c4 | 2009-04-21 22:37:11 +0000 | [diff] [blame] | 355 | Out << nl << ' ' << I.getKey() << " : "; |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 356 | I.getData().print(Out); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 357 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 358 | } |