Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1 | //===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==// |
| 2 | // |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 3 | // The LLValM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Constant Propagation via Graph Reachability |
| 11 | // |
| 12 | // This files defines a simple analysis that performs path-sensitive |
| 13 | // constant propagation within a function. An example use of this analysis |
| 14 | // is to perform simple checks for NULL dereferences. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 19 | #include "clang/AST/Expr.h" |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/Support/Casting.h" |
| 24 | #include "llvm/Support/DataTypes.h" |
| 25 | #include "llvm/ADT/APSInt.h" |
| 26 | #include "llvm/ADT/FoldingSet.h" |
| 27 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Streams.h" |
| 32 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 33 | #include <functional> |
| 34 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 35 | #ifndef NDEBUG |
| 36 | #include "llvm/Support/GraphWriter.h" |
| 37 | #include <sstream> |
| 38 | #endif |
| 39 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 40 | using namespace clang; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 41 | using llvm::dyn_cast; |
| 42 | using llvm::cast; |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 43 | using llvm::APSInt; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | |
| 45 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 46 | /// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 47 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | namespace { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 51 | class SymbolID { |
| 52 | unsigned Data; |
| 53 | public: |
| 54 | SymbolID() : Data(~0) {} |
| 55 | SymbolID(unsigned x) : Data(x) {} |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 57 | bool isInitialized() const { return Data != (unsigned) ~0; } |
| 58 | operator unsigned() const { assert (isInitialized()); return Data; } |
| 59 | }; |
| 60 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 61 | class VISIBILITY_HIDDEN ValueKey { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 62 | uintptr_t Raw; |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 63 | void operator=(const ValueKey& RHS); // Do not implement. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 65 | public: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 66 | enum Kind { IsSubExpr=0x0, IsBlkExpr=0x1, IsDecl=0x2, // L-Value Bindings. |
| 67 | IsSymbol=0x3, // Symbol Bindings. |
| 68 | Flags=0x3 }; |
| 69 | |
| 70 | inline Kind getKind() const { |
| 71 | return (Kind) (Raw & Flags); |
| 72 | } |
| 73 | |
| 74 | inline void* getPtr() const { |
| 75 | assert (getKind() != IsSymbol); |
| 76 | return reinterpret_cast<void*>(Raw & ~Flags); |
| 77 | } |
| 78 | |
| 79 | inline SymbolID getSymbolID() const { |
| 80 | assert (getKind() == IsSymbol); |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 81 | return Raw >> 2; |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 84 | ValueKey(const ValueDecl* VD) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 85 | : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) { |
| 86 | assert(VD && "ValueDecl cannot be NULL."); |
| 87 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 5c1b996 | 2008-01-24 19:43:37 +0000 | [diff] [blame] | 89 | ValueKey(Stmt* S, bool isBlkExpr = false) |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 90 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExpr)){ |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 91 | assert(S && "Tracked statement cannot be NULL."); |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 94 | ValueKey(SymbolID V) |
| 95 | : Raw((V << 2) | IsSymbol) {} |
| 96 | |
| 97 | bool isSymbol() const { return getKind() == IsSymbol; } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 98 | bool isSubExpr() const { return getKind() == IsSubExpr; } |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 99 | bool isBlkExpr() const { return getKind() == IsBlkExpr; } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 100 | bool isDecl() const { return getKind() == IsDecl; } |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 101 | bool isStmt() const { return getKind() <= IsBlkExpr; } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 102 | |
| 103 | inline void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 104 | ID.AddInteger(isSymbol() ? 1 : 0); |
| 105 | |
| 106 | if (isSymbol()) |
Ted Kremenek | f264562 | 2008-01-28 22:25:21 +0000 | [diff] [blame] | 107 | ID.AddInteger(getSymbolID()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 108 | else |
| 109 | ID.AddPointer(getPtr()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | inline bool operator==(const ValueKey& X) const { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 113 | return isSymbol() ? getSymbolID() == X.getSymbolID() |
| 114 | : getPtr() == X.getPtr(); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | inline bool operator!=(const ValueKey& X) const { |
| 118 | return !operator==(X); |
| 119 | } |
| 120 | |
| 121 | inline bool operator<(const ValueKey& X) const { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 122 | if (isSymbol()) |
| 123 | return X.isSymbol() ? getSymbolID() < X.getSymbolID() : false; |
Ted Kremenek | 5c1b996 | 2008-01-24 19:43:37 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 125 | return getPtr() < X.getPtr(); |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 127 | }; |
| 128 | } // end anonymous namespace |
| 129 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 130 | // Machinery to get cast<> and dyn_cast<> working with ValueKey. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 131 | namespace llvm { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 132 | template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) { |
| 133 | return V.getKind() == ValueKey::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 134 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 135 | template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 136 | return ((unsigned) V.getKind()) < ValueKey::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 137 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 138 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 139 | typedef const ValueDecl* ret_type; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 140 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 141 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 142 | typedef const Stmt* ret_type; |
| 143 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 144 | template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 145 | typedef void* SimpleType; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 146 | static inline SimpleType getSimplifiedValue(const ValueKey &V) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 147 | return V.getPtr(); |
| 148 | } |
| 149 | }; |
| 150 | } // end llvm namespace |
| 151 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 152 | |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | // SymbolManager. |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | |
| 157 | namespace { |
| 158 | class VISIBILITY_HIDDEN SymbolData { |
| 159 | uintptr_t Data; |
| 160 | public: |
| 161 | enum Kind { ParmKind = 0x0, Mask = 0x3 }; |
| 162 | |
| 163 | SymbolData(ParmVarDecl* D) |
| 164 | : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {} |
| 165 | |
| 166 | inline Kind getKind() const { return (Kind) (Data & Mask); } |
| 167 | inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); } |
| 168 | inline bool operator==(const SymbolData& R) const { return Data == R.Data; } |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | // Machinery to get cast<> and dyn_cast<> working with SymbolData. |
| 173 | namespace llvm { |
| 174 | template<> inline bool isa<ParmVarDecl,SymbolData>(const SymbolData& V) { |
| 175 | return V.getKind() == SymbolData::ParmKind; |
| 176 | } |
| 177 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<ParmVarDecl,SymbolData> { |
| 178 | typedef const ParmVarDecl* ret_type; |
| 179 | }; |
| 180 | template<> struct VISIBILITY_HIDDEN simplify_type<SymbolData> { |
| 181 | typedef void* SimpleType; |
| 182 | static inline SimpleType getSimplifiedValue(const SymbolData &V) { |
| 183 | return V.getPtr(); |
| 184 | } |
| 185 | }; |
| 186 | } // end llvm namespace |
| 187 | |
| 188 | namespace { |
| 189 | class VISIBILITY_HIDDEN SymbolManager { |
| 190 | std::vector<SymbolData> SymbolToData; |
| 191 | |
| 192 | typedef llvm::DenseMap<void*,SymbolID> MapTy; |
| 193 | MapTy DataToSymbol; |
| 194 | |
| 195 | public: |
| 196 | SymbolData getSymbolData(SymbolID id) const { |
| 197 | assert (id < SymbolToData.size()); |
| 198 | return SymbolToData[id]; |
| 199 | } |
| 200 | |
| 201 | SymbolID getSymbol(ParmVarDecl* D); |
| 202 | }; |
| 203 | } // end anonymous namespace |
| 204 | |
| 205 | SymbolID SymbolManager::getSymbol(ParmVarDecl* D) { |
| 206 | SymbolID& X = DataToSymbol[D]; |
| 207 | |
| 208 | if (!X.isInitialized()) { |
| 209 | X = SymbolToData.size(); |
| 210 | SymbolToData.push_back(D); |
| 211 | } |
| 212 | |
| 213 | return X; |
| 214 | } |
| 215 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 216 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 217 | // ValueManager. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 218 | //===----------------------------------------------------------------------===// |
| 219 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 220 | namespace { |
| 221 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 222 | typedef llvm::ImmutableSet<APSInt > APSIntSetTy; |
| 223 | |
| 224 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 225 | class VISIBILITY_HIDDEN ValueManager { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 226 | ASTContext& Ctx; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 228 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<APSInt> > APSIntSetTy; |
| 229 | APSIntSetTy APSIntSet; |
| 230 | |
| 231 | llvm::BumpPtrAllocator BPAlloc; |
| 232 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 233 | public: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 234 | ValueManager(ASTContext& ctx) : Ctx(ctx) {} |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 235 | ~ValueManager(); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 237 | ASTContext& getContext() const { return Ctx; } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 238 | APSInt& getValue(const APSInt& X); |
| 239 | APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
| 240 | APSInt& getValue(uint64_t X, QualType T, |
| 241 | SourceLocation Loc = SourceLocation()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 242 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 243 | } // end anonymous namespace |
| 244 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 245 | ValueManager::~ValueManager() { |
| 246 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 247 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 248 | // frees an aux. memory allocated to represent very large constants. |
| 249 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 250 | I->getValue().~APSInt(); |
| 251 | } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 252 | |
| 253 | APSInt& ValueManager::getValue(const APSInt& X) { |
| 254 | llvm::FoldingSetNodeID ID; |
| 255 | void* InsertPos; |
| 256 | typedef llvm::FoldingSetNodeWrapper<APSInt> FoldNodeTy; |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 258 | X.Profile(ID); |
| 259 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 261 | if (!P) { |
| 262 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 263 | new (P) FoldNodeTy(X); |
| 264 | APSIntSet.InsertNode(P, InsertPos); |
| 265 | } |
| 266 | |
| 267 | return *P; |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 270 | APSInt& ValueManager::getValue(uint64_t X, unsigned BitWidth, bool isUnsigned) { |
| 271 | APSInt V(BitWidth, isUnsigned); |
| 272 | V = X; |
| 273 | return getValue(V); |
| 274 | } |
| 275 | |
| 276 | APSInt& ValueManager::getValue(uint64_t X, QualType T, SourceLocation Loc) { |
| 277 | unsigned bits = Ctx.getTypeSize(T, Loc); |
| 278 | APSInt V(bits, T->isUnsignedIntegerType()); |
| 279 | V = X; |
| 280 | return getValue(V); |
| 281 | } |
| 282 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 283 | //===----------------------------------------------------------------------===// |
| 284 | // Expression Values. |
| 285 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 287 | namespace { |
| 288 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 289 | class VISIBILITY_HIDDEN RValue { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 290 | public: |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 291 | enum BaseKind { LValueKind=0x0, NonLValueKind=0x1, |
| 292 | UninitializedKind=0x2, InvalidKind=0x3, BaseFlags = 0x3 }; |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 294 | private: |
Ted Kremenek | f264562 | 2008-01-28 22:25:21 +0000 | [diff] [blame] | 295 | void* Data; |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 296 | unsigned Kind; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 297 | |
| 298 | protected: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 299 | RValue(const void* d, bool isLValue, unsigned ValKind) |
Ted Kremenek | f264562 | 2008-01-28 22:25:21 +0000 | [diff] [blame] | 300 | : Data(const_cast<void*>(d)), |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 301 | Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << 2)) {} |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 303 | explicit RValue(BaseKind k) |
| 304 | : Data(0), Kind(k) {} |
Ted Kremenek | f264562 | 2008-01-28 22:25:21 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 306 | void* getRawPtr() const { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 307 | return reinterpret_cast<void*>(Data); |
| 308 | } |
| 309 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 310 | public: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 311 | ~RValue() {}; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 313 | RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const; |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 315 | unsigned getRawKind() const { return Kind; } |
| 316 | BaseKind getBaseKind() const { return (BaseKind) (Kind & 0x3); } |
| 317 | unsigned getSubKind() const { return (Kind & ~0x3) >> 2; } |
| 318 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 319 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 320 | ID.AddInteger((unsigned) getRawKind()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 321 | ID.AddPointer(reinterpret_cast<void*>(Data)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 324 | bool operator==(const RValue& RHS) const { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 325 | return getRawKind() == RHS.getRawKind() && Data == RHS.Data; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 328 | inline bool isValid() const { return getRawKind() != InvalidKind; } |
| 329 | inline bool isInvalid() const { return getRawKind() == InvalidKind; } |
| 330 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 331 | void print(std::ostream& OS) const; |
| 332 | void print() const { print(*llvm::cerr.stream()); } |
| 333 | |
| 334 | // Implement isa<T> support. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 335 | static inline bool classof(const RValue*) { return true; } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 338 | class VISIBILITY_HIDDEN InvalidValue : public RValue { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 339 | public: |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 340 | InvalidValue() : RValue(InvalidKind) {} |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 342 | static inline bool classof(const RValue* V) { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 343 | return V->getBaseKind() == InvalidKind; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 344 | } |
| 345 | }; |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 346 | |
| 347 | class VISIBILITY_HIDDEN UninitializedValue : public RValue { |
| 348 | public: |
| 349 | UninitializedValue() : RValue(UninitializedKind) {} |
| 350 | |
| 351 | static inline bool classof(const RValue* V) { |
| 352 | return V->getBaseKind() == UninitializedKind; |
| 353 | } |
| 354 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 356 | class VISIBILITY_HIDDEN NonLValue : public RValue { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 357 | protected: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 358 | NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {} |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 359 | |
| 360 | public: |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 361 | void print(std::ostream& Out) const; |
| 362 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 363 | // Arithmetic operators. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 364 | NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 365 | NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 366 | NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 367 | NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 368 | NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 369 | NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 371 | // Equality operators. |
| 372 | NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const; |
| 373 | NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const; |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 375 | // Utility methods to create NonLValues. |
| 376 | static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 377 | SourceLocation Loc = SourceLocation()); |
| 378 | |
| 379 | static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I); |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 380 | static NonLValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 382 | static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) { |
| 383 | return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy); |
| 384 | } |
| 385 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 386 | // Implement isa<T> support. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 387 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 388 | return V->getBaseKind() >= NonLValueKind; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 389 | } |
| 390 | }; |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 391 | |
| 392 | class VISIBILITY_HIDDEN LValue : public RValue { |
| 393 | protected: |
| 394 | LValue(unsigned SubKind, void* D) : RValue(D, true, SubKind) {} |
| 395 | |
| 396 | public: |
| 397 | |
| 398 | // Equality operators. |
| 399 | NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const; |
| 400 | NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const; |
| 401 | |
| 402 | // Implement isa<T> support. |
| 403 | static inline bool classof(const RValue* V) { |
| 404 | return V->getBaseKind() == LValueKind; |
| 405 | } |
| 406 | }; |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 407 | |
| 408 | } // end anonymous namespace |
| 409 | |
| 410 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 411 | // LValues. |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | namespace { |
| 415 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 416 | enum { LValueDeclKind, NumLValueKind }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 417 | |
| 418 | class VISIBILITY_HIDDEN LValueDecl : public LValue { |
| 419 | public: |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 420 | LValueDecl(const ValueDecl* vd) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 421 | : LValue(LValueDeclKind,const_cast<ValueDecl*>(vd)) {} |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 422 | |
| 423 | ValueDecl* getDecl() const { |
| 424 | return static_cast<ValueDecl*>(getRawPtr()); |
| 425 | } |
| 426 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 427 | inline bool operator==(const LValueDecl& R) const { |
| 428 | return getDecl() == R.getDecl(); |
| 429 | } |
| 430 | |
| 431 | inline bool operator!=(const LValueDecl& R) const { |
| 432 | return getDecl() != R.getDecl(); |
| 433 | } |
| 434 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 435 | // Implement isa<T> support. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 436 | static inline bool classof(const RValue* V) { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 437 | return V->getSubKind() == LValueDeclKind; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 438 | } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 441 | } // end anonymous namespace |
| 442 | |
| 443 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 444 | // Non-LValues. |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
| 447 | namespace { |
| 448 | |
Ted Kremenek | f264562 | 2008-01-28 22:25:21 +0000 | [diff] [blame] | 449 | enum { SymbolicNonLValueKind, ConcreteIntKind, ConstrainedIntegerKind, |
| 450 | NumNonLValueKind }; |
| 451 | |
| 452 | class VISIBILITY_HIDDEN SymbolicNonLValue : public NonLValue { |
| 453 | public: |
| 454 | SymbolicNonLValue(unsigned SymID) |
| 455 | : NonLValue(SymbolicNonLValueKind, |
| 456 | reinterpret_cast<void*>((uintptr_t) SymID)) {} |
| 457 | |
| 458 | SymbolID getSymbolID() const { |
| 459 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 460 | } |
| 461 | |
| 462 | static inline bool classof(const RValue* V) { |
| 463 | return V->getSubKind() == SymbolicNonLValueKind; |
| 464 | } |
| 465 | }; |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 466 | |
| 467 | class VISIBILITY_HIDDEN ConcreteInt : public NonLValue { |
| 468 | public: |
| 469 | ConcreteInt(const APSInt& V) : NonLValue(ConcreteIntKind, &V) {} |
| 470 | |
| 471 | const APSInt& getValue() const { |
| 472 | return *static_cast<APSInt*>(getRawPtr()); |
| 473 | } |
| 474 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 475 | // Arithmetic operators. |
| 476 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 477 | ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 478 | return ValMgr.getValue(getValue() + V.getValue()); |
| 479 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 480 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 481 | ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 482 | return ValMgr.getValue(getValue() - V.getValue()); |
| 483 | } |
| 484 | |
| 485 | ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 486 | return ValMgr.getValue(getValue() * V.getValue()); |
| 487 | } |
| 488 | |
| 489 | ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 490 | return ValMgr.getValue(getValue() / V.getValue()); |
| 491 | } |
| 492 | |
| 493 | ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 494 | return ValMgr.getValue(getValue() % V.getValue()); |
| 495 | } |
| 496 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 497 | ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 498 | assert (U->getType() == U->getSubExpr()->getType()); |
| 499 | assert (U->getType()->isIntegerType()); |
| 500 | return ValMgr.getValue(-getValue()); |
| 501 | } |
| 502 | |
| 503 | // Casting. |
| 504 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 505 | ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 506 | assert (CastExpr->getType()->isIntegerType()); |
| 507 | |
| 508 | APSInt X(getValue()); |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 509 | X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(), |
| 510 | CastExpr->getLocStart())); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 511 | return ValMgr.getValue(X); |
| 512 | } |
| 513 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 514 | // Equality operators. |
| 515 | |
| 516 | ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 517 | const APSInt& Val = getValue(); |
| 518 | return ValMgr.getValue(Val == V.getValue() ? 1U : 0U, |
| 519 | Val.getBitWidth(), Val.isUnsigned()); |
| 520 | } |
| 521 | |
| 522 | ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const { |
| 523 | const APSInt& Val = getValue(); |
| 524 | return ValMgr.getValue(Val != V.getValue() ? 1U : 0U, |
| 525 | Val.getBitWidth(), Val.isUnsigned()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Implement isa<T> support. |
| 529 | static inline bool classof(const RValue* V) { |
| 530 | return V->getSubKind() == ConcreteIntKind; |
| 531 | } |
| 532 | }; |
| 533 | |
| 534 | } // end anonymous namespace |
| 535 | |
| 536 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 537 | // Transfer function dispatch for Non-LValues. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 538 | //===----------------------------------------------------------------------===// |
| 539 | |
| 540 | RValue RValue::Cast(ValueManager& ValMgr, Expr* CastExpr) const { |
| 541 | switch (getSubKind()) { |
| 542 | case ConcreteIntKind: |
| 543 | return cast<ConcreteInt>(this)->Cast(ValMgr, CastExpr); |
| 544 | default: |
| 545 | return InvalidValue(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | NonLValue NonLValue::UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const { |
| 550 | switch (getSubKind()) { |
| 551 | case ConcreteIntKind: |
| 552 | return cast<ConcreteInt>(this)->UnaryMinus(ValMgr, U); |
| 553 | default: |
| 554 | return cast<NonLValue>(InvalidValue()); |
| 555 | } |
| 556 | } |
| 557 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 558 | #define NONLVALUE_DISPATCH_CASE(k1,k2,Op)\ |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 559 | case (k1##Kind*NumNonLValueKind+k2##Kind):\ |
| 560 | return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS)); |
| 561 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 562 | #define NONLVALUE_DISPATCH(Op)\ |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 563 | switch (getSubKind()*NumNonLValueKind+RHS.getSubKind()){\ |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 564 | NONLVALUE_DISPATCH_CASE(ConcreteInt,ConcreteInt,Op)\ |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 565 | default:\ |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 566 | if (getBaseKind() == UninitializedKind ||\ |
| 567 | RHS.getBaseKind() == UninitializedKind)\ |
| 568 | return cast<NonLValue>(UninitializedValue());\ |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 569 | assert (!isValid() || !RHS.isValid() && "Missing case.");\ |
| 570 | break;\ |
| 571 | }\ |
| 572 | return cast<NonLValue>(InvalidValue()); |
| 573 | |
| 574 | NonLValue NonLValue::Add(ValueManager& ValMgr, const NonLValue& RHS) const { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 575 | NONLVALUE_DISPATCH(Add) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | NonLValue NonLValue::Sub(ValueManager& ValMgr, const NonLValue& RHS) const { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 579 | NONLVALUE_DISPATCH(Sub) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | NonLValue NonLValue::Mul(ValueManager& ValMgr, const NonLValue& RHS) const { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 583 | NONLVALUE_DISPATCH(Mul) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | NonLValue NonLValue::Div(ValueManager& ValMgr, const NonLValue& RHS) const { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 587 | NONLVALUE_DISPATCH(Div) |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | NonLValue NonLValue::Rem(ValueManager& ValMgr, const NonLValue& RHS) const { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 591 | NONLVALUE_DISPATCH(Rem) |
| 592 | } |
| 593 | |
| 594 | NonLValue NonLValue::EQ(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 595 | NONLVALUE_DISPATCH(EQ) |
| 596 | } |
| 597 | |
| 598 | NonLValue NonLValue::NE(ValueManager& ValMgr, const NonLValue& RHS) const { |
| 599 | NONLVALUE_DISPATCH(NE) |
| 600 | } |
| 601 | |
| 602 | #undef NONLVALUE_DISPATCH_CASE |
| 603 | #undef NONLVALUE_DISPATCH |
| 604 | |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | // Transfer function dispatch for LValues. |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | |
| 609 | |
| 610 | NonLValue LValue::EQ(ValueManager& ValMgr, const LValue& RHS) const { |
| 611 | if (getSubKind() != RHS.getSubKind()) |
| 612 | return NonLValue::GetIntTruthValue(ValMgr, false); |
| 613 | |
| 614 | switch (getSubKind()) { |
| 615 | default: |
| 616 | assert(false && "EQ not implemented for this LValue."); |
| 617 | return cast<NonLValue>(InvalidValue()); |
| 618 | |
| 619 | case LValueDeclKind: { |
| 620 | bool b = cast<LValueDecl>(*this) == cast<LValueDecl>(RHS); |
| 621 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | NonLValue LValue::NE(ValueManager& ValMgr, const LValue& RHS) const { |
| 627 | if (getSubKind() != RHS.getSubKind()) |
| 628 | return NonLValue::GetIntTruthValue(ValMgr, false); |
| 629 | |
| 630 | switch (getSubKind()) { |
| 631 | default: |
| 632 | assert(false && "EQ not implemented for this LValue."); |
| 633 | return cast<NonLValue>(InvalidValue()); |
| 634 | |
| 635 | case LValueDeclKind: { |
| 636 | bool b = cast<LValueDecl>(*this) != cast<LValueDecl>(RHS); |
| 637 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 638 | } |
| 639 | } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 644 | // Utility methods for constructing Non-LValues. |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 645 | //===----------------------------------------------------------------------===// |
| 646 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 647 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 648 | SourceLocation Loc) { |
| 649 | |
| 650 | return ConcreteInt(ValMgr.getValue(X, T, Loc)); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | NonLValue NonLValue::GetValue(ValueManager& ValMgr, IntegerLiteral* I) { |
| 654 | return ConcreteInt(ValMgr.getValue(APSInt(I->getValue(), |
| 655 | I->getType()->isUnsignedIntegerType()))); |
| 656 | } |
| 657 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 658 | NonLValue NonLValue::GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl* D) { |
| 659 | return SymbolicNonLValue(SymMgr.getSymbol(D)); |
| 660 | } |
| 661 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 662 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 663 | // Pretty-Printing. |
| 664 | //===----------------------------------------------------------------------===// |
| 665 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 666 | void RValue::print(std::ostream& Out) const { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 667 | switch (getBaseKind()) { |
| 668 | case InvalidKind: |
| 669 | Out << "Invalid"; |
| 670 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 672 | case NonLValueKind: |
| 673 | cast<NonLValue>(this)->print(Out); |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 674 | break; |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 676 | case LValueKind: |
| 677 | assert (false && "FIXME: LValue printing not implemented."); |
| 678 | break; |
| 679 | |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 680 | case UninitializedKind: |
| 681 | Out << "Uninitialized"; |
| 682 | break; |
| 683 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 684 | default: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 685 | assert (false && "Invalid RValue."); |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 689 | void NonLValue::print(std::ostream& Out) const { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 690 | switch (getSubKind()) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 691 | case ConcreteIntKind: |
| 692 | Out << cast<ConcreteInt>(this)->getValue().toString(); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 693 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 695 | case SymbolicNonLValueKind: |
| 696 | Out << "sym-" << cast<SymbolicNonLValue>(this)->getSymbolID(); |
| 697 | break; |
| 698 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 699 | default: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 700 | assert (false && "Pretty-printed not implemented for this NonLValue."); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 706 | // ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
| 708 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 709 | typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 710 | |
| 711 | namespace clang { |
| 712 | template<> |
| 713 | struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> { |
| 714 | static inline void* toPtr(ValueMapTy M) { |
| 715 | return reinterpret_cast<void*>(M.getRoot()); |
| 716 | } |
| 717 | static inline ValueMapTy toState(void* P) { |
| 718 | return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P)); |
| 719 | } |
| 720 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 724 | // The Checker. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 725 | //===----------------------------------------------------------------------===// |
| 726 | |
| 727 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 729 | class VISIBILITY_HIDDEN GRConstants { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 730 | |
| 731 | public: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 732 | typedef ValueMapTy StateTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 733 | typedef GRNodeBuilder<GRConstants> NodeBuilder; |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 734 | typedef ExplodedGraph<GRConstants> GraphTy; |
| 735 | typedef GraphTy::NodeTy NodeTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 736 | |
| 737 | class NodeSet { |
| 738 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 739 | ImplTy Impl; |
| 740 | public: |
| 741 | |
| 742 | NodeSet() {} |
| 743 | NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); } |
| 744 | |
| 745 | void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); } |
| 746 | |
| 747 | typedef ImplTy::iterator iterator; |
| 748 | typedef ImplTy::const_iterator const_iterator; |
| 749 | |
| 750 | unsigned size() const { return Impl.size(); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 751 | bool empty() const { return Impl.empty(); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 752 | |
| 753 | iterator begin() { return Impl.begin(); } |
| 754 | iterator end() { return Impl.end(); } |
| 755 | |
| 756 | const_iterator begin() const { return Impl.begin(); } |
| 757 | const_iterator end() const { return Impl.end(); } |
| 758 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 759 | |
| 760 | protected: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 761 | /// G - the simulation graph. |
| 762 | GraphTy& G; |
| 763 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 764 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 765 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 766 | LiveVariables Liveness; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 767 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 768 | /// Builder - The current GRNodeBuilder which is used when building the nodes |
| 769 | /// for a given statement. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 770 | NodeBuilder* Builder; |
| 771 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 772 | /// StateMgr - Object that manages the data for all created states. |
| 773 | ValueMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 774 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 775 | /// ValueMgr - Object that manages the data for all created RValues. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 776 | ValueManager ValMgr; |
| 777 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 778 | /// SymMgr - Object that manages the symbol information. |
| 779 | SymbolManager SymMgr; |
| 780 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 781 | /// StmtEntryNode - The immediate predecessor node. |
| 782 | NodeTy* StmtEntryNode; |
| 783 | |
| 784 | /// CurrentStmt - The current block-level statement. |
| 785 | Stmt* CurrentStmt; |
| 786 | |
| 787 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 788 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 789 | ASTContext& getContext() const { return G.getContext(); } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 790 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 791 | public: |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 792 | GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()), |
| 793 | Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL), |
| 794 | CurrentStmt(NULL) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 795 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 796 | // Compute liveness information. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 797 | Liveness.runOnCFG(G.getCFG()); |
| 798 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 799 | } |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 800 | |
| 801 | /// getCFG - Returns the CFG associated with this analysis. |
| 802 | CFG& getCFG() { return G.getCFG(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 803 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 804 | /// getInitialState - Return the initial state used for the root vertex |
| 805 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 806 | StateTy getInitialState() { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 807 | StateTy St = StateMgr.GetEmptyMap(); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 808 | |
| 809 | // Iterate the parameters. |
| 810 | FunctionDecl& F = G.getFunctionDecl(); |
| 811 | |
| 812 | for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end(); |
| 813 | I!=E; ++I) { |
| 814 | |
| 815 | // For now we only support symbolic values for non-pointer types. |
| 816 | if ((*I)->getType()->isPointerType() || |
| 817 | (*I)->getType()->isReferenceType()) |
| 818 | continue; |
| 819 | |
| 820 | // FIXME: Set these values to a symbol, not Uninitialized. |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 821 | St = SetValue(St, LValueDecl(*I), NonLValue::GetSymbolValue(SymMgr, *I)); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 824 | return St; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 825 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 827 | /// ProcessStmt - Called by GREngine. Used to generate new successor |
| 828 | /// nodes by processing the 'effects' of a block-level statement. |
| 829 | void ProcessStmt(Stmt* S, NodeBuilder& builder); |
| 830 | |
| 831 | /// RemoveDeadBindings - Return a new state that is the same as 'M' except |
| 832 | /// that all subexpression mappings are removed and that any |
| 833 | /// block-level expressions that are not live at 'S' also have their |
| 834 | /// mappings removed. |
| 835 | StateTy RemoveDeadBindings(Stmt* S, StateTy M); |
| 836 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 837 | StateTy SetValue(StateTy St, Stmt* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 838 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 839 | StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 840 | return SetValue(St, const_cast<Stmt*>(S), V); |
| 841 | } |
| 842 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 843 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 844 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 845 | RValue GetValue(const StateTy& St, Stmt* S); |
| 846 | inline RValue GetValue(const StateTy& St, const Stmt* S) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 847 | return GetValue(St, const_cast<Stmt*>(S)); |
| 848 | } |
| 849 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 850 | RValue GetValue(const StateTy& St, const LValue& LV); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 851 | LValue GetLValue(const StateTy& St, Stmt* S); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 852 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 853 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 854 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 855 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 856 | /// other functions that handle specific kinds of statements. |
| 857 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 858 | |
| 859 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 860 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 861 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 862 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 863 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 864 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 865 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 866 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 867 | |
| 868 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
| 869 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 870 | }; |
| 871 | } // end anonymous namespace |
| 872 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 873 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 874 | void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) { |
| 875 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 876 | |
| 877 | StmtEntryNode = builder.getLastNode(); |
| 878 | CurrentStmt = S; |
| 879 | NodeSet Dst; |
| 880 | StateCleaned = false; |
| 881 | |
| 882 | Visit(S, StmtEntryNode, Dst); |
| 883 | |
| 884 | // If no nodes were generated, generate a new node that has all the |
| 885 | // dead mappings removed. |
| 886 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 887 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 888 | builder.generateNode(S, St, StmtEntryNode); |
| 889 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 890 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 891 | CurrentStmt = NULL; |
| 892 | StmtEntryNode = NULL; |
| 893 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 896 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 897 | RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 898 | switch (LV.getSubKind()) { |
| 899 | case LValueDeclKind: { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 900 | StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl()); |
| 901 | return T ? T->getValue().second : InvalidValue(); |
| 902 | } |
| 903 | default: |
| 904 | assert (false && "Invalid LValue."); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 905 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | return InvalidValue(); |
| 909 | } |
| 910 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 911 | RValue GRConstants::GetValue(const StateTy& St, Stmt* S) { |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 912 | for (;;) { |
| 913 | switch (S->getStmtClass()) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 914 | |
| 915 | // ParenExprs are no-ops. |
| 916 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 917 | case Stmt::ParenExprClass: |
| 918 | S = cast<ParenExpr>(S)->getSubExpr(); |
| 919 | continue; |
| 920 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 921 | // DeclRefExprs can either evaluate to an LValue or a Non-LValue |
| 922 | // (assuming an implicit "load") depending on the context. In this |
| 923 | // context we assume that we are retrieving the value contained |
| 924 | // within the referenced variables. |
| 925 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 926 | case Stmt::DeclRefExprClass: |
| 927 | return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl())); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 928 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 929 | // Integer literals evaluate to an RValue. Simply retrieve the |
| 930 | // RValue for the literal. |
| 931 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 932 | case Stmt::IntegerLiteralClass: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 933 | return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S)); |
| 934 | |
| 935 | // Casts where the source and target type are the same |
| 936 | // are no-ops. We blast through these to get the descendant |
| 937 | // subexpression that has a value. |
| 938 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 939 | case Stmt::ImplicitCastExprClass: { |
| 940 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 941 | if (C->getType() == C->getSubExpr()->getType()) { |
| 942 | S = C->getSubExpr(); |
| 943 | continue; |
| 944 | } |
| 945 | break; |
| 946 | } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 947 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 948 | case Stmt::CastExprClass: { |
| 949 | CastExpr* C = cast<CastExpr>(S); |
| 950 | if (C->getType() == C->getSubExpr()->getType()) { |
| 951 | S = C->getSubExpr(); |
| 952 | continue; |
| 953 | } |
| 954 | break; |
| 955 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 956 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 957 | // Handle all other Stmt* using a lookup. |
| 958 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 959 | default: |
| 960 | break; |
| 961 | }; |
| 962 | |
| 963 | break; |
| 964 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 965 | |
Ted Kremenek | 5c1b996 | 2008-01-24 19:43:37 +0000 | [diff] [blame] | 966 | StateTy::TreeTy* T = St.SlimFind(S); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 967 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 968 | return T ? T->getValue().second : InvalidValue(); |
| 969 | } |
| 970 | |
| 971 | LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 972 | while (ParenExpr* P = dyn_cast<ParenExpr>(S)) |
| 973 | S = P->getSubExpr(); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 974 | |
| 975 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 976 | return LValueDecl(DR->getDecl()); |
| 977 | |
| 978 | return cast<LValue>(GetValue(St, S)); |
| 979 | } |
| 980 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 981 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 982 | GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S, |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 983 | const RValue& V) { |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 984 | assert (S); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 985 | |
| 986 | if (!StateCleaned) { |
| 987 | St = RemoveDeadBindings(CurrentStmt, St); |
| 988 | StateCleaned = true; |
| 989 | } |
| 990 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 991 | bool isBlkExpr = false; |
| 992 | |
| 993 | if (S == CurrentStmt) { |
| 994 | isBlkExpr = getCFG().isBlkExpr(S); |
| 995 | |
| 996 | if (!isBlkExpr) |
| 997 | return St; |
| 998 | } |
Ted Kremenek | daadf45 | 2008-01-24 19:28:01 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1000 | return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V) |
| 1001 | : St; |
| 1002 | } |
| 1003 | |
| 1004 | GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV, |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1005 | const RValue& V) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1006 | if (!LV.isValid()) |
| 1007 | return St; |
| 1008 | |
| 1009 | if (!StateCleaned) { |
| 1010 | St = RemoveDeadBindings(CurrentStmt, St); |
| 1011 | StateCleaned = true; |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 1012 | } |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 1014 | switch (LV.getSubKind()) { |
| 1015 | case LValueDeclKind: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1016 | return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V) |
| 1017 | : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl()); |
| 1018 | |
| 1019 | default: |
| 1020 | assert ("SetValue for given LValue type not yet implemented."); |
| 1021 | return St; |
| 1022 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1025 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1026 | // Note: in the code below, we can assign a new map to M since the |
| 1027 | // iterators are iterating over the tree of the *original* map. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1028 | StateTy::iterator I = M.begin(), E = M.end(); |
| 1029 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 1031 | for (; I!=E && !I.getKey().isSymbol(); ++I) { |
| 1032 | // Remove old bindings for subexpressions and "dead" |
| 1033 | // block-level expressions. |
| 1034 | if (I.getKey().isSubExpr() || |
| 1035 | I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){ |
| 1036 | M = StateMgr.Remove(M, I.getKey()); |
| 1037 | } |
| 1038 | else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls. |
| 1039 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 1040 | if (!Liveness.isLive(Loc, V)) |
| 1041 | M = StateMgr.Remove(M, I.getKey()); |
| 1042 | } |
| 1043 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1045 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1048 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred, |
| 1049 | GRConstants::StateTy St) { |
| 1050 | |
| 1051 | // If the state hasn't changed, don't generate a new node. |
| 1052 | if (St == Pred->getState()) |
| 1053 | return; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1054 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1055 | Dst.Add(Builder->generateNode(S, St, Pred)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1056 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1057 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1058 | void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred, |
| 1059 | GRConstants::NodeSet& Dst) { |
| 1060 | |
| 1061 | QualType T = CastE->getType(); |
| 1062 | |
| 1063 | // Check for redundant casts. |
| 1064 | if (E->getType() == T) { |
| 1065 | Dst.Add(Pred); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | NodeSet S1; |
| 1070 | Visit(E, Pred, S1); |
| 1071 | |
| 1072 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1073 | NodeTy* N = *I1; |
| 1074 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1075 | const RValue& V = GetValue(St, E); |
| 1076 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1077 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 1081 | GRConstants::NodeSet& Dst) { |
| 1082 | |
| 1083 | StateTy St = Pred->getState(); |
| 1084 | |
| 1085 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 1086 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 1087 | const Expr* E = VD->getInit(); |
| 1088 | St = SetValue(St, LValueDecl(VD), |
| 1089 | E ? GetValue(St, E) : UninitializedValue()); |
| 1090 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1091 | |
| 1092 | Nodify(Dst, DS, Pred, St); |
| 1093 | |
| 1094 | if (Dst.empty()) |
| 1095 | Dst.Add(Pred); |
| 1096 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1097 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1098 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 1099 | GRConstants::NodeTy* Pred, |
| 1100 | GRConstants::NodeSet& Dst) { |
| 1101 | NodeSet S1; |
| 1102 | Visit(U->getSubExpr(), Pred, S1); |
| 1103 | |
| 1104 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1105 | NodeTy* N1 = *I1; |
| 1106 | StateTy St = N1->getState(); |
| 1107 | |
| 1108 | switch (U->getOpcode()) { |
| 1109 | case UnaryOperator::PostInc: { |
| 1110 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1111 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1112 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1113 | U->getLocStart()); |
Ted Kremenek | e0cf9c8 | 2008-01-24 19:00:57 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1115 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1116 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | case UnaryOperator::PostDec: { |
| 1121 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1122 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1123 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1124 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1125 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1126 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1127 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 1128 | break; |
| 1129 | } |
| 1130 | |
| 1131 | case UnaryOperator::PreInc: { |
| 1132 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1133 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1134 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1135 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1136 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1137 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1138 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 1139 | break; |
| 1140 | } |
| 1141 | |
| 1142 | case UnaryOperator::PreDec: { |
| 1143 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1144 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1145 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1146 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1147 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1148 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1149 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 1150 | break; |
| 1151 | } |
| 1152 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 1153 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1154 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 1155 | Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1159 | default: ; |
| 1160 | assert (false && "Not implemented."); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1165 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 1166 | GRConstants::NodeTy* Pred, |
| 1167 | GRConstants::NodeSet& Dst) { |
| 1168 | NodeSet S1; |
| 1169 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1171 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1172 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1173 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1174 | // When getting the value for the LHS, check if we are in an assignment. |
| 1175 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 1176 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1177 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 1178 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1179 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 1180 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1182 | NodeSet S2; |
| 1183 | Visit(B->getRHS(), N1, S2); |
| 1184 | |
| 1185 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
| 1186 | NodeTy* N2 = *I2; |
| 1187 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1188 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1189 | |
| 1190 | switch (B->getOpcode()) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1191 | default: |
| 1192 | Dst.Add(N2); |
| 1193 | break; |
| 1194 | |
| 1195 | // Arithmetic opreators. |
| 1196 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1197 | case BinaryOperator::Add: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1198 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1199 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1200 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1201 | Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | case BinaryOperator::Sub: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1206 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1207 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1208 | Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | } |
| 1211 | |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1212 | case BinaryOperator::Mul: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1213 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1214 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1215 | Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2))); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | } |
| 1218 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 1219 | case BinaryOperator::Div: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1220 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1221 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1222 | Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2))); |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | |
Ted Kremenek | cce207d | 2008-01-28 22:26:15 +0000 | [diff] [blame] | 1226 | case BinaryOperator::Rem: { |
| 1227 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1228 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1229 | Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2))); |
| 1230 | break; |
| 1231 | } |
| 1232 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1233 | // Assignment operators. |
| 1234 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1235 | case BinaryOperator::Assign: { |
| 1236 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1237 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1238 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2)); |
| 1239 | break; |
| 1240 | } |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1241 | |
| 1242 | case BinaryOperator::AddAssign: { |
| 1243 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1244 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1245 | NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1246 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1247 | break; |
| 1248 | } |
| 1249 | |
| 1250 | case BinaryOperator::SubAssign: { |
| 1251 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1252 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1253 | NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1254 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1255 | break; |
| 1256 | } |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1257 | |
| 1258 | case BinaryOperator::MulAssign: { |
| 1259 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1260 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1261 | NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1262 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1263 | break; |
| 1264 | } |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 1265 | |
| 1266 | case BinaryOperator::DivAssign: { |
| 1267 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1268 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1269 | NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 1270 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1271 | break; |
| 1272 | } |
Ted Kremenek | 10099a6 | 2008-01-28 22:28:54 +0000 | [diff] [blame] | 1273 | |
| 1274 | case BinaryOperator::RemAssign: { |
| 1275 | const LValue& L1 = cast<LValue>(V1); |
| 1276 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1277 | NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2)); |
| 1278 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1279 | break; |
| 1280 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1281 | |
| 1282 | // Equality operators. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1283 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame^] | 1284 | case BinaryOperator::EQ: |
| 1285 | // FIXME: should we allow XX.EQ() to return a set of values, |
| 1286 | // allowing state bifurcation? In such cases, they will also |
| 1287 | // modify the state (meaning that a new state will be returned |
| 1288 | // as well). |
| 1289 | assert (B->getType() == getContext().IntTy); |
| 1290 | |
| 1291 | if (isa<LValue>(V1)) { |
| 1292 | const LValue& L1 = cast<LValue>(V1); |
| 1293 | const LValue& L2 = cast<LValue>(V2); |
| 1294 | St = SetValue(St, B, L1.EQ(ValMgr, L2)); |
| 1295 | } |
| 1296 | else { |
| 1297 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1298 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1299 | St = SetValue(St, B, R1.EQ(ValMgr, R2)); |
| 1300 | } |
| 1301 | |
| 1302 | Nodify(Dst, B, N2, St); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1305 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1306 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1307 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1308 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1310 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 1311 | GRConstants::NodeSet& Dst) { |
| 1312 | |
| 1313 | // FIXME: add metadata to the CFG so that we can disable |
| 1314 | // this check when we KNOW that there is no block-level subexpression. |
| 1315 | // The motivation is that this check requires a hashtable lookup. |
| 1316 | |
| 1317 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1318 | Dst.Add(Pred); |
| 1319 | return; |
| 1320 | } |
| 1321 | |
| 1322 | switch (S->getStmtClass()) { |
| 1323 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1324 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1325 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1326 | break; |
| 1327 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1328 | case Stmt::UnaryOperatorClass: |
| 1329 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 1330 | break; |
| 1331 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1332 | case Stmt::ParenExprClass: |
| 1333 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1334 | break; |
| 1335 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1336 | case Stmt::ImplicitCastExprClass: { |
| 1337 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1338 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1339 | break; |
| 1340 | } |
| 1341 | |
| 1342 | case Stmt::CastExprClass: { |
| 1343 | CastExpr* C = cast<CastExpr>(S); |
| 1344 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1345 | break; |
| 1346 | } |
| 1347 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1348 | case Stmt::DeclStmtClass: |
| 1349 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1350 | break; |
| 1351 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1352 | default: |
| 1353 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1354 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1355 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1358 | //===----------------------------------------------------------------------===// |
| 1359 | // Driver. |
| 1360 | //===----------------------------------------------------------------------===// |
| 1361 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1362 | #ifndef NDEBUG |
| 1363 | namespace llvm { |
| 1364 | template<> |
| 1365 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1366 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1367 | |
| 1368 | static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1369 | switch (kind) { |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1370 | case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1371 | case ValueKey::IsDecl: Out << "Variables:\\l"; break; |
| 1372 | case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1373 | default: assert (false && "Unknown ValueKey type."); |
| 1374 | } |
| 1375 | } |
| 1376 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1377 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
| 1378 | ValueKey::Kind kind, bool isFirstGroup = false) { |
| 1379 | bool isFirst = true; |
| 1380 | |
| 1381 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) { |
| 1382 | if (I.getKey().getKind() != kind) |
| 1383 | continue; |
| 1384 | |
| 1385 | if (isFirst) { |
| 1386 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1387 | PrintKindLabel(Out, kind); |
| 1388 | isFirst = false; |
| 1389 | } |
| 1390 | else |
| 1391 | Out << "\\l"; |
| 1392 | |
| 1393 | Out << ' '; |
| 1394 | |
| 1395 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1396 | Out << V->getName(); |
| 1397 | else { |
| 1398 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1399 | Out << " (" << (void*) E << ") "; |
| 1400 | E->printPretty(Out); |
| 1401 | } |
| 1402 | |
| 1403 | Out << " : "; |
| 1404 | I.getData().print(Out); |
| 1405 | } |
| 1406 | } |
| 1407 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1408 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1409 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1410 | |
| 1411 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1412 | ProgramPoint Loc = N->getLocation(); |
| 1413 | |
| 1414 | switch (Loc.getKind()) { |
| 1415 | case ProgramPoint::BlockEntranceKind: |
| 1416 | Out << "Block Entrance: B" |
| 1417 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1418 | break; |
| 1419 | |
| 1420 | case ProgramPoint::BlockExitKind: |
| 1421 | assert (false); |
| 1422 | break; |
| 1423 | |
| 1424 | case ProgramPoint::PostStmtKind: { |
| 1425 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1426 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1427 | << (void*) L.getStmt() << ' '; |
| 1428 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1429 | L.getStmt()->printPretty(Out); |
| 1430 | break; |
| 1431 | } |
| 1432 | |
| 1433 | default: { |
| 1434 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1435 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1436 | << E.getDst()->getBlockID() << ')'; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1440 | Out << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1441 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1442 | PrintKind(Out, N->getState(), ValueKey::IsDecl, true); |
| 1443 | PrintKind(Out, N->getState(), ValueKey::IsBlkExpr); |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1444 | PrintKind(Out, N->getState(), ValueKey::IsSubExpr); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1445 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1446 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1447 | return Out.str(); |
| 1448 | } |
| 1449 | }; |
| 1450 | } // end llvm namespace |
| 1451 | #endif |
| 1452 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1453 | namespace clang { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1454 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) { |
| 1455 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1456 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1457 | #ifndef NDEBUG |
| 1458 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
| 1459 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1460 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1461 | } // end clang namespace |