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