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