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