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 | // |
| 10 | // This files defines RValue, LValue, and NonLValue, classes that represent |
| 11 | // abstract r-values for use with path-sensitive value tracking. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "RValues.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using llvm::dyn_cast; |
| 19 | using llvm::cast; |
| 20 | using llvm::APSInt; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // SymbolManager. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | SymbolID SymbolManager::getSymbol(ParmVarDecl* D) { |
| 27 | SymbolID& X = DataToSymbol[D]; |
| 28 | |
| 29 | if (!X.isInitialized()) { |
| 30 | X = SymbolToData.size(); |
| 31 | SymbolToData.push_back(D); |
| 32 | } |
| 33 | |
| 34 | return X; |
| 35 | } |
| 36 | |
| 37 | SymbolManager::SymbolManager() {} |
| 38 | SymbolManager::~SymbolManager() {} |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 41 | // Values and ValueManager. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | ValueManager::~ValueManager() { |
| 45 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 46 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 47 | // frees an aux. memory allocated to represent very large constants. |
| 48 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 49 | I->getValue().~APSInt(); |
| 50 | } |
| 51 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 52 | const APSInt& ValueManager::getValue(const APSInt& X) { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 53 | llvm::FoldingSetNodeID ID; |
| 54 | void* InsertPos; |
| 55 | typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy; |
| 56 | |
| 57 | X.Profile(ID); |
| 58 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 59 | |
| 60 | if (!P) { |
| 61 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 62 | new (P) FoldNodeTy(X); |
| 63 | APSIntSet.InsertNode(P, InsertPos); |
| 64 | } |
| 65 | |
| 66 | return *P; |
| 67 | } |
| 68 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 69 | const APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, |
| 70 | bool isUnsigned) { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 71 | APSInt V(BitWidth, isUnsigned); |
| 72 | V = X; |
| 73 | return getValue(V); |
| 74 | } |
| 75 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 76 | const APSInt& ValueManager::getValue(uint64_t X, QualType T, |
| 77 | SourceLocation Loc) { |
| 78 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 79 | unsigned bits = Ctx.getTypeSize(T, Loc); |
| 80 | APSInt V(bits, T->isUnsignedIntegerType()); |
| 81 | V = X; |
| 82 | return getValue(V); |
| 83 | } |
| 84 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 85 | const SymIntConstraint& |
| 86 | ValueManager::getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
| 87 | const llvm::APSInt& V) { |
| 88 | |
| 89 | llvm::FoldingSetNodeID ID; |
| 90 | SymIntConstraint::Profile(ID, sym, Op, V); |
| 91 | void* InsertPos; |
| 92 | |
| 93 | SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos); |
| 94 | |
| 95 | if (!C) { |
| 96 | C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>(); |
| 97 | new (C) SymIntConstraint(sym, Op, V); |
| 98 | SymIntCSet.InsertNode(C, InsertPos); |
| 99 | } |
| 100 | |
| 101 | return *C; |
| 102 | } |
| 103 | |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 104 | //===----------------------------------------------------------------------===// |
| 105 | // Transfer function for Casts. |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 109 | switch (getBaseKind()) { |
| 110 | default: assert(false && "Invalid RValue."); break; |
| 111 | case LValueKind: return cast<LValue>(this)->Cast(ValMgr, CastExpr); |
| 112 | case NonLValueKind: return cast<NonLValue>(this)->Cast(ValMgr, CastExpr); |
| 113 | case UninitializedKind: case InvalidKind: break; |
| 114 | } |
| 115 | |
| 116 | return *this; |
| 117 | } |
| 118 | |
| 119 | RValue LValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 120 | if (CastExpr->getType()->isPointerType()) |
| 121 | return *this; |
| 122 | |
| 123 | assert (CastExpr->getType()->isIntegerType()); |
| 124 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 125 | if (!isa<lval::ConcreteInt>(*this)) |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 126 | return InvalidValue(); |
| 127 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 128 | APSInt V = cast<lval::ConcreteInt>(this)->getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 129 | QualType T = CastExpr->getType(); |
| 130 | V.setIsUnsigned(T->isUnsignedIntegerType()); |
| 131 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 132 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | RValue NonLValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 136 | if (!isa<nonlval::ConcreteInt>(this)) |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 137 | return InvalidValue(); |
| 138 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 139 | APSInt V = cast<nonlval::ConcreteInt>(this)->getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 140 | QualType T = CastExpr->getType(); |
| 141 | V.setIsUnsigned(T->isUnsignedIntegerType()); |
| 142 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 143 | |
| 144 | if (CastExpr->getType()->isPointerType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 145 | return lval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 146 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 147 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 148 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 149 | |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | // Transfer function dispatch for Non-LValues. |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 154 | NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 155 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 156 | case nonlval::ConcreteIntKind: |
| 157 | return cast<nonlval::ConcreteInt>(this)->UnaryMinus(ValMgr, U); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 158 | default: |
| 159 | return cast<NonLValue>(InvalidValue()); |
| 160 | } |
| 161 | } |
| 162 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 163 | NonLValue NonLValue::BitwiseComplement(ValueManager& ValMgr) const { |
| 164 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 165 | case nonlval::ConcreteIntKind: |
| 166 | return cast<nonlval::ConcreteInt>(this)->BitwiseComplement(ValMgr); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 167 | default: |
| 168 | return cast<NonLValue>(InvalidValue()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 173 | #define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\ |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 174 | case (k1##Kind*nonlval::NumKind+k2##Kind):\ |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 175 | return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS)); |
| 176 | |
| 177 | #define NONLVALUE_DISPATCH(Op)\ |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 178 | switch (getSubKind()*nonlval::NumKind+RHS.getSubKind()){\ |
| 179 | NONLVALUE_DISPATCH_CASE(nonlval::ConcreteInt,nonlval::ConcreteInt,Op)\ |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 180 | default:\ |
| 181 | if (getBaseKind() == UninitializedKind ||\ |
| 182 | RHS.getBaseKind() == UninitializedKind)\ |
| 183 | return cast<NonLValue>(UninitializedValue());\ |
| 184 | assert (!isValid() || !RHS.isValid() && "Missing case.");\ |
| 185 | break;\ |
| 186 | }\ |
| 187 | return cast<NonLValue>(InvalidValue()); |
| 188 | |
| 189 | NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 190 | NONLVALUE_DISPATCH(Add) |
| 191 | } |
| 192 | |
| 193 | NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 194 | NONLVALUE_DISPATCH(Sub) |
| 195 | } |
| 196 | |
| 197 | NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 198 | NONLVALUE_DISPATCH(Mul) |
| 199 | } |
| 200 | |
| 201 | NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 202 | NONLVALUE_DISPATCH(Div) |
| 203 | } |
| 204 | |
| 205 | NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 206 | NONLVALUE_DISPATCH(Rem) |
| 207 | } |
| 208 | |
| 209 | NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 210 | NONLVALUE_DISPATCH(EQ) |
| 211 | } |
| 212 | |
| 213 | NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 214 | NONLVALUE_DISPATCH(NE) |
| 215 | } |
| 216 | |
| 217 | #undef NONLVALUE_DISPATCH_CASE |
| 218 | #undef NONLVALUE_DISPATCH |
| 219 | |
| 220 | //===----------------------------------------------------------------------===// |
| 221 | // Transfer function dispatch for LValues. |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | |
| 224 | |
| 225 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
| 226 | if (getSubKind() != RHS.getSubKind()) |
| 227 | return NonLValue::GetIntTruthValue(ValMgr, false); |
| 228 | |
| 229 | switch (getSubKind()) { |
| 230 | default: |
| 231 | assert(false && "EQ not implemented for this LValue."); |
| 232 | return cast<NonLValue>(InvalidValue()); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 234 | case lval::ConcreteIntKind: { |
| 235 | bool b = cast<lval::ConcreteInt>(this)->getValue() == |
| 236 | cast<lval::ConcreteInt>(RHS).getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 237 | |
| 238 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 239 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 241 | case lval::DeclValKind: { |
| 242 | bool b = cast<lval::DeclVal>(*this) == cast<lval::DeclVal>(RHS); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 243 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
| 249 | if (getSubKind() != RHS.getSubKind()) |
| 250 | return NonLValue::GetIntTruthValue(ValMgr, true); |
| 251 | |
| 252 | switch (getSubKind()) { |
| 253 | default: |
| 254 | assert(false && "EQ not implemented for this LValue."); |
| 255 | return cast<NonLValue>(InvalidValue()); |
| 256 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 257 | case lval::ConcreteIntKind: { |
| 258 | bool b = cast<lval::ConcreteInt>(this)->getValue() != |
| 259 | cast<lval::ConcreteInt>(RHS).getValue(); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 260 | |
| 261 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 262 | } |
| 263 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 264 | case lval::DeclValKind: { |
| 265 | bool b = cast<lval::DeclVal>(*this) != cast<lval::DeclVal>(RHS); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 266 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | // Utility methods for constructing Non-LValues. |
| 274 | //===----------------------------------------------------------------------===// |
| 275 | |
| 276 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 277 | SourceLocation Loc) { |
| 278 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 279 | return nonlval::ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 283 | return nonlval::ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 284 | I->getType()->isUnsignedIntegerType()))); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | RValue RValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 288 | QualType T = D->getType(); |
| 289 | |
| 290 | if (T->isPointerType() || T->isReferenceType()) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 291 | return lval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 292 | else |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 293 | return nonlval::SymbolVal(SymMgr.getSymbol(D)); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | // Pretty-Printing. |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | |
| 300 | void RValue::print(std::ostream& Out) const { |
| 301 | switch (getBaseKind()) { |
| 302 | case InvalidKind: |
| 303 | Out << "Invalid"; |
| 304 | break; |
| 305 | |
| 306 | case NonLValueKind: |
| 307 | cast<NonLValue>(this)->print(Out); |
| 308 | break; |
| 309 | |
| 310 | case LValueKind: |
| 311 | cast<LValue>(this)->print(Out); |
| 312 | break; |
| 313 | |
| 314 | case UninitializedKind: |
| 315 | Out << "Uninitialized"; |
| 316 | break; |
| 317 | |
| 318 | default: |
| 319 | assert (false && "Invalid RValue."); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void NonLValue::print(std::ostream& Out) const { |
| 324 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 325 | case nonlval::ConcreteIntKind: |
| 326 | Out << cast<nonlval::ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 327 | break; |
| 328 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 329 | case nonlval::SymbolValKind: |
| 330 | Out << '$' << cast<nonlval::SymbolVal>(this)->getSymbolID(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 331 | break; |
| 332 | |
| 333 | default: |
| 334 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void LValue::print(std::ostream& Out) const { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 340 | switch (getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 341 | case lval::ConcreteIntKind: |
| 342 | Out << cast<lval::ConcreteInt>(this)->getValue().toString() |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 343 | << " (LValue)"; |
| 344 | break; |
| 345 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 346 | case lval::SymbolValKind: |
| 347 | Out << '$' << cast<lval::SymbolVal>(this)->getSymbolID(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 348 | break; |
| 349 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 350 | case lval::DeclValKind: |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 351 | Out << '&' |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame^] | 352 | << cast<lval::DeclVal>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | default: |
| 356 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 357 | break; |
| 358 | } |
| 359 | } |