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