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 | cc409b7 | 2008-02-14 17:30:51 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/RValues.h" |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | using llvm::dyn_cast; |
| 20 | using llvm::cast; |
| 21 | using llvm::APSInt; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 24 | // Symbol Iteration. |
| 25 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 27 | RVal::symbol_iterator RVal::symbol_begin() const { |
| 28 | if (isa<lval::SymbolVal>(this)) |
| 29 | return (symbol_iterator) (&Data); |
| 30 | else if (isa<nonlval::SymbolVal>(this)) |
| 31 | return (symbol_iterator) (&Data); |
| 32 | else if (isa<nonlval::SymIntConstraintVal>(this)) { |
| 33 | const SymIntConstraint& C = |
| 34 | cast<nonlval::SymIntConstraintVal>(this)->getConstraint(); |
| 35 | |
| 36 | return (symbol_iterator) &C.getSymbol(); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return NULL; |
| 40 | } |
| 41 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 42 | RVal::symbol_iterator RVal::symbol_end() const { |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 43 | symbol_iterator X = symbol_begin(); |
| 44 | return X ? X+1 : NULL; |
| 45 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 48 | // Transfer function dispatch for Non-LVals. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 51 | RVal |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 52 | nonlval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 53 | const nonlval::ConcreteInt& R) const { |
| 54 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 55 | const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 56 | |
| 57 | if (X) |
| 58 | return nonlval::ConcreteInt(*X); |
| 59 | else |
| 60 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | // Bitwise-Complement. |
| 65 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 66 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 67 | nonlval::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const { |
| 68 | return BasicVals.getValue(~getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 71 | // Unary Minus. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 73 | nonlval::ConcreteInt |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 74 | nonlval::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 75 | assert (U->getType() == U->getSubExpr()->getType()); |
| 76 | assert (U->getType()->isIntegerType()); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 77 | return BasicVals.getValue(-getValue()); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 81 | // Transfer function dispatch for LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 84 | RVal |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 85 | lval::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals, BinaryOperator::Opcode Op, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 86 | const lval::ConcreteInt& R) const { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 87 | |
| 88 | assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub || |
| 89 | (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE)); |
| 90 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 91 | const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 92 | |
| 93 | if (X) |
| 94 | return lval::ConcreteInt(*X); |
| 95 | else |
| 96 | return UndefinedVal(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 99 | NonLVal LVal::EQ(BasicValueFactory& BasicVals, const LVal& R) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 101 | switch (getSubKind()) { |
| 102 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 103 | assert(false && "EQ not implemented for this LVal."); |
| 104 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 106 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 107 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 108 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 109 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 111 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 112 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 113 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 115 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 116 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 117 | BinaryOperator::EQ, |
| 118 | cast<lval::ConcreteInt>(this)->getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 119 | |
| 120 | return nonlval::SymIntConstraintVal(C); |
| 121 | } |
| 122 | |
| 123 | break; |
| 124 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 125 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 126 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 127 | |
| 128 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 129 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 130 | BinaryOperator::EQ, |
| 131 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 132 | |
| 133 | return nonlval::SymIntConstraintVal(C); |
| 134 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 136 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement unification."); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 137 | |
| 138 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 141 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 142 | if (isa<lval::DeclVal>(R)) { |
| 143 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 144 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 148 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 150 | return NonLVal::MakeIntTruthVal(BasicVals, false); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 153 | NonLVal LVal::NE(BasicValueFactory& BasicVals, const LVal& R) const { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 154 | switch (getSubKind()) { |
| 155 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 156 | assert(false && "NE not implemented for this LVal."); |
| 157 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 159 | case lval::ConcreteIntKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 160 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 161 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | cast<lval::ConcreteInt>(R).getValue(); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 164 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 165 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 166 | else if (isa<lval::SymbolVal>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 167 | |
| 168 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 169 | BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 170 | BinaryOperator::NE, |
| 171 | cast<lval::ConcreteInt>(this)->getValue()); |
| 172 | |
| 173 | return nonlval::SymIntConstraintVal(C); |
| 174 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 176 | break; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 178 | case lval::SymbolValKind: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 179 | if (isa<lval::ConcreteInt>(R)) { |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 180 | |
| 181 | const SymIntConstraint& C = |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 182 | BasicVals.getConstraint(cast<lval::SymbolVal>(this)->getSymbol(), |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 183 | BinaryOperator::NE, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 184 | cast<lval::ConcreteInt>(R).getValue()); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 185 | |
| 186 | return nonlval::SymIntConstraintVal(C); |
| 187 | } |
| 188 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 189 | assert (!isa<lval::SymbolVal>(R) && "FIXME: Implement sym !=."); |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 190 | |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | case lval::DeclValKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 195 | if (isa<lval::DeclVal>(R)) { |
| 196 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(R); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 197 | return NonLVal::MakeIntTruthVal(BasicVals, b); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 198 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 200 | break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 201 | } |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 203 | return NonLVal::MakeIntTruthVal(BasicVals, true); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 207 | // Utility methods for constructing Non-LVals. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 208 | //===----------------------------------------------------------------------===// |
| 209 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 210 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) { |
| 211 | return nonlval::ConcreteInt(BasicVals.getValue(X, T)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 214 | NonLVal NonLVal::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 216 | return nonlval::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(), |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 217 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 220 | NonLVal NonLVal::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) { |
| 221 | return nonlval::ConcreteInt(BasicVals.getTruthValue(b)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 224 | RVal RVal::GetSymbolValue(SymbolManager& SymMgr, VarDecl* D) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 226 | QualType T = D->getType(); |
| 227 | |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame^] | 228 | if (T->isPointerLikeType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 229 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame^] | 230 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 234 | // Utility methods for constructing LVals. |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 235 | //===----------------------------------------------------------------------===// |
| 236 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 237 | LVal LVal::MakeVal(AddrLabelExpr* E) { return lval::GotoLabel(E->getLabel()); } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 239 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 240 | // Utility methods for constructing RVals (both NonLVals and LVals). |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | |
| 243 | RVal RVal::MakeVal(BasicValueFactory& BasicVals, DeclRefExpr* E) { |
| 244 | |
| 245 | ValueDecl* D = cast<DeclRefExpr>(E)->getDecl(); |
| 246 | |
| 247 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 248 | return lval::DeclVal(VD); |
| 249 | } |
| 250 | else if (EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) { |
| 251 | |
| 252 | // FIXME: Do we need to cache a copy of this enum, since it |
| 253 | // already has persistent storage? We do this because we |
| 254 | // are comparing states using pointer equality. Perhaps there is |
| 255 | // a better way, since APInts are fairly lightweight. |
| 256 | |
| 257 | return nonlval::ConcreteInt(BasicVals.getValue(ED->getInitVal())); |
| 258 | } |
| 259 | else if (FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) { |
| 260 | return lval::FuncVal(FD); |
| 261 | } |
| 262 | |
| 263 | assert (false && |
| 264 | "ValueDecl support for this ValueDecl not implemented."); |
| 265 | |
| 266 | return UnknownVal(); |
| 267 | } |
| 268 | |
| 269 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 270 | // Pretty-Printing. |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 273 | void RVal::printStdErr() const { print(*llvm::cerr.stream()); } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 275 | void RVal::print(std::ostream& Out) const { |
| 276 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 277 | switch (getBaseKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 279 | case UnknownKind: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 280 | Out << "Invalid"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 282 | case NonLValKind: |
| 283 | cast<NonLVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 285 | case LValKind: |
| 286 | cast<LVal>(this)->print(Out); break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 288 | case UndefinedKind: |
| 289 | Out << "Undefined"; break; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 290 | |
| 291 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 292 | assert (false && "Invalid RVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 296 | static void printOpcode(std::ostream& Out, BinaryOperator::Opcode Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 297 | |
| 298 | switch (Op) { |
| 299 | case BinaryOperator::Mul: Out << '*' ; break; |
| 300 | case BinaryOperator::Div: Out << '/' ; break; |
| 301 | case BinaryOperator::Rem: Out << '%' ; break; |
| 302 | case BinaryOperator::Add: Out << '+' ; break; |
| 303 | case BinaryOperator::Sub: Out << '-' ; break; |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 304 | case BinaryOperator::Shl: Out << "<<" ; break; |
| 305 | case BinaryOperator::Shr: Out << ">>" ; break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 306 | case BinaryOperator::LT: Out << "<" ; break; |
| 307 | case BinaryOperator::GT: Out << '>' ; break; |
| 308 | case BinaryOperator::LE: Out << "<=" ; break; |
| 309 | case BinaryOperator::GE: Out << ">=" ; break; |
| 310 | case BinaryOperator::EQ: Out << "==" ; break; |
| 311 | case BinaryOperator::NE: Out << "!=" ; break; |
| 312 | case BinaryOperator::And: Out << '&' ; break; |
| 313 | case BinaryOperator::Xor: Out << '^' ; break; |
| 314 | case BinaryOperator::Or: Out << '|' ; break; |
| 315 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 316 | default: assert(false && "Not yet implemented."); |
| 317 | } |
| 318 | } |
| 319 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 320 | void NonLVal::print(std::ostream& Out) const { |
| 321 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 322 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 324 | case nonlval::ConcreteIntKind: |
| 325 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 326 | |
| 327 | if (cast<nonlval::ConcreteInt>(this)->getValue().isUnsigned()) |
| 328 | Out << 'U'; |
| 329 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 330 | break; |
| 331 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 332 | case nonlval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 333 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 334 | break; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 335 | |
| 336 | case nonlval::SymIntConstraintValKind: { |
| 337 | const nonlval::SymIntConstraintVal& C = |
| 338 | *cast<nonlval::SymIntConstraintVal>(this); |
| 339 | |
| 340 | Out << '$' << C.getConstraint().getSymbol() << ' '; |
| 341 | printOpcode(Out, C.getConstraint().getOpcode()); |
| 342 | Out << ' ' << C.getConstraint().getInt().toString(); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 343 | |
| 344 | if (C.getConstraint().getInt().isUnsigned()) |
| 345 | Out << 'U'; |
| 346 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 349 | |
| 350 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 351 | assert (false && "Pretty-printed not implemented for this NonLVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 356 | void LVal::print(std::ostream& Out) const { |
| 357 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 358 | switch (getSubKind()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 360 | case lval::ConcreteIntKind: |
| 361 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 362 | << " (LVal)"; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 363 | break; |
| 364 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 365 | case lval::SymbolValKind: |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 366 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbol(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 367 | break; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 368 | |
| 369 | case lval::GotoLabelKind: |
| 370 | Out << "&&" |
| 371 | << cast<lval::GotoLabel>(this)->getLabel()->getID()->getName(); |
| 372 | break; |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 374 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 375 | Out << '&' |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 376 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
| 377 | break; |
| 378 | |
| 379 | case lval::FuncValKind: |
| 380 | Out << "function " |
| 381 | << cast<lval::FuncVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 385 | assert (false && "Pretty-printing not implemented for this LVal."); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | } |