Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1 | //= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- 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 | // |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 10 | // This file defines SVal, Loc, and NonLoc, classes that represent |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 11 | // abstract r-values for use with path-sensitive value tracking. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | using llvm::dyn_cast; |
| 21 | using llvm::cast; |
| 22 | using llvm::APSInt; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 25 | // Symbol iteration within an SVal. |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Utility methods. |
| 31 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 33 | /// getAsLocSymbol - If this SVal is a location (subclasses Loc) and |
| 34 | /// wraps a symbol, return that SymbolRef. Otherwise return a SymbolRef |
| 35 | /// where 'isValid()' returns false. |
| 36 | SymbolRef SVal::getAsLocSymbol() const { |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 37 | if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) { |
| 38 | const MemRegion *R = X->getRegion(); |
| 39 | |
| 40 | while (R) { |
| 41 | // Blast through region views. |
| 42 | if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) { |
| 43 | R = View->getSuperRegion(); |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R)) |
| 48 | return SymR->getSymbol(); |
| 49 | |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 54 | return 0; |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// getAsSymbol - If this Sval wraps a symbol return that SymbolRef. |
| 58 | /// Otherwise return a SymbolRef where 'isValid()' returns false. |
| 59 | SymbolRef SVal::getAsSymbol() const { |
| 60 | if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this)) |
| 61 | return X->getSymbol(); |
| 62 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 63 | if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this)) |
| 64 | if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression())) |
| 65 | return Y; |
| 66 | |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 67 | return getAsLocSymbol(); |
| 68 | } |
| 69 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 70 | /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then |
| 71 | /// return that expression. Otherwise return NULL. |
| 72 | const SymExpr *SVal::getAsSymbolicExpression() const { |
| 73 | if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this)) |
| 74 | return X->getSymbolicExpression(); |
| 75 | |
| 76 | return getAsSymbol(); |
| 77 | } |
| 78 | |
| 79 | bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const { |
| 80 | return itr == X.itr; |
| 81 | } |
| 82 | |
| 83 | bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const { |
| 84 | return itr != X.itr; |
| 85 | } |
| 86 | |
| 87 | SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) { |
| 88 | itr.push_back(SE); |
| 89 | while (!isa<SymbolData>(itr.back())) expand(); |
| 90 | } |
| 91 | |
| 92 | SVal::symbol_iterator& SVal::symbol_iterator::operator++() { |
| 93 | assert(!itr.empty() && "attempting to iterate on an 'end' iterator"); |
| 94 | assert(isa<SymbolData>(itr.back())); |
| 95 | itr.pop_back(); |
| 96 | if (!itr.empty()) |
| 97 | while (!isa<SymbolData>(itr.back())) expand(); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | SymbolRef SVal::symbol_iterator::operator*() { |
| 102 | assert(!itr.empty() && "attempting to dereference an 'end' iterator"); |
| 103 | return cast<SymbolData>(itr.back()); |
| 104 | } |
| 105 | |
| 106 | void SVal::symbol_iterator::expand() { |
| 107 | const SymExpr *SE = itr.back(); |
| 108 | itr.pop_back(); |
| 109 | |
| 110 | if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { |
| 111 | itr.push_back(SIE->getLHS()); |
| 112 | return; |
| 113 | } |
| 114 | else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) { |
| 115 | itr.push_back(SSE->getLHS()); |
| 116 | itr.push_back(SSE->getRHS()); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | assert(false && "unhandled expansion case"); |
| 121 | } |
| 122 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6fac4e | 2008-10-30 18:01:28 +0000 | [diff] [blame] | 124 | // Other Iterators. |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const { |
| 128 | return getValue()->begin(); |
| 129 | } |
| 130 | |
| 131 | nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const { |
| 132 | return getValue()->end(); |
| 133 | } |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 136 | // Useful predicates. |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 139 | bool SVal::isZeroConstant() const { |
| 140 | if (isa<loc::ConcreteInt>(*this)) |
| 141 | return cast<loc::ConcreteInt>(*this).getValue() == 0; |
| 142 | else if (isa<nonloc::ConcreteInt>(*this)) |
| 143 | return cast<nonloc::ConcreteInt>(*this).getValue() == 0; |
Ted Kremenek | 40fc5c7 | 2008-07-18 15:54:51 +0000 | [diff] [blame] | 144 | else |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 150 | // Transfer function dispatch for Non-Locs. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 152 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 153 | SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, |
Ted Kremenek | 75b0a1c | 2008-07-18 15:59:33 +0000 | [diff] [blame] | 154 | BinaryOperator::Opcode Op, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 155 | const nonloc::ConcreteInt& R) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | 75b0a1c | 2008-07-18 15:59:33 +0000 | [diff] [blame] | 157 | const llvm::APSInt* X = |
| 158 | BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 159 | |
| 160 | if (X) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 161 | return nonloc::ConcreteInt(*X); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 162 | else |
| 163 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 166 | // Bitwise-Complement. |
| 167 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 168 | nonloc::ConcreteInt |
| 169 | nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const { |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 170 | return BasicVals.getValue(~getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 173 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 174 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 175 | nonloc::ConcreteInt |
| 176 | nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 177 | assert (U->getType() == U->getSubExpr()->getType()); |
| 178 | assert (U->getType()->isIntegerType()); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 179 | return BasicVals.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 183 | // Transfer function dispatch for Locs. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | |
Ted Kremenek | ccaad9d | 2008-10-30 17:53:23 +0000 | [diff] [blame] | 186 | SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, |
| 187 | BinaryOperator::Opcode Op, |
| 188 | const loc::ConcreteInt& R) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 189 | |
| 190 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 191 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 192 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 193 | const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 194 | |
| 195 | if (X) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 196 | return loc::ConcreteInt(*X); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 197 | else |
| 198 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 201 | //===----------------------------------------------------------------------===// |
Ted Kremenek | da9ae60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 202 | // Utility methods for constructing SVals. |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | |
Ted Kremenek | 8e5fb28 | 2009-04-09 16:46:55 +0000 | [diff] [blame] | 205 | SVal ValueManager::makeZeroVal(QualType T) { |
Ted Kremenek | da9ae60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 206 | if (Loc::IsLocType(T)) |
| 207 | return Loc::MakeNull(BasicVals); |
| 208 | |
| 209 | if (T->isIntegerType()) |
| 210 | return NonLoc::MakeVal(BasicVals, 0, T); |
| 211 | |
| 212 | // FIXME: Handle floats. |
| 213 | // FIXME: Handle structs. |
| 214 | return UnknownVal(); |
| 215 | } |
| 216 | |
| 217 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 218 | // Utility methods for constructing Non-Locs. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 221 | NonLoc ValueManager::makeNonLoc(SymbolRef sym) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 222 | return nonloc::SymbolVal(sym); |
| 223 | } |
| 224 | |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 225 | NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, |
| 226 | const APSInt& v, QualType T) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 227 | // The Environment ensures we always get a persistent APSInt in |
| 228 | // BasicValueFactory, so we don't need to get the APSInt from |
| 229 | // BasicValueFactory again. |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 230 | assert(!Loc::IsLocType(T)); |
| 231 | return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T)); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 234 | NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, |
| 235 | const SymExpr *rhs, QualType T) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 236 | assert(SymMgr.getType(lhs) == SymMgr.getType(rhs)); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 237 | assert(!Loc::IsLocType(T)); |
| 238 | return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T)); |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 241 | NonLoc NonLoc::MakeIntVal(BasicValueFactory& BasicVals, uint64_t X, |
| 242 | bool isUnsigned) { |
| 243 | return nonloc::ConcreteInt(BasicVals.getIntValue(X, isUnsigned)); |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 244 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 245 | |
Zhongxing Xu | 8b86273 | 2008-11-24 09:38:21 +0000 | [diff] [blame] | 246 | NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, |
| 247 | unsigned BitWidth, bool isUnsigned) { |
| 248 | return nonloc::ConcreteInt(BasicVals.getValue(X, BitWidth, isUnsigned)); |
| 249 | } |
| 250 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 251 | NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) { |
| 252 | return nonloc::ConcreteInt(BasicVals.getValue(X, T)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 255 | NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 256 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 257 | return nonloc::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 258 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 261 | NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APInt& I, |
| 262 | bool isUnsigned) { |
| 263 | return nonloc::ConcreteInt(BasicVals.getValue(I, isUnsigned)); |
| 264 | } |
| 265 | |
Zhongxing Xu | 8b86273 | 2008-11-24 09:38:21 +0000 | [diff] [blame] | 266 | NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APSInt& I) { |
| 267 | return nonloc::ConcreteInt(BasicVals.getValue(I)); |
| 268 | } |
| 269 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 270 | NonLoc NonLoc::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) { |
| 271 | return nonloc::ConcreteInt(BasicVals.getTruthValue(b)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 274 | NonLoc ValueManager::makeTruthVal(bool b, QualType T) { |
| 275 | return nonloc::ConcreteInt(BasicVals.getTruthValue(b, T)); |
| 276 | } |
| 277 | |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 278 | NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals, |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 279 | BasicValueFactory& BasicVals) { |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 280 | return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals)); |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 283 | SVal ValueManager::getRValueSymbolVal(const MemRegion* R) { |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 284 | SymbolRef sym = SymMgr.getRegionRValueSymbol(R); |
| 285 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 286 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 287 | QualType T = TR->getRValueType(SymMgr.getContext()); |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 288 | |
| 289 | // If T is of function pointer type, create a CodeTextRegion wrapping a |
| 290 | // symbol. |
| 291 | if (T->isFunctionPointerType()) { |
| 292 | return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T)); |
| 293 | } |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 294 | |
| 295 | if (Loc::IsLocType(T)) |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 296 | return Loc::MakeVal(MemMgr.getSymbolicRegion(sym)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 298 | // Only handle integers for now. |
Zhongxing Xu | 867418f | 2009-04-09 05:57:11 +0000 | [diff] [blame] | 299 | if (T->isIntegerType() && T->isScalarType()) |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 300 | return makeNonLoc(sym); |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return UnknownVal(); |
Zhongxing Xu | eabf776 | 2008-11-19 11:03:17 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 306 | SVal ValueManager::getConjuredSymbolVal(const Expr* E, unsigned Count) { |
Ted Kremenek | bb9b271 | 2009-03-20 20:10:45 +0000 | [diff] [blame] | 307 | QualType T = E->getType(); |
Zhongxing Xu | 867418f | 2009-04-09 05:57:11 +0000 | [diff] [blame] | 308 | SymbolRef sym = SymMgr.getConjuredSymbol(E, Count); |
| 309 | |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 310 | // If T is of function pointer type, create a CodeTextRegion wrapping a |
| 311 | // symbol. |
| 312 | if (T->isFunctionPointerType()) { |
| 313 | return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T)); |
| 314 | } |
| 315 | |
Zhongxing Xu | 867418f | 2009-04-09 05:57:11 +0000 | [diff] [blame] | 316 | if (Loc::IsLocType(T)) |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 317 | return Loc::MakeVal(MemMgr.getSymbolicRegion(sym)); |
Zhongxing Xu | 867418f | 2009-04-09 05:57:11 +0000 | [diff] [blame] | 318 | |
| 319 | if (T->isIntegerType() && T->isScalarType()) |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 320 | return makeNonLoc(sym); |
Ted Kremenek | bb9b271 | 2009-03-20 20:10:45 +0000 | [diff] [blame] | 321 | |
| 322 | return UnknownVal(); |
| 323 | } |
| 324 | |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 325 | SVal ValueManager::getConjuredSymbolVal(const Expr* E, QualType T, |
| 326 | unsigned Count) { |
| 327 | |
Zhongxing Xu | fe1635b | 2009-04-09 06:30:17 +0000 | [diff] [blame] | 328 | SymbolRef sym = SymMgr.getConjuredSymbol(E, T, Count); |
| 329 | |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 330 | // If T is of function pointer type, create a CodeTextRegion wrapping a |
| 331 | // symbol. |
| 332 | if (T->isFunctionPointerType()) { |
| 333 | return Loc::MakeVal(MemMgr.getCodeTextRegion(sym, T)); |
| 334 | } |
| 335 | |
Zhongxing Xu | fe1635b | 2009-04-09 06:30:17 +0000 | [diff] [blame] | 336 | if (Loc::IsLocType(T)) |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 337 | return Loc::MakeVal(MemMgr.getSymbolicRegion(sym)); |
Zhongxing Xu | fe1635b | 2009-04-09 06:30:17 +0000 | [diff] [blame] | 338 | |
| 339 | if (T->isIntegerType() && T->isScalarType()) |
Ted Kremenek | fc3388d | 2009-04-10 18:11:44 +0000 | [diff] [blame] | 340 | return makeNonLoc(sym); |
Zhongxing Xu | fe1635b | 2009-04-09 06:30:17 +0000 | [diff] [blame] | 341 | |
| 342 | return UnknownVal(); |
| 343 | } |
| 344 | |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 345 | SVal ValueManager::getFunctionPointer(const FunctionDecl* FD) { |
| 346 | CodeTextRegion* R |
| 347 | = MemMgr.getCodeTextRegion(FD, Context.getPointerType(FD->getType())); |
Zhongxing Xu | 0a095fb | 2009-04-20 02:27:09 +0000 | [diff] [blame^] | 348 | return loc::MemRegionVal(R); |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Ted Kremenek | 632e8b8 | 2008-10-30 17:44:46 +0000 | [diff] [blame] | 351 | nonloc::LocAsInteger nonloc::LocAsInteger::Make(BasicValueFactory& Vals, Loc V, |
| 352 | unsigned Bits) { |
| 353 | return LocAsInteger(Vals.getPersistentSValWithData(V, Bits)); |
| 354 | } |
| 355 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 356 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 357 | // Utility methods for constructing Locs. |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 358 | //===----------------------------------------------------------------------===// |
| 359 | |
Zhongxing Xu | 2fdf555 | 2008-12-09 10:51:19 +0000 | [diff] [blame] | 360 | Loc Loc::MakeVal(const MemRegion* R) { return loc::MemRegionVal(R); } |
| 361 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 362 | Loc Loc::MakeVal(AddrLabelExpr* E) { return loc::GotoLabel(E->getLabel()); } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | da9ae60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 364 | Loc Loc::MakeNull(BasicValueFactory &BasicVals) { |
| 365 | return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth()); |
| 366 | } |
| 367 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
| 369 | // Pretty-Printing. |
| 370 | //===----------------------------------------------------------------------===// |
| 371 | |
Daniel Dunbar | 4a77edb | 2009-03-10 18:00:19 +0000 | [diff] [blame] | 372 | void SVal::printStdErr() const { print(llvm::errs()); } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 373 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 374 | void SVal::print(std::ostream& Out) const { |
Ted Kremenek | b8b4161 | 2008-10-30 18:35:10 +0000 | [diff] [blame] | 375 | llvm::raw_os_ostream out(Out); |
| 376 | print(out); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 379 | void SVal::print(llvm::raw_ostream& Out) const { |
| 380 | |
| 381 | switch (getBaseKind()) { |
| 382 | |
| 383 | case UnknownKind: |
| 384 | Out << "Invalid"; break; |
| 385 | |
| 386 | case NonLocKind: |
| 387 | cast<NonLoc>(this)->print(Out); break; |
| 388 | |
| 389 | case LocKind: |
| 390 | cast<Loc>(this)->print(Out); break; |
| 391 | |
| 392 | case UndefinedKind: |
| 393 | Out << "Undefined"; break; |
| 394 | |
| 395 | default: |
| 396 | assert (false && "Invalid SVal."); |
| 397 | } |
| 398 | } |
| 399 | |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 400 | void NonLoc::print(llvm::raw_ostream& Out) const { |
| 401 | |
| 402 | switch (getSubKind()) { |
| 403 | |
| 404 | case nonloc::ConcreteIntKind: |
| 405 | Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue(); |
| 406 | |
| 407 | if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned()) |
| 408 | Out << 'U'; |
| 409 | |
| 410 | break; |
| 411 | |
| 412 | case nonloc::SymbolValKind: |
| 413 | Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol(); |
| 414 | break; |
| 415 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 416 | case nonloc::SymExprValKind: { |
| 417 | const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this); |
| 418 | const SymExpr *SE = C.getSymbolicExpression(); |
| 419 | Out << SE; |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 420 | break; |
| 421 | } |
| 422 | |
| 423 | case nonloc::LocAsIntegerKind: { |
| 424 | const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this); |
| 425 | C.getLoc().print(Out); |
| 426 | Out << " [as " << C.getNumBits() << " bit integer]"; |
| 427 | break; |
| 428 | } |
| 429 | |
Ted Kremenek | a6fac4e | 2008-10-30 18:01:28 +0000 | [diff] [blame] | 430 | case nonloc::CompoundValKind: { |
| 431 | const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this); |
Ted Kremenek | b8b4161 | 2008-10-30 18:35:10 +0000 | [diff] [blame] | 432 | Out << " {"; |
| 433 | bool first = true; |
| 434 | for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) { |
| 435 | if (first) { Out << ' '; first = false; } |
| 436 | else Out << ", "; |
Ted Kremenek | a6fac4e | 2008-10-30 18:01:28 +0000 | [diff] [blame] | 437 | (*I).print(Out); |
Ted Kremenek | b8b4161 | 2008-10-30 18:35:10 +0000 | [diff] [blame] | 438 | } |
Ted Kremenek | a6fac4e | 2008-10-30 18:01:28 +0000 | [diff] [blame] | 439 | Out << " }"; |
| 440 | break; |
| 441 | } |
| 442 | |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 443 | default: |
| 444 | assert (false && "Pretty-printed not implemented for this NonLoc."); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void Loc::print(llvm::raw_ostream& Out) const { |
| 450 | |
| 451 | switch (getSubKind()) { |
| 452 | |
| 453 | case loc::ConcreteIntKind: |
| 454 | Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() |
| 455 | << " (Loc)"; |
| 456 | break; |
| 457 | |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 458 | case loc::GotoLabelKind: |
| 459 | Out << "&&" |
| 460 | << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 461 | break; |
| 462 | |
| 463 | case loc::MemRegionKind: |
| 464 | Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString(); |
| 465 | break; |
| 466 | |
| 467 | case loc::FuncValKind: |
| 468 | Out << "function " |
| 469 | << cast<loc::FuncVal>(this)->getDecl()->getIdentifier()->getName(); |
| 470 | break; |
| 471 | |
Zhongxing Xu | 9012bff | 2008-10-24 06:00:12 +0000 | [diff] [blame] | 472 | default: |
| 473 | assert (false && "Pretty-printing not implemented for this Loc."); |
| 474 | break; |
| 475 | } |
| 476 | } |