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