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