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