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 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines RVal, LVal, and NonLVal, 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 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 28 | RVal::symbol_iterator RVal::symbol_begin() const { |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 29 | |
| 30 | // FIXME: This is a rat's nest. Cleanup. |
| 31 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 32 | if (isa<lval::SymbolVal>(this)) |
| 33 | return (symbol_iterator) (&Data); |
| 34 | else if (isa<nonlval::SymbolVal>(this)) |
| 35 | return (symbol_iterator) (&Data); |
| 36 | else if (isa<nonlval::SymIntConstraintVal>(this)) { |
| 37 | const SymIntConstraint& C = |
| 38 | cast<nonlval::SymIntConstraintVal>(this)->getConstraint(); |
| 39 | |
| 40 | return (symbol_iterator) &C.getSymbol(); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 41 | } |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 42 | else if (isa<nonlval::LValAsInteger>(this)) { |
| 43 | const nonlval::LValAsInteger& V = cast<nonlval::LValAsInteger>(*this); |
| 44 | return V.getPersistentLVal().symbol_begin(); |
| 45 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame^] | 46 | |
| 47 | // FIXME: We need to iterate over the symbols of regions. |
| 48 | |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 52 | RVal::symbol_iterator RVal::symbol_end() const { |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 53 | symbol_iterator X = symbol_begin(); |
| 54 | return X ? X+1 : NULL; |
| 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 | |
| 61 | bool RVal::isZeroConstant() const { |
| 62 | if (isa<lval::ConcreteInt>(*this)) |
| 63 | return cast<lval::ConcreteInt>(*this).getValue() == 0; |
| 64 | else if (isa<nonlval::ConcreteInt>(*this)) |
| 65 | return cast<nonlval::ConcreteInt>(*this).getValue() == 0; |
| 66 | else |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 72 | // Transfer function dispatch for Non-LVals. |
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 | |
Ted Kremenek | 75b0a1c | 2008-07-18 15:59:33 +0000 | [diff] [blame] | 75 | RVal nonlval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, |
| 76 | BinaryOperator::Opcode Op, |
| 77 | const nonlval::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) |
| 83 | return nonlval::ConcreteInt(*X); |
| 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 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 90 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 91 | nonlval::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const { |
| 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 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 97 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 98 | nonlval::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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 105 | // Transfer function dispatch for LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 108 | RVal |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 109 | lval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 110 | const lval::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) |
| 118 | return lval::ConcreteInt(*X); |
| 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 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 123 | NonLVal LVal::EQ(BasicValueFactory& BasicVals, const LVal& 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: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 127 | assert(false && "EQ not implemented for this LVal."); |
| 128 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 130 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 131 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 132 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 133 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 135 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 136 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 137 | else if (isa<lval::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 = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 140 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 141 | BinaryOperator::EQ, |
| 142 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 143 | |
| 144 | return nonlval::SymIntConstraintVal(C); |
| 145 | } |
| 146 | |
| 147 | break; |
| 148 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 149 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 150 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 151 | |
| 152 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 153 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 154 | BinaryOperator::EQ, |
| 155 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 156 | |
| 157 | return nonlval::SymIntConstraintVal(C); |
| 158 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 160 | assert (!isa<lval::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 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 165 | case lval::MemRegionKind: |
| 166 | if (isa<lval::MemRegionVal>(R)) { |
| 167 | bool b = cast<lval::MemRegionVal>(*this) == cast<lval::MemRegionVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 168 | return NonLVal::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 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 174 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 177 | NonLVal LVal::NE(BasicValueFactory& BasicVals, const LVal& R) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 178 | switch (getSubKind()) { |
| 179 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 180 | assert(false && "NE not implemented for this LVal."); |
| 181 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 183 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 184 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 185 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 186 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 188 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 190 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 191 | |
| 192 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 193 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 194 | BinaryOperator::NE, |
| 195 | cast<lval::ConcreteInt>(this)->getValue()); |
| 196 | |
| 197 | return nonlval::SymIntConstraintVal(C); |
| 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 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 202 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 203 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 204 | |
| 205 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 206 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 207 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 208 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 209 | |
| 210 | return nonlval::SymIntConstraintVal(C); |
| 211 | } |
| 212 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 213 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement sym !=."); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 214 | |
| 215 | break; |
| 216 | } |
| 217 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 218 | case lval::MemRegionKind: |
| 219 | if (isa<lval::MemRegionVal>(R)) { |
| 220 | bool b = cast<lval::MemRegionVal>(*this)==cast<lval::MemRegionVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 221 | return NonLVal::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 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 227 | return NonLVal::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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 231 | // Utility methods for constructing Non-LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 232 | //===----------------------------------------------------------------------===// |
| 233 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 234 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) { |
| 235 | return nonlval::ConcreteInt(BasicVals.getValue(X, T)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 238 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 240 | return nonlval::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 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 244 | NonLVal NonLVal::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) { |
| 245 | return nonlval::ConcreteInt(BasicVals.getTruthValue(b)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 248 | RVal RVal::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 | |
Ted Kremenek | 8da6ca9 | 2008-10-01 05:02:13 +0000 | [diff] [blame] | 252 | if (LVal::IsLValType(T)) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 253 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame] | 255 | return nonlval::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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 259 | // Utility methods for constructing LVals. |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
| 261 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 262 | LVal LVal::MakeVal(AddrLabelExpr* E) { return lval::GotoLabel(E->getLabel()); } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 264 | LVal LVal::MakeVal(StringLiteral* S) { |
| 265 | return lval::StringLiteralVal(S); |
| 266 | } |
| 267 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // Pretty-Printing. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 272 | void RVal::printStdErr() const { print(*llvm::cerr.stream()); } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 274 | void RVal::print(std::ostream& Out) const { |
| 275 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 276 | switch (getBaseKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 278 | case UnknownKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 279 | Out << "Invalid"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 281 | case NonLValKind: |
| 282 | cast<NonLVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 284 | case LValKind: |
| 285 | cast<LVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 287 | case UndefinedKind: |
| 288 | Out << "Undefined"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 289 | |
| 290 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 291 | assert (false && "Invalid RVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 295 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 296 | |
| 297 | switch (Op) { |
| 298 | case BinaryOperator::Mul: Out << '*' ; break; |
| 299 | case BinaryOperator::Div: Out << '/' ; break; |
| 300 | case BinaryOperator::Rem: Out << '%' ; break; |
| 301 | case BinaryOperator::Add: Out << '+' ; break; |
| 302 | case BinaryOperator::Sub: Out << '-' ; break; |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 303 | case BinaryOperator::Shl: Out << "<<" ; break; |
| 304 | case BinaryOperator::Shr: Out << ">>" ; break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 305 | case BinaryOperator::LT: Out << "<" ; break; |
| 306 | case BinaryOperator::GT: Out << '>' ; break; |
| 307 | case BinaryOperator::LE: Out << "<=" ; break; |
| 308 | case BinaryOperator::GE: Out << ">=" ; break; |
| 309 | case BinaryOperator::EQ: Out << "==" ; break; |
| 310 | case BinaryOperator::NE: Out << "!=" ; break; |
| 311 | case BinaryOperator::And: Out << '&' ; break; |
| 312 | case BinaryOperator::Xor: Out << '^' ; break; |
| 313 | case BinaryOperator::Or: Out << '|' ; break; |
| 314 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 315 | default: assert(false && "Not yet implemented."); |
| 316 | } |
| 317 | } |
| 318 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 319 | void NonLVal::print(std::ostream& Out) const { |
| 320 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 321 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 323 | case nonlval::ConcreteIntKind: |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 324 | Out << cast<nonlval::ConcreteInt>(this)->getValue().getZExtValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 325 | |
| 326 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 327 | Out << 'U'; |
| 328 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 331 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 332 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 333 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 334 | |
| 335 | case nonlval::SymIntConstraintValKind: { |
| 336 | const nonlval::SymIntConstraintVal& C = |
| 337 | *cast<nonlval::SymIntConstraintVal>(this); |
| 338 | |
| 339 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 340 | printOpcode(Out, C.getConstraint().getOpcode()); |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 341 | Out << ' ' << C.getConstraint().getInt().getZExtValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 342 | |
| 343 | if (C.getConstraint().getInt().isUnsigned()) |
| 344 | Out << 'U'; |
| 345 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 346 | break; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | case nonlval::LValAsIntegerKind: { |
| 350 | const nonlval::LValAsInteger& C = *cast<nonlval::LValAsInteger>(this); |
| 351 | C.getLVal().print(Out); |
| 352 | Out << " [as " << C.getNumBits() << " bit integer]"; |
| 353 | break; |
| 354 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 355 | |
| 356 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 357 | assert (false && "Pretty-printed not implemented for this NonLVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 362 | void LVal::print(std::ostream& Out) const { |
| 363 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 364 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 366 | case lval::ConcreteIntKind: |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 367 | Out << cast<lval::ConcreteInt>(this)->getValue().getZExtValue() |
| 368 | << " (LVal)"; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 369 | break; |
| 370 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 371 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 372 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 373 | break; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 374 | |
| 375 | case lval::GotoLabelKind: |
| 376 | Out << "&&" |
| 377 | << cast<lval::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 378 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 380 | case lval::MemRegionKind: |
| 381 | Out << '&' << cast<lval::MemRegionVal>(this)->getRegion()->getString(); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case lval::FuncValKind: |
| 385 | Out << "function " |
| 386 | << cast<lval::FuncVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 387 | break; |
| 388 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 389 | case lval::StringLiteralValKind: |
| 390 | Out << "literal \"" |
| 391 | << cast<lval::StringLiteralVal>(this)->getLiteral()->getStrData() |
| 392 | << "\""; |
| 393 | break; |
| 394 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 395 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 396 | assert (false && "Pretty-printing not implemented for this LVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |