Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1 | //===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==// |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame^] | 3 | // The LLVM 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: |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 736 | Out << '&' |
| 737 | << cast<LValueDecl>(this)->getDecl()->getIdentifier()->getName(); |
Ted Kremenek | 4150abf | 2008-01-31 00:09:56 +0000 | [diff] [blame] | 738 | break; |
| 739 | |
| 740 | default: |
| 741 | assert (false && "Pretty-printed not implemented for this LValue."); |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 747 | // ValueMapTy - A ImmutableMap type Stmt*/Decl*/Symbols to RValues. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 748 | //===----------------------------------------------------------------------===// |
| 749 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 750 | typedef llvm::ImmutableMap<ValueKey,RValue> ValueMapTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 751 | |
| 752 | namespace clang { |
| 753 | template<> |
| 754 | struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> { |
| 755 | static inline void* toPtr(ValueMapTy M) { |
| 756 | return reinterpret_cast<void*>(M.getRoot()); |
| 757 | } |
| 758 | static inline ValueMapTy toState(void* P) { |
| 759 | return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P)); |
| 760 | } |
| 761 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 764 | typedef ValueMapTy StateTy; |
| 765 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 766 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 767 | // The Checker. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 768 | // |
| 769 | // FIXME: This checker logic should be eventually broken into two components. |
| 770 | // The first is the "meta"-level checking logic; the code that |
| 771 | // does the Stmt visitation, fetching values from the map, etc. |
| 772 | // The second part does the actual state manipulation. This way we |
| 773 | // get more of a separate of concerns of these two pieces, with the |
| 774 | // latter potentially being refactored back into the main checking |
| 775 | // logic. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 776 | //===----------------------------------------------------------------------===// |
| 777 | |
| 778 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 779 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 780 | class VISIBILITY_HIDDEN GRConstants { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 781 | |
| 782 | public: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 783 | typedef ValueMapTy StateTy; |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 784 | typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder; |
| 785 | typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder; |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 786 | typedef ExplodedGraph<GRConstants> GraphTy; |
| 787 | typedef GraphTy::NodeTy NodeTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 788 | |
| 789 | class NodeSet { |
| 790 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 791 | ImplTy Impl; |
| 792 | public: |
| 793 | |
| 794 | NodeSet() {} |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 795 | NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 797 | void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 798 | |
| 799 | typedef ImplTy::iterator iterator; |
| 800 | typedef ImplTy::const_iterator const_iterator; |
| 801 | |
| 802 | unsigned size() const { return Impl.size(); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 803 | bool empty() const { return Impl.empty(); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 804 | |
| 805 | iterator begin() { return Impl.begin(); } |
| 806 | iterator end() { return Impl.end(); } |
| 807 | |
| 808 | const_iterator begin() const { return Impl.begin(); } |
| 809 | const_iterator end() const { return Impl.end(); } |
| 810 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 811 | |
| 812 | protected: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 813 | /// G - the simulation graph. |
| 814 | GraphTy& G; |
| 815 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 816 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 817 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 818 | LiveVariables Liveness; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 819 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 820 | /// Builder - The current GRStmtNodeBuilder which is used when building the nodes |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 821 | /// for a given statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 822 | StmtNodeBuilder* Builder; |
| 823 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 824 | /// StateMgr - Object that manages the data for all created states. |
| 825 | ValueMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 827 | /// ValueMgr - Object that manages the data for all created RValues. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 828 | ValueManager ValMgr; |
| 829 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 830 | /// SymMgr - Object that manages the symbol information. |
| 831 | SymbolManager SymMgr; |
| 832 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 833 | /// StmtEntryNode - The immediate predecessor node. |
| 834 | NodeTy* StmtEntryNode; |
| 835 | |
| 836 | /// CurrentStmt - The current block-level statement. |
| 837 | Stmt* CurrentStmt; |
| 838 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 839 | /// UninitBranches - Nodes in the ExplodedGraph that result from |
| 840 | /// taking a branch based on an uninitialized value. |
| 841 | typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy; |
| 842 | UninitBranchesTy UninitBranches; |
| 843 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 844 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 845 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 846 | ASTContext& getContext() const { return G.getContext(); } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 847 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 848 | public: |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 849 | GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()), |
| 850 | Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL), |
| 851 | CurrentStmt(NULL) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 852 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 853 | // Compute liveness information. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 854 | Liveness.runOnCFG(G.getCFG()); |
| 855 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 856 | } |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 857 | |
| 858 | /// getCFG - Returns the CFG associated with this analysis. |
| 859 | CFG& getCFG() { return G.getCFG(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 861 | /// getInitialState - Return the initial state used for the root vertex |
| 862 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 863 | StateTy getInitialState() { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 864 | StateTy St = StateMgr.GetEmptyMap(); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 865 | |
| 866 | // Iterate the parameters. |
| 867 | FunctionDecl& F = G.getFunctionDecl(); |
| 868 | |
| 869 | for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end(); |
Ted Kremenek | 4150abf | 2008-01-31 00:09:56 +0000 | [diff] [blame] | 870 | I!=E; ++I) |
| 871 | St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I)); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 872 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 873 | return St; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 874 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 875 | |
| 876 | bool isUninitControlFlow(const NodeTy* N) const { |
| 877 | return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0; |
| 878 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 879 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 880 | /// ProcessStmt - Called by GREngine. Used to generate new successor |
| 881 | /// nodes by processing the 'effects' of a block-level statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 882 | void ProcessStmt(Stmt* S, StmtNodeBuilder& builder); |
| 883 | |
| 884 | /// ProcessBranch - Called by GREngine. Used to generate successor |
| 885 | /// nodes by processing the 'effects' of a branch condition. |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 886 | void ProcessBranch(Stmt* Condition, Stmt* Term, BranchNodeBuilder& builder); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 887 | |
| 888 | /// RemoveDeadBindings - Return a new state that is the same as 'M' except |
| 889 | /// that all subexpression mappings are removed and that any |
| 890 | /// block-level expressions that are not live at 'S' also have their |
| 891 | /// mappings removed. |
| 892 | StateTy RemoveDeadBindings(Stmt* S, StateTy M); |
| 893 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 894 | StateTy SetValue(StateTy St, Stmt* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 895 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 896 | StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 897 | return SetValue(St, const_cast<Stmt*>(S), V); |
| 898 | } |
| 899 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 900 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 901 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 902 | RValue GetValue(const StateTy& St, Stmt* S); |
| 903 | inline RValue GetValue(const StateTy& St, const Stmt* S) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 904 | return GetValue(St, const_cast<Stmt*>(S)); |
| 905 | } |
| 906 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 907 | RValue GetValue(const StateTy& St, const LValue& LV); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 908 | LValue GetLValue(const StateTy& St, Stmt* S); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 909 | |
| 910 | /// Assume - Create new state by assuming that a given expression |
| 911 | /// is true or false. |
| 912 | inline StateTy Assume(StateTy St, RValue Cond, bool Assumption, |
| 913 | bool& isFeasible) { |
| 914 | if (isa<LValue>(Cond)) |
| 915 | return Assume(St, cast<LValue>(Cond), Assumption, isFeasible); |
| 916 | else |
| 917 | return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible); |
| 918 | } |
| 919 | |
| 920 | StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible); |
| 921 | StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 922 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 923 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 925 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 926 | /// other functions that handle specific kinds of statements. |
| 927 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 928 | |
| 929 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 930 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 931 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 932 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 933 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 934 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 935 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 936 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 937 | |
| 938 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
| 939 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 940 | }; |
| 941 | } // end anonymous namespace |
| 942 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 943 | |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 944 | void GRConstants::ProcessBranch(Stmt* Condition, Stmt* Term, |
| 945 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 946 | |
| 947 | StateTy PrevState = builder.getState(); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 949 | // Remove old bindings for subexpressions. |
| 950 | for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I) |
| 951 | if (I.getKey().isSubExpr()) |
| 952 | PrevState = StateMgr.Remove(PrevState, I.getKey()); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 954 | RValue V = GetValue(PrevState, Condition); |
| 955 | |
| 956 | switch (V.getBaseKind()) { |
| 957 | default: |
| 958 | break; |
| 959 | |
| 960 | case RValue::InvalidKind: |
| 961 | builder.generateNode(PrevState, true); |
| 962 | builder.generateNode(PrevState, false); |
| 963 | return; |
| 964 | |
| 965 | case RValue::UninitializedKind: { |
| 966 | NodeTy* N = builder.generateNode(PrevState, true); |
| 967 | |
| 968 | if (N) { |
| 969 | N->markAsSink(); |
| 970 | UninitBranches.insert(N); |
| 971 | } |
| 972 | |
| 973 | builder.markInfeasible(false); |
| 974 | return; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | // Process the true branch. |
| 979 | bool isFeasible = true; |
| 980 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 981 | |
| 982 | if (isFeasible) builder.generateNode(St, true); |
| 983 | else { |
| 984 | builder.markInfeasible(true); |
| 985 | isFeasible = true; |
| 986 | } |
| 987 | |
| 988 | // Process the false branch. |
| 989 | St = Assume(PrevState, V, false, isFeasible); |
| 990 | |
| 991 | if (isFeasible) builder.generateNode(St, false); |
| 992 | else builder.markInfeasible(false); |
| 993 | |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 996 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 997 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 998 | |
| 999 | StmtEntryNode = builder.getLastNode(); |
| 1000 | CurrentStmt = S; |
| 1001 | NodeSet Dst; |
| 1002 | StateCleaned = false; |
| 1003 | |
| 1004 | Visit(S, StmtEntryNode, Dst); |
| 1005 | |
| 1006 | // If no nodes were generated, generate a new node that has all the |
| 1007 | // dead mappings removed. |
| 1008 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 1009 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 1010 | builder.generateNode(S, St, StmtEntryNode); |
| 1011 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1012 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1013 | CurrentStmt = NULL; |
| 1014 | StmtEntryNode = NULL; |
| 1015 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1018 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1019 | RValue GRConstants::GetValue(const StateTy& St, const LValue& LV) { |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 1020 | switch (LV.getSubKind()) { |
| 1021 | case LValueDeclKind: { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1022 | StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl()); |
| 1023 | return T ? T->getValue().second : InvalidValue(); |
| 1024 | } |
| 1025 | default: |
| 1026 | assert (false && "Invalid LValue."); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 1027 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | return InvalidValue(); |
| 1031 | } |
| 1032 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1033 | RValue GRConstants::GetValue(const StateTy& St, Stmt* S) { |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 1034 | for (;;) { |
| 1035 | switch (S->getStmtClass()) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1036 | |
| 1037 | // ParenExprs are no-ops. |
| 1038 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 1039 | case Stmt::ParenExprClass: |
| 1040 | S = cast<ParenExpr>(S)->getSubExpr(); |
| 1041 | continue; |
| 1042 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1043 | // DeclRefExprs can either evaluate to an LValue or a Non-LValue |
| 1044 | // (assuming an implicit "load") depending on the context. In this |
| 1045 | // context we assume that we are retrieving the value contained |
| 1046 | // within the referenced variables. |
| 1047 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 1048 | case Stmt::DeclRefExprClass: |
| 1049 | return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl())); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 1050 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1051 | // Integer literals evaluate to an RValue. Simply retrieve the |
| 1052 | // RValue for the literal. |
| 1053 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 1054 | case Stmt::IntegerLiteralClass: |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1055 | return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(S)); |
| 1056 | |
| 1057 | // Casts where the source and target type are the same |
| 1058 | // are no-ops. We blast through these to get the descendant |
| 1059 | // subexpression that has a value. |
| 1060 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1061 | case Stmt::ImplicitCastExprClass: { |
| 1062 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1063 | if (C->getType() == C->getSubExpr()->getType()) { |
| 1064 | S = C->getSubExpr(); |
| 1065 | continue; |
| 1066 | } |
| 1067 | break; |
| 1068 | } |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1069 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1070 | case Stmt::CastExprClass: { |
| 1071 | CastExpr* C = cast<CastExpr>(S); |
| 1072 | if (C->getType() == C->getSubExpr()->getType()) { |
| 1073 | S = C->getSubExpr(); |
| 1074 | continue; |
| 1075 | } |
| 1076 | break; |
| 1077 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1078 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1079 | // Handle all other Stmt* using a lookup. |
| 1080 | |
Ted Kremenek | 671c9e8 | 2008-01-24 00:50:08 +0000 | [diff] [blame] | 1081 | default: |
| 1082 | break; |
| 1083 | }; |
| 1084 | |
| 1085 | break; |
| 1086 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1087 | |
Ted Kremenek | 5c1b996 | 2008-01-24 19:43:37 +0000 | [diff] [blame] | 1088 | StateTy::TreeTy* T = St.SlimFind(S); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1090 | return T ? T->getValue().second : InvalidValue(); |
| 1091 | } |
| 1092 | |
| 1093 | LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1094 | while (ParenExpr* P = dyn_cast<ParenExpr>(S)) |
| 1095 | S = P->getSubExpr(); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 1098 | return LValueDecl(DR->getDecl()); |
| 1099 | |
| 1100 | return cast<LValue>(GetValue(St, S)); |
| 1101 | } |
| 1102 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1103 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1104 | GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S, |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1105 | const RValue& V) { |
Ted Kremenek | cc1c365 | 2008-01-25 23:43:12 +0000 | [diff] [blame] | 1106 | assert (S); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (!StateCleaned) { |
| 1109 | St = RemoveDeadBindings(CurrentStmt, St); |
| 1110 | StateCleaned = true; |
| 1111 | } |
| 1112 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1113 | bool isBlkExpr = false; |
| 1114 | |
| 1115 | if (S == CurrentStmt) { |
| 1116 | isBlkExpr = getCFG().isBlkExpr(S); |
| 1117 | |
| 1118 | if (!isBlkExpr) |
| 1119 | return St; |
| 1120 | } |
Ted Kremenek | daadf45 | 2008-01-24 19:28:01 +0000 | [diff] [blame] | 1121 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1122 | return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V) |
| 1123 | : St; |
| 1124 | } |
| 1125 | |
| 1126 | GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV, |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1127 | const RValue& V) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1128 | if (!LV.isValid()) |
| 1129 | return St; |
| 1130 | |
| 1131 | if (!StateCleaned) { |
| 1132 | St = RemoveDeadBindings(CurrentStmt, St); |
| 1133 | StateCleaned = true; |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 1134 | } |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 1135 | |
Ted Kremenek | f13794e | 2008-01-24 23:19:54 +0000 | [diff] [blame] | 1136 | switch (LV.getSubKind()) { |
| 1137 | case LValueDeclKind: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1138 | return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V) |
| 1139 | : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl()); |
| 1140 | |
| 1141 | default: |
| 1142 | assert ("SetValue for given LValue type not yet implemented."); |
| 1143 | return St; |
| 1144 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1147 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1148 | // Note: in the code below, we can assign a new map to M since the |
| 1149 | // iterators are iterating over the tree of the *original* map. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1150 | StateTy::iterator I = M.begin(), E = M.end(); |
| 1151 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 1153 | for (; I!=E && !I.getKey().isSymbol(); ++I) { |
| 1154 | // Remove old bindings for subexpressions and "dead" |
| 1155 | // block-level expressions. |
| 1156 | if (I.getKey().isSubExpr() || |
| 1157 | I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){ |
| 1158 | M = StateMgr.Remove(M, I.getKey()); |
| 1159 | } |
| 1160 | else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls. |
| 1161 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 1162 | if (!Liveness.isLive(Loc, V)) |
| 1163 | M = StateMgr.Remove(M, I.getKey()); |
| 1164 | } |
| 1165 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1167 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1170 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred, |
| 1171 | GRConstants::StateTy St) { |
| 1172 | |
| 1173 | // If the state hasn't changed, don't generate a new node. |
| 1174 | if (St == Pred->getState()) |
| 1175 | return; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1176 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1177 | Dst.Add(Builder->generateNode(S, St, Pred)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1178 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1179 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1180 | void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred, |
| 1181 | GRConstants::NodeSet& Dst) { |
| 1182 | |
| 1183 | QualType T = CastE->getType(); |
| 1184 | |
| 1185 | // Check for redundant casts. |
| 1186 | if (E->getType() == T) { |
| 1187 | Dst.Add(Pred); |
| 1188 | return; |
| 1189 | } |
| 1190 | |
| 1191 | NodeSet S1; |
| 1192 | Visit(E, Pred, S1); |
| 1193 | |
| 1194 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1195 | NodeTy* N = *I1; |
| 1196 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1197 | const RValue& V = GetValue(St, E); |
| 1198 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1199 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 1203 | GRConstants::NodeSet& Dst) { |
| 1204 | |
| 1205 | StateTy St = Pred->getState(); |
| 1206 | |
| 1207 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 1208 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 1209 | const Expr* E = VD->getInit(); |
| 1210 | St = SetValue(St, LValueDecl(VD), |
| 1211 | E ? GetValue(St, E) : UninitializedValue()); |
| 1212 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1213 | |
| 1214 | Nodify(Dst, DS, Pred, St); |
| 1215 | |
| 1216 | if (Dst.empty()) |
| 1217 | Dst.Add(Pred); |
| 1218 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1219 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1220 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 1221 | GRConstants::NodeTy* Pred, |
| 1222 | GRConstants::NodeSet& Dst) { |
| 1223 | NodeSet S1; |
| 1224 | Visit(U->getSubExpr(), Pred, S1); |
| 1225 | |
| 1226 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1227 | NodeTy* N1 = *I1; |
| 1228 | StateTy St = N1->getState(); |
| 1229 | |
| 1230 | switch (U->getOpcode()) { |
| 1231 | case UnaryOperator::PostInc: { |
| 1232 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1233 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1234 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1235 | U->getLocStart()); |
Ted Kremenek | e0cf9c8 | 2008-01-24 19:00:57 +0000 | [diff] [blame] | 1236 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1237 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1238 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 1239 | break; |
| 1240 | } |
| 1241 | |
| 1242 | case UnaryOperator::PostDec: { |
| 1243 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1244 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1245 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1246 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1247 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1248 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1249 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 1250 | break; |
| 1251 | } |
| 1252 | |
| 1253 | case UnaryOperator::PreInc: { |
| 1254 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1255 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1256 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1257 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1258 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1259 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1260 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | case UnaryOperator::PreDec: { |
| 1265 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1266 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1267 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 1268 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1269 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1270 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1271 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 1272 | break; |
| 1273 | } |
| 1274 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 1275 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1276 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 1277 | Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 1281 | case UnaryOperator::AddrOf: { |
| 1282 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 1283 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 1284 | break; |
| 1285 | } |
| 1286 | |
| 1287 | case UnaryOperator::Deref: { |
| 1288 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 1289 | Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1))); |
| 1290 | break; |
| 1291 | } |
| 1292 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1293 | default: ; |
| 1294 | assert (false && "Not implemented."); |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1299 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 1300 | GRConstants::NodeTy* Pred, |
| 1301 | GRConstants::NodeSet& Dst) { |
| 1302 | NodeSet S1; |
| 1303 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1304 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1305 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 1306 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1307 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1308 | // When getting the value for the LHS, check if we are in an assignment. |
| 1309 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 1310 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1311 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 1312 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1313 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 1314 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1316 | NodeSet S2; |
| 1317 | Visit(B->getRHS(), N1, S2); |
| 1318 | |
| 1319 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
| 1320 | NodeTy* N2 = *I2; |
| 1321 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1322 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1323 | |
| 1324 | switch (B->getOpcode()) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1325 | default: |
| 1326 | Dst.Add(N2); |
| 1327 | break; |
| 1328 | |
| 1329 | // Arithmetic opreators. |
| 1330 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1331 | case BinaryOperator::Add: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1332 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1333 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1335 | Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | case BinaryOperator::Sub: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1340 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1341 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1342 | Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1343 | break; |
| 1344 | } |
| 1345 | |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1346 | case BinaryOperator::Mul: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1347 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1348 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1349 | Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2))); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1350 | break; |
| 1351 | } |
| 1352 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 1353 | case BinaryOperator::Div: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1354 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1355 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1356 | Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2))); |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | } |
| 1359 | |
Ted Kremenek | cce207d | 2008-01-28 22:26:15 +0000 | [diff] [blame] | 1360 | case BinaryOperator::Rem: { |
| 1361 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1362 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1363 | Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2))); |
| 1364 | break; |
| 1365 | } |
| 1366 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1367 | // Assignment operators. |
| 1368 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1369 | case BinaryOperator::Assign: { |
| 1370 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1371 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1372 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2)); |
| 1373 | break; |
| 1374 | } |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1375 | |
| 1376 | case BinaryOperator::AddAssign: { |
| 1377 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1378 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1379 | NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1380 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | case BinaryOperator::SubAssign: { |
| 1385 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1386 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1387 | NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1388 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1389 | break; |
| 1390 | } |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1391 | |
| 1392 | case BinaryOperator::MulAssign: { |
| 1393 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1394 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1395 | NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 1396 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1397 | break; |
| 1398 | } |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 1399 | |
| 1400 | case BinaryOperator::DivAssign: { |
| 1401 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 1402 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1403 | NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 1404 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1405 | break; |
| 1406 | } |
Ted Kremenek | 10099a6 | 2008-01-28 22:28:54 +0000 | [diff] [blame] | 1407 | |
| 1408 | case BinaryOperator::RemAssign: { |
| 1409 | const LValue& L1 = cast<LValue>(V1); |
| 1410 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 1411 | NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2)); |
| 1412 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 1413 | break; |
| 1414 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Equality operators. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1417 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1418 | case BinaryOperator::EQ: |
| 1419 | // FIXME: should we allow XX.EQ() to return a set of values, |
| 1420 | // allowing state bifurcation? In such cases, they will also |
| 1421 | // modify the state (meaning that a new state will be returned |
| 1422 | // as well). |
| 1423 | assert (B->getType() == getContext().IntTy); |
| 1424 | |
| 1425 | if (isa<LValue>(V1)) { |
| 1426 | const LValue& L1 = cast<LValue>(V1); |
| 1427 | const LValue& L2 = cast<LValue>(V2); |
| 1428 | St = SetValue(St, B, L1.EQ(ValMgr, L2)); |
| 1429 | } |
| 1430 | else { |
| 1431 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 1432 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 1433 | St = SetValue(St, B, R1.EQ(ValMgr, R2)); |
| 1434 | } |
| 1435 | |
| 1436 | Nodify(Dst, B, N2, St); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1439 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1440 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1441 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1442 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1443 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1444 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 1445 | GRConstants::NodeSet& Dst) { |
| 1446 | |
| 1447 | // FIXME: add metadata to the CFG so that we can disable |
| 1448 | // this check when we KNOW that there is no block-level subexpression. |
| 1449 | // The motivation is that this check requires a hashtable lookup. |
| 1450 | |
| 1451 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1452 | Dst.Add(Pred); |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | switch (S->getStmtClass()) { |
| 1457 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1458 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1459 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1460 | break; |
| 1461 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 1462 | case Stmt::UnaryOperatorClass: |
| 1463 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 1464 | break; |
| 1465 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1466 | case Stmt::ParenExprClass: |
| 1467 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1468 | break; |
| 1469 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 1470 | case Stmt::ImplicitCastExprClass: { |
| 1471 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1472 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1473 | break; |
| 1474 | } |
| 1475 | |
| 1476 | case Stmt::CastExprClass: { |
| 1477 | CastExpr* C = cast<CastExpr>(S); |
| 1478 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1479 | break; |
| 1480 | } |
| 1481 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1482 | case Stmt::DeclStmtClass: |
| 1483 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1484 | break; |
| 1485 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1486 | default: |
| 1487 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1488 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1489 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1492 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1493 | // "Assume" logic. |
| 1494 | //===----------------------------------------------------------------------===// |
| 1495 | |
| 1496 | StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption, |
| 1497 | bool& isFeasible) { |
| 1498 | return St; |
| 1499 | } |
| 1500 | |
| 1501 | StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption, |
| 1502 | bool& isFeasible) { |
| 1503 | |
| 1504 | switch (Cond.getSubKind()) { |
| 1505 | default: |
| 1506 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1507 | return St; |
| 1508 | |
| 1509 | case ConcreteIntKind: { |
| 1510 | bool b = cast<ConcreteInt>(Cond).getValue() != 0; |
| 1511 | isFeasible = b ? Assumption : !Assumption; |
| 1512 | return St; |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1519 | // Driver. |
| 1520 | //===----------------------------------------------------------------------===// |
| 1521 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1522 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1523 | static GRConstants* GraphPrintCheckerState; |
| 1524 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1525 | namespace llvm { |
| 1526 | template<> |
| 1527 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1528 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1529 | |
| 1530 | static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1531 | switch (kind) { |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1532 | case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1533 | case ValueKey::IsDecl: Out << "Variables:\\l"; break; |
| 1534 | case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1535 | default: assert (false && "Unknown ValueKey type."); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1539 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
| 1540 | ValueKey::Kind kind, bool isFirstGroup = false) { |
| 1541 | bool isFirst = true; |
| 1542 | |
| 1543 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) { |
| 1544 | if (I.getKey().getKind() != kind) |
| 1545 | continue; |
| 1546 | |
| 1547 | if (isFirst) { |
| 1548 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1549 | PrintKindLabel(Out, kind); |
| 1550 | isFirst = false; |
| 1551 | } |
| 1552 | else |
| 1553 | Out << "\\l"; |
| 1554 | |
| 1555 | Out << ' '; |
| 1556 | |
| 1557 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1558 | Out << V->getName(); |
| 1559 | else { |
| 1560 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1561 | Out << " (" << (void*) E << ") "; |
| 1562 | E->printPretty(Out); |
| 1563 | } |
| 1564 | |
| 1565 | Out << " : "; |
| 1566 | I.getData().print(Out); |
| 1567 | } |
| 1568 | } |
| 1569 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1570 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1571 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1572 | |
| 1573 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1574 | ProgramPoint Loc = N->getLocation(); |
| 1575 | |
| 1576 | switch (Loc.getKind()) { |
| 1577 | case ProgramPoint::BlockEntranceKind: |
| 1578 | Out << "Block Entrance: B" |
| 1579 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1580 | break; |
| 1581 | |
| 1582 | case ProgramPoint::BlockExitKind: |
| 1583 | assert (false); |
| 1584 | break; |
| 1585 | |
| 1586 | case ProgramPoint::PostStmtKind: { |
| 1587 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1588 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1589 | << (void*) L.getStmt() << ' '; |
| 1590 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1591 | L.getStmt()->printPretty(Out); |
| 1592 | break; |
| 1593 | } |
| 1594 | |
| 1595 | default: { |
| 1596 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1597 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1598 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1599 | |
| 1600 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1601 | Out << "\\|Terminator: "; |
| 1602 | E.getSrc()->printTerminator(Out); |
| 1603 | |
| 1604 | if (isa<SwitchStmt>(T)) { |
| 1605 | // FIXME |
| 1606 | } |
| 1607 | else { |
| 1608 | Out << "\\lCondition: "; |
| 1609 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1610 | Out << "true"; |
| 1611 | else |
| 1612 | Out << "false"; |
| 1613 | } |
| 1614 | |
| 1615 | Out << "\\l"; |
| 1616 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1617 | |
| 1618 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1619 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1620 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 1624 | Out << "\\|StateID: " << (void*) N->getState().getRoot() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1625 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1626 | PrintKind(Out, N->getState(), ValueKey::IsDecl, true); |
| 1627 | PrintKind(Out, N->getState(), ValueKey::IsBlkExpr); |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 1628 | PrintKind(Out, N->getState(), ValueKey::IsSubExpr); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1629 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1630 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1631 | return Out.str(); |
| 1632 | } |
| 1633 | }; |
| 1634 | } // end llvm namespace |
| 1635 | #endif |
| 1636 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1637 | namespace clang { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1638 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) { |
| 1639 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1640 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1641 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1642 | GraphPrintCheckerState = &Engine.getCheckerState(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1643 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1644 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1645 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1646 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1647 | } // end clang namespace |