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" |
| 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 21 | |
| 22 | #include "llvm/Support/Casting.h" |
| 23 | #include "llvm/Support/DataTypes.h" |
| 24 | #include "llvm/ADT/APSInt.h" |
| 25 | #include "llvm/ADT/FoldingSet.h" |
| 26 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
| 30 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Streams.h" |
| 32 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 33 | #ifndef NDEBUG |
| 34 | #include "llvm/Support/GraphWriter.h" |
| 35 | #include <sstream> |
| 36 | #endif |
| 37 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 38 | using namespace clang; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 39 | using llvm::dyn_cast; |
| 40 | using llvm::cast; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 43 | /// ValueKey - A variant smart pointer that wraps either a ValueDecl* or a |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | namespace { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 47 | |
| 48 | class VISIBILITY_HIDDEN ValueKey { |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 49 | uintptr_t Raw; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 50 | public: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 51 | enum Kind { IsSubExp=0x0, IsDecl=0x1, IsBlkExpr=0x2, Flags=0x3 }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 52 | inline void* getPtr() const { return reinterpret_cast<void*>(Raw & ~Flags); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 53 | inline Kind getKind() const { return (Kind) (Raw & Flags); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 55 | ValueKey(const ValueDecl* VD) |
| 56 | : Raw(reinterpret_cast<uintptr_t>(VD) | IsDecl) {} |
| 57 | |
| 58 | ValueKey(Stmt* S, bool isBlkExpr) |
| 59 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkExpr ? IsBlkExpr : IsSubExp)){} |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 60 | |
| 61 | bool isSubExpr() const { return getKind() == IsSubExp; } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 62 | bool isDecl() const { return getKind() == IsDecl; } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 63 | |
| 64 | inline void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 9849185 | 2008-01-16 05:51:13 +0000 | [diff] [blame] | 65 | ID.AddPointer(getPtr()); |
| 66 | ID.AddInteger((unsigned) getKind()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | inline bool operator==(const ValueKey& X) const { |
| 70 | return Raw == X.Raw; |
| 71 | } |
| 72 | |
| 73 | inline bool operator!=(const ValueKey& X) const { |
| 74 | return !operator==(X); |
| 75 | } |
| 76 | |
| 77 | inline bool operator<(const ValueKey& X) const { |
| 78 | Kind k = getKind(), Xk = X.getKind(); |
| 79 | |
| 80 | return k == Xk ? getPtr() < X.getPtr() |
| 81 | : ((unsigned) k) < ((unsigned) Xk); |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 83 | }; |
| 84 | } // end anonymous namespace |
| 85 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 86 | // Machinery to get cast<> and dyn_cast<> working with ValueKey. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 87 | namespace llvm { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 88 | template<> inline bool isa<ValueDecl,ValueKey>(const ValueKey& V) { |
| 89 | return V.getKind() == ValueKey::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 90 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 91 | template<> inline bool isa<Stmt,ValueKey>(const ValueKey& V) { |
| 92 | return ((unsigned) V.getKind()) != ValueKey::IsDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 93 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 94 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,ValueKey> { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 95 | typedef const ValueDecl* ret_type; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 96 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 97 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,ValueKey> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 98 | typedef const Stmt* ret_type; |
| 99 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 100 | template<> struct VISIBILITY_HIDDEN simplify_type<ValueKey> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 101 | typedef void* SimpleType; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 102 | static inline SimpleType getSimplifiedValue(const ValueKey &V) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 103 | return V.getPtr(); |
| 104 | } |
| 105 | }; |
| 106 | } // end llvm namespace |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 109 | // ValueManager. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
| 111 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 112 | namespace { |
| 113 | |
| 114 | typedef llvm::ImmutableSet<llvm::APSInt > APSIntSetTy; |
| 115 | |
| 116 | class VISIBILITY_HIDDEN ValueManager { |
| 117 | |
| 118 | |
| 119 | APSIntSetTy::Factory APSIntSetFactory; |
| 120 | |
| 121 | public: |
| 122 | ValueManager() {} |
| 123 | ~ValueManager() {} |
| 124 | |
| 125 | APSIntSetTy GetEmptyAPSIntSet() { |
| 126 | return APSIntSetFactory.GetEmptySet(); |
| 127 | } |
| 128 | |
| 129 | APSIntSetTy AddToSet(const APSIntSetTy& Set, const llvm::APSInt& Val) { |
| 130 | return APSIntSetFactory.Add(Set, Val); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 131 | } |
| 132 | }; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 133 | } // end anonymous namespace |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | // Expression Values. |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
| 139 | namespace { |
| 140 | |
| 141 | class VISIBILITY_HIDDEN ExprValue { |
| 142 | public: |
| 143 | enum Kind { // L-Values. |
| 144 | LValueDeclKind = 0x0, |
| 145 | // Special "Invalid" value. |
| 146 | InvalidValueKind = 0x1, |
| 147 | // R-Values. |
| 148 | RValueMayEqualSetKind = 0x2, |
| 149 | // Note that the Lvalue and RValue "kinds" overlap; |
| 150 | // the "InvalidValue" class can be used either as |
| 151 | // an LValue or RValue. |
| 152 | MinLValueKind = 0x0, MaxLValueKind = 0x1, |
| 153 | MinRValueKind = 0x1, MaxRValueKind = 0x2 }; |
| 154 | |
| 155 | private: |
| 156 | enum Kind kind; |
| 157 | void* Data; |
| 158 | |
| 159 | protected: |
| 160 | ExprValue(Kind k, void* d) : kind(k), Data(d) {} |
| 161 | |
| 162 | void* getRawPtr() const { return Data; } |
| 163 | |
| 164 | public: |
| 165 | ~ExprValue() {}; |
| 166 | |
| 167 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 168 | ID.AddInteger((unsigned) getKind()); |
| 169 | ID.AddPointer(Data); |
| 170 | } |
| 171 | |
| 172 | bool operator==(const ExprValue& RHS) const { |
| 173 | return kind == RHS.kind && Data == RHS.Data; |
| 174 | } |
| 175 | |
| 176 | Kind getKind() const { return kind; } |
| 177 | bool isValid() const { return getKind() != InvalidValueKind; } |
| 178 | |
| 179 | void print(std::ostream& OS) const; |
| 180 | void print() const { print(*llvm::cerr.stream()); } |
| 181 | |
| 182 | // Implement isa<T> support. |
| 183 | static inline bool classof(const ExprValue*) { return true; } |
| 184 | }; |
| 185 | |
| 186 | class VISIBILITY_HIDDEN InvalidValue : public ExprValue { |
| 187 | public: |
| 188 | InvalidValue() : ExprValue(InvalidValueKind, NULL) {} |
| 189 | |
| 190 | static inline bool classof(const ExprValue* V) { |
| 191 | return V->getKind() == InvalidValueKind; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | } // end anonymous namespace |
| 196 | |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | // "R-Values": Interface. |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | |
| 201 | namespace { |
| 202 | class VISIBILITY_HIDDEN RValue : public ExprValue { |
| 203 | protected: |
| 204 | RValue(Kind k, void* d) : ExprValue(k,d) {} |
| 205 | |
| 206 | public: |
| 207 | RValue Add(ValueManager& ValMgr, const RValue& RHS) const; |
| 208 | RValue Sub(ValueManager& ValMgr, const RValue& RHS) const; |
| 209 | |
| 210 | static RValue GetRValue(ValueManager& ValMgr, IntegerLiteral* S); |
| 211 | |
| 212 | // Implement isa<T> support. |
| 213 | static inline bool classof(const ExprValue* V) { |
| 214 | return V->getKind() >= MinRValueKind; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | class VISIBILITY_HIDDEN RValueMayEqualSet : public RValue { |
| 219 | public: |
| 220 | RValueMayEqualSet(const APSIntSetTy& S) |
| 221 | : RValue(RValueMayEqualSetKind, S.getRoot()) {} |
| 222 | |
| 223 | APSIntSetTy GetValues() const { |
| 224 | return APSIntSetTy(reinterpret_cast<APSIntSetTy::TreeTy*>(getRawPtr())); |
| 225 | } |
| 226 | |
| 227 | RValueMayEqualSet Add(ValueManager& ValMgr, const RValueMayEqualSet& V) const; |
| 228 | RValueMayEqualSet Sub(ValueManager& ValMgr, const RValueMayEqualSet& V) const; |
| 229 | |
| 230 | // Implement isa<T> support. |
| 231 | static inline bool classof(const ExprValue* V) { |
| 232 | return V->getKind() == RValueMayEqualSetKind; |
| 233 | } |
| 234 | }; |
| 235 | } // end anonymous namespace |
| 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // "R-Values": Implementation. |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
| 241 | #define RVALUE_DISPATCH_CASE(k1,k2,Op)\ |
| 242 | case ((k1##Kind+(MaxRValueKind-MinRValueKind))+(k2##Kind - MinRValueKind)):\ |
| 243 | return cast<k1>(*this).Op(ValMgr,cast<k2>(RHS)); |
| 244 | |
| 245 | #define RVALUE_DISPATCH(Op)\ |
| 246 | switch (getKind()+(MaxRValueKind-MinRValueKind)+(RHS.getKind()-MinRValueKind)){\ |
| 247 | RVALUE_DISPATCH_CASE(RValueMayEqualSet,RValueMayEqualSet,Op)\ |
| 248 | default:\ |
| 249 | assert (!isValid() || !RHS.isValid() && "Missing case.");\ |
| 250 | break;\ |
| 251 | }\ |
| 252 | return cast<RValue>(InvalidValue()); |
| 253 | |
| 254 | RValue RValue::Add(ValueManager& ValMgr, const RValue& RHS) const { |
| 255 | RVALUE_DISPATCH(Add) |
| 256 | } |
| 257 | |
| 258 | RValue RValue::Sub(ValueManager& ValMgr, const RValue& RHS) const { |
| 259 | RVALUE_DISPATCH(Sub) |
| 260 | } |
| 261 | |
| 262 | #undef RVALUE_DISPATCH_CASE |
| 263 | #undef RVALUE_DISPATCH |
| 264 | |
| 265 | RValueMayEqualSet |
| 266 | RValueMayEqualSet::Add(ValueManager& ValMgr, |
| 267 | const RValueMayEqualSet& RHS) const { |
| 268 | |
| 269 | APSIntSetTy S1 = GetValues(); |
| 270 | APSIntSetTy S2 = RHS.GetValues(); |
| 271 | |
| 272 | APSIntSetTy M = ValMgr.GetEmptyAPSIntSet(); |
| 273 | |
| 274 | for (APSIntSetTy::iterator I1=S1.begin(), E1=S2.end(); I1!=E1; ++I1) |
| 275 | for (APSIntSetTy::iterator I2=S2.begin(), E2=S2.end(); I2!=E2; ++I2) |
| 276 | M = ValMgr.AddToSet(M, *I1 + *I2); |
| 277 | |
| 278 | return M; |
| 279 | } |
| 280 | |
| 281 | RValueMayEqualSet |
| 282 | RValueMayEqualSet::Sub(ValueManager& ValMgr, |
| 283 | const RValueMayEqualSet& RHS) const { |
| 284 | |
| 285 | APSIntSetTy S1 = GetValues(); |
| 286 | APSIntSetTy S2 = RHS.GetValues(); |
| 287 | |
| 288 | APSIntSetTy M = ValMgr.GetEmptyAPSIntSet(); |
| 289 | |
| 290 | for (APSIntSetTy::iterator I1=S1.begin(), E1=S2.end(); I1!=E1; ++I1) |
| 291 | for (APSIntSetTy::iterator I2=S2.begin(), E2=S2.end(); I2!=E2; ++I2) |
| 292 | M = ValMgr.AddToSet(M, *I1 - *I2); |
| 293 | |
| 294 | return M; |
| 295 | } |
| 296 | |
| 297 | RValue RValue::GetRValue(ValueManager& ValMgr, IntegerLiteral* S) { |
| 298 | return RValueMayEqualSet(ValMgr.AddToSet(ValMgr.GetEmptyAPSIntSet(), |
| 299 | S->getValue())); |
| 300 | } |
| 301 | |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | // "L-Values". |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | |
| 306 | namespace { |
| 307 | |
| 308 | class VISIBILITY_HIDDEN LValue : public ExprValue { |
| 309 | protected: |
| 310 | LValue(Kind k, void* D) : ExprValue(k, D) {} |
| 311 | |
| 312 | public: |
| 313 | // Implement isa<T> support. |
| 314 | static inline bool classof(const ExprValue* V) { |
| 315 | return V->getKind() <= MaxLValueKind; |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | class VISIBILITY_HIDDEN LValueDecl : public LValue { |
| 320 | public: |
| 321 | LValueDecl(ValueDecl* vd) : LValue(LValueDeclKind,vd) {} |
| 322 | |
| 323 | ValueDecl* getDecl() const { |
| 324 | return static_cast<ValueDecl*>(getRawPtr()); |
| 325 | } |
| 326 | |
| 327 | // Implement isa<T> support. |
| 328 | static inline bool classof(const ExprValue* V) { |
| 329 | return V->getKind() == LValueDeclKind; |
| 330 | } |
| 331 | }; |
| 332 | } // end anonymous namespace |
| 333 | |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | // Pretty-Printing. |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | |
| 338 | void ExprValue::print(std::ostream& Out) const { |
| 339 | switch (getKind()) { |
| 340 | case InvalidValueKind: |
| 341 | Out << "Invalid"; break; |
| 342 | |
| 343 | case RValueMayEqualSetKind: { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 344 | APSIntSetTy S = cast<RValueMayEqualSet>(this)->GetValues(); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 345 | bool first = true; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 347 | for (APSIntSetTy::iterator I=S.begin(), E=S.end(); I!=E; ++I) { |
| 348 | if (first) first = false; |
| 349 | else Out << " | "; |
| 350 | |
| 351 | Out << (*I).toString(); |
| 352 | } |
| 353 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | |
| 357 | default: |
| 358 | assert (false && "Pretty-printed not implemented for this ExprValue."); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // ValueMapTy - A ImmutableMap type Stmt*/Decl* to ExprValues. |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | typedef llvm::ImmutableMap<ValueKey,ExprValue> ValueMapTy; |
| 368 | |
| 369 | namespace clang { |
| 370 | template<> |
| 371 | struct VISIBILITY_HIDDEN GRTrait<ValueMapTy> { |
| 372 | static inline void* toPtr(ValueMapTy M) { |
| 373 | return reinterpret_cast<void*>(M.getRoot()); |
| 374 | } |
| 375 | static inline ValueMapTy toState(void* P) { |
| 376 | return ValueMapTy(static_cast<ValueMapTy::TreeTy*>(P)); |
| 377 | } |
| 378 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | // The Checker! |
| 383 | //===----------------------------------------------------------------------===// |
| 384 | |
| 385 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 387 | class VISIBILITY_HIDDEN GRConstants { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 388 | |
| 389 | public: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 390 | typedef ValueMapTy StateTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 391 | typedef GRNodeBuilder<GRConstants> NodeBuilder; |
| 392 | typedef ExplodedNode<StateTy> NodeTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 393 | |
| 394 | class NodeSet { |
| 395 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 396 | ImplTy Impl; |
| 397 | public: |
| 398 | |
| 399 | NodeSet() {} |
| 400 | NodeSet(NodeTy* N) { assert (N && !N->isInfeasible()); Impl.push_back(N); } |
| 401 | |
| 402 | void Add(NodeTy* N) { if (N && !N->isInfeasible()) Impl.push_back(N); } |
| 403 | |
| 404 | typedef ImplTy::iterator iterator; |
| 405 | typedef ImplTy::const_iterator const_iterator; |
| 406 | |
| 407 | unsigned size() const { return Impl.size(); } |
| 408 | |
| 409 | iterator begin() { return Impl.begin(); } |
| 410 | iterator end() { return Impl.end(); } |
| 411 | |
| 412 | const_iterator begin() const { return Impl.begin(); } |
| 413 | const_iterator end() const { return Impl.end(); } |
| 414 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 415 | |
| 416 | protected: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 417 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 418 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 419 | LiveVariables* Liveness; |
| 420 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 421 | /// Builder - The current GRNodeBuilder which is used when building the nodes |
| 422 | /// for a given statement. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 423 | NodeBuilder* Builder; |
| 424 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 425 | /// StateMgr - Object that manages the data for all created states. |
| 426 | ValueMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 428 | /// ValueMgr - Object that manages the data for all created ExprValues. |
| 429 | ValueManager ValMgr; |
| 430 | |
| 431 | /// cfg - the current CFG. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 432 | CFG* cfg; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 433 | |
| 434 | /// StmtEntryNode - The immediate predecessor node. |
| 435 | NodeTy* StmtEntryNode; |
| 436 | |
| 437 | /// CurrentStmt - The current block-level statement. |
| 438 | Stmt* CurrentStmt; |
| 439 | |
| 440 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 442 | public: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 443 | GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL), |
| 444 | StmtEntryNode(NULL), CurrentStmt(NULL) {} |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 445 | |
| 446 | ~GRConstants() { delete Liveness; } |
| 447 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 448 | /// getCFG - Returns the CFG associated with this analysis. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 449 | CFG& getCFG() { assert (cfg); return *cfg; } |
| 450 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 451 | /// Initialize - Initialize the checker's state based on the specified |
| 452 | /// CFG. This results in liveness information being computed for |
| 453 | /// each block-level statement in the CFG. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 454 | void Initialize(CFG& c) { |
| 455 | cfg = &c; |
| 456 | Liveness = new LiveVariables(c); |
| 457 | Liveness->runOnCFG(c); |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 458 | Liveness->runOnAllBlocks(c, NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 461 | /// getInitialState - Return the initial state used for the root vertex |
| 462 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 463 | StateTy getInitialState() { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 464 | return StateMgr.GetEmptyMap(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 465 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 467 | /// ProcessStmt - Called by GREngine. Used to generate new successor |
| 468 | /// nodes by processing the 'effects' of a block-level statement. |
| 469 | void ProcessStmt(Stmt* S, NodeBuilder& builder); |
| 470 | |
| 471 | /// RemoveDeadBindings - Return a new state that is the same as 'M' except |
| 472 | /// that all subexpression mappings are removed and that any |
| 473 | /// block-level expressions that are not live at 'S' also have their |
| 474 | /// mappings removed. |
| 475 | StateTy RemoveDeadBindings(Stmt* S, StateTy M); |
| 476 | |
| 477 | StateTy SetValue(StateTy St, Stmt* S, const ExprValue& V, |
| 478 | bool isBlkExpr = false); |
| 479 | |
| 480 | StateTy SetValue(StateTy St, const LValue& LV, const ExprValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 482 | ExprValue GetValue(const StateTy& St, Stmt* S); |
| 483 | ExprValue GetValue(const StateTy& St, const LValue& LV); |
| 484 | LValue GetLValue(const StateTy& St, Stmt* S); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 486 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 488 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 489 | /// other functions that handle specific kinds of statements. |
| 490 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
| 491 | |
| 492 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
| 493 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 494 | }; |
| 495 | } // end anonymous namespace |
| 496 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 497 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 498 | void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) { |
| 499 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 500 | |
| 501 | StmtEntryNode = builder.getLastNode(); |
| 502 | CurrentStmt = S; |
| 503 | NodeSet Dst; |
| 504 | StateCleaned = false; |
| 505 | |
| 506 | Visit(S, StmtEntryNode, Dst); |
| 507 | |
| 508 | // If no nodes were generated, generate a new node that has all the |
| 509 | // dead mappings removed. |
| 510 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 511 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 512 | builder.generateNode(S, St, StmtEntryNode); |
| 513 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 515 | CurrentStmt = NULL; |
| 516 | StmtEntryNode = NULL; |
| 517 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 520 | |
| 521 | ExprValue GRConstants::GetValue(const StateTy& St, const LValue& LV) { |
| 522 | switch (LV.getKind()) { |
| 523 | case ExprValue::LValueDeclKind: { |
| 524 | StateTy::TreeTy* T = St.SlimFind(cast<LValueDecl>(LV).getDecl()); |
| 525 | return T ? T->getValue().second : InvalidValue(); |
| 526 | } |
| 527 | default: |
| 528 | assert (false && "Invalid LValue."); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 529 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | return InvalidValue(); |
| 533 | } |
| 534 | |
| 535 | ExprValue GRConstants::GetValue(const StateTy& St, Stmt* S) { |
| 536 | if (Expr* E = dyn_cast<Expr>(S)) |
| 537 | S = E->IgnoreParens(); |
| 538 | |
| 539 | switch (S->getStmtClass()) { |
| 540 | case Stmt::DeclRefExprClass: |
| 541 | return GetValue(St, LValueDecl(cast<DeclRefExpr>(S)->getDecl())); |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 542 | |
| 543 | case Stmt::IntegerLiteralClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 544 | return RValue::GetRValue(ValMgr, cast<IntegerLiteral>(S)); |
| 545 | |
| 546 | default: |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 547 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 548 | }; |
| 549 | |
| 550 | StateTy::TreeTy* T = St.SlimFind(ValueKey(S, getCFG().isBlkExpr(S))); |
| 551 | return T ? T->getValue().second : InvalidValue(); |
| 552 | } |
| 553 | |
| 554 | LValue GRConstants::GetLValue(const StateTy& St, Stmt* S) { |
| 555 | if (Expr* E = dyn_cast<Expr>(S)) |
| 556 | S = E->IgnoreParens(); |
| 557 | |
| 558 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) |
| 559 | return LValueDecl(DR->getDecl()); |
| 560 | |
| 561 | return cast<LValue>(GetValue(St, S)); |
| 562 | } |
| 563 | |
| 564 | GRConstants::StateTy GRConstants::SetValue(StateTy St, Stmt* S, |
| 565 | const ExprValue& V, bool isBlkExpr) { |
| 566 | |
| 567 | if (!StateCleaned) { |
| 568 | St = RemoveDeadBindings(CurrentStmt, St); |
| 569 | StateCleaned = true; |
| 570 | } |
| 571 | |
| 572 | return V.isValid() ? StateMgr.Add(St, ValueKey(S,isBlkExpr), V) |
| 573 | : St; |
| 574 | } |
| 575 | |
| 576 | GRConstants::StateTy GRConstants::SetValue(StateTy St, const LValue& LV, |
| 577 | const ExprValue& V) { |
| 578 | if (!LV.isValid()) |
| 579 | return St; |
| 580 | |
| 581 | if (!StateCleaned) { |
| 582 | St = RemoveDeadBindings(CurrentStmt, St); |
| 583 | StateCleaned = true; |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 584 | } |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 585 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 586 | switch (LV.getKind()) { |
| 587 | case ExprValue::LValueDeclKind: |
| 588 | return V.isValid() ? StateMgr.Add(St, cast<LValueDecl>(LV).getDecl(), V) |
| 589 | : StateMgr.Remove(St, cast<LValueDecl>(LV).getDecl()); |
| 590 | |
| 591 | default: |
| 592 | assert ("SetValue for given LValue type not yet implemented."); |
| 593 | return St; |
| 594 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 597 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 598 | // Note: in the code below, we can assign a new map to M since the |
| 599 | // iterators are iterating over the tree of the *original* map. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 600 | StateTy::iterator I = M.begin(), E = M.end(); |
| 601 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 602 | // Remove old bindings for subexpressions. |
| 603 | for (; I!=E && I.getKey().getKind() == ValueKey::IsSubExp; ++I) |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 604 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 605 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 606 | // Remove bindings for "dead" decls. |
| 607 | for (; I!=E && I.getKey().getKind() == ValueKey::IsDecl; ++I) |
| 608 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 609 | if (!Liveness->isLive(Loc, V)) |
| 610 | M = StateMgr.Remove(M, I.getKey()); |
| 611 | |
| 612 | // Remove bindings for "dead" block-level expressions. |
| 613 | for (; I!=E; ++I) |
| 614 | if (!Liveness->isLive(Loc, cast<Stmt>(I.getKey()))) |
| 615 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 616 | |
| 617 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 620 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred, |
| 621 | GRConstants::StateTy St) { |
| 622 | |
| 623 | // If the state hasn't changed, don't generate a new node. |
| 624 | if (St == Pred->getState()) |
| 625 | return; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 626 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 627 | Dst.Add(Builder->generateNode(S, St, Pred)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 628 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 629 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 630 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 631 | GRConstants::NodeTy* Pred, |
| 632 | GRConstants::NodeSet& Dst) { |
| 633 | NodeSet S1; |
| 634 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 635 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 636 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 637 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 638 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 639 | // When getting the value for the LHS, check if we are in an assignment. |
| 640 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 641 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
| 642 | // evaluated to LValueDecl's instead of to an RValue. |
| 643 | const ExprValue& V1 = |
| 644 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 645 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 646 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 647 | NodeSet S2; |
| 648 | Visit(B->getRHS(), N1, S2); |
| 649 | |
| 650 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
| 651 | NodeTy* N2 = *I2; |
| 652 | StateTy St = N2->getState(); |
| 653 | const ExprValue& V2 = GetValue(St, B->getRHS()); |
| 654 | |
| 655 | switch (B->getOpcode()) { |
| 656 | case BinaryOperator::Add: { |
| 657 | const RValue& R1 = cast<RValue>(V1); |
| 658 | const RValue& R2 = cast<RValue>(V2); |
| 659 | |
| 660 | Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2))); |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | case BinaryOperator::Sub: { |
| 665 | const RValue& R1 = cast<RValue>(V1); |
| 666 | const RValue& R2 = cast<RValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 667 | Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2))); |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | case BinaryOperator::Assign: { |
| 672 | const LValue& L1 = cast<LValue>(V1); |
| 673 | const RValue& R2 = cast<RValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 674 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2)); |
| 675 | break; |
| 676 | } |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame^] | 677 | |
| 678 | case BinaryOperator::AddAssign: { |
| 679 | const LValue& L1 = cast<LValue>(V1); |
| 680 | RValue R1 = cast<RValue>(GetValue(N1->getState(), L1)); |
| 681 | RValue Result = R1.Add(ValMgr, cast<RValue>(V2)); |
| 682 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | case BinaryOperator::SubAssign: { |
| 687 | const LValue& L1 = cast<LValue>(V1); |
| 688 | RValue R1 = cast<RValue>(GetValue(N1->getState(), L1)); |
| 689 | RValue Result = R1.Sub(ValMgr, cast<RValue>(V2)); |
| 690 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 691 | break; |
| 692 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 693 | |
| 694 | default: |
| 695 | Dst.Add(N2); |
| 696 | break; |
| 697 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 698 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 699 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 700 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 702 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 703 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 704 | GRConstants::NodeSet& Dst) { |
| 705 | |
| 706 | // FIXME: add metadata to the CFG so that we can disable |
| 707 | // this check when we KNOW that there is no block-level subexpression. |
| 708 | // The motivation is that this check requires a hashtable lookup. |
| 709 | |
| 710 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 711 | Dst.Add(Pred); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | switch (S->getStmtClass()) { |
| 716 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame^] | 717 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 718 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 719 | break; |
| 720 | |
| 721 | case Stmt::ParenExprClass: |
| 722 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 723 | break; |
| 724 | |
| 725 | default: |
| 726 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 727 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 728 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 731 | //===----------------------------------------------------------------------===// |
| 732 | // Driver. |
| 733 | //===----------------------------------------------------------------------===// |
| 734 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 735 | #ifndef NDEBUG |
| 736 | namespace llvm { |
| 737 | template<> |
| 738 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 739 | public DefaultDOTGraphTraits { |
| 740 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 741 | static void PrintKind(std::ostringstream& Out, ValueKey::Kind kind) { |
| 742 | switch (kind) { |
| 743 | case ValueKey::IsSubExp: Out << "Sub-Expressions:\\l"; break; |
| 744 | case ValueKey::IsDecl: Out << "Variables:\\l"; break; |
| 745 | case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 746 | default: assert (false && "Unknown ValueKey type."); |
| 747 | } |
| 748 | } |
| 749 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 750 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 751 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 752 | |
| 753 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 754 | ProgramPoint Loc = N->getLocation(); |
| 755 | |
| 756 | switch (Loc.getKind()) { |
| 757 | case ProgramPoint::BlockEntranceKind: |
| 758 | Out << "Block Entrance: B" |
| 759 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 760 | break; |
| 761 | |
| 762 | case ProgramPoint::BlockExitKind: |
| 763 | assert (false); |
| 764 | break; |
| 765 | |
| 766 | case ProgramPoint::PostStmtKind: { |
| 767 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 768 | Out << "(" << (void*) L.getStmt() << ") "; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 769 | L.getStmt()->printPretty(Out); |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | default: { |
| 774 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 775 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 776 | << E.getDst()->getBlockID() << ')'; |
| 777 | } |
| 778 | } |
| 779 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 780 | Out << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 781 | |
| 782 | GRConstants::StateTy M = N->getState(); |
| 783 | bool isFirst = true; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 784 | ValueKey::Kind kind; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 785 | |
| 786 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end(); I!=E; ++I) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 787 | if (!isFirst) { |
| 788 | ValueKey::Kind newKind = I.getKey().getKind(); |
| 789 | |
| 790 | if (newKind != kind) { |
| 791 | Out << "\\l\\l"; |
| 792 | PrintKind(Out, newKind); |
| 793 | } |
| 794 | else |
| 795 | Out << "\\l"; |
| 796 | |
| 797 | kind = newKind; |
| 798 | } |
| 799 | else { |
| 800 | kind = I.getKey().getKind(); |
| 801 | PrintKind(Out, kind); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 802 | isFirst = false; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | Out << ' '; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 806 | |
| 807 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 808 | Out << V->getName(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 809 | } |
| 810 | else { |
| 811 | Stmt* E = cast<Stmt>(I.getKey()); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 812 | Out << " (" << (void*) E << ") "; |
| 813 | E->printPretty(Out); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 816 | Out << " : "; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 817 | I.getData().print(Out); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 820 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 821 | return Out.str(); |
| 822 | } |
| 823 | }; |
| 824 | } // end llvm namespace |
| 825 | #endif |
| 826 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 827 | namespace clang { |
| 828 | void RunGRConstants(CFG& cfg) { |
| 829 | GREngine<GRConstants> Engine(cfg); |
| 830 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 831 | #ifndef NDEBUG |
| 832 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
| 833 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 834 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 835 | } // end clang namespace |