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