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