Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
Zhongxing Xu | c506357 | 2010-03-16 13:14:16 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | #include "clang/Analysis/AnalysisContext.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Checker/PathSensitive/GRState.h" |
| 24 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
| 25 | #include "clang/Checker/PathSensitive/MemRegion.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ImmutableList.h" |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ImmutableMap.h" |
| 28 | #include "llvm/ADT/Optional.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
Ted Kremenek | 66d5142 | 2010-04-09 20:26:58 +0000 | [diff] [blame] | 32 | using llvm::Optional; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 35 | // Representation of binding keys. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | namespace { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 39 | class BindingKey { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 40 | public: |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 41 | enum Kind { Direct = 0x0, Default = 0x1 }; |
| 42 | private: |
| 43 | llvm ::PointerIntPair<const MemRegion*, 1> P; |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 44 | uint64_t Offset; |
| 45 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 46 | explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 47 | : P(r, (unsigned) k), Offset(offset) { assert(r); } |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 48 | public: |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 50 | bool isDefault() const { return P.getInt() == Default; } |
| 51 | bool isDirect() const { return P.getInt() == Direct; } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 53 | const MemRegion *getRegion() const { return P.getPointer(); } |
| 54 | uint64_t getOffset() const { return Offset; } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 56 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 57 | ID.AddPointer(P.getOpaqueValue()); |
| 58 | ID.AddInteger(Offset); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 61 | static BindingKey Make(const MemRegion *R, Kind k); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 63 | bool operator<(const BindingKey &X) const { |
| 64 | if (P.getOpaqueValue() < X.P.getOpaqueValue()) |
| 65 | return true; |
| 66 | if (P.getOpaqueValue() > X.P.getOpaqueValue()) |
| 67 | return false; |
| 68 | return Offset < X.Offset; |
| 69 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 71 | bool operator==(const BindingKey &X) const { |
| 72 | return P.getOpaqueValue() == X.P.getOpaqueValue() && |
| 73 | Offset == X.Offset; |
| 74 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 75 | }; |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 76 | } // end anonymous namespace |
| 77 | |
| 78 | namespace llvm { |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 79 | static inline |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 80 | llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) { |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 81 | os << '(' << K.getRegion() << ',' << K.getOffset() |
| 82 | << ',' << (K.isDirect() ? "direct" : "default") |
| 83 | << ')'; |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 84 | return os; |
| 85 | } |
| 86 | } // end llvm namespace |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 89 | // Actual Store type. |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 92 | typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 95 | // Fine-grained control of RegionStoreManager. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 99 | struct minimal_features_tag {}; |
| 100 | struct maximal_features_tag {}; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 102 | class RegionStoreFeatures { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 103 | bool SupportsFields; |
| 104 | bool SupportsRemaining; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 106 | public: |
| 107 | RegionStoreFeatures(minimal_features_tag) : |
| 108 | SupportsFields(false), SupportsRemaining(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 110 | RegionStoreFeatures(maximal_features_tag) : |
| 111 | SupportsFields(true), SupportsRemaining(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 113 | void enableFields(bool t) { SupportsFields = t; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 115 | bool supportsFields() const { return SupportsFields; } |
| 116 | bool supportsRemaining() const { return SupportsRemaining; } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 121 | // Utility functions. |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
| 124 | static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) { |
| 125 | if (ty->isAnyPointerType()) |
| 126 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 128 | return ty->isIntegerType() && ty->isScalarType() && |
| 129 | Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy); |
| 130 | } |
| 131 | |
| 132 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 133 | // Main RegionStore logic. |
| 134 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 135 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 136 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 138 | class RegionStoreSubRegionMap : public SubRegionMap { |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 139 | public: |
| 140 | typedef llvm::ImmutableSet<const MemRegion*> Set; |
| 141 | typedef llvm::DenseMap<const MemRegion*, Set> Map; |
| 142 | private: |
| 143 | Set::Factory F; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 144 | Map M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 145 | public: |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 146 | bool add(const MemRegion* Parent, const MemRegion* SubRegion) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 147 | Map::iterator I = M.find(Parent); |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 148 | |
| 149 | if (I == M.end()) { |
Ted Kremenek | 4ed4598 | 2009-08-05 05:31:02 +0000 | [diff] [blame] | 150 | M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion))); |
Ted Kremenek | d8c0192 | 2009-08-05 19:09:24 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
| 154 | I->second = F.Add(I->second, SubRegion); |
| 155 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 158 | void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 160 | ~RegionStoreSubRegionMap() {} |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 162 | const Set *getSubRegions(const MemRegion *Parent) const { |
| 163 | Map::const_iterator I = M.find(Parent); |
| 164 | return I == M.end() ? NULL : &I->second; |
| 165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 167 | bool iterSubRegions(const MemRegion* Parent, Visitor& V) const { |
Jeffrey Yasskin | 3958b50 | 2009-11-10 01:17:45 +0000 | [diff] [blame] | 168 | Map::const_iterator I = M.find(Parent); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 169 | |
| 170 | if (I == M.end()) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 171 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 173 | Set S = I->second; |
| 174 | for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 175 | if (!V.Visit(Parent, *SI)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 176 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 179 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | }; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 183 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 184 | class RegionStoreManager : public StoreManager { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 185 | const RegionStoreFeatures Features; |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 186 | RegionBindings::Factory RBFactory; |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 187 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 188 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f) |
Ted Kremenek | f7a0cf4 | 2009-07-29 21:43:22 +0000 | [diff] [blame] | 190 | : StoreManager(mgr), |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 191 | Features(f), |
Ted Kremenek | 8928d41 | 2010-02-04 04:14:49 +0000 | [diff] [blame] | 192 | RBFactory(mgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 193 | |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 194 | SubRegionMap *getSubRegionMap(Store store) { |
| 195 | return getRegionStoreSubRegionMap(store); |
| 196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 198 | RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 200 | Optional<SVal> getBinding(RegionBindings B, const MemRegion *R); |
| 201 | Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R); |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 202 | /// getDefaultBinding - Returns an SVal* representing an optional default |
| 203 | /// binding associated with a region and its subregions. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 204 | Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 206 | /// setImplicitDefaultValue - Set the default binding for the provided |
| 207 | /// MemRegion to the value implicitly defined for compound literals when |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 208 | /// the value is not specified. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 209 | Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 211 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 212 | /// type. 'Array' represents the lvalue of the array being decayed |
| 213 | /// to a pointer, and the returned SVal represents the decayed |
| 214 | /// version of that lvalue (i.e., a pointer to the first element of |
| 215 | /// the array). This is called by GRExprEngine when evaluating |
| 216 | /// casts from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 217 | SVal ArrayToPointer(Loc Array); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 218 | |
Zhongxing Xu | 461147f | 2010-02-05 05:24:20 +0000 | [diff] [blame] | 219 | SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 220 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | Store getInitialStore(const LocationContext *InitLoc) { |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 222 | return RBFactory.GetEmptyMap().getRoot(); |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 223 | } |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 225 | //===-------------------------------------------------------------------===// |
| 226 | // Binding values to regions. |
| 227 | //===-------------------------------------------------------------------===// |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 228 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 229 | Store InvalidateRegions(Store store, |
| 230 | const MemRegion * const *Begin, |
| 231 | const MemRegion * const *End, |
| 232 | const Expr *E, unsigned Count, |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 233 | InvalidatedSymbols *IS, |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 234 | bool invalidateGlobals, |
| 235 | InvalidatedRegions *Regions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 237 | public: // Made public for helper classes. |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 238 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 239 | void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R, |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 240 | RegionStoreSubRegionMap &M); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 242 | RegionBindings Add(RegionBindings B, BindingKey K, SVal V); |
| 243 | |
| 244 | RegionBindings Add(RegionBindings B, const MemRegion *R, |
| 245 | BindingKey::Kind k, SVal V); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 247 | const SVal *Lookup(RegionBindings B, BindingKey K); |
| 248 | const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 249 | |
| 250 | RegionBindings Remove(RegionBindings B, BindingKey K); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 251 | RegionBindings Remove(RegionBindings B, const MemRegion *R, |
| 252 | BindingKey::Kind k); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 254 | RegionBindings Remove(RegionBindings B, const MemRegion *R) { |
| 255 | return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 256 | } |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 258 | Store Remove(Store store, BindingKey K); |
| 259 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 260 | public: // Part of public interface to class. |
| 261 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 262 | Store Bind(Store store, Loc LV, SVal V); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 263 | |
Zhongxing Xu | 5446009 | 2010-06-01 04:49:26 +0000 | [diff] [blame] | 264 | // BindDefault is only used to initialize a region with a default value. |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 265 | Store BindDefault(Store store, const MemRegion *R, SVal V) { |
Zhongxing Xu | 5446009 | 2010-06-01 04:49:26 +0000 | [diff] [blame] | 266 | RegionBindings B = GetRegionBindings(store); |
| 267 | assert(!Lookup(B, R, BindingKey::Default)); |
| 268 | assert(!Lookup(B, R, BindingKey::Direct)); |
| 269 | return Add(B, R, BindingKey::Default, V).getRoot(); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 272 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, |
| 273 | const LocationContext *LC, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 275 | Store BindDecl(Store store, const VarRegion *VR, SVal InitVal); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 276 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 277 | Store BindDeclWithNoInit(Store store, const VarRegion *) { |
| 278 | return store; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 279 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 281 | /// BindStruct - Bind a compound value to a structure. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 282 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 284 | Store BindArray(Store store, const TypedRegion* R, SVal V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
| 286 | /// KillStruct - Set the entire struct to unknown. |
Ted Kremenek | 281e9dc | 2010-07-29 00:28:47 +0000 | [diff] [blame] | 287 | Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 289 | Store Remove(Store store, Loc LV); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 291 | |
| 292 | //===------------------------------------------------------------------===// |
| 293 | // Loading values from regions. |
| 294 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 296 | /// The high level logic for this method is this: |
| 297 | /// Retrieve (L) |
| 298 | /// if L has binding |
| 299 | /// return L's binding |
| 300 | /// else if L is in killset |
| 301 | /// return unknown |
| 302 | /// else |
| 303 | /// if L is on stack or heap |
| 304 | /// return undefined |
| 305 | /// else |
| 306 | /// return symbolic |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 307 | SVal Retrieve(Store store, Loc L, QualType T = QualType()); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 308 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 309 | SVal RetrieveElement(Store store, const ElementRegion *R); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 310 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 311 | SVal RetrieveField(Store store, const FieldRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 313 | SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 315 | SVal RetrieveVar(Store store, const VarRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 317 | SVal RetrieveLazySymbol(const TypedRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 319 | SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 320 | QualType Ty, const MemRegion *superR); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 322 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | /// struct copy: |
| 324 | /// struct s x, y; |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 325 | /// x = y; |
| 326 | /// y's value is retrieved by this method. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 327 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 329 | SVal RetrieveArray(Store store, const TypedRegion* R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 331 | /// Used to lazily generate derived symbols for bindings that are defined |
| 332 | /// implicitly by default bindings in a super region. |
| 333 | Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B, |
| 334 | const MemRegion *superR, |
| 335 | const TypedRegion *R, QualType Ty); |
| 336 | |
Zhongxing Xu | 944ebc6 | 2009-12-21 06:52:24 +0000 | [diff] [blame] | 337 | /// Get the state and region whose binding this region R corresponds to. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 338 | std::pair<Store, const MemRegion*> |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 339 | GetLazyBinding(RegionBindings B, const MemRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 341 | Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store, |
| 342 | const TypedRegion *R); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 343 | |
| 344 | //===------------------------------------------------------------------===// |
| 345 | // State pruning. |
| 346 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 348 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 349 | /// It returns a new Store with these values removed. |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 350 | const GRState *RemoveDeadBindings(GRState &state, |
Zhongxing Xu | 9579898 | 2010-05-26 03:27:35 +0000 | [diff] [blame] | 351 | const StackFrameContext *LCtx, |
| 352 | SymbolReaper& SymReaper, |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 353 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
| 354 | |
Jordy Rose | ff59efd | 2010-08-03 20:44:35 +0000 | [diff] [blame] | 355 | Store EnterStackFrame(const GRState *state, const StackFrameContext *frame); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 357 | //===------------------------------------------------------------------===// |
| 358 | // Region "extents". |
| 359 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 361 | // FIXME: This method will soon be eliminated; see the note in Store.h. |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 362 | DefinedOrUnknownSVal getSizeInElements(const GRState *state, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 363 | const MemRegion* R, QualType EleTy); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 364 | |
| 365 | //===------------------------------------------------------------------===// |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 366 | // Utility methods. |
| 367 | //===------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 369 | static inline RegionBindings GetRegionBindings(Store store) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 370 | return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 371 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 373 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 374 | const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 375 | |
| 376 | void iterBindings(Store store, BindingsHandler& f) { |
Ted Kremenek | 0e9910f | 2010-06-17 00:24:42 +0000 | [diff] [blame] | 377 | RegionBindings B = GetRegionBindings(store); |
| 378 | for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 379 | const BindingKey &K = I.getKey(); |
| 380 | if (!K.isDirect()) |
| 381 | continue; |
| 382 | if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) { |
| 383 | // FIXME: Possibly incorporate the offset? |
| 384 | if (!f.HandleBinding(*this, store, R, I.getData())) |
| 385 | return; |
| 386 | } |
| 387 | } |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 388 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | } // end anonymous namespace |
| 392 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// |
| 394 | // RegionStore creation. |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
| 397 | StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
| 398 | RegionStoreFeatures F = maximal_features_tag(); |
| 399 | return new RegionStoreManager(StMgr, F); |
| 400 | } |
| 401 | |
| 402 | StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) { |
| 403 | RegionStoreFeatures F = minimal_features_tag(); |
| 404 | F.enableFields(true); |
| 405 | return new RegionStoreManager(StMgr, F); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 408 | void |
| 409 | RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | const SubRegion *R) { |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 411 | const MemRegion *superR = R->getSuperRegion(); |
| 412 | if (add(superR, R)) |
| 413 | if (const SubRegion *sr = dyn_cast<SubRegion>(superR)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | WL.push_back(sr); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 417 | RegionStoreSubRegionMap* |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 418 | RegionStoreManager::getRegionStoreSubRegionMap(Store store) { |
| 419 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 420 | RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 422 | llvm::SmallVector<const SubRegion*, 10> WL; |
| 423 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 424 | for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 425 | if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 426 | M->process(WL, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | // We also need to record in the subregion map "intermediate" regions that |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 429 | // don't have direct bindings but are super regions of those that do. |
| 430 | while (!WL.empty()) { |
| 431 | const SubRegion *R = WL.back(); |
| 432 | WL.pop_back(); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 433 | M->process(WL, R); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 436 | return M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 437 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 439 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 440 | // Region Cluster analysis. |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
| 443 | namespace { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 444 | template <typename DERIVED> |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 445 | class ClusterAnalysis { |
| 446 | protected: |
| 447 | typedef BumpVector<BindingKey> RegionCluster; |
| 448 | typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap; |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 449 | llvm::DenseMap<const RegionCluster*, unsigned> Visited; |
| 450 | typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10> |
| 451 | WorkList; |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 452 | |
| 453 | BumpVectorContext BVC; |
| 454 | ClusterMap ClusterM; |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 455 | WorkList WL; |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 456 | |
| 457 | RegionStoreManager &RM; |
| 458 | ASTContext &Ctx; |
| 459 | ValueManager &ValMgr; |
| 460 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 461 | RegionBindings B; |
| 462 | |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 463 | public: |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 464 | ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr, |
| 465 | RegionBindings b) |
| 466 | : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()), |
| 467 | B(b) {} |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 469 | RegionBindings getRegionBindings() const { return B; } |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 471 | RegionCluster &AddToCluster(BindingKey K) { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 472 | const MemRegion *R = K.getRegion(); |
| 473 | const MemRegion *baseR = R->getBaseRegion(); |
| 474 | RegionCluster &C = getCluster(baseR); |
| 475 | C.push_back(K, BVC); |
| 476 | static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 477 | return C; |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 478 | } |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 479 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 480 | bool isVisited(const MemRegion *R) { |
| 481 | return (bool) Visited[&getCluster(R->getBaseRegion())]; |
| 482 | } |
| 483 | |
| 484 | RegionCluster& getCluster(const MemRegion *R) { |
| 485 | RegionCluster *&CRef = ClusterM[R]; |
| 486 | if (!CRef) { |
| 487 | void *Mem = BVC.getAllocator().template Allocate<RegionCluster>(); |
| 488 | CRef = new (Mem) RegionCluster(BVC, 10); |
| 489 | } |
| 490 | return *CRef; |
| 491 | } |
| 492 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 493 | void GenerateClusters(bool includeGlobals = false) { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 494 | // Scan the entire set of bindings and make the region clusters. |
| 495 | for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){ |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 496 | RegionCluster &C = AddToCluster(RI.getKey()); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 497 | if (const MemRegion *R = RI.getData().getAsRegion()) { |
| 498 | // Generate a cluster, but don't add the region to the cluster |
| 499 | // if there aren't any bindings. |
| 500 | getCluster(R->getBaseRegion()); |
| 501 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 502 | if (includeGlobals) { |
| 503 | const MemRegion *R = RI.getKey().getRegion(); |
| 504 | if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace())) |
| 505 | AddToWorkList(R, C); |
| 506 | } |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 509 | |
| 510 | bool AddToWorkList(const MemRegion *R, RegionCluster &C) { |
| 511 | if (unsigned &visited = Visited[&C]) |
| 512 | return false; |
| 513 | else |
| 514 | visited = 1; |
| 515 | |
| 516 | WL.push_back(std::make_pair(R, &C)); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | bool AddToWorkList(BindingKey K) { |
| 521 | return AddToWorkList(K.getRegion()); |
| 522 | } |
| 523 | |
| 524 | bool AddToWorkList(const MemRegion *R) { |
| 525 | const MemRegion *baseR = R->getBaseRegion(); |
| 526 | return AddToWorkList(baseR, getCluster(baseR)); |
| 527 | } |
| 528 | |
| 529 | void RunWorkList() { |
| 530 | while (!WL.empty()) { |
| 531 | const MemRegion *baseR; |
| 532 | RegionCluster *C; |
| 533 | llvm::tie(baseR, C) = WL.back(); |
| 534 | WL.pop_back(); |
| 535 | |
| 536 | // First visit the cluster. |
| 537 | static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end()); |
| 538 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 539 | // Next, visit the base region. |
| 540 | static_cast<DERIVED*>(this)->VisitBaseRegion(baseR); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | public: |
| 545 | void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {} |
| 546 | void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {} |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 547 | void VisitBaseRegion(const MemRegion *baseR) {} |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 548 | }; |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 552 | // Binding invalidation. |
| 553 | //===----------------------------------------------------------------------===// |
| 554 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 555 | void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B, |
| 556 | const MemRegion *R, |
| 557 | RegionStoreSubRegionMap &M) { |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | df16501 | 2010-02-02 22:38:47 +0000 | [diff] [blame] | 559 | if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R)) |
| 560 | for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end(); |
| 561 | I != E; ++I) |
| 562 | RemoveSubRegionBindings(B, *I, M); |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 563 | |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 564 | B = Remove(B, R); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 567 | namespace { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 568 | class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker> |
| 569 | { |
| 570 | const Expr *Ex; |
| 571 | unsigned Count; |
Ted Kremenek | c1ddcab | 2010-02-13 00:54:03 +0000 | [diff] [blame] | 572 | StoreManager::InvalidatedSymbols *IS; |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 573 | StoreManager::InvalidatedRegions *Regions; |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 574 | public: |
Ted Kremenek | 24c37ad | 2010-02-13 01:52:33 +0000 | [diff] [blame] | 575 | InvalidateRegionsWorker(RegionStoreManager &rm, |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 576 | GRStateManager &stateMgr, |
| 577 | RegionBindings b, |
| 578 | const Expr *ex, unsigned count, |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 579 | StoreManager::InvalidatedSymbols *is, |
| 580 | StoreManager::InvalidatedRegions *r) |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 581 | : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b), |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 582 | Ex(ex), Count(count), IS(is), Regions(r) {} |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 583 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 584 | void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E); |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 585 | void VisitBaseRegion(const MemRegion *baseR); |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 586 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 587 | private: |
Ted Kremenek | c1ddcab | 2010-02-13 00:54:03 +0000 | [diff] [blame] | 588 | void VisitBinding(SVal V); |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 589 | }; |
Ted Kremenek | 5b29065 | 2010-02-03 04:16:00 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Ted Kremenek | c1ddcab | 2010-02-13 00:54:03 +0000 | [diff] [blame] | 592 | void InvalidateRegionsWorker::VisitBinding(SVal V) { |
Ted Kremenek | c1ddcab | 2010-02-13 00:54:03 +0000 | [diff] [blame] | 593 | // A symbol? Mark it touched by the invalidation. |
| 594 | if (IS) |
| 595 | if (SymbolRef Sym = V.getAsSymbol()) |
| 596 | IS->insert(Sym); |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 597 | |
Ted Kremenek | 24c37ad | 2010-02-13 01:52:33 +0000 | [diff] [blame] | 598 | if (const MemRegion *R = V.getAsRegion()) { |
| 599 | AddToWorkList(R); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | // Is it a LazyCompoundVal? All references get invalidated as well. |
| 604 | if (const nonloc::LazyCompoundVal *LCS = |
| 605 | dyn_cast<nonloc::LazyCompoundVal>(&V)) { |
| 606 | |
| 607 | const MemRegion *LazyR = LCS->getRegion(); |
| 608 | RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore()); |
| 609 | |
| 610 | for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){ |
Ted Kremenek | 0ea0e8b | 2010-07-06 23:53:29 +0000 | [diff] [blame] | 611 | const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion()); |
| 612 | if (baseR && baseR->isSubRegionOf(LazyR)) |
Ted Kremenek | 24c37ad | 2010-02-13 01:52:33 +0000 | [diff] [blame] | 613 | VisitBinding(RI.getData()); |
| 614 | } |
| 615 | |
| 616 | return; |
| 617 | } |
| 618 | } |
| 619 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 620 | void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR, |
| 621 | BindingKey *I, BindingKey *E) { |
| 622 | for ( ; I != E; ++I) { |
| 623 | // Get the old binding. Is it a region? If so, add it to the worklist. |
| 624 | const BindingKey &K = *I; |
| 625 | if (const SVal *V = RM.Lookup(B, K)) |
| 626 | VisitBinding(*V); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 627 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 628 | B = RM.Remove(B, K); |
| 629 | } |
| 630 | } |
Ted Kremenek | a4fab03 | 2010-03-10 07:19:59 +0000 | [diff] [blame] | 631 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 632 | void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 633 | if (IS) { |
| 634 | // Symbolic region? Mark that symbol touched by the invalidation. |
| 635 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) |
| 636 | IS->insert(SR->getSymbol()); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 639 | // BlockDataRegion? If so, invalidate captured variables that are passed |
| 640 | // by reference. |
| 641 | if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) { |
| 642 | for (BlockDataRegion::referenced_vars_iterator |
| 643 | BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ; |
| 644 | BI != BE; ++BI) { |
| 645 | const VarRegion *VR = *BI; |
| 646 | const VarDecl *VD = VR->getDecl(); |
| 647 | if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) |
| 648 | AddToWorkList(VR); |
| 649 | } |
| 650 | return; |
| 651 | } |
| 652 | |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 653 | // Otherwise, we have a normal data region. Record that we touched the region. |
| 654 | if (Regions) |
| 655 | Regions->push_back(baseR); |
| 656 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 657 | if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) { |
| 658 | // Invalidate the region by setting its default value to |
| 659 | // conjured symbol. The type of the symbol is irrelavant. |
| 660 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy, |
| 661 | Count); |
| 662 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | if (!baseR->isBoundable()) |
| 667 | return; |
| 668 | |
| 669 | const TypedRegion *TR = cast<TypedRegion>(baseR); |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 670 | QualType T = TR->getValueType(); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 671 | |
| 672 | // Invalidate the binding. |
| 673 | if (const RecordType *RT = T->getAsStructureType()) { |
| 674 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 675 | // No record definition. There is nothing we can do. |
| 676 | if (!RD) { |
| 677 | B = RM.Remove(B, baseR); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | // Invalidate the region by setting its default value to |
| 682 | // conjured symbol. The type of the symbol is irrelavant. |
| 683 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy, |
| 684 | Count); |
| 685 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) { |
| 690 | // Set the default value of the array to conjured symbol. |
| 691 | DefinedOrUnknownSVal V = |
| 692 | ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count); |
| 693 | B = RM.Add(B, baseR, BindingKey::Default, V); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count); |
| 698 | assert(SymbolManager::canSymbolicate(T) || V.isUnknown()); |
| 699 | B = RM.Add(B, baseR, BindingKey::Direct, V); |
Ted Kremenek | 1004a9f | 2009-07-29 18:16:25 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 702 | Store RegionStoreManager::InvalidateRegions(Store store, |
| 703 | const MemRegion * const *I, |
| 704 | const MemRegion * const *E, |
| 705 | const Expr *Ex, unsigned Count, |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 706 | InvalidatedSymbols *IS, |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 707 | bool invalidateGlobals, |
| 708 | InvalidatedRegions *Regions) { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 709 | InvalidateRegionsWorker W(*this, StateMgr, |
| 710 | RegionStoreManager::GetRegionBindings(store), |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 711 | Ex, Count, IS, Regions); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 712 | |
| 713 | // Scan the bindings and generate the clusters. |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 714 | W.GenerateClusters(invalidateGlobals); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 715 | |
| 716 | // Add I .. E to the worklist. |
| 717 | for ( ; I != E; ++I) |
| 718 | W.AddToWorkList(*I); |
| 719 | |
| 720 | W.RunWorkList(); |
| 721 | |
| 722 | // Return the new bindings. |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 723 | RegionBindings B = W.getRegionBindings(); |
| 724 | |
| 725 | if (invalidateGlobals) { |
| 726 | // Bind the non-static globals memory space to a new symbol that we will |
| 727 | // use to derive the bindings for all non-static globals. |
| 728 | const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(); |
| 729 | SVal V = |
| 730 | ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex, |
| 731 | /* symbol type, doesn't matter */ Ctx.IntTy, |
| 732 | Count); |
| 733 | B = Add(B, BindingKey::Make(GS, BindingKey::Default), V); |
Jordy Rose | c2b7dfa | 2010-08-14 20:44:32 +0000 | [diff] [blame] | 734 | |
| 735 | // Even if there are no bindings in the global scope, we still need to |
| 736 | // record that we touched it. |
| 737 | if (Regions) |
| 738 | Regions->push_back(GS); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | return B.getRoot(); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 742 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 743 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 744 | //===----------------------------------------------------------------------===// |
| 745 | // Extents for regions. |
| 746 | //===----------------------------------------------------------------------===// |
| 747 | |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 748 | DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 749 | const MemRegion *R, |
| 750 | QualType EleTy) { |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 751 | SVal Size = cast<SubRegion>(R)->getExtent(ValMgr); |
| 752 | SValuator &SVator = ValMgr.getSValuator(); |
| 753 | const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size); |
| 754 | if (!SizeInt) |
| 755 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 757 | CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue()); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 758 | CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 760 | // If a variable is reinterpreted as a type that doesn't fit into a larger |
| 761 | // type evenly, round it down. |
| 762 | // This is a signed value, since it's used in arithmetic with signed indices. |
| 763 | return ValMgr.makeIntVal(RegionSize / EleSize, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 766 | //===----------------------------------------------------------------------===// |
| 767 | // Location and region casting. |
| 768 | //===----------------------------------------------------------------------===// |
| 769 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 770 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 771 | /// type. 'Array' represents the lvalue of the array being decayed |
| 772 | /// to a pointer, and the returned SVal represents the decayed |
| 773 | /// version of that lvalue (i.e., a pointer to the first element of |
| 774 | /// the array). This is called by GRExprEngine when evaluating casts |
| 775 | /// from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 776 | SVal RegionStoreManager::ArrayToPointer(Loc Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 777 | if (!isa<loc::MemRegionVal>(Array)) |
| 778 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 780 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 781 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 783 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 784 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 786 | // Strip off typedefs from the ArrayRegion's ValueType. |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 787 | QualType T = ArrayR->getValueType().getDesugaredType(); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 788 | ArrayType *AT = cast<ArrayType>(T); |
| 789 | T = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Ted Kremenek | 75185b5 | 2009-07-16 00:00:11 +0000 | [diff] [blame] | 791 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 792 | return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 795 | //===----------------------------------------------------------------------===// |
| 796 | // Pointer arithmetic. |
| 797 | //===----------------------------------------------------------------------===// |
| 798 | |
Zhongxing Xu | 461147f | 2010-02-05 05:24:20 +0000 | [diff] [blame] | 799 | SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R, |
Ted Kremenek | 5c73462 | 2009-06-26 00:41:43 +0000 | [diff] [blame] | 800 | QualType resultTy) { |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 801 | // Assume the base location is MemRegionVal. |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 802 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 803 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 804 | |
Jordy Rose | eac4a00 | 2010-06-28 08:26:15 +0000 | [diff] [blame] | 805 | // Special case for zero RHS. |
| 806 | if (R.isZeroConstant()) { |
| 807 | switch (Op) { |
| 808 | default: |
| 809 | // Handle it normally. |
| 810 | break; |
| 811 | case BinaryOperator::Add: |
| 812 | case BinaryOperator::Sub: |
| 813 | // FIXME: does this need to be casted to match resultTy? |
| 814 | return L; |
| 815 | } |
| 816 | } |
| 817 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 818 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 819 | const ElementRegion *ER = 0; |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 820 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 821 | switch (MR->getKind()) { |
| 822 | case MemRegion::SymbolicRegionKind: { |
| 823 | const SymbolicRegion *SR = cast<SymbolicRegion>(MR); |
Ted Kremenek | df74e25 | 2009-08-02 05:15:23 +0000 | [diff] [blame] | 824 | SymbolRef Sym = SR->getSymbol(); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 825 | QualType T = Sym->getType(Ctx); |
Ted Kremenek | bcf62a9 | 2009-08-25 22:55:09 +0000 | [diff] [blame] | 826 | QualType EleTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Ted Kremenek | bcf62a9 | 2009-08-25 22:55:09 +0000 | [diff] [blame] | 828 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 829 | EleTy = PT->getPointeeType(); |
| 830 | else |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 831 | EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 833 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 834 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | break; |
Zhongxing Xu | 005f07b | 2009-06-19 04:51:14 +0000 | [diff] [blame] | 836 | } |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 837 | case MemRegion::AllocaRegionKind: { |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 838 | const AllocaRegion *AR = cast<AllocaRegion>(MR); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 839 | QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes. |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 840 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 841 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | break; |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 843 | } |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 844 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 845 | case MemRegion::ElementRegionKind: { |
| 846 | ER = cast<ElementRegion>(MR); |
| 847 | break; |
| 848 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 850 | // Not yet handled. |
| 851 | case MemRegion::VarRegionKind: |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 852 | case MemRegion::StringRegionKind: { |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 854 | } |
| 855 | // Fall-through. |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 856 | case MemRegion::CompoundLiteralRegionKind: |
| 857 | case MemRegion::FieldRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 858 | case MemRegion::ObjCIvarRegionKind: |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 859 | case MemRegion::CXXObjectRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 860 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 862 | case MemRegion::FunctionTextRegionKind: |
| 863 | case MemRegion::BlockTextRegionKind: |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 864 | case MemRegion::BlockDataRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 865 | // Technically this can happen if people do funny things with casts. |
| 866 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 868 | case MemRegion::CXXThisRegionKind: |
| 869 | assert(0 && |
| 870 | "Cannot perform pointer arithmetic on implicit argument 'this'"); |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 871 | case MemRegion::GenericMemSpaceRegionKind: |
| 872 | case MemRegion::StackLocalsSpaceRegionKind: |
| 873 | case MemRegion::StackArgumentsSpaceRegionKind: |
| 874 | case MemRegion::HeapSpaceRegionKind: |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 875 | case MemRegion::NonStaticGlobalSpaceRegionKind: |
| 876 | case MemRegion::StaticGlobalSpaceRegionKind: |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 877 | case MemRegion::UnknownSpaceRegionKind: |
Ted Kremenek | 3bccf08 | 2009-07-11 00:58:27 +0000 | [diff] [blame] | 878 | assert(0 && "Cannot perform pointer arithmetic on a MemSpace"); |
| 879 | return UnknownVal(); |
Zhongxing Xu | 5414a5c | 2009-06-21 13:24:24 +0000 | [diff] [blame] | 880 | } |
Zhongxing Xu | 2b1dc17 | 2009-03-11 07:43:49 +0000 | [diff] [blame] | 881 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 882 | SVal Idx = ER->getIndex(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 883 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 884 | |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 885 | // For now, only support: |
| 886 | // (a) concrete integer indices that can easily be resolved |
| 887 | // (b) 0 + symbolic index |
| 888 | if (Base) { |
| 889 | if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) { |
| 890 | // FIXME: Should use SValuator here. |
| 891 | SVal NewIdx = |
| 892 | Base->evalBinOp(ValMgr, Op, |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 893 | cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset))); |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 894 | const MemRegion* NewER = |
| 895 | MRMgr.getElementRegion(ER->getElementType(), NewIdx, |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 896 | ER->getSuperRegion(), Ctx); |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 897 | return ValMgr.makeLoc(NewER); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 898 | } |
Ted Kremenek | cd8f6ac | 2009-10-06 01:39:48 +0000 | [diff] [blame] | 899 | if (0 == Base->getValue()) { |
| 900 | const MemRegion* NewER = |
| 901 | MRMgr.getElementRegion(ER->getElementType(), R, |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 902 | ER->getSuperRegion(), Ctx); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 903 | return ValMgr.makeLoc(NewER); |
| 904 | } |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 905 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 907 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 910 | //===----------------------------------------------------------------------===// |
| 911 | // Loading values from regions. |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 914 | Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B, |
Zhongxing Xu | bdfa85f | 2010-05-29 06:23:24 +0000 | [diff] [blame] | 915 | const MemRegion *R) { |
Zhongxing Xu | 42c67bf | 2010-05-29 06:49:04 +0000 | [diff] [blame] | 916 | |
| 917 | if (const SVal *V = Lookup(B, R, BindingKey::Direct)) |
| 918 | return *V; |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 919 | |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 920 | return Optional<SVal>(); |
| 921 | } |
| 922 | |
| 923 | Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B, |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 924 | const MemRegion *R) { |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 925 | if (R->isBoundable()) |
| 926 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 927 | if (TR->getValueType()->isUnionType()) |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 928 | return UnknownVal(); |
| 929 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 930 | if (const SVal *V = Lookup(B, R, BindingKey::Default)) |
| 931 | return *V; |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 932 | |
| 933 | return Optional<SVal>(); |
| 934 | } |
| 935 | |
| 936 | Optional<SVal> RegionStoreManager::getBinding(RegionBindings B, |
| 937 | const MemRegion *R) { |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 938 | |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 939 | if (const Optional<SVal> &V = getDirectBinding(B, R)) |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 940 | return V; |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 942 | return getDefaultBinding(B, R); |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 945 | static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) { |
| 946 | RTy = Ctx.getCanonicalType(RTy); |
| 947 | UsedTy = Ctx.getCanonicalType(UsedTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 949 | if (RTy == UsedTy) |
| 950 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
| 952 | |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 953 | // Recursively check the types. We basically want to see if a pointer value |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t* |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 955 | // represents a reinterpretation. |
| 956 | if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | const PointerType *PRTy = RTy->getAs<PointerType>(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 958 | const PointerType *PUsedTy = UsedTy->getAs<PointerType>(); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 959 | |
| 960 | return PUsedTy && PRTy && |
| 961 | IsReinterpreted(PRTy->getPointeeType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | PUsedTy->getPointeeType(), Ctx); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | return true; |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 968 | SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 969 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 970 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 971 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 972 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 973 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 974 | if (isa<loc::ConcreteInt>(L)) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 975 | return UndefinedVal(); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 976 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 977 | const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 978 | |
Tom Care | 7b05030 | 2010-06-25 18:22:31 +0000 | [diff] [blame] | 979 | if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) { |
| 980 | if (T.isNull()) { |
| 981 | const SymbolicRegion *SR = cast<SymbolicRegion>(MR); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 982 | T = SR->getSymbol()->getType(Ctx); |
Tom Care | 7b05030 | 2010-06-25 18:22:31 +0000 | [diff] [blame] | 983 | } |
Zhongxing Xu | 8149185 | 2010-02-08 08:43:02 +0000 | [diff] [blame] | 984 | MR = GetElementZeroRegion(MR, T); |
Tom Care | 7b05030 | 2010-06-25 18:22:31 +0000 | [diff] [blame] | 985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Zhongxing Xu | 2db08ca | 2010-03-01 05:29:02 +0000 | [diff] [blame] | 987 | if (isa<CodeTextRegion>(MR)) { |
| 988 | assert(0 && "Why load from a code text region?"); |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 989 | return UnknownVal(); |
Zhongxing Xu | 2db08ca | 2010-03-01 05:29:02 +0000 | [diff] [blame] | 990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 992 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 993 | // instead of 'Loc', and have the other Loc cases handled at a higher level. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 994 | const TypedRegion *R = cast<TypedRegion>(MR); |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 995 | QualType RTy = R->getValueType(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 996 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 997 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 998 | // |
| 999 | // int x = ...; |
| 1000 | // int *p = &x; |
| 1001 | // char *q = (char*) p; |
| 1002 | // char c = *q; // returns the first byte of 'x'. |
| 1003 | // |
| 1004 | // Such funny addressing will occur due to layering of regions. |
| 1005 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1006 | #if 0 |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 1007 | ASTContext &Ctx = getContext(); |
| 1008 | if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) { |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1009 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
| 1010 | R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx); |
Ted Kremenek | a6275a5 | 2009-07-15 02:31:43 +0000 | [diff] [blame] | 1011 | RTy = T; |
Ted Kremenek | 41fb0df | 2009-07-15 04:23:32 +0000 | [diff] [blame] | 1012 | assert(Ctx.getCanonicalType(RTy) == |
| 1013 | Ctx.getCanonicalType(R->getValueType(Ctx))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | } |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1015 | #endif |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1016 | |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1017 | if (RTy->isStructureOrClassType()) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1018 | return RetrieveStruct(store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Ted Kremenek | d4e5a60 | 2009-08-06 21:43:54 +0000 | [diff] [blame] | 1020 | // FIXME: Handle unions. |
| 1021 | if (RTy->isUnionType()) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1022 | return UnknownVal(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (RTy->isArrayType()) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1025 | return RetrieveArray(store, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1026 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 1027 | // FIXME: handle Vector types. |
| 1028 | if (RTy->isVectorType()) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1029 | return UnknownVal(); |
Zhongxing Xu | 99c2030 | 2009-06-28 14:16:39 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1032 | return CastRetrievedVal(RetrieveField(store, FR), FR, T, false); |
Zhongxing Xu | 99c2030 | 2009-06-28 14:16:39 +0000 | [diff] [blame] | 1033 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1034 | if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { |
| 1035 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 1036 | // value to the element type. Eventually we want to compose these values |
| 1037 | // more intelligently. For example, an 'element' can encompass multiple |
| 1038 | // bound regions (e.g., several bound bytes), or could be a subset of |
| 1039 | // a larger value. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1040 | return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1043 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { |
| 1044 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 1045 | // value to the ivar type. What we should model is stores to ivars |
| 1046 | // that blow past the extent of the ivar. If the address of the ivar is |
| 1047 | // reinterpretted, it is possible we stored a different value that could |
| 1048 | // fit within the ivar. Either we need to cast these when storing them |
| 1049 | // or reinterpret them lazily (as we do here). |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1050 | return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1053 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 1054 | // FIXME: Here we actually perform an implicit conversion from the loaded |
| 1055 | // value to the variable type. What we should model is stores to variables |
| 1056 | // that blow past the extent of the variable. If the address of the |
| 1057 | // variable is reinterpretted, it is possible we stored a different value |
| 1058 | // that could fit within the variable. Either we need to cast these when |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1059 | // storing them or reinterpret them lazily (as we do here). |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1060 | return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1061 | } |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 1062 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1063 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1064 | const SVal *V = Lookup(B, R, BindingKey::Direct); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Check if the region has a binding. |
| 1067 | if (V) |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1068 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1069 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1070 | // The location does not have a bound value. This means that it has |
| 1071 | // the value it had upon its creation and/or entry to the analyzed |
| 1072 | // function/method. These are either symbolic values or 'undefined'. |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 1073 | if (R->hasStackNonParametersStorage()) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1074 | // All stack variables are considered to have undefined values |
| 1075 | // upon creation. All heap allocated blocks are considered to |
| 1076 | // have undefined values as well unless they are explicitly bound |
| 1077 | // to specific values. |
Zhongxing Xu | c999ed7 | 2010-02-04 02:39:47 +0000 | [diff] [blame] | 1078 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 1081 | // All other values are symbolic. |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 1082 | return ValMgr.getRegionValueSymbolVal(R); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1085 | std::pair<Store, const MemRegion *> |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1086 | RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1087 | if (Optional<SVal> OV = getDirectBinding(B, R)) |
| 1088 | if (const nonloc::LazyCompoundVal *V = |
| 1089 | dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer())) |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1090 | return std::make_pair(V->getStore(), V->getRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1092 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1093 | const std::pair<Store, const MemRegion *> &X = |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1094 | GetLazyBinding(B, ER->getSuperRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | 8ec4aac | 2010-02-09 19:11:53 +0000 | [diff] [blame] | 1096 | if (X.second) |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1097 | return std::make_pair(X.first, |
| 1098 | MRMgr.getElementRegionWithSuper(ER, X.second)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | } |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1100 | else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) { |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1101 | const std::pair<Store, const MemRegion *> &X = |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1102 | GetLazyBinding(B, FR->getSuperRegion()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Ted Kremenek | 8ec4aac | 2010-02-09 19:11:53 +0000 | [diff] [blame] | 1104 | if (X.second) |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1105 | return std::make_pair(X.first, |
| 1106 | MRMgr.getFieldRegionWithSuper(FR, X.second)); |
| 1107 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1108 | // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is |
Zhongxing Xu | dcbcbdc | 2010-02-10 02:02:10 +0000 | [diff] [blame] | 1109 | // possible for a valid lazy binding. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1110 | return std::make_pair((Store) 0, (const MemRegion *) 0); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1111 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1112 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1113 | SVal RegionStoreManager::RetrieveElement(Store store, |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1114 | const ElementRegion* R) { |
| 1115 | // Check if the region has a binding. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1116 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 1117 | if (const Optional<SVal> &V = getDirectBinding(B, R)) |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1118 | return *V; |
| 1119 | |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 1120 | const MemRegion* superR = R->getSuperRegion(); |
| 1121 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1122 | // Check if the region is an element region of a string literal. |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 1123 | if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) { |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1124 | // FIXME: Handle loads from strings where the literal is treated as |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1125 | // an integer, e.g., *((unsigned int*)"hello") |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1126 | QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType(); |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1127 | if (T != Ctx.getCanonicalType(R->getElementType())) |
| 1128 | return UnknownVal(); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1129 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1130 | const StringLiteral *Str = StrR->getStringLiteral(); |
| 1131 | SVal Idx = R->getIndex(); |
| 1132 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) { |
| 1133 | int64_t i = CI->getValue().getSExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | int64_t byteLength = Str->getByteLength(); |
Jordy Rose | 167cc37 | 2010-07-29 06:40:33 +0000 | [diff] [blame] | 1135 | // Technically, only i == byteLength is guaranteed to be null. |
| 1136 | // However, such overflows should be caught before reaching this point; |
| 1137 | // the only time such an access would be made is if a string literal was |
| 1138 | // used to initialize a larger array. |
| 1139 | char c = (i >= byteLength) ? '\0' : Str->getStrData()[i]; |
Ted Kremenek | 95efe0f | 2009-09-29 16:36:48 +0000 | [diff] [blame] | 1140 | return ValMgr.makeIntVal(c, T); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Ted Kremenek | a709b87 | 2010-05-31 01:22:04 +0000 | [diff] [blame] | 1144 | // Handle the case where we are indexing into a larger scalar object. |
| 1145 | // For example, this handles: |
| 1146 | // int x = ... |
| 1147 | // char *y = &x; |
| 1148 | // return *y; |
| 1149 | // FIXME: This is a hack, and doesn't do anything really intelligent yet. |
Zhongxing Xu | 7caf9b3 | 2010-08-02 04:56:14 +0000 | [diff] [blame] | 1150 | const RegionRawOffset &O = R->getAsArrayOffset(); |
Ted Kremenek | a709b87 | 2010-05-31 01:22:04 +0000 | [diff] [blame] | 1151 | if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1152 | QualType baseT = baseR->getValueType(); |
Ted Kremenek | a709b87 | 2010-05-31 01:22:04 +0000 | [diff] [blame] | 1153 | if (baseT->isScalarType()) { |
| 1154 | QualType elemT = R->getElementType(); |
| 1155 | if (elemT->isScalarType()) { |
| 1156 | if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) { |
| 1157 | if (const Optional<SVal> &V = getDirectBinding(B, superR)) { |
| 1158 | if (SymbolRef parentSym = V->getAsSymbol()) |
| 1159 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | a709b87 | 2010-05-31 01:22:04 +0000 | [diff] [blame] | 1161 | if (V->isUnknownOrUndef()) |
| 1162 | return *V; |
| 1163 | // Other cases: give up. We are indexing into a larger object |
| 1164 | // that has some value, but we don't know how to handle that yet. |
| 1165 | return UnknownVal(); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
Zhongxing Xu | 42c67bf | 2010-05-29 06:49:04 +0000 | [diff] [blame] | 1169 | } |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1170 | } |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1171 | return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1174 | SVal RegionStoreManager::RetrieveField(Store store, |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1175 | const FieldRegion* R) { |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1176 | |
| 1177 | // Check if the region has a binding. |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1178 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 1179 | if (const Optional<SVal> &V = getDirectBinding(B, R)) |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1180 | return *V; |
| 1181 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1182 | QualType Ty = R->getValueType(); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1183 | return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion()); |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1186 | Optional<SVal> |
| 1187 | RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B, |
| 1188 | const MemRegion *superR, |
| 1189 | const TypedRegion *R, |
| 1190 | QualType Ty) { |
| 1191 | |
| 1192 | if (const Optional<SVal> &D = getDefaultBinding(B, superR)) { |
| 1193 | if (SymbolRef parentSym = D->getAsSymbol()) |
| 1194 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
| 1195 | |
| 1196 | if (D->isZeroConstant()) |
| 1197 | return ValMgr.makeZeroVal(Ty); |
| 1198 | |
| 1199 | if (D->isUnknownOrUndef()) |
| 1200 | return *D; |
| 1201 | |
| 1202 | assert(0 && "Unknown default value"); |
| 1203 | } |
| 1204 | |
| 1205 | return Optional<SVal>(); |
| 1206 | } |
| 1207 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1208 | SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1209 | const TypedRegion *R, |
| 1210 | QualType Ty, |
| 1211 | const MemRegion *superR) { |
| 1212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | // At this point we have already checked in either RetrieveElement or |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1214 | // RetrieveField if 'R' has a direct binding. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1216 | RegionBindings B = GetRegionBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1218 | while (superR) { |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1219 | if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty)) |
| 1220 | return *D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1222 | // If our super region is a field or element itself, walk up the region |
| 1223 | // hierarchy to see if there is a default value installed in an ancestor. |
| 1224 | if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) { |
| 1225 | superR = cast<SubRegion>(superR)->getSuperRegion(); |
| 1226 | continue; |
| 1227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1229 | break; |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1230 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1232 | // Lazy binding? |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1233 | Store lazyBindingStore = NULL; |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1234 | const MemRegion *lazyBindingRegion = NULL; |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1235 | llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Ted Kremenek | 8ec4aac | 2010-02-09 19:11:53 +0000 | [diff] [blame] | 1237 | if (lazyBindingRegion) { |
| 1238 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion)) |
| 1239 | return RetrieveElement(lazyBindingStore, ER); |
Zhongxing Xu | bfcaf80 | 2010-02-05 02:26:30 +0000 | [diff] [blame] | 1240 | return RetrieveField(lazyBindingStore, |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1241 | cast<FieldRegion>(lazyBindingRegion)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 1244 | if (R->hasStackNonParametersStorage()) { |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1245 | if (isa<ElementRegion>(R)) { |
| 1246 | // Currently we don't reason specially about Clang-style vectors. Check |
| 1247 | // if superR is a vector and if so return Unknown. |
| 1248 | if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1249 | if (typedSuperR->getValueType()->isVectorType()) |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1250 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | } |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1254 | return UndefinedVal(); |
Ted Kremenek | 566a6fa | 2009-08-06 22:33:36 +0000 | [diff] [blame] | 1255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 1257 | // All other values are symbolic. |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 1258 | return ValMgr.getRegionValueSymbolVal(R); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1259 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1261 | SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){ |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1262 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1263 | // Check if the region has a binding. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1264 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1265 | |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 1266 | if (const Optional<SVal> &V = getDirectBinding(B, R)) |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1267 | return *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1269 | const MemRegion *superR = R->getSuperRegion(); |
| 1270 | |
Ted Kremenek | ab22ee9 | 2009-10-20 01:20:57 +0000 | [diff] [blame] | 1271 | // Check if the super region has a default binding. |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 1272 | if (const Optional<SVal> &V = getDefaultBinding(B, superR)) { |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1273 | if (SymbolRef parentSym = V->getAsSymbol()) |
| 1274 | return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1276 | // Other cases: give up. |
| 1277 | return UnknownVal(); |
| 1278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1280 | return RetrieveLazySymbol(R); |
Ted Kremenek | 25c5457 | 2009-07-20 22:58:02 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1283 | SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1285 | // Check if the region has a binding. |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1286 | RegionBindings B = GetRegionBindings(store); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Ted Kremenek | 2cf073b | 2010-03-30 20:30:52 +0000 | [diff] [blame] | 1288 | if (const Optional<SVal> &V = getDirectBinding(B, R)) |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1289 | return *V; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1291 | // Lazily derive a value for the VarRegion. |
| 1292 | const VarDecl *VD = R->getDecl(); |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1293 | QualType T = VD->getType(); |
| 1294 | const MemSpaceRegion *MS = R->getMemorySpace(); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1295 | |
| 1296 | if (isa<UnknownSpaceRegion>(MS) || |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1297 | isa<StackArgumentsSpaceRegion>(MS)) |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 1298 | return ValMgr.getRegionValueSymbolVal(R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1300 | if (isa<GlobalsSpaceRegion>(MS)) { |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1301 | if (isa<NonStaticGlobalSpaceRegion>(MS)) { |
Ted Kremenek | 4552ff0 | 2010-03-30 20:31:04 +0000 | [diff] [blame] | 1302 | // Is 'VD' declared constant? If so, retrieve the constant value. |
| 1303 | QualType CT = Ctx.getCanonicalType(T); |
| 1304 | if (CT.isConstQualified()) { |
| 1305 | const Expr *Init = VD->getInit(); |
| 1306 | // Do the null check first, as we want to call 'IgnoreParenCasts'. |
| 1307 | if (Init) |
| 1308 | if (const IntegerLiteral *IL = |
| 1309 | dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) { |
| 1310 | const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL); |
| 1311 | return ValMgr.getSValuator().EvalCast(V, Init->getType(), |
| 1312 | IL->getType()); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1316 | if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT)) |
| 1317 | return V.getValue(); |
| 1318 | |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 1319 | return ValMgr.getRegionValueSymbolVal(R); |
Ted Kremenek | 4552ff0 | 2010-03-30 20:31:04 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1322 | if (T->isIntegerType()) |
| 1323 | return ValMgr.makeIntVal(0, T); |
Ted Kremenek | 81861ab | 2010-02-06 04:04:46 +0000 | [diff] [blame] | 1324 | if (T->isPointerType()) |
| 1325 | return ValMgr.makeNull(); |
| 1326 | |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1327 | return UnknownVal(); |
Ted Kremenek | 4dc1566 | 2010-02-06 03:57:59 +0000 | [diff] [blame] | 1328 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | 9031dd7 | 2009-07-21 00:12:07 +0000 | [diff] [blame] | 1330 | return UndefinedVal(); |
| 1331 | } |
| 1332 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1333 | SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) { |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1334 | // All other values are symbolic. |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 1335 | return ValMgr.getRegionValueSymbolVal(R); |
Ted Kremenek | 5bd2fe3 | 2009-07-15 06:09:28 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1338 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1339 | QualType T = R->getValueType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1340 | assert(T->isStructureOrClassType()); |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1341 | return ValMgr.makeLazyCompoundVal(store, R); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1344 | SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1345 | assert(isa<ConstantArrayType>(R->getValueType())); |
Zhongxing Xu | 576bb92 | 2010-02-05 03:01:53 +0000 | [diff] [blame] | 1346 | return ValMgr.makeLazyCompoundVal(store, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1349 | //===----------------------------------------------------------------------===// |
| 1350 | // Binding values to regions. |
| 1351 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1352 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1353 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1354 | if (isa<loc::MemRegionVal>(L)) |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1355 | if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion()) |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1356 | return Remove(GetRegionBindings(store), R).getRoot(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1358 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1361 | Store RegionStoreManager::Bind(Store store, Loc L, SVal V) { |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 1362 | if (isa<loc::ConcreteInt>(L)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1363 | return store; |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 1364 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1365 | // If we get here, the location should be a region. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1366 | const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1368 | // Check if the region is a struct region. |
| 1369 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1370 | if (TR->getValueType()->isStructureOrClassType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1371 | return BindStruct(store, TR, V); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1373 | // Special case: the current region represents a cast and it and the super |
| 1374 | // region both have pointer types or intptr_t types. If so, perform the |
| 1375 | // bind to the super region. |
| 1376 | // This is needed to support OSAtomicCompareAndSwap and friends or other |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | // loads that treat integers as pointers and vis versa. |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1378 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 1379 | if (ER->getIndex().isZeroConstant()) { |
| 1380 | if (const TypedRegion *superR = |
| 1381 | dyn_cast<TypedRegion>(ER->getSuperRegion())) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1382 | QualType superTy = superR->getValueType(); |
| 1383 | QualType erTy = ER->getValueType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (IsAnyPointerOrIntptr(superTy, Ctx) && |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1386 | IsAnyPointerOrIntptr(erTy, Ctx)) { |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 1387 | V = ValMgr.getSValuator().EvalCast(V, superTy, erTy); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1388 | return Bind(store, loc::MemRegionVal(superR), V); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1389 | } |
Ted Kremenek | 69181a8 | 2009-09-21 22:58:52 +0000 | [diff] [blame] | 1390 | // For now, just invalidate the fields of the struct/union/class. |
| 1391 | // FIXME: Precisely handle the fields of the record. |
Jordy Rose | 58f8b20 | 2010-08-05 03:28:45 +0000 | [diff] [blame] | 1392 | if (superTy->isStructureOrClassType()) |
| 1393 | return KillStruct(store, superR, UnknownVal()); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | } |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 1397 | else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 1398 | // Binding directly to a symbolic region should be treated as binding |
| 1399 | // to element 0. |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 1400 | QualType T = SR->getSymbol()->getType(Ctx); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1401 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1402 | // FIXME: Is this the right way to handle symbols that are references? |
| 1403 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 1404 | T = PT->getPointeeType(); |
| 1405 | else |
| 1406 | T = T->getAs<ReferenceType>()->getPointeeType(); |
| 1407 | |
Ted Kremenek | 0954cde | 2009-09-24 04:11:44 +0000 | [diff] [blame] | 1408 | R = GetElementZeroRegion(SR, T); |
| 1409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 1411 | // Perform the binding. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1412 | RegionBindings B = GetRegionBindings(store); |
| 1413 | return Add(B, R, BindingKey::Direct, V).getRoot(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1416 | Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR, |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1417 | SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 1418 | |
Ted Kremenek | f6f56d4 | 2009-11-04 00:09:15 +0000 | [diff] [blame] | 1419 | QualType T = VR->getDecl()->getType(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1421 | if (T->isArrayType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1422 | return BindArray(store, VR, InitVal); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1423 | if (T->isStructureOrClassType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1424 | return BindStruct(store, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 1425 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1426 | return Bind(store, ValMgr.makeLoc(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1427 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1428 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1429 | // FIXME: this method should be merged into Bind(). |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1430 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 1431 | const CompoundLiteralExpr *CL, |
| 1432 | const LocationContext *LC, |
| 1433 | SVal V) { |
| 1434 | return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 1435 | V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 1438 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1439 | Store RegionStoreManager::setImplicitDefaultValue(Store store, |
| 1440 | const MemRegion *R, |
| 1441 | QualType T) { |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1442 | RegionBindings B = GetRegionBindings(store); |
| 1443 | SVal V; |
| 1444 | |
| 1445 | if (Loc::IsLocType(T)) |
| 1446 | V = ValMgr.makeNull(); |
| 1447 | else if (T->isIntegerType()) |
| 1448 | V = ValMgr.makeZeroVal(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1449 | else if (T->isStructureOrClassType() || T->isArrayType()) { |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1450 | // Set the default value to a zero constant when it is a structure |
| 1451 | // or array. The type doesn't really matter. |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 1452 | V = ValMgr.makeZeroVal(Ctx.IntTy); |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1453 | } |
| 1454 | else { |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1455 | return store; |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1456 | } |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1457 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1458 | return Add(B, R, BindingKey::Default, V).getRoot(); |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1459 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1460 | |
| 1461 | Store RegionStoreManager::BindArray(Store store, const TypedRegion* R, |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1462 | SVal Init) { |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1463 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1464 | const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType())); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1465 | QualType ElementTy = AT->getElementType(); |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1466 | Optional<uint64_t> Size; |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1467 | |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1468 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1469 | Size = CAT->getSize().getZExtValue(); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1470 | |
Jordy Rose | 167cc37 | 2010-07-29 06:40:33 +0000 | [diff] [blame] | 1471 | // Check if the init expr is a string literal. |
| 1472 | if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) { |
| 1473 | const StringRegion *S = cast<StringRegion>(MRV->getRegion()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1474 | |
Jordy Rose | 167cc37 | 2010-07-29 06:40:33 +0000 | [diff] [blame] | 1475 | // Treat the string as a lazy compound value. |
| 1476 | nonloc::LazyCompoundVal LCV = |
| 1477 | cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S)); |
| 1478 | return CopyLazyBindings(LCV, store, R); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1481 | // Handle lazy compound values. |
| 1482 | if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1483 | return CopyLazyBindings(*LCV, store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Remaining case: explicit compound values. |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1486 | |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1487 | if (Init.isUnknown()) |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1488 | return setImplicitDefaultValue(store, R, ElementTy); |
| 1489 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1490 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1491 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1492 | uint64_t i = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1494 | for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) { |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1495 | // The init list might be shorter than the array length. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1496 | if (VI == VE) |
| 1497 | break; |
| 1498 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 1499 | SVal Idx = ValMgr.makeArrayIndex(i); |
Zhongxing Xu | 57663fe | 2010-08-15 10:08:38 +0000 | [diff] [blame^] | 1500 | const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1502 | if (ElementTy->isStructureOrClassType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1503 | store = BindStruct(store, ER, *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1504 | else |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1505 | store = Bind(store, ValMgr.makeLoc(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Ted Kremenek | 027e266 | 2009-11-19 20:20:24 +0000 | [diff] [blame] | 1508 | // If the init list is shorter than the array length, set the |
| 1509 | // array default value. |
Ted Kremenek | fee9081 | 2010-01-26 23:51:00 +0000 | [diff] [blame] | 1510 | if (Size.hasValue() && i < Size.getValue()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1511 | store = setImplicitDefaultValue(store, R, ElementTy); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1512 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1513 | return store; |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1516 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, |
| 1517 | SVal V) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1519 | if (!Features.supportsFields()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1520 | return store; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 1522 | QualType T = R->getValueType(); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1523 | assert(T->isStructureOrClassType()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1524 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1525 | const RecordType* RT = T->getAs<RecordType>(); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1526 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | c45a825 | 2009-03-11 09:07:35 +0000 | [diff] [blame] | 1527 | |
| 1528 | if (!RD->isDefinition()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1529 | return store; |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1530 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1531 | // Handle lazy compound values. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1532 | if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V)) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1533 | return CopyLazyBindings(*LCV, store, R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 281e9dc | 2010-07-29 00:28:47 +0000 | [diff] [blame] | 1535 | // We may get non-CompoundVal accidentally due to imprecise cast logic or |
| 1536 | // that we are binding symbolic struct value. Kill the field values, and if |
| 1537 | // the value is symbolic go and bind it as a "default" binding. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1538 | if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) |
Ted Kremenek | 281e9dc | 2010-07-29 00:28:47 +0000 | [diff] [blame] | 1539 | return KillStruct(store, R, isa<nonloc::SymbolVal>(V) ? V : UnknownVal()); |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 1540 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1541 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1542 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1543 | |
| 1544 | RecordDecl::field_iterator FI, FE; |
| 1545 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1546 | for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1547 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1548 | if (VI == VE) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1549 | break; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1550 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1551 | QualType FTy = (*FI)->getType(); |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1552 | const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1553 | |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1554 | if (FTy->isArrayType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1555 | store = BindArray(store, FR, *VI); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1556 | else if (FTy->isStructureOrClassType()) |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1557 | store = BindStruct(store, FR, *VI); |
Ted Kremenek | cf54959 | 2009-09-22 21:19:14 +0000 | [diff] [blame] | 1558 | else |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1559 | store = Bind(store, ValMgr.makeLoc(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1562 | // There may be fewer values in the initialize list than the fields of struct. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1563 | if (FI != FE) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1564 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1565 | B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false)); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1566 | store = B.getRoot(); |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1567 | } |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1568 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1569 | return store; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Ted Kremenek | 281e9dc | 2010-07-29 00:28:47 +0000 | [diff] [blame] | 1572 | Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R, |
| 1573 | SVal DefaultVal) { |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1574 | RegionBindings B = GetRegionBindings(store); |
| 1575 | llvm::OwningPtr<RegionStoreSubRegionMap> |
| 1576 | SubRegions(getRegionStoreSubRegionMap(store)); |
| 1577 | RemoveSubRegionBindings(B, R, *SubRegions); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1578 | |
Zhongxing Xu | e4df9c4 | 2009-06-25 05:52:16 +0000 | [diff] [blame] | 1579 | // Set the default value of the struct region to "unknown". |
Ted Kremenek | 281e9dc | 2010-07-29 00:28:47 +0000 | [diff] [blame] | 1580 | return Add(B, R, BindingKey::Default, DefaultVal).getRoot(); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1583 | Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V, |
| 1584 | Store store, const TypedRegion *R) { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1585 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1586 | // Nuke the old bindings stemming from R. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1587 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1588 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | llvm::OwningPtr<RegionStoreSubRegionMap> |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1590 | SubRegions(getRegionStoreSubRegionMap(store)); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1591 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | // B and DVM are updated after the call to RemoveSubRegionBindings. |
Zhongxing Xu | 13d5017 | 2009-10-11 08:08:02 +0000 | [diff] [blame] | 1593 | RemoveSubRegionBindings(B, R, *SubRegions.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1595 | // Now copy the bindings. This amounts to just binding 'V' to 'R'. This |
| 1596 | // results in a zero-copy algorithm. |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1597 | return Add(B, R, BindingKey::Direct, V).getRoot(); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | //===----------------------------------------------------------------------===// |
| 1601 | // "Raw" retrievals and bindings. |
| 1602 | //===----------------------------------------------------------------------===// |
| 1603 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1604 | BindingKey BindingKey::Make(const MemRegion *R, Kind k) { |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1605 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Zhongxing Xu | 7caf9b3 | 2010-08-02 04:56:14 +0000 | [diff] [blame] | 1606 | const RegionRawOffset &O = ER->getAsArrayOffset(); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1607 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1608 | if (O.getRegion()) |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1609 | return BindingKey(O.getRegion(), O.getByteOffset(), k); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1610 | |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1611 | // FIXME: There are some ElementRegions for which we cannot compute |
| 1612 | // raw offsets yet, including regions with symbolic offsets. |
| 1613 | } |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1614 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1615 | return BindingKey(R, 0, k); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1618 | RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1619 | return RBFactory.Add(B, K, V); |
| 1620 | } |
| 1621 | |
| 1622 | RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R, |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1623 | BindingKey::Kind k, SVal V) { |
| 1624 | return Add(B, BindingKey::Make(R, k), V); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1627 | const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) { |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1628 | return B.lookup(K); |
| 1629 | } |
| 1630 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1631 | const SVal *RegionStoreManager::Lookup(RegionBindings B, |
| 1632 | const MemRegion *R, |
| 1633 | BindingKey::Kind k) { |
| 1634 | return Lookup(B, BindingKey::Make(R, k)); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) { |
| 1638 | return RBFactory.Remove(B, K); |
| 1639 | } |
| 1640 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1641 | RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R, |
| 1642 | BindingKey::Kind k){ |
| 1643 | return Remove(B, BindingKey::Make(R, k)); |
Ted Kremenek | 1c1ae6b | 2010-01-11 00:07:44 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | Store RegionStoreManager::Remove(Store store, BindingKey K) { |
| 1647 | RegionBindings B = GetRegionBindings(store); |
| 1648 | return Remove(B, K).getRoot(); |
Ted Kremenek | a5e81f1 | 2009-08-06 01:20:57 +0000 | [diff] [blame] | 1649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1651 | //===----------------------------------------------------------------------===// |
| 1652 | // State pruning. |
| 1653 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1654 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1655 | namespace { |
| 1656 | class RemoveDeadBindingsWorker : |
| 1657 | public ClusterAnalysis<RemoveDeadBindingsWorker> { |
| 1658 | llvm::SmallVector<const SymbolicRegion*, 12> Postponed; |
| 1659 | SymbolReaper &SymReaper; |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 1660 | const StackFrameContext *CurrentLCtx; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1661 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1662 | public: |
| 1663 | RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr, |
| 1664 | RegionBindings b, SymbolReaper &symReaper, |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 1665 | const StackFrameContext *LCtx) |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1666 | : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b), |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 1667 | SymReaper(symReaper), CurrentLCtx(LCtx) {} |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Called by ClusterAnalysis. |
| 1670 | void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C); |
| 1671 | void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1672 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1673 | void VisitBindingKey(BindingKey K); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1674 | bool UpdatePostponed(); |
| 1675 | void VisitBinding(SVal V); |
| 1676 | }; |
| 1677 | } |
| 1678 | |
| 1679 | void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR, |
| 1680 | RegionCluster &C) { |
| 1681 | |
| 1682 | if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) { |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 1683 | if (SymReaper.isLive(VR)) |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1684 | AddToWorkList(baseR, C); |
| 1685 | |
| 1686 | return; |
| 1687 | } |
| 1688 | |
| 1689 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) { |
| 1690 | if (SymReaper.isLive(SR->getSymbol())) |
| 1691 | AddToWorkList(SR, C); |
| 1692 | else |
| 1693 | Postponed.push_back(SR); |
| 1694 | |
| 1695 | return; |
| 1696 | } |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 1697 | |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1698 | if (isa<NonStaticGlobalSpaceRegion>(baseR)) { |
| 1699 | AddToWorkList(baseR, C); |
| 1700 | return; |
| 1701 | } |
| 1702 | |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 1703 | // CXXThisRegion in the current or parent location context is live. |
| 1704 | if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) { |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1705 | const StackArgumentsSpaceRegion *StackReg = |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 1706 | cast<StackArgumentsSpaceRegion>(TR->getSuperRegion()); |
| 1707 | const StackFrameContext *RegCtx = StackReg->getStackFrame(); |
| 1708 | if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)) |
| 1709 | AddToWorkList(TR, C); |
| 1710 | } |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR, |
| 1714 | BindingKey *I, BindingKey *E) { |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1715 | for ( ; I != E; ++I) |
| 1716 | VisitBindingKey(*I); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | void RemoveDeadBindingsWorker::VisitBinding(SVal V) { |
| 1720 | // Is it a LazyCompoundVal? All referenced regions are live as well. |
| 1721 | if (const nonloc::LazyCompoundVal *LCS = |
| 1722 | dyn_cast<nonloc::LazyCompoundVal>(&V)) { |
| 1723 | |
| 1724 | const MemRegion *LazyR = LCS->getRegion(); |
| 1725 | RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore()); |
| 1726 | for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){ |
Ted Kremenek | 0ea0e8b | 2010-07-06 23:53:29 +0000 | [diff] [blame] | 1727 | const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion()); |
| 1728 | if (baseR && baseR->isSubRegionOf(LazyR)) |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1729 | VisitBinding(RI.getData()); |
| 1730 | } |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | // If V is a region, then add it to the worklist. |
| 1735 | if (const MemRegion *R = V.getAsRegion()) |
| 1736 | AddToWorkList(R); |
| 1737 | |
| 1738 | // Update the set of live symbols. |
| 1739 | for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end(); |
| 1740 | SI!=SE;++SI) |
| 1741 | SymReaper.markLive(*SI); |
| 1742 | } |
| 1743 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1744 | void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) { |
| 1745 | const MemRegion *R = K.getRegion(); |
| 1746 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1747 | // Mark this region "live" by adding it to the worklist. This will cause |
| 1748 | // use to visit all regions in the cluster (if we haven't visited them |
| 1749 | // already). |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1750 | if (AddToWorkList(R)) { |
| 1751 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 1752 | // should continue to track that symbol. |
| 1753 | if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R)) |
| 1754 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1755 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1756 | // For BlockDataRegions, enqueue the VarRegions for variables marked |
| 1757 | // with __block (passed-by-reference). |
| 1758 | // via BlockDeclRefExprs. |
| 1759 | if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) { |
| 1760 | for (BlockDataRegion::referenced_vars_iterator |
| 1761 | RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end(); |
| 1762 | RI != RE; ++RI) { |
| 1763 | if ((*RI)->getDecl()->getAttr<BlocksAttr>()) |
| 1764 | AddToWorkList(*RI); |
| 1765 | } |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1767 | // No possible data bindings on a BlockDataRegion. |
| 1768 | return; |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1769 | } |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Ted Kremenek | 75a2d94 | 2010-04-01 00:15:55 +0000 | [diff] [blame] | 1772 | // Visit the data binding for K. |
| 1773 | if (const SVal *V = RM.Lookup(B, K)) |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1774 | VisitBinding(*V); |
| 1775 | } |
| 1776 | |
| 1777 | bool RemoveDeadBindingsWorker::UpdatePostponed() { |
| 1778 | // See if any postponed SymbolicRegions are actually live now, after |
| 1779 | // having done a scan. |
| 1780 | bool changed = false; |
| 1781 | |
| 1782 | for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator |
| 1783 | I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) { |
| 1784 | if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) { |
| 1785 | if (SymReaper.isLive(SR->getSymbol())) { |
| 1786 | changed |= AddToWorkList(SR); |
| 1787 | *I = NULL; |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | return changed; |
| 1793 | } |
| 1794 | |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 1795 | const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state, |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 1796 | const StackFrameContext *LCtx, |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 1797 | SymbolReaper& SymReaper, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1798 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | { |
Zhongxing Xu | 9579898 | 2010-05-26 03:27:35 +0000 | [diff] [blame] | 1800 | RegionBindings B = GetRegionBindings(state.getStore()); |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 1801 | RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx); |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1802 | W.GenerateClusters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1804 | // Enqueue the region roots onto the worklist. |
Ted Kremenek | 9e17cc6 | 2009-09-29 06:35:00 +0000 | [diff] [blame] | 1805 | for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(), |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1806 | E=RegionRoots.end(); I!=E; ++I) |
| 1807 | W.AddToWorkList(*I); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1808 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1809 | do W.RunWorkList(); while (W.UpdatePostponed()); |
Ted Kremenek | e5ea0ca | 2010-03-10 07:20:03 +0000 | [diff] [blame] | 1810 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1811 | // We have now scanned the store, marking reachable regions and symbols |
| 1812 | // as live. We now remove all the regions that are dead from the store |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | // as well as update DSymbols with the set symbols that are now dead. |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1814 | for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1815 | const BindingKey &K = I.getKey(); |
| 1816 | |
Ted Kremenek | b7118f7 | 2010-03-10 16:38:41 +0000 | [diff] [blame] | 1817 | // If the cluster has been visited, we know the region has been marked. |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1818 | if (W.isVisited(K.getRegion())) |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1819 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1821 | // Remove the dead entry. |
| 1822 | B = Remove(B, K); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1824 | // Mark all non-live symbols that this binding references as dead. |
| 1825 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion())) |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1826 | SymReaper.maybeDead(SymR->getSymbol()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Ted Kremenek | e393f4a | 2010-02-03 03:06:46 +0000 | [diff] [blame] | 1828 | SVal X = I.getData(); |
Ted Kremenek | 093569c | 2009-08-02 05:00:15 +0000 | [diff] [blame] | 1829 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 1830 | for (; SI != SE; ++SI) |
| 1831 | SymReaper.maybeDead(*SI); |
| 1832 | } |
Zhongxing Xu | 9579898 | 2010-05-26 03:27:35 +0000 | [diff] [blame] | 1833 | state.setStore(B.getRoot()); |
| 1834 | const GRState *s = StateMgr.getPersistentState(state); |
Zhongxing Xu | 9579898 | 2010-05-26 03:27:35 +0000 | [diff] [blame] | 1835 | return s; |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Ted Kremenek | 5499b84 | 2010-03-10 16:32:56 +0000 | [diff] [blame] | 1838 | |
Jordy Rose | ff59efd | 2010-08-03 20:44:35 +0000 | [diff] [blame] | 1839 | Store RegionStoreManager::EnterStackFrame(const GRState *state, |
| 1840 | const StackFrameContext *frame) { |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1841 | FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl()); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1842 | FunctionDecl::param_const_iterator PI = FD->param_begin(); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 1843 | Store store = state->getStore(); |
Zhongxing Xu | c506357 | 2010-03-16 13:14:16 +0000 | [diff] [blame] | 1844 | |
| 1845 | if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) { |
| 1846 | CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end(); |
| 1847 | |
| 1848 | // Copy the arg expression value to the arg variables. |
| 1849 | for (; AI != AE; ++AI, ++PI) { |
| 1850 | SVal ArgVal = state->getSVal(*AI); |
| 1851 | store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal); |
| 1852 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1853 | } else if (const CXXConstructExpr *CE = |
Zhongxing Xu | c506357 | 2010-03-16 13:14:16 +0000 | [diff] [blame] | 1854 | dyn_cast<CXXConstructExpr>(frame->getCallSite())) { |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 1855 | CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(), |
Zhongxing Xu | c506357 | 2010-03-16 13:14:16 +0000 | [diff] [blame] | 1856 | AE = CE->arg_end(); |
| 1857 | |
| 1858 | // Copy the arg expression value to the arg variables. |
| 1859 | for (; AI != AE; ++AI, ++PI) { |
| 1860 | SVal ArgVal = state->getSVal(*AI); |
| 1861 | store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal); |
| 1862 | } |
| 1863 | } else |
Jordy Rose | ff59efd | 2010-08-03 20:44:35 +0000 | [diff] [blame] | 1864 | llvm_unreachable("Unhandled call expression."); |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1865 | |
Jordy Rose | ff59efd | 2010-08-03 20:44:35 +0000 | [diff] [blame] | 1866 | return store; |
Zhongxing Xu | 4e3c1f7 | 2009-10-13 02:24:55 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1869 | //===----------------------------------------------------------------------===// |
| 1870 | // Utility methods. |
| 1871 | //===----------------------------------------------------------------------===// |
| 1872 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1873 | void RegionStoreManager::print(Store store, llvm::raw_ostream& OS, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1874 | const char* nl, const char *sep) { |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1875 | RegionBindings B = GetRegionBindings(store); |
Ted Kremenek | ab22ee9 | 2009-10-20 01:20:57 +0000 | [diff] [blame] | 1876 | OS << "Store (direct and default bindings):" << nl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Ted Kremenek | 451ac09 | 2009-08-06 04:50:20 +0000 | [diff] [blame] | 1878 | for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | OS << ' ' << I.getKey() << " : " << I.getData() << nl; |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1880 | } |