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 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 30 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 31 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 34 | // Fine-grained control of RegionStoreManager. |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | namespace { |
| 38 | struct VISIBILITY_HIDDEN minimal_features_tag {}; |
| 39 | struct VISIBILITY_HIDDEN maximal_features_tag {}; |
| 40 | |
| 41 | class VISIBILITY_HIDDEN RegionStoreFeatures { |
| 42 | bool SupportsFields; |
| 43 | bool SupportsRemaining; |
| 44 | |
| 45 | public: |
| 46 | RegionStoreFeatures(minimal_features_tag) : |
| 47 | SupportsFields(false), SupportsRemaining(false) {} |
| 48 | |
| 49 | RegionStoreFeatures(maximal_features_tag) : |
| 50 | SupportsFields(true), SupportsRemaining(false) {} |
| 51 | |
| 52 | void enableFields(bool t) { SupportsFields = t; } |
| 53 | |
| 54 | bool supportsFields() const { return SupportsFields; } |
| 55 | bool supportsRemaining() const { return SupportsRemaining; } |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 60 | // Region "Views" |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | // |
| 63 | // MemRegions can be layered on top of each other. This GDM entry tracks |
| 64 | // what are the MemRegions that layer a given MemRegion. |
| 65 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 66 | typedef llvm::ImmutableSet<const MemRegion*> RegionViews; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 67 | namespace { class VISIBILITY_HIDDEN RegionViewMap {}; } |
| 68 | static int RegionViewMapIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 69 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 70 | template<> struct GRStateTrait<RegionViewMap> |
| 71 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, |
| 72 | RegionViews> > { |
| 73 | |
| 74 | static void* GDMIndex() { return &RegionViewMapIndex; } |
| 75 | }; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 76 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 77 | |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 78 | // RegionCasts records the current cast type of a region. |
| 79 | namespace { class VISIBILITY_HIDDEN RegionCasts {}; } |
| 80 | static int RegionCastsIndex = 0; |
| 81 | namespace clang { |
| 82 | template<> struct GRStateTrait<RegionCasts> |
| 83 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, |
| 84 | QualType> > { |
| 85 | static void* GDMIndex() { return &RegionCastsIndex; } |
| 86 | }; |
| 87 | } |
| 88 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // Region "Extents" |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // |
| 93 | // MemRegions represent chunks of memory with a size (their "extent"). This |
| 94 | // GDM entry tracks the extents for regions. Extents are in bytes. |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 95 | // |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 96 | namespace { class VISIBILITY_HIDDEN RegionExtents {}; } |
| 97 | static int RegionExtentsIndex = 0; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 98 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 99 | template<> struct GRStateTrait<RegionExtents> |
| 100 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 101 | static void* GDMIndex() { return &RegionExtentsIndex; } |
| 102 | }; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 106 | // Regions with default values. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 107 | //===----------------------------------------------------------------------===// |
| 108 | // |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 109 | // This GDM entry tracks what regions have a default value if they have no bound |
| 110 | // value and have not been killed. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 111 | // |
| 112 | namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; } |
| 113 | static int RegionDefaultValueIndex = 0; |
| 114 | namespace clang { |
| 115 | template<> struct GRStateTrait<RegionDefaultValue> |
| 116 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 117 | static void* GDMIndex() { return &RegionDefaultValueIndex; } |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | // Main RegionStore logic. |
| 123 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 124 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 125 | namespace { |
| 126 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 127 | class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap { |
| 128 | typedef llvm::DenseMap<const MemRegion*, |
| 129 | llvm::ImmutableSet<const MemRegion*> > Map; |
| 130 | |
| 131 | llvm::ImmutableSet<const MemRegion*>::Factory F; |
| 132 | Map M; |
| 133 | |
| 134 | public: |
| 135 | void add(const MemRegion* Parent, const MemRegion* SubRegion) { |
| 136 | Map::iterator I = M.find(Parent); |
| 137 | M.insert(std::make_pair(Parent, |
| 138 | F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion))); |
| 139 | } |
| 140 | |
| 141 | ~RegionStoreSubRegionMap() {} |
| 142 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 143 | bool iterSubRegions(const MemRegion* Parent, Visitor& V) const { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 144 | Map::iterator I = M.find(Parent); |
| 145 | |
| 146 | if (I == M.end()) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 147 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 148 | |
| 149 | llvm::ImmutableSet<const MemRegion*> S = I->second; |
| 150 | for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end(); |
| 151 | SI != SE; ++SI) { |
| 152 | if (!V.Visit(Parent, *SI)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 153 | return false; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 154 | } |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 155 | |
| 156 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 160 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 161 | const RegionStoreFeatures Features; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 162 | RegionBindingsTy::Factory RBFactory; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 163 | RegionViews::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 165 | const MemRegion* SelfRegion; |
| 166 | const ImplicitParamDecl *SelfDecl; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 167 | |
| 168 | public: |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 169 | RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f) |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 170 | : StoreManager(mgr, true), |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 171 | Features(f), |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 172 | RBFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 173 | RVFactory(mgr.getAllocator()), |
Ted Kremenek | c62abc1 | 2009-04-21 21:51:34 +0000 | [diff] [blame] | 174 | SelfRegion(0), SelfDecl(0) { |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 175 | if (const ObjCMethodDecl* MD = |
| 176 | dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl())) |
| 177 | SelfDecl = MD->getSelfDecl(); |
| 178 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 179 | |
| 180 | virtual ~RegionStoreManager() {} |
| 181 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 182 | SubRegionMap* getSubRegionMap(const GRState *state); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 184 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 185 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 186 | /// associated StringRegion, and the lvalue of a StringLiteral is |
| 187 | /// the lvalue of that region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 188 | SVal getLValueString(const GRState *state, const StringLiteral* S); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 189 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 190 | /// getLValueCompoundLiteral - Returns an SVal representing the |
| 191 | /// lvalue of a compound literal. Within RegionStore a compound |
| 192 | /// literal has an associated region, and the lvalue of the |
| 193 | /// compound literal is the lvalue of that region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 194 | SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 196 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 197 | /// variable. Within RegionStore a variable has an associated |
| 198 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 199 | SVal getLValueVar(const GRState *state, const VarDecl* VD); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 201 | SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 203 | SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 205 | SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 207 | SVal getLValueElement(const GRState *state, QualType elementType, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 208 | SVal Base, SVal Offset); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 209 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 211 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 212 | /// type. 'Array' represents the lvalue of the array being decayed |
| 213 | /// to a pointer, and the returned SVal represents the decayed |
| 214 | /// version of that lvalue (i.e., a pointer to the first element of |
| 215 | /// the array). This is called by GRExprEngine when evaluating |
| 216 | /// casts from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 217 | SVal ArrayToPointer(Loc Array); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 219 | SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L, |
Ted Kremenek | 5c73462 | 2009-06-26 00:41:43 +0000 | [diff] [blame] | 220 | NonLoc R, QualType resultTy); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 221 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 222 | Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 223 | |
| 224 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 225 | /// 'this' object (C++). When used when analyzing a normal function this |
| 226 | /// method returns NULL. |
| 227 | const MemRegion* getSelfRegion(Store) { |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 228 | if (!SelfDecl) |
| 229 | return 0; |
| 230 | |
| 231 | if (!SelfRegion) { |
| 232 | const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()); |
| 233 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 234 | MRMgr.getHeapRegion()); |
| 235 | } |
| 236 | |
| 237 | return SelfRegion; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 238 | } |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 239 | |
| 240 | //===-------------------------------------------------------------------===// |
| 241 | // Binding values to regions. |
| 242 | //===-------------------------------------------------------------------===// |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 243 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 244 | const GRState *Bind(const GRState *state, Loc LV, SVal V); |
| 245 | |
| 246 | const GRState *BindCompoundLiteral(const GRState *state, |
| 247 | const CompoundLiteralExpr* CL, SVal V); |
| 248 | |
| 249 | const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal); |
| 250 | |
| 251 | const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) { |
| 252 | return state; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 253 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 255 | /// BindStruct - Bind a compound value to a structure. |
| 256 | const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V); |
| 257 | |
| 258 | const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V); |
| 259 | |
| 260 | /// KillStruct - Set the entire struct to unknown. |
| 261 | const GRState *KillStruct(const GRState *state, const TypedRegion* R); |
| 262 | |
| 263 | const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V); |
| 264 | |
| 265 | Store Remove(Store store, Loc LV); |
| 266 | |
| 267 | //===------------------------------------------------------------------===// |
| 268 | // Loading values from regions. |
| 269 | //===------------------------------------------------------------------===// |
| 270 | |
| 271 | /// The high level logic for this method is this: |
| 272 | /// Retrieve (L) |
| 273 | /// if L has binding |
| 274 | /// return L's binding |
| 275 | /// else if L is in killset |
| 276 | /// return unknown |
| 277 | /// else |
| 278 | /// if L is on stack or heap |
| 279 | /// return undefined |
| 280 | /// else |
| 281 | /// return symbolic |
| 282 | SVal Retrieve(const GRState *state, Loc L, QualType T = QualType()); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 283 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 284 | SVal RetrieveElement(const GRState* state, const ElementRegion* R); |
| 285 | |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 286 | SVal RetrieveField(const GRState* state, const FieldRegion* R); |
| 287 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 288 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 289 | /// struct copy: |
| 290 | /// struct s x, y; |
| 291 | /// x = y; |
| 292 | /// y's value is retrieved by this method. |
| 293 | SVal RetrieveStruct(const GRState *St, const TypedRegion* R); |
| 294 | |
| 295 | SVal RetrieveArray(const GRState *St, const TypedRegion* R); |
| 296 | |
| 297 | //===------------------------------------------------------------------===// |
| 298 | // State pruning. |
| 299 | //===------------------------------------------------------------------===// |
| 300 | |
| 301 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 302 | /// It returns a new Store with these values removed. |
| 303 | Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper, |
| 304 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
| 305 | |
| 306 | //===------------------------------------------------------------------===// |
| 307 | // Region "extents". |
| 308 | //===------------------------------------------------------------------===// |
| 309 | |
| 310 | const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent); |
| 311 | SVal getSizeInElements(const GRState *state, const MemRegion* R); |
| 312 | |
| 313 | //===------------------------------------------------------------------===// |
| 314 | // Region "views". |
| 315 | //===------------------------------------------------------------------===// |
| 316 | |
| 317 | const GRState *AddRegionView(const GRState *state, const MemRegion* View, |
| 318 | const MemRegion* Base); |
| 319 | |
| 320 | const GRState *RemoveRegionView(const GRState *state, const MemRegion* View, |
| 321 | const MemRegion* Base); |
| 322 | |
| 323 | //===------------------------------------------------------------------===// |
| 324 | // Utility methods. |
| 325 | //===------------------------------------------------------------------===// |
| 326 | |
Ted Kremenek | 48ce7de | 2009-07-06 20:21:51 +0000 | [diff] [blame] | 327 | const GRState *setCastType(const GRState *state, const MemRegion* R, |
| 328 | QualType T); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 329 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 330 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 331 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 332 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 334 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 335 | const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 336 | |
| 337 | void iterBindings(Store store, BindingsHandler& f) { |
| 338 | // FIXME: Implement. |
| 339 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 341 | // FIXME: Remove. |
| 342 | BasicValueFactory& getBasicVals() { |
| 343 | return StateMgr.getBasicVals(); |
| 344 | } |
| 345 | |
| 346 | // FIXME: Remove. |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 347 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | } // end anonymous namespace |
| 351 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 352 | //===----------------------------------------------------------------------===// |
| 353 | // RegionStore creation. |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | |
| 356 | StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
| 357 | RegionStoreFeatures F = maximal_features_tag(); |
| 358 | return new RegionStoreManager(StMgr, F); |
| 359 | } |
| 360 | |
| 361 | StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) { |
| 362 | RegionStoreFeatures F = minimal_features_tag(); |
| 363 | F.enableFields(true); |
| 364 | return new RegionStoreManager(StMgr, F); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 367 | SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 368 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
| 369 | RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap(); |
| 370 | |
| 371 | for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 372 | if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey())) |
| 373 | M->add(R->getSuperRegion(), R); |
| 374 | } |
| 375 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 376 | return M; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 377 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 378 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 379 | //===----------------------------------------------------------------------===// |
| 380 | // getLValueXXX methods. |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 383 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 384 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 385 | /// associated StringRegion, and the lvalue of a StringLiteral is the |
| 386 | /// lvalue of that region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 387 | SVal RegionStoreManager::getLValueString(const GRState *St, |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 388 | const StringLiteral* S) { |
| 389 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 390 | } |
| 391 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 392 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 393 | /// variable. Within RegionStore a variable has an associated |
| 394 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 395 | SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) { |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 396 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 397 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 399 | /// getLValueCompoundLiteral - Returns an SVal representing the lvalue |
| 400 | /// of a compound literal. Within RegionStore a compound literal |
| 401 | /// has an associated region, and the lvalue of the compound literal |
| 402 | /// is the lvalue of that region. |
| 403 | SVal |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 404 | RegionStoreManager::getLValueCompoundLiteral(const GRState *St, |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 405 | const CompoundLiteralExpr* CL) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 406 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 407 | } |
| 408 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 409 | SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D, |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 410 | SVal Base) { |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 411 | return getLValueFieldOrIvar(St, Base, D); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 414 | SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base, |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 415 | const FieldDecl* D) { |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 416 | return getLValueFieldOrIvar(St, Base, D); |
| 417 | } |
| 418 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 419 | SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base, |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 420 | const Decl* D) { |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 421 | if (Base.isUnknownOrUndef()) |
| 422 | return Base; |
| 423 | |
| 424 | Loc BaseL = cast<Loc>(Base); |
| 425 | const MemRegion* BaseR = 0; |
| 426 | |
| 427 | switch (BaseL.getSubKind()) { |
| 428 | case loc::MemRegionKind: |
| 429 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 430 | break; |
| 431 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 432 | case loc::GotoLabelKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 433 | // These are anormal cases. Flag an undefined value. |
| 434 | return UndefinedVal(); |
| 435 | |
| 436 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 437 | // While these seem funny, this can happen through casts. |
| 438 | // FIXME: What we should return is the field offset. For example, |
| 439 | // add the field offset to the integer value. That way funny things |
| 440 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 441 | return Base; |
| 442 | |
| 443 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 444 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 445 | return Base; |
| 446 | } |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 447 | |
| 448 | // NOTE: We must have this check first because ObjCIvarDecl is a subclass |
| 449 | // of FieldDecl. |
| 450 | if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D)) |
| 451 | return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 453 | return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR)); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 456 | SVal RegionStoreManager::getLValueElement(const GRState *St, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 457 | QualType elementType, |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 458 | SVal Base, SVal Offset) { |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | de7ec63 | 2009-03-09 22:44:49 +0000 | [diff] [blame] | 460 | // If the base is an unknown or undefined value, just return it back. |
| 461 | // FIXME: For absolute pointer addresses, we just return that value back as |
| 462 | // well, although in reality we should return the offset added to that |
| 463 | // value. |
| 464 | if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base)) |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 465 | return Base; |
| 466 | |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 467 | // Only handle integer offsets... for now. |
| 468 | if (!isa<nonloc::ConcreteInt>(Offset)) |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 469 | return UnknownVal(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 470 | |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 471 | const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 472 | |
| 473 | // Pointer of any type can be cast and used as array base. |
| 474 | const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion); |
| 475 | |
| 476 | if (!ElemR) { |
| 477 | // |
| 478 | // If the base region is not an ElementRegion, create one. |
| 479 | // This can happen in the following example: |
| 480 | // |
| 481 | // char *p = __builtin_alloc(10); |
| 482 | // p[1] = 8; |
| 483 | // |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 484 | // Observe that 'p' binds to an AllocaRegion. |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 485 | // |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 486 | |
| 487 | // Offset might be unsigned. We have to convert it to signed ConcreteInt. |
| 488 | if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) { |
| 489 | const llvm::APSInt& OffI = CI->getValue(); |
| 490 | if (OffI.isUnsigned()) { |
| 491 | llvm::APSInt Tmp = OffI; |
| 492 | Tmp.setIsSigned(true); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 493 | Offset = ValMgr.makeIntVal(Tmp); |
Zhongxing Xu | 5ea2ffc | 2009-02-19 08:37:16 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 496 | return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset, |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 497 | BaseRegion, getContext())); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 498 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 499 | |
| 500 | SVal BaseIdx = ElemR->getIndex(); |
| 501 | |
| 502 | if (!isa<nonloc::ConcreteInt>(BaseIdx)) |
| 503 | return UnknownVal(); |
| 504 | |
| 505 | const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue(); |
| 506 | const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue(); |
| 507 | assert(BaseIdxI.isSigned()); |
| 508 | |
| 509 | // FIXME: This appears to be the assumption of this code. We should review |
| 510 | // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it |
| 511 | // can't we need to put a comment here. If it can, we should handle it. |
| 512 | assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth()); |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 513 | |
Zhongxing Xu | ce76078 | 2009-05-09 13:20:07 +0000 | [diff] [blame] | 514 | const MemRegion *ArrayR = ElemR->getSuperRegion(); |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 515 | SVal NewIdx; |
| 516 | |
| 517 | if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) { |
| 518 | // 'Offset' might be unsigned. We have to convert it to signed and |
| 519 | // possibly extend it. |
| 520 | llvm::APSInt Tmp = OffI; |
| 521 | |
| 522 | if (OffI.getBitWidth() < BaseIdxI.getBitWidth()) |
| 523 | Tmp.extend(BaseIdxI.getBitWidth()); |
| 524 | |
| 525 | Tmp.setIsSigned(true); |
| 526 | Tmp += BaseIdxI; // Compute the new offset. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 527 | NewIdx = ValMgr.makeIntVal(Tmp); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 528 | } |
Ted Kremenek | a7ac944 | 2009-01-22 20:27:48 +0000 | [diff] [blame] | 529 | else |
| 530 | NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI)); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 531 | |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 532 | return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR, |
| 533 | getContext())); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 536 | //===----------------------------------------------------------------------===// |
| 537 | // Extents for regions. |
| 538 | //===----------------------------------------------------------------------===// |
| 539 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 540 | SVal RegionStoreManager::getSizeInElements(const GRState *state, |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 541 | const MemRegion *R) { |
| 542 | |
| 543 | switch (R->getKind()) { |
| 544 | case MemRegion::MemSpaceRegionKind: |
| 545 | assert(0 && "Cannot index into a MemSpace"); |
| 546 | return UnknownVal(); |
| 547 | |
| 548 | case MemRegion::CodeTextRegionKind: |
| 549 | // Technically this can happen if people do funny things with casts. |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 550 | return UnknownVal(); |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 551 | |
| 552 | // Not yet handled. |
| 553 | case MemRegion::AllocaRegionKind: |
| 554 | case MemRegion::CompoundLiteralRegionKind: |
| 555 | case MemRegion::ElementRegionKind: |
| 556 | case MemRegion::FieldRegionKind: |
| 557 | case MemRegion::ObjCIvarRegionKind: |
| 558 | case MemRegion::ObjCObjectRegionKind: |
| 559 | case MemRegion::SymbolicRegionKind: |
| 560 | return UnknownVal(); |
| 561 | |
| 562 | case MemRegion::StringRegionKind: { |
| 563 | const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral(); |
| 564 | // We intentionally made the size value signed because it participates in |
| 565 | // operations with signed indices. |
| 566 | return ValMgr.makeIntVal(Str->getByteLength()+1, false); |
Ted Kremenek | 14553ab | 2009-01-30 00:08:43 +0000 | [diff] [blame] | 567 | } |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 568 | |
| 569 | // TypedViewRegion will soon be removed. |
| 570 | case MemRegion::TypedViewRegionKind: |
| 571 | return UnknownVal(); |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 573 | case MemRegion::VarRegionKind: { |
| 574 | const VarRegion* VR = cast<VarRegion>(R); |
| 575 | // Get the type of the variable. |
| 576 | QualType T = VR->getDesugaredValueType(getContext()); |
| 577 | |
| 578 | // FIXME: Handle variable-length arrays. |
| 579 | if (isa<VariableArrayType>(T)) |
| 580 | return UnknownVal(); |
| 581 | |
| 582 | if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) { |
| 583 | // return the size as signed integer. |
| 584 | return ValMgr.makeIntVal(CAT->getSize(), false); |
| 585 | } |
| 586 | |
| 587 | const QualType* CastTy = state->get<RegionCasts>(VR); |
| 588 | |
| 589 | // If the VarRegion is cast to other type, compute the size with respect to |
| 590 | // that type. |
| 591 | if (CastTy) { |
| 592 | QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType(); |
| 593 | QualType VarTy = VR->getValueType(getContext()); |
| 594 | uint64_t EleSize = getContext().getTypeSize(EleTy); |
| 595 | uint64_t VarSize = getContext().getTypeSize(VarTy); |
| 596 | assert(VarSize != 0); |
| 597 | return ValMgr.makeIntVal(VarSize/EleSize, false); |
| 598 | } |
| 599 | |
| 600 | // Clients can use ordinary variables as if they were arrays. These |
| 601 | // essentially are arrays of size 1. |
| 602 | return ValMgr.makeIntVal(1, false); |
Zhongxing Xu | 41fd018 | 2009-05-06 11:51:48 +0000 | [diff] [blame] | 603 | } |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 604 | |
| 605 | case MemRegion::BEG_DECL_REGIONS: |
| 606 | case MemRegion::END_DECL_REGIONS: |
| 607 | case MemRegion::BEG_TYPED_REGIONS: |
| 608 | case MemRegion::END_TYPED_REGIONS: |
| 609 | assert(0 && "Infeasible region"); |
| 610 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 611 | } |
Ted Kremenek | 7ecbfbc | 2009-07-10 22:30:06 +0000 | [diff] [blame^] | 612 | |
| 613 | assert(0 && "Unreachable"); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 614 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 617 | const GRState *RegionStoreManager::setExtent(const GRState *state, |
| 618 | const MemRegion *region, |
| 619 | SVal extent) { |
| 620 | return state->set<RegionExtents>(region, extent); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | //===----------------------------------------------------------------------===// |
| 624 | // Location and region casting. |
| 625 | //===----------------------------------------------------------------------===// |
| 626 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 627 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 628 | /// type. 'Array' represents the lvalue of the array being decayed |
| 629 | /// to a pointer, and the returned SVal represents the decayed |
| 630 | /// version of that lvalue (i.e., a pointer to the first element of |
| 631 | /// the array). This is called by GRExprEngine when evaluating casts |
| 632 | /// from arrays to pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 633 | SVal RegionStoreManager::ArrayToPointer(Loc Array) { |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 634 | if (!isa<loc::MemRegionVal>(Array)) |
| 635 | return UnknownVal(); |
| 636 | |
| 637 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 638 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 639 | |
Ted Kremenek | bbee1a7 | 2009-01-13 01:03:27 +0000 | [diff] [blame] | 640 | if (!ArrayR) |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 641 | return UnknownVal(); |
| 642 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 643 | // Strip off typedefs from the ArrayRegion's ValueType. |
| 644 | QualType T = ArrayR->getValueType(getContext())->getDesugaredType(); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 645 | ArrayType *AT = cast<ArrayType>(T); |
| 646 | T = AT->getElementType(); |
| 647 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 648 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 649 | ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext()); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 650 | |
| 651 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 654 | //===----------------------------------------------------------------------===// |
| 655 | // Pointer arithmetic. |
| 656 | //===----------------------------------------------------------------------===// |
| 657 | |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 658 | SVal RegionStoreManager::EvalBinOp(const GRState *state, |
Ted Kremenek | 5c73462 | 2009-06-26 00:41:43 +0000 | [diff] [blame] | 659 | BinaryOperator::Opcode Op, Loc L, NonLoc R, |
| 660 | QualType resultTy) { |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 661 | // Assume the base location is MemRegionVal. |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 662 | if (!isa<loc::MemRegionVal>(L)) |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 663 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 664 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 665 | const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 666 | const ElementRegion *ER = 0; |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 667 | |
| 668 | // If the operand is a symbolic or alloca region, create the first element |
| 669 | // region on it. |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 670 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) { |
Zhongxing Xu | 005f07b | 2009-06-19 04:51:14 +0000 | [diff] [blame] | 671 | QualType T; |
| 672 | // If the SymbolicRegion was cast to another type, use that type. |
| 673 | if (const QualType *t = state->get<RegionCasts>(SR)) { |
| 674 | T = *t; |
| 675 | } else { |
| 676 | // Otherwise use the symbol's type. |
| 677 | SymbolRef Sym = SR->getSymbol(); |
| 678 | T = Sym->getType(getContext()); |
| 679 | } |
Zhongxing Xu | 53454dc | 2009-06-12 03:59:12 +0000 | [diff] [blame] | 680 | QualType EleTy = T->getAsPointerType()->getPointeeType(); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 681 | |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 682 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 683 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext()); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 684 | } |
| 685 | else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) { |
| 686 | // Get the alloca region's current cast type. |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 688 | |
| 689 | GRStateTrait<RegionCasts>::lookup_type T = state->get<RegionCasts>(AR); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 690 | assert(T && "alloca region has no type."); |
| 691 | QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType(); |
| 692 | SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 693 | ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext()); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 694 | } |
Zhongxing Xu | 5414a5c | 2009-06-21 13:24:24 +0000 | [diff] [blame] | 695 | else if (isa<FieldRegion>(MR)) { |
| 696 | // Not track pointer arithmetic on struct fields. |
| 697 | return UnknownVal(); |
| 698 | } |
| 699 | else { |
Zhongxing Xu | c4761f5 | 2009-05-09 15:18:12 +0000 | [diff] [blame] | 700 | ER = cast<ElementRegion>(MR); |
Zhongxing Xu | 5414a5c | 2009-06-21 13:24:24 +0000 | [diff] [blame] | 701 | } |
Zhongxing Xu | 2b1dc17 | 2009-03-11 07:43:49 +0000 | [diff] [blame] | 702 | |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 703 | SVal Idx = ER->getIndex(); |
| 704 | |
| 705 | nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx); |
| 706 | nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R); |
| 707 | |
| 708 | // Only support concrete integer indexes for now. |
| 709 | if (Base && Offset) { |
Ted Kremenek | 610e81d | 2009-03-13 15:35:24 +0000 | [diff] [blame] | 710 | // FIXME: For now, convert the signedness and bitwidth of offset in case |
| 711 | // they don't match. This can result from pointer arithmetic. In reality, |
| 712 | // we should figure out what are the proper semantics and implement them. |
| 713 | // |
Ted Kremenek | 1060a3c | 2009-03-13 15:39:16 +0000 | [diff] [blame] | 714 | // This addresses the test case test/Analysis/ptr-arith.c |
| 715 | // |
Ted Kremenek | 610e81d | 2009-03-13 15:35:24 +0000 | [diff] [blame] | 716 | nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(), |
| 717 | Offset->getValue())); |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 718 | SVal NewIdx = Base->evalBinOp(ValMgr, Op, OffConverted); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 719 | const MemRegion* NewER = |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 720 | MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(), |
| 721 | getContext()); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 722 | return ValMgr.makeLoc(NewER); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 723 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return UnknownVal(); |
Zhongxing Xu | 94aa6c1 | 2009-03-02 07:52:23 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 729 | //===----------------------------------------------------------------------===// |
| 730 | // Loading values from regions. |
| 731 | //===----------------------------------------------------------------------===// |
| 732 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 733 | SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) { |
| 734 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 735 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 736 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 737 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 738 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 739 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 740 | if (isa<loc::ConcreteInt>(L)) |
| 741 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 742 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 743 | const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 744 | |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 745 | // FIXME: return symbolic value for these cases. |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 746 | // Example: |
| 747 | // void f(int* p) { int x = *p; } |
Zhongxing Xu | 9184412 | 2009-05-20 09:18:48 +0000 | [diff] [blame] | 748 | // char* p = alloca(); |
| 749 | // read(p); |
| 750 | // c = *p; |
| 751 | if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR)) |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 752 | return UnknownVal(); |
| 753 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 754 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 755 | // 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] | 756 | const TypedRegion *R = cast<TypedRegion>(MR); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 757 | assert(R && "bad region"); |
| 758 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 759 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 760 | // |
| 761 | // int x = ...; |
| 762 | // int *p = &x; |
| 763 | // char *q = (char*) p; |
| 764 | // char c = *q; // returns the first byte of 'x'. |
| 765 | // |
| 766 | // Such funny addressing will occur due to layering of regions. |
| 767 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 768 | QualType RTy = R->getValueType(getContext()); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 769 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 770 | if (RTy->isStructureType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 771 | return RetrieveStruct(state, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 772 | |
| 773 | if (RTy->isArrayType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 774 | return RetrieveArray(state, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 775 | |
Zhongxing Xu | 1038f9f | 2009-03-09 09:15:51 +0000 | [diff] [blame] | 776 | // FIXME: handle Vector types. |
| 777 | if (RTy->isVectorType()) |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 778 | return UnknownVal(); |
Zhongxing Xu | 99c2030 | 2009-06-28 14:16:39 +0000 | [diff] [blame] | 779 | |
| 780 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) |
| 781 | return RetrieveField(state, FR); |
| 782 | |
| 783 | if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) |
| 784 | return RetrieveElement(state, ER); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 785 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 786 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 787 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 788 | |
| 789 | // Check if the region has a binding. |
| 790 | if (V) |
| 791 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 792 | |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 793 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { |
| 794 | const MemRegion *SR = IVR->getSuperRegion(); |
| 795 | |
| 796 | // If the super region is 'self' then return the symbol representing |
| 797 | // the value of the ivar upon entry to the method. |
| 798 | if (SR == SelfRegion) { |
| 799 | // FIXME: Do we need to handle the case where the super region |
| 800 | // has a view? We want to canonicalize the bindings. |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 801 | return ValMgr.getRegionValueSymbolVal(R); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | // Otherwise, we need a new symbol. For now return Unknown. |
| 805 | return UnknownVal(); |
| 806 | } |
Zhongxing Xu | 562c4d9 | 2009-01-23 11:22:12 +0000 | [diff] [blame] | 807 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 808 | // The location does not have a bound value. This means that it has |
| 809 | // the value it had upon its creation and/or entry to the analyzed |
| 810 | // function/method. These are either symbolic values or 'undefined'. |
| 811 | |
| 812 | // We treat function parameters as symbolic values. |
Ted Kremenek | 6fd8f91 | 2009-01-22 23:43:57 +0000 | [diff] [blame] | 813 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 814 | const VarDecl *VD = VR->getDecl(); |
| 815 | |
Ted Kremenek | cec3525 | 2009-03-04 00:23:05 +0000 | [diff] [blame] | 816 | if (VD == SelfDecl) |
| 817 | return loc::MemRegionVal(getSelfRegion(0)); |
| 818 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 819 | if (VR->hasGlobalsOrParametersStorage()) |
| 820 | return ValMgr.getRegionValueSymbolValOrUnknown(VR, VD->getType()); |
Ted Kremenek | 3de2d3c | 2009-03-05 04:50:08 +0000 | [diff] [blame] | 821 | } |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 822 | |
Ted Kremenek | bb7c96f | 2009-06-23 18:17:08 +0000 | [diff] [blame] | 823 | if (R->hasHeapOrStackStorage()) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 824 | // All stack variables are considered to have undefined values |
| 825 | // upon creation. All heap allocated blocks are considered to |
| 826 | // have undefined values as well unless they are explicitly bound |
| 827 | // to specific values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 828 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 831 | // If the region is already cast to another type, use that type to create the |
| 832 | // symbol value. |
| 833 | if (const QualType *p = state->get<RegionCasts>(R)) { |
| 834 | QualType T = *p; |
| 835 | RTy = T->getAsPointerType()->getPointeeType(); |
| 836 | } |
| 837 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 838 | // All other values are symbolic. |
| 839 | return ValMgr.getRegionValueSymbolValOrUnknown(R, RTy); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 842 | SVal RegionStoreManager::RetrieveElement(const GRState* state, |
| 843 | const ElementRegion* R) { |
| 844 | // Check if the region has a binding. |
| 845 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 846 | if (const SVal* V = B.lookup(R)) |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 847 | return *V; |
| 848 | |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 849 | const MemRegion* superR = R->getSuperRegion(); |
| 850 | |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 851 | // Check if the region is an element region of a string literal. |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 852 | if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) { |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 853 | const StringLiteral *Str = StrR->getStringLiteral(); |
| 854 | SVal Idx = R->getIndex(); |
| 855 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) { |
| 856 | int64_t i = CI->getValue().getSExtValue(); |
| 857 | char c; |
| 858 | if (i == Str->getByteLength()) |
| 859 | c = '\0'; |
| 860 | else |
| 861 | c = Str->getStrData()[i]; |
| 862 | return ValMgr.makeIntVal(c, getContext().CharTy); |
| 863 | } |
| 864 | } |
| 865 | |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 866 | // Check if the super region has a default value. |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 867 | if (const SVal *D = state->get<RegionDefaultValue>(superR)) { |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 868 | if (D->hasConjuredSymbol()) |
| 869 | return ValMgr.getRegionValueSymbolVal(R); |
| 870 | else |
| 871 | return *D; |
| 872 | } |
| 873 | |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 874 | // Check if the super region has a binding. |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 875 | if (B.lookup(superR)) { |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 876 | // We do not extract the bit value from super region for now. |
Zhongxing Xu | 8834af3 | 2009-07-03 06:11:41 +0000 | [diff] [blame] | 877 | return UnknownVal(); |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 878 | } |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 879 | |
| 880 | if (R->hasHeapStorage()) { |
| 881 | // FIXME: If the region has heap storage and we know nothing special |
| 882 | // about its bindings, should we instead return UnknownVal? Seems like |
| 883 | // we should only return UndefinedVal in the cases where we know the value |
| 884 | // will be undefined. |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 885 | return UndefinedVal(); |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 888 | if (R->hasStackStorage() && !R->hasParametersStorage()) { |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 889 | // Currently we don't reason specially about Clang-style vectors. Check |
| 890 | // if superR is a vector and if so return Unknown. |
| 891 | if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) { |
| 892 | if (typedSuperR->getValueType(getContext())->isVectorType()) |
| 893 | return UnknownVal(); |
| 894 | } |
| 895 | |
| 896 | return UndefinedVal(); |
| 897 | } |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 898 | |
| 899 | QualType Ty = R->getValueType(getContext()); |
| 900 | |
| 901 | // If the region is already cast to another type, use that type to create the |
| 902 | // symbol value. |
| 903 | if (const QualType *p = state->get<RegionCasts>(R)) |
| 904 | Ty = (*p)->getAsPointerType()->getPointeeType(); |
| 905 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 906 | return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty); |
Zhongxing Xu | c00346f | 2009-06-25 05:29:39 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 909 | SVal RegionStoreManager::RetrieveField(const GRState* state, |
| 910 | const FieldRegion* R) { |
| 911 | QualType Ty = R->getValueType(getContext()); |
| 912 | |
| 913 | // Check if the region has a binding. |
| 914 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 915 | if (const SVal* V = B.lookup(R)) |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 916 | return *V; |
| 917 | |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 918 | const MemRegion* superR = R->getSuperRegion(); |
| 919 | if (const SVal* D = state->get<RegionDefaultValue>(superR)) { |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 920 | if (D->hasConjuredSymbol()) |
| 921 | return ValMgr.getRegionValueSymbolVal(R); |
| 922 | |
| 923 | if (D->isZeroConstant()) |
| 924 | return ValMgr.makeZeroVal(Ty); |
| 925 | |
| 926 | if (D->isUnknown()) |
| 927 | return *D; |
| 928 | |
| 929 | assert(0 && "Unknown default value"); |
| 930 | } |
| 931 | |
Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 932 | // FIXME: Is this correct? Should it be UnknownVal? |
| 933 | if (R->hasHeapStorage()) |
| 934 | return UndefinedVal(); |
| 935 | |
| 936 | if (R->hasStackStorage() && !R->hasParametersStorage()) |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 937 | return UndefinedVal(); |
| 938 | |
| 939 | // If the region is already cast to another type, use that type to create the |
| 940 | // symbol value. |
| 941 | if (const QualType *p = state->get<RegionCasts>(R)) { |
| 942 | QualType tmp = *p; |
| 943 | Ty = tmp->getAsPointerType()->getPointeeType(); |
| 944 | } |
| 945 | |
Ted Kremenek | bb2b433 | 2009-07-02 22:16:42 +0000 | [diff] [blame] | 946 | // All other values are symbolic. |
| 947 | return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty); |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 950 | SVal RegionStoreManager::RetrieveStruct(const GRState *state, |
| 951 | const TypedRegion* R){ |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 952 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 953 | assert(T->isStructureType()); |
| 954 | |
Zhongxing Xu | b7507d1 | 2009-06-11 07:27:30 +0000 | [diff] [blame] | 955 | const RecordType* RT = T->getAsStructureType(); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 956 | RecordDecl* RD = RT->getDecl(); |
| 957 | assert(RD->isDefinition()); |
| 958 | |
| 959 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 960 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 961 | // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a |
| 962 | // reverse iterator, we should implement one. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 963 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 965 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 966 | FieldEnd = Fields.rend(); |
| 967 | Field != FieldEnd; ++Field) { |
| 968 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 969 | QualType FTy = (*Field)->getType(); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 970 | SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 971 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 972 | } |
| 973 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 974 | return ValMgr.makeCompoundVal(T, StructVal); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 977 | SVal RegionStoreManager::RetrieveArray(const GRState *state, |
| 978 | const TypedRegion * R) { |
| 979 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 980 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 981 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 982 | |
| 983 | llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList(); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 984 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 2ee5214 | 2009-05-11 12:48:56 +0000 | [diff] [blame] | 985 | llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 986 | |
| 987 | for (; i < Size; ++i) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 988 | SVal Idx = ValMgr.makeIntVal(i); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 989 | ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R, |
| 990 | getContext()); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 991 | QualType ETy = ER->getElementType(); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 992 | SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 993 | ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal); |
| 994 | } |
| 995 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 996 | return ValMgr.makeCompoundVal(T, ArrayVal); |
Zhongxing Xu | 3e001f3 | 2009-05-03 00:27:40 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 999 | //===----------------------------------------------------------------------===// |
| 1000 | // Binding values to regions. |
| 1001 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1002 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1003 | Store RegionStoreManager::Remove(Store store, Loc L) { |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1004 | const MemRegion* R = 0; |
| 1005 | |
| 1006 | if (isa<loc::MemRegionVal>(L)) |
| 1007 | R = cast<loc::MemRegionVal>(L).getRegion(); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1008 | |
| 1009 | if (R) { |
| 1010 | RegionBindingsTy B = GetRegionBindings(store); |
| 1011 | return RBFactory.Remove(B, R).getRoot(); |
| 1012 | } |
| 1013 | |
| 1014 | return store; |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1017 | const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) { |
Zhongxing Xu | 87453d1 | 2009-06-28 10:16:11 +0000 | [diff] [blame] | 1018 | if (isa<loc::ConcreteInt>(L)) |
| 1019 | return state; |
| 1020 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1021 | // If we get here, the location should be a region. |
| 1022 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Check if the region is a struct region. |
| 1025 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 1026 | if (TR->getValueType(getContext())->isStructureType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1027 | return BindStruct(state, TR, V); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1028 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1029 | RegionBindingsTy B = GetRegionBindings(state->getStore()); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1030 | |
Zhongxing Xu | e4df9c4 | 2009-06-25 05:52:16 +0000 | [diff] [blame] | 1031 | B = RBFactory.Add(B, R, V); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1033 | return state->makeWithStore(B.getRoot()); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1036 | const GRState *RegionStoreManager::BindDecl(const GRState *state, |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1037 | const VarDecl* VD, SVal InitVal) { |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 1038 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1039 | QualType T = VD->getType(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1040 | VarRegion* VR = MRMgr.getVarRegion(VD); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1042 | if (T->isArrayType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1043 | return BindArray(state, VR, InitVal); |
Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1044 | if (T->isStructureType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1045 | return BindStruct(state, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 1046 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1047 | return Bind(state, ValMgr.makeLoc(VR), InitVal); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1048 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 1049 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1050 | // FIXME: this method should be merged into Bind(). |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1051 | const GRState * |
| 1052 | RegionStoreManager::BindCompoundLiteral(const GRState *state, |
| 1053 | const CompoundLiteralExpr* CL, |
| 1054 | SVal V) { |
| 1055 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 1056 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1057 | return Bind(state, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1060 | const GRState *RegionStoreManager::BindArray(const GRState *state, |
| 1061 | const TypedRegion* R, |
| 1062 | SVal Init) { |
| 1063 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1064 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1065 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1066 | QualType ElementTy = CAT->getElementType(); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1067 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1068 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1069 | llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Check if the init expr is a StringLiteral. |
| 1072 | if (isa<loc::MemRegionVal>(Init)) { |
| 1073 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 1074 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 1075 | const char* str = S->getStrData(); |
| 1076 | unsigned len = S->getByteLength(); |
| 1077 | unsigned j = 0; |
| 1078 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1079 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 1080 | // in the array that are not covered by the string literal are initialized |
| 1081 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1082 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1083 | if (j >= len) |
| 1084 | break; |
| 1085 | |
Zhongxing Xu | 3038c5a | 2009-06-23 06:13:19 +0000 | [diff] [blame] | 1086 | SVal Idx = ValMgr.makeIntVal(i); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1087 | ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext()); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1088 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1089 | SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1090 | state = Bind(state, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1093 | return state; |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1096 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1097 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 1098 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1099 | for (; i < Size; ++i, ++VI) { |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1100 | // The init list might be shorter than the array length. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1101 | if (VI == VE) |
| 1102 | break; |
| 1103 | |
Zhongxing Xu | 3038c5a | 2009-06-23 06:13:19 +0000 | [diff] [blame] | 1104 | SVal Idx = ValMgr.makeIntVal(i); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1105 | ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (CAT->getElementType()->isStructureType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1108 | state = BindStruct(state, ER, *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1109 | else |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1110 | state = Bind(state, ValMgr.makeLoc(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Zhongxing Xu | e3a765f | 2009-06-24 00:56:31 +0000 | [diff] [blame] | 1113 | // If the init list is shorter than the array length, set the array default |
| 1114 | // value. |
| 1115 | if (i < Size) { |
| 1116 | if (ElementTy->isIntegerType()) { |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1117 | SVal V = ValMgr.makeZeroVal(ElementTy); |
Zhongxing Xu | e3a765f | 2009-06-24 00:56:31 +0000 | [diff] [blame] | 1118 | state = setDefaultValue(state, R, V); |
Zhongxing Xu | 087d6c2 | 2009-06-23 05:23:38 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1122 | return state; |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1125 | const GRState * |
| 1126 | RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R, |
| 1127 | SVal V) { |
| 1128 | |
| 1129 | if (!Features.supportsFields()) |
| 1130 | return state; |
| 1131 | |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1132 | QualType T = R->getValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1133 | assert(T->isStructureType()); |
| 1134 | |
Zhongxing Xu | b13ecdb | 2009-03-12 03:45:35 +0000 | [diff] [blame] | 1135 | const RecordType* RT = T->getAsRecordType(); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1136 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | c45a825 | 2009-03-11 09:07:35 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (!RD->isDefinition()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1139 | return state; |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1140 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1141 | // We may get non-CompoundVal accidentally due to imprecise cast logic. |
| 1142 | // Ignore them and kill the field values. |
| 1143 | if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) |
| 1144 | return KillStruct(state, R); |
Zhongxing Xu | 3f6978a | 2009-06-11 09:11:27 +0000 | [diff] [blame] | 1145 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1146 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1147 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1148 | |
| 1149 | RecordDecl::field_iterator FI, FE; |
| 1150 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1151 | 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] | 1152 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1153 | if (VI == VE) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1154 | break; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1155 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 1156 | QualType FTy = (*FI)->getType(); |
| 1157 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 1158 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1159 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1160 | state = Bind(state, ValMgr.makeLoc(FR), *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1161 | else if (FTy->isArrayType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1162 | state = BindArray(state, FR, *VI); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 1163 | else if (FTy->isStructureType()) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1164 | state = BindStruct(state, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1167 | // There may be fewer values in the initialize list than the fields of struct. |
Zhongxing Xu | 490b0f0 | 2009-06-25 04:50:44 +0000 | [diff] [blame] | 1168 | if (FI != FE) |
| 1169 | state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false)); |
Zhongxing Xu | dbdf219 | 2009-06-23 05:43:16 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1171 | return state; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1174 | const GRState *RegionStoreManager::KillStruct(const GRState *state, |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1175 | const TypedRegion* R){ |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1176 | |
Zhongxing Xu | e4df9c4 | 2009-06-25 05:52:16 +0000 | [diff] [blame] | 1177 | // Set the default value of the struct region to "unknown". |
| 1178 | state = state->set<RegionDefaultValue>(R, UnknownVal()); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Remove all bindings for the subregions of the struct. |
Zhongxing Xu | e4df9c4 | 2009-06-25 05:52:16 +0000 | [diff] [blame] | 1181 | Store store = state->getStore(); |
| 1182 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1183 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1184 | const MemRegion* R = I.getKey(); |
| 1185 | if (const SubRegion* subRegion = dyn_cast<SubRegion>(R)) |
| 1186 | if (subRegion->isSubRegionOf(R)) |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1187 | store = Remove(store, ValMgr.makeLoc(subRegion)); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1190 | return state->makeWithStore(store); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1193 | //===----------------------------------------------------------------------===// |
| 1194 | // Region views. |
| 1195 | //===----------------------------------------------------------------------===// |
| 1196 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1197 | const GRState *RegionStoreManager::AddRegionView(const GRState *state, |
| 1198 | const MemRegion* View, |
| 1199 | const MemRegion* Base) { |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1200 | |
| 1201 | // First, retrieve the region view of the base region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1202 | const RegionViews* d = state->get<RegionViewMap>(Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1203 | RegionViews L = d ? *d : RVFactory.GetEmptySet(); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Now add View to the region view. |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1206 | L = RVFactory.Add(L, View); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Create a new state with the new region view. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1209 | return state->set<RegionViewMap>(Base, L); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 1210 | } |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1212 | const GRState *RegionStoreManager::RemoveRegionView(const GRState *state, |
| 1213 | const MemRegion* View, |
| 1214 | const MemRegion* Base) { |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1215 | // Retrieve the region view of the base region. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1216 | const RegionViews* d = state->get<RegionViewMap>(Base); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1217 | |
| 1218 | // If the base region has no view, return. |
| 1219 | if (!d) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1220 | return state; |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Remove the view. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1223 | return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View)); |
Zhongxing Xu | 5834ed6 | 2009-01-13 01:49:57 +0000 | [diff] [blame] | 1224 | } |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 1225 | |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 1226 | const GRState *RegionStoreManager::setCastType(const GRState *state, |
| 1227 | const MemRegion* R, QualType T) { |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1228 | return state->set<RegionCasts>(R, T); |
Zhongxing Xu | 2079494 | 2009-05-06 08:33:50 +0000 | [diff] [blame] | 1229 | } |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 1230 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1231 | const GRState *RegionStoreManager::setDefaultValue(const GRState *state, |
| 1232 | const MemRegion* R, SVal V) { |
| 1233 | return state->set<RegionDefaultValue>(R, V); |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 1234 | } |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1235 | |
| 1236 | //===----------------------------------------------------------------------===// |
| 1237 | // State pruning. |
| 1238 | //===----------------------------------------------------------------------===// |
| 1239 | |
| 1240 | static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) { |
| 1241 | if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) { |
| 1242 | const MemRegion *R = XR->getRegion(); |
| 1243 | |
| 1244 | while (R) { |
| 1245 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { |
| 1246 | SymReaper.markLive(SR->getSymbol()); |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 1251 | R = SR->getSuperRegion(); |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI) |
| 1262 | SymReaper.markLive(*SI); |
| 1263 | } |
| 1264 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1265 | Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1266 | SymbolReaper& SymReaper, |
| 1267 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1268 | { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1269 | Store store = state->getStore(); |
| 1270 | RegionBindingsTy B = GetRegionBindings(store); |
| 1271 | |
| 1272 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 1273 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 1274 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 1275 | |
| 1276 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 1277 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 1278 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 1279 | // overhead). |
| 1280 | llvm::BumpPtrAllocator TmpAlloc; |
| 1281 | |
| 1282 | // Factory objects. |
| 1283 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 1284 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 1285 | |
| 1286 | // The backmap from regions to subregions. |
| 1287 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 1288 | |
| 1289 | // Do a pass over the regions in the store. For VarRegions we check if |
| 1290 | // the variable is still live and if so add it to the list of live roots. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 1291 | // For other regions we populate our region backmap. |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1292 | llvm::SmallVector<const MemRegion*, 10> IntermediateRoots; |
| 1293 | |
| 1294 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1295 | IntermediateRoots.push_back(I.getKey()); |
| 1296 | } |
| 1297 | |
| 1298 | while (!IntermediateRoots.empty()) { |
| 1299 | const MemRegion* R = IntermediateRoots.back(); |
| 1300 | IntermediateRoots.pop_back(); |
| 1301 | |
| 1302 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1303 | if (SymReaper.isLive(Loc, VR->getDecl())) { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1304 | RegionRoots.push_back(VR); // This is a live "root". |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1305 | } |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1306 | } |
| 1307 | else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) { |
| 1308 | if (SymReaper.isLive(SR->getSymbol())) |
| 1309 | RegionRoots.push_back(SR); |
| 1310 | } |
| 1311 | else { |
| 1312 | // Get the super region for R. |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 1313 | const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1314 | |
| 1315 | // Get the current set of subregions for SuperR. |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 1316 | const SubRegionsTy* SRptr = SubRegMap.lookup(superR); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1317 | SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 1318 | |
| 1319 | // Add R to the subregions of SuperR. |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 1320 | SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R)); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Super region may be VarRegion or subregion of another VarRegion. Add it |
| 1323 | // to the work list. |
Ted Kremenek | 8b2ba31 | 2009-07-01 23:30:34 +0000 | [diff] [blame] | 1324 | if (isa<SubRegion>(superR)) |
| 1325 | IntermediateRoots.push_back(superR); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 1330 | // of the store. We want to find all live symbols and dead regions. |
| 1331 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 1332 | |
| 1333 | while (!RegionRoots.empty()) { |
| 1334 | // Dequeue the next region on the worklist. |
| 1335 | const MemRegion* R = RegionRoots.back(); |
| 1336 | RegionRoots.pop_back(); |
| 1337 | |
| 1338 | // Check if we have already processed this region. |
| 1339 | if (Marked.count(R)) continue; |
| 1340 | |
| 1341 | // Mark this region as processed. This is needed for termination in case |
| 1342 | // a region is referenced more than once. |
| 1343 | Marked.insert(R); |
| 1344 | |
| 1345 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 1346 | // should continue to track that symbol. |
| 1347 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 1348 | SymReaper.markLive(SymR->getSymbol()); |
| 1349 | |
| 1350 | // Get the data binding for R (if any). |
| 1351 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 1352 | if (Xptr) { |
| 1353 | SVal X = *Xptr; |
| 1354 | UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols. |
| 1355 | |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1356 | // If X is a region, then add it to the RegionRoots. |
| 1357 | if (const MemRegion *RX = X.getAsRegion()) { |
| 1358 | RegionRoots.push_back(RX); |
| 1359 | |
| 1360 | // Mark the super region of the RX as live. |
| 1361 | // e.g.: int x; char *y = (char*) &x; if (*y) ... |
| 1362 | // 'y' => element region. 'x' is its super region. |
| 1363 | // We only add one level super region for now. |
Zhongxing Xu | 5ff8e8c | 2009-07-01 02:12:57 +0000 | [diff] [blame] | 1364 | // FIXME: maybe multiple level of super regions should be added. |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1365 | if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) { |
| 1366 | RegionRoots.push_back(SR->getSuperRegion()); |
| 1367 | } |
| 1368 | } |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | // Get the subregions of R. These are RegionRoots as well since they |
| 1372 | // represent values that are also bound to R. |
| 1373 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 1374 | if (!SRptr) continue; |
| 1375 | SubRegionsTy SR = *SRptr; |
| 1376 | |
| 1377 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 1378 | RegionRoots.push_back(*I); |
Zhongxing Xu | 7abe019 | 2009-06-30 12:32:59 +0000 | [diff] [blame] | 1379 | |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | // We have now scanned the store, marking reachable regions and symbols |
| 1383 | // as live. We now remove all the regions that are dead from the store |
| 1384 | // as well as update DSymbols with the set symbols that are now dead. |
| 1385 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1386 | const MemRegion* R = I.getKey(); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1387 | // If this region live? Is so, none of its symbols are dead. |
| 1388 | if (Marked.count(R)) |
| 1389 | continue; |
| 1390 | |
| 1391 | // Remove this dead region from the store. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1392 | store = Remove(store, ValMgr.makeLoc(R)); |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Mark all non-live symbols that this region references as dead. |
| 1395 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 1396 | SymReaper.maybeDead(SymR->getSymbol()); |
| 1397 | |
| 1398 | SVal X = I.getData(); |
| 1399 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 1400 | for (; SI != SE; ++SI) SymReaper.maybeDead(*SI); |
| 1401 | } |
| 1402 | |
| 1403 | return store; |
| 1404 | } |
| 1405 | |
| 1406 | //===----------------------------------------------------------------------===// |
| 1407 | // Utility methods. |
| 1408 | //===----------------------------------------------------------------------===// |
| 1409 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1410 | void RegionStoreManager::print(Store store, llvm::raw_ostream& OS, |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1411 | const char* nl, const char *sep) { |
Ted Kremenek | 9af46f5 | 2009-06-16 22:36:44 +0000 | [diff] [blame] | 1412 | RegionBindingsTy B = GetRegionBindings(store); |
| 1413 | OS << "Store:" << nl; |
| 1414 | |
| 1415 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1416 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 1417 | I.getData().print(OS); OS << nl; |
| 1418 | } |
| 1419 | } |