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