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