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