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