Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1 | //== RValues.h - 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 | #ifndef LLVM_CLANG_ANALYSIS_RVALUE_H |
| 16 | #define LLVM_CLANG_ANALYSIS_RVALUE_H |
| 17 | |
| 18 | // FIXME: reduce the number of includes. |
| 19 | |
| 20 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/ASTContext.h" |
| 24 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 25 | |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/DataTypes.h" |
| 28 | #include "llvm/ADT/APSInt.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/Support/Allocator.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | #include "llvm/Support/Streams.h" |
| 36 | |
| 37 | #include <functional> |
| 38 | |
| 39 | //==------------------------------------------------------------------------==// |
| 40 | // RValue "management" data structures. |
| 41 | //==------------------------------------------------------------------------==// |
| 42 | |
| 43 | namespace clang { |
| 44 | |
| 45 | class SymbolID { |
| 46 | unsigned Data; |
| 47 | public: |
| 48 | SymbolID() : Data(~0) {} |
| 49 | SymbolID(unsigned x) : Data(x) {} |
| 50 | |
| 51 | bool isInitialized() const { return Data != (unsigned) ~0; } |
| 52 | operator unsigned() const { assert (isInitialized()); return Data; } |
| 53 | }; |
| 54 | |
| 55 | class SymbolData { |
| 56 | uintptr_t Data; |
| 57 | public: |
| 58 | enum Kind { ParmKind = 0x0, Mask = 0x3 }; |
| 59 | |
| 60 | SymbolData(ParmVarDecl* D) |
| 61 | : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {} |
| 62 | |
| 63 | inline Kind getKind() const { return (Kind) (Data & Mask); } |
| 64 | inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); } |
| 65 | inline bool operator==(const SymbolData& R) const { return Data == R.Data; } |
| 66 | }; |
| 67 | |
| 68 | class SymbolManager { |
| 69 | std::vector<SymbolData> SymbolToData; |
| 70 | |
| 71 | typedef llvm::DenseMap<void*,SymbolID> MapTy; |
| 72 | MapTy DataToSymbol; |
| 73 | |
| 74 | public: |
| 75 | SymbolManager(); |
| 76 | ~SymbolManager(); |
| 77 | |
| 78 | SymbolData getSymbolData(SymbolID id) const { |
| 79 | assert (id < SymbolToData.size()); |
| 80 | return SymbolToData[id]; |
| 81 | } |
| 82 | |
| 83 | SymbolID getSymbol(ParmVarDecl* D); |
| 84 | }; |
| 85 | |
| 86 | class ValueManager { |
| 87 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> > |
| 88 | APSIntSetTy; |
| 89 | |
| 90 | ASTContext& Ctx; |
| 91 | APSIntSetTy APSIntSet; |
| 92 | llvm::BumpPtrAllocator BPAlloc; |
| 93 | |
| 94 | public: |
| 95 | ValueManager(ASTContext& ctx) : Ctx(ctx) {} |
| 96 | ~ValueManager(); |
| 97 | |
| 98 | ASTContext& getContext() const { return Ctx; } |
| 99 | llvm::APSInt& getValue(const llvm::APSInt& X); |
| 100 | llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
| 101 | llvm::APSInt& getValue(uint64_t X, QualType T, |
| 102 | SourceLocation Loc = SourceLocation()); |
| 103 | }; |
| 104 | |
| 105 | //==------------------------------------------------------------------------==// |
| 106 | // Base RValue types. |
| 107 | //==------------------------------------------------------------------------==// |
| 108 | |
| 109 | class RValue { |
| 110 | public: |
| 111 | enum BaseKind { LValueKind=0x0, |
| 112 | NonLValueKind=0x1, |
| 113 | UninitializedKind=0x2, |
| 114 | InvalidKind=0x3 }; |
| 115 | |
| 116 | enum { BaseBits = 2, |
| 117 | BaseMask = 0x3 }; |
| 118 | |
| 119 | private: |
| 120 | void* Data; |
| 121 | unsigned Kind; |
| 122 | |
| 123 | protected: |
| 124 | RValue(const void* d, bool isLValue, unsigned ValKind) |
| 125 | : Data(const_cast<void*>(d)), |
| 126 | Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {} |
| 127 | |
| 128 | explicit RValue(BaseKind k) |
| 129 | : Data(0), Kind(k) {} |
| 130 | |
| 131 | void* getRawPtr() const { |
| 132 | return reinterpret_cast<void*>(Data); |
| 133 | } |
| 134 | |
| 135 | public: |
| 136 | ~RValue() {}; |
| 137 | |
| 138 | RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const; |
| 139 | |
| 140 | unsigned getRawKind() const { return Kind; } |
| 141 | BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); } |
| 142 | unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; } |
| 143 | |
| 144 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 145 | ID.AddInteger((unsigned) getRawKind()); |
| 146 | ID.AddPointer(reinterpret_cast<void*>(Data)); |
| 147 | } |
| 148 | |
| 149 | bool operator==(const RValue& RHS) const { |
| 150 | return getRawKind() == RHS.getRawKind() && Data == RHS.Data; |
| 151 | } |
| 152 | |
| 153 | static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D); |
| 154 | |
| 155 | inline bool isValid() const { return getRawKind() != InvalidKind; } |
| 156 | inline bool isInvalid() const { return getRawKind() == InvalidKind; } |
| 157 | |
| 158 | void print(std::ostream& OS) const; |
| 159 | void print() const { print(*llvm::cerr.stream()); } |
| 160 | |
| 161 | // Implement isa<T> support. |
| 162 | static inline bool classof(const RValue*) { return true; } |
| 163 | }; |
| 164 | |
| 165 | class InvalidValue : public RValue { |
| 166 | public: |
| 167 | InvalidValue() : RValue(InvalidKind) {} |
| 168 | |
| 169 | static inline bool classof(const RValue* V) { |
| 170 | return V->getBaseKind() == InvalidKind; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | class UninitializedValue : public RValue { |
| 175 | public: |
| 176 | UninitializedValue() : RValue(UninitializedKind) {} |
| 177 | |
| 178 | static inline bool classof(const RValue* V) { |
| 179 | return V->getBaseKind() == UninitializedKind; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | class NonLValue : public RValue { |
| 184 | protected: |
| 185 | NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {} |
| 186 | |
| 187 | public: |
| 188 | void print(std::ostream& Out) const; |
| 189 | |
| 190 | // Arithmetic operators. |
| 191 | NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 192 | NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 193 | NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 194 | NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 195 | NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 196 | NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
| 197 | |
| 198 | // Equality operators. |
| 199 | NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 200 | NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 201 | |
| 202 | // Utility methods to create NonLValues. |
| 203 | static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 204 | SourceLocation Loc = SourceLocation()); |
| 205 | |
| 206 | static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I); |
| 207 | |
| 208 | static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) { |
| 209 | return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy); |
| 210 | } |
| 211 | |
| 212 | // Implement isa<T> support. |
| 213 | static inline bool classof(const RValue* V) { |
| 214 | return V->getBaseKind() >= NonLValueKind; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | class LValue : public RValue { |
| 219 | protected: |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame^] | 220 | LValue(unsigned SubKind, const void* D) : RValue(const_cast<void*>(D), |
| 221 | true, SubKind) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 222 | |
| 223 | public: |
| 224 | void print(std::ostream& Out) const; |
| 225 | |
| 226 | // Equality operators. |
| 227 | NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const; |
| 228 | NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const; |
| 229 | |
| 230 | // Implement isa<T> support. |
| 231 | static inline bool classof(const RValue* V) { |
| 232 | return V->getBaseKind() == LValueKind; |
| 233 | } |
| 234 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 235 | |
| 236 | //==------------------------------------------------------------------------==// |
| 237 | // Subclasses of NonLValue. |
| 238 | //==------------------------------------------------------------------------==// |
| 239 | |
| 240 | enum NonLValueKind { SymbolicNonLValueKind, ConcreteIntKind, |
| 241 | NumNonLValueKind }; |
| 242 | |
| 243 | class SymbolicNonLValue : public NonLValue { |
| 244 | public: |
| 245 | SymbolicNonLValue(unsigned SymID) |
| 246 | : NonLValue(SymbolicNonLValueKind, |
| 247 | reinterpret_cast<void*>((uintptr_t) SymID)) {} |
| 248 | |
| 249 | SymbolID getSymbolID() const { |
| 250 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 251 | } |
| 252 | |
| 253 | static inline bool classof(const RValue* V) { |
| 254 | return V->getSubKind() == SymbolicNonLValueKind; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | class ConcreteInt : public NonLValue { |
| 259 | public: |
| 260 | ConcreteInt(const llvm::APSInt& V) : NonLValue(ConcreteIntKind, &V) {} |
| 261 | |
| 262 | const llvm::APSInt& getValue() const { |
| 263 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 264 | } |
| 265 | |
| 266 | // Arithmetic operators. |
| 267 | |
| 268 | ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 269 | return ValMgr.getValue(getValue() + V.getValue()); |
| 270 | } |
| 271 | |
| 272 | ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 273 | return ValMgr.getValue(getValue() - V.getValue()); |
| 274 | } |
| 275 | |
| 276 | ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 277 | return ValMgr.getValue(getValue() * V.getValue()); |
| 278 | } |
| 279 | |
| 280 | ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 281 | return ValMgr.getValue(getValue() / V.getValue()); |
| 282 | } |
| 283 | |
| 284 | ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 285 | return ValMgr.getValue(getValue() % V.getValue()); |
| 286 | } |
| 287 | |
| 288 | ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 289 | assert (U->getType() == U->getSubExpr()->getType()); |
| 290 | assert (U->getType()->isIntegerType()); |
| 291 | return ValMgr.getValue(-getValue()); |
| 292 | } |
| 293 | |
| 294 | // Casting. |
| 295 | |
| 296 | ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 297 | assert (CastExpr->getType()->isIntegerType()); |
| 298 | |
| 299 | llvm::APSInt X(getValue()); |
| 300 | X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(), |
| 301 | CastExpr->getLocStart())); |
| 302 | return ValMgr.getValue(X); |
| 303 | } |
| 304 | |
| 305 | // Equality operators. |
| 306 | |
| 307 | ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 308 | const llvm::APSInt& Val = getValue(); |
| 309 | return ValMgr.getValue(Val == V.getValue() ? 1U : 0U, |
| 310 | Val.getBitWidth(), Val.isUnsigned()); |
| 311 | } |
| 312 | |
| 313 | ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 314 | const llvm::APSInt& Val = getValue(); |
| 315 | return ValMgr.getValue(Val != V.getValue() ? 1U : 0U, |
| 316 | Val.getBitWidth(), Val.isUnsigned()); |
| 317 | } |
| 318 | |
| 319 | // Implement isa<T> support. |
| 320 | static inline bool classof(const RValue* V) { |
| 321 | return V->getSubKind() == ConcreteIntKind; |
| 322 | } |
| 323 | }; |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame^] | 324 | |
| 325 | //==------------------------------------------------------------------------==// |
| 326 | // Subclasses of LValue. |
| 327 | //==------------------------------------------------------------------------==// |
| 328 | |
| 329 | enum LValueKind { SymbolicLValueKind, LValueDeclKind, |
| 330 | ConcreteIntLValueKind, NumLValueKind }; |
| 331 | |
| 332 | class SymbolicLValue : public LValue { |
| 333 | public: |
| 334 | SymbolicLValue(unsigned SymID) |
| 335 | : LValue(SymbolicLValueKind, reinterpret_cast<void*>((uintptr_t) SymID)) {} |
| 336 | |
| 337 | SymbolID getSymbolID() const { |
| 338 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 339 | } |
| 340 | |
| 341 | static inline bool classof(const RValue* V) { |
| 342 | return V->getSubKind() == SymbolicLValueKind; |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | class LValueDecl : public LValue { |
| 347 | public: |
| 348 | LValueDecl(const ValueDecl* vd) : LValue(LValueDeclKind,vd) {} |
| 349 | |
| 350 | ValueDecl* getDecl() const { |
| 351 | return static_cast<ValueDecl*>(getRawPtr()); |
| 352 | } |
| 353 | |
| 354 | inline bool operator==(const LValueDecl& R) const { |
| 355 | return getDecl() == R.getDecl(); |
| 356 | } |
| 357 | |
| 358 | inline bool operator!=(const LValueDecl& R) const { |
| 359 | return getDecl() != R.getDecl(); |
| 360 | } |
| 361 | |
| 362 | // Implement isa<T> support. |
| 363 | static inline bool classof(const RValue* V) { |
| 364 | return V->getSubKind() == LValueDeclKind; |
| 365 | } |
| 366 | }; |
| 367 | |
| 368 | class ConcreteIntLValue : public LValue { |
| 369 | public: |
| 370 | ConcreteIntLValue(const llvm::APSInt& V) : LValue(ConcreteIntLValueKind, &V) {} |
| 371 | |
| 372 | const llvm::APSInt& getValue() const { |
| 373 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 374 | } |
| 375 | |
| 376 | // Arithmetic operators. |
| 377 | |
| 378 | ConcreteIntLValue Add(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 379 | return ValMgr.getValue(getValue() + V.getValue()); |
| 380 | } |
| 381 | |
| 382 | ConcreteIntLValue Sub(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 383 | return ValMgr.getValue(getValue() - V.getValue()); |
| 384 | } |
| 385 | |
| 386 | // Equality operators. |
| 387 | |
| 388 | ConcreteInt EQ(ValueManager& ValMgr, const ConcreteIntLValue& V) const { |
| 389 | const llvm::APSInt& Val = getValue(); |
| 390 | return ValMgr.getValue(Val == V.getValue() ? 1U : 0U, |
| 391 | Val.getBitWidth(), Val.isUnsigned()); |
| 392 | } |
| 393 | |
| 394 | ConcreteInt NE(ValueManager& ValMgr, const ConcreteIntLValue& V) const { |
| 395 | const llvm::APSInt& Val = getValue(); |
| 396 | return ValMgr.getValue(Val != V.getValue() ? 1U : 0U, |
| 397 | Val.getBitWidth(), Val.isUnsigned()); |
| 398 | } |
| 399 | |
| 400 | // Implement isa<T> support. |
| 401 | static inline bool classof(const RValue* V) { |
| 402 | return V->getSubKind() == ConcreteIntLValueKind; |
| 403 | } |
| 404 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 405 | |
| 406 | } // end clang namespace |
| 407 | |
| 408 | //==------------------------------------------------------------------------==// |
| 409 | // Casting machinery to get cast<> and dyn_cast<> working with SymbolData. |
| 410 | //==------------------------------------------------------------------------==// |
| 411 | |
| 412 | namespace llvm { |
| 413 | |
| 414 | template<> inline bool |
| 415 | isa<clang::ParmVarDecl,clang::SymbolData>(const clang::SymbolData& V) { |
| 416 | return V.getKind() == clang::SymbolData::ParmKind; |
| 417 | } |
| 418 | |
| 419 | template<> struct cast_retty_impl<clang::ParmVarDecl,clang::SymbolData> { |
| 420 | typedef const clang::ParmVarDecl* ret_type; |
| 421 | }; |
| 422 | |
| 423 | template<> struct simplify_type<clang::SymbolData> { |
| 424 | typedef void* SimpleType; |
| 425 | static inline SimpleType getSimplifiedValue(const clang::SymbolData &V) { |
| 426 | return V.getPtr(); |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | } // end llvm namespace |
| 431 | |
| 432 | #endif |