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