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" |
| 21 | |
| 22 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 29 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Region "Views" |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // |
| 36 | // MemRegions can be layered on top of each other. This GDM entry tracks |
| 37 | // what are the MemRegions that layer a given MemRegion. |
| 38 | // |
| 39 | typedef llvm::ImmutableList<const MemRegion*> RegionViews; |
| 40 | namespace { class VISIBILITY_HIDDEN RegionViewMap {}; } |
| 41 | static int RegionViewMapIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 42 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 43 | template<> struct GRStateTrait<RegionViewMap> |
| 44 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, |
| 45 | RegionViews> > { |
| 46 | |
| 47 | static void* GDMIndex() { return &RegionViewMapIndex; } |
| 48 | }; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 49 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Region "Extents" |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // |
| 55 | // MemRegions represent chunks of memory with a size (their "extent"). This |
| 56 | // GDM entry tracks the extents for regions. Extents are in bytes. |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 57 | // |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 58 | namespace { class VISIBILITY_HIDDEN RegionExtents {}; } |
| 59 | static int RegionExtentsIndex = 0; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 60 | namespace clang { |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 61 | template<> struct GRStateTrait<RegionExtents> |
| 62 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 63 | static void* GDMIndex() { return &RegionExtentsIndex; } |
| 64 | }; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Region "killsets". |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // |
| 71 | // RegionStore lazily adds value bindings to regions when the analyzer |
| 72 | // handles assignment statements. Killsets track which default values have |
| 73 | // been killed, thus distinguishing between "unknown" values and default |
| 74 | // values. |
| 75 | // |
| 76 | namespace { class VISIBILITY_HIDDEN RegionKills {}; } |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 77 | static int RegionKillsIndex = 0; |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 78 | namespace clang { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 79 | template<> struct GRStateTrait<RegionKills> |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 80 | : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > { |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 81 | static void* GDMIndex() { return &RegionKillsIndex; } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 85 | //===----------------------------------------------------------------------===// |
| 86 | // Regions with default values of '0'. |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // |
| 89 | // This GDM entry tracks what regions have a default value of 0 if they |
| 90 | // have no bound value and have not been killed. |
| 91 | // |
| 92 | namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; } |
| 93 | static int RegionDefaultValueIndex = 0; |
| 94 | namespace clang { |
| 95 | template<> struct GRStateTrait<RegionDefaultValue> |
| 96 | : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > { |
| 97 | static void* GDMIndex() { return &RegionDefaultValueIndex; } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // Main RegionStore logic. |
| 103 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 104 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 105 | namespace { |
| 106 | |
| 107 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 108 | RegionBindingsTy::Factory RBFactory; |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 109 | RegionViews::Factory RVFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 110 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 111 | GRStateManager& StateMgr; |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 112 | |
| 113 | public: |
| 114 | RegionStoreManager(GRStateManager& mgr) |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 115 | : StoreManager(mgr.getAllocator()), |
| 116 | RBFactory(mgr.getAllocator()), |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 117 | RVFactory(mgr.getAllocator()), |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 118 | StateMgr(mgr) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 119 | |
| 120 | virtual ~RegionStoreManager() {} |
| 121 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 122 | MemRegionManager& getRegionManager() { return MRMgr; } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 123 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 124 | const GRState* BindCompoundLiteral(const GRState* St, |
| 125 | const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 127 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 128 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 129 | /// associated StringRegion, and the lvalue of a StringLiteral is |
| 130 | /// the lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 131 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 132 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 133 | /// getLValueCompoundLiteral - Returns an SVal representing the |
| 134 | /// lvalue of a compound literal. Within RegionStore a compound |
| 135 | /// literal has an associated region, and the lvalue of the |
| 136 | /// compound literal is the lvalue of that region. |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 137 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 138 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 139 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 140 | /// variable. Within RegionStore a variable has an associated |
| 141 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 142 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 143 | |
| 144 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 145 | |
| 146 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 147 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 148 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 149 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 150 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 151 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 152 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 153 | /// type. 'Array' represents the lvalue of the array being decayed |
| 154 | /// to a pointer, and the returned SVal represents the decayed |
| 155 | /// version of that lvalue (i.e., a pointer to the first element of |
| 156 | /// the array). This is called by GRExprEngine when evaluating |
| 157 | /// casts from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 158 | SVal ArrayToPointer(SVal Array); |
| 159 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 160 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 161 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 162 | /// casted and 'CastToTy' the result type of the cast. |
| 163 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 164 | QualType CastToTy); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 165 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 166 | /// The high level logic for this method is this: |
| 167 | /// Retrieve (L) |
| 168 | /// if L has binding |
| 169 | /// return L's binding |
| 170 | /// else if L is in killset |
| 171 | /// return unknown |
| 172 | /// else |
| 173 | /// if L is on stack or heap |
| 174 | /// return undefined |
| 175 | /// else |
| 176 | /// return symbolic |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 177 | SVal Retrieve(const GRState* state, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 178 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 179 | const GRState* Bind(const GRState* St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 180 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 181 | Store Remove(Store store, Loc LV); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 182 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 183 | Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 184 | |
| 185 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 186 | /// 'this' object (C++). When used when analyzing a normal function this |
| 187 | /// method returns NULL. |
| 188 | const MemRegion* getSelfRegion(Store) { |
| 189 | assert (false && "Not implemented."); |
| 190 | return 0; |
| 191 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 193 | /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values. |
| 194 | /// It returns a new Store with these values removed, and populates LSymbols |
| 195 | // and DSymbols with the known set of live and dead symbols respectively. |
| 196 | Store RemoveDeadBindings(const GRState* state, Stmt* Loc, |
| 197 | const LiveVariables& Live, |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 198 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 199 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 201 | void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 202 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 203 | const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal); |
| 204 | |
| 205 | const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) { |
| 206 | return St; |
| 207 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 208 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 209 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 210 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 211 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 212 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 213 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 214 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 215 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 216 | |
| 217 | void iterBindings(Store store, BindingsHandler& f) { |
| 218 | // FIXME: Implement. |
| 219 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 220 | |
| 221 | private: |
| 222 | Loc getVarLoc(const VarDecl* VD) { |
| 223 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 224 | } |
| 225 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 226 | const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 227 | |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 228 | /// Retrieve the values in a struct and return a CompoundVal, used when doing |
| 229 | /// struct copy: |
| 230 | /// struct s x, y; |
| 231 | /// x = y; |
| 232 | /// y's value is retrieved by this method. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 233 | SVal RetrieveStruct(const GRState* St, const TypedRegion* R); |
Zhongxing Xu | 0b242ec | 2008-12-04 01:12:41 +0000 | [diff] [blame] | 234 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 235 | const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 236 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 237 | // Utility methods. |
| 238 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 239 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 240 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 241 | |
| 242 | const GRState* AddRegionView(const GRState* St, |
| 243 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | } // end anonymous namespace |
| 247 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 248 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 249 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 252 | |
| 253 | /// getLValueString - Returns an SVal representing the lvalue of a |
| 254 | /// StringLiteral. Within RegionStore a StringLiteral has an |
| 255 | /// associated StringRegion, and the lvalue of a StringLiteral is the |
| 256 | /// lvalue of that region. |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 257 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 258 | const StringLiteral* S) { |
| 259 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 260 | } |
| 261 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 262 | /// getLValueVar - Returns an SVal that represents the lvalue of a |
| 263 | /// variable. Within RegionStore a variable has an associated |
| 264 | /// VarRegion, and the lvalue of the variable is the lvalue of that region. |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 265 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 266 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 267 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 269 | /// getLValueCompoundLiteral - Returns an SVal representing the lvalue |
| 270 | /// of a compound literal. Within RegionStore a compound literal |
| 271 | /// has an associated region, and the lvalue of the compound literal |
| 272 | /// is the lvalue of that region. |
| 273 | SVal |
| 274 | RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 275 | const CompoundLiteralExpr* CL) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 276 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 277 | } |
| 278 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 279 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 280 | SVal Base) { |
| 281 | return UnknownVal(); |
| 282 | } |
| 283 | |
| 284 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 285 | const FieldDecl* D) { |
| 286 | if (Base.isUnknownOrUndef()) |
| 287 | return Base; |
| 288 | |
| 289 | Loc BaseL = cast<Loc>(Base); |
| 290 | const MemRegion* BaseR = 0; |
| 291 | |
| 292 | switch (BaseL.getSubKind()) { |
| 293 | case loc::MemRegionKind: |
| 294 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 295 | break; |
| 296 | |
| 297 | case loc::SymbolValKind: |
| 298 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 299 | break; |
| 300 | |
| 301 | case loc::GotoLabelKind: |
| 302 | case loc::FuncValKind: |
| 303 | // These are anormal cases. Flag an undefined value. |
| 304 | return UndefinedVal(); |
| 305 | |
| 306 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 307 | // While these seem funny, this can happen through casts. |
| 308 | // FIXME: What we should return is the field offset. For example, |
| 309 | // add the field offset to the integer value. That way funny things |
| 310 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 311 | return Base; |
| 312 | |
| 313 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 314 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 315 | return Base; |
| 316 | } |
| 317 | |
| 318 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 319 | } |
| 320 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 321 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 322 | SVal Base, SVal Offset) { |
| 323 | if (Base.isUnknownOrUndef()) |
| 324 | return Base; |
| 325 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 326 | if (isa<loc::SymbolVal>(Base)) |
| 327 | return Base; |
| 328 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 329 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 330 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 331 | // Pointer of any type can be cast and used as array base. We do not support |
| 332 | // that case yet. |
| 333 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 334 | // Record what we have seen in real code. |
| 335 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 336 | return UnknownVal(); |
| 337 | } |
| 338 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 339 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 340 | |
| 341 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 342 | |
| 343 | SVal Idx = ElemR->getIndex(); |
| 344 | |
| 345 | nonloc::ConcreteInt *CI1, *CI2; |
| 346 | |
| 347 | // Only handle integer indices for now. |
| 348 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 349 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 350 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 351 | // Temporary SVal to hold a potential signed and extended APSInt. |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 352 | SVal SignedInt; |
| 353 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 354 | // Index might be unsigned. We have to convert it to signed. It might also |
| 355 | // be less wide than the size. We have to extend it. |
| 356 | if (CI2->getValue().isUnsigned() || |
| 357 | CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 358 | llvm::APSInt SI = CI2->getValue(); |
Sebastian Redl | ddee68b | 2008-11-24 19:39:40 +0000 | [diff] [blame] | 359 | if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) |
| 360 | SI.extend(CI1->getValue().getBitWidth()); |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 361 | SI.setIsSigned(true); |
| 362 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 363 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 364 | } |
| 365 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 366 | SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 367 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 368 | ElemR->getArrayRegion())); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return UnknownVal(); |
| 372 | } |
| 373 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 374 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 375 | const MemRegion* R) { |
| 376 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 377 | // Get the type of the variable. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 378 | QualType T = VR->getRValueType(getContext()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 379 | |
| 380 | // It must be of array type. |
| 381 | const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 382 | |
| 383 | // return the size as signed integer. |
| 384 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 385 | } |
| 386 | |
| 387 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 388 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 389 | // We intentionally made the size value signed because it participates in |
| 390 | // operations with signed indices. |
Zhongxing Xu | 4b89e03 | 2008-11-24 05:16:01 +0000 | [diff] [blame] | 391 | return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) { |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 395 | GRStateRef state(St, StateMgr); |
| 396 | |
| 397 | // Get the size of the super region in bytes. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 398 | const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion()); |
| 399 | assert(Extent && "region extent not exist"); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 400 | |
| 401 | // Assume it's ConcreteInt for now. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 402 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 403 | |
| 404 | // Get the size of the element in bits. |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 405 | QualType LvT = ATR->getLValueType(getContext()); |
| 406 | QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType(); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 407 | |
| 408 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 409 | |
| 410 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 411 | false); |
| 412 | |
| 413 | // Calculate the number of elements. |
| 414 | |
| 415 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 416 | // signed? |
| 417 | if (SSize.isUnsigned()) |
| 418 | SSize.setIsSigned(true); |
| 419 | |
| 420 | // FIXME: move this operation into BasicVals. |
| 421 | const llvm::APSInt S = |
| 422 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 423 | |
| 424 | return NonLoc::MakeVal(getBasicVals(), S); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 428 | // FIXME: Unsupported yet. |
| 429 | FR = 0; |
| 430 | return UnknownVal(); |
| 431 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 432 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 433 | assert(0 && "Other regions are not supported yet."); |
Ted Kremenek | a21362d | 2009-01-06 19:12:06 +0000 | [diff] [blame] | 434 | return UnknownVal(); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 437 | /// ArrayToPointer - Emulates the "decay" of an array to a pointer |
| 438 | /// type. 'Array' represents the lvalue of the array being decayed |
| 439 | /// to a pointer, and the returned SVal represents the decayed |
| 440 | /// version of that lvalue (i.e., a pointer to the first element of |
| 441 | /// the array). This is called by GRExprEngine when evaluating casts |
| 442 | /// from arrays to pointers. |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 443 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 444 | // FIXME: This should be factored into GRExprEngine. This allows |
| 445 | // us to pass a "loc" instead of an "SVal" for "Array". |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 446 | if (Array.isUnknownOrUndef()) |
| 447 | return Array; |
| 448 | |
| 449 | if (!isa<loc::MemRegionVal>(Array)) |
| 450 | return UnknownVal(); |
| 451 | |
| 452 | const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 453 | const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R); |
| 454 | |
| 455 | if (ArrayR) |
| 456 | return UnknownVal(); |
| 457 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 458 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 459 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 460 | |
| 461 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 464 | StoreManager::CastResult |
| 465 | RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 466 | QualType CastToTy) { |
| 467 | |
| 468 | // Return the same region if the region types are compatible. |
| 469 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 470 | ASTContext& Ctx = StateMgr.getContext(); |
| 471 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 472 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 473 | |
| 474 | if (Ta == Tb) |
| 475 | return CastResult(state, R); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 476 | } |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 477 | |
| 478 | // FIXME: We should handle the case when we are casting *back* to a |
| 479 | // previous type. For example: |
| 480 | // |
| 481 | // void* x = ...; |
| 482 | // char* y = (char*) x; |
| 483 | // void* z = (void*) y; // <-- we should get the same region that is |
| 484 | // bound to 'x' |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 485 | const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R); |
| 486 | return CastResult(AddRegionView(state, ViewR, R), ViewR); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 489 | SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 490 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 491 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 492 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 493 | // FIXME: What does loc::SymbolVal represent? It represents the value |
| 494 | // of a location but that value is not known. In the future we should |
| 495 | // handle potential aliasing relationships; e.g. a loc::SymbolVal could |
| 496 | // be an alias for a particular region. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 497 | if (isa<loc::SymbolVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 498 | return UnknownVal(); |
| 499 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 500 | // FIXME: Is this even possible? Shouldn't this be treated as a null |
| 501 | // dereference at a higher level? |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 502 | if (isa<loc::ConcreteInt>(L)) |
| 503 | return UndefinedVal(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 504 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 505 | // FIXME: Should this be refactored into GRExprEngine or GRStateManager? |
| 506 | // It seems that all StoreManagers would do the same thing here. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 507 | if (isa<loc::FuncVal>(L)) |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 508 | return L; |
| 509 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 510 | // FIXME: Perhaps this method should just take a 'const MemRegion*' argument |
| 511 | // instead of 'Loc', and have the other Loc cases handled at a higher level. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 512 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 513 | assert(R && "bad region"); |
| 514 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 515 | // FIXME: We should eventually handle funny addressing. e.g.: |
| 516 | // |
| 517 | // int x = ...; |
| 518 | // int *p = &x; |
| 519 | // char *q = (char*) p; |
| 520 | // char c = *q; // returns the first byte of 'x'. |
| 521 | // |
| 522 | // Such funny addressing will occur due to layering of regions. |
| 523 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 524 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 525 | if (TR->getRValueType(getContext())->isStructureType()) |
| 526 | return RetrieveStruct(St, TR); |
| 527 | |
| 528 | RegionBindingsTy B = GetRegionBindings(St->getStore()); |
| 529 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 530 | |
| 531 | // Check if the region has a binding. |
| 532 | if (V) |
| 533 | return *V; |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 534 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 535 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 536 | |
| 537 | // FIXME: Do we even need a killset? If 'Unknown' is explicitly |
| 538 | // bound to to a region won't this be enough? (that's basically |
| 539 | // what a killset is). RemoveDeadBindings should only remove |
| 540 | // bindings that are no longer accessible, which means that won't |
| 541 | // ever be read. |
| 542 | |
| 543 | // Check if the region is in killset. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 544 | if (state.contains<RegionKills>(R)) |
| 545 | return UnknownVal(); |
| 546 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 547 | // The location does not have a bound value. This means that it has |
| 548 | // the value it had upon its creation and/or entry to the analyzed |
| 549 | // function/method. These are either symbolic values or 'undefined'. |
| 550 | |
| 551 | // We treat function parameters as symbolic values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 552 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) |
| 553 | if (isa<ParmVarDecl>(VR->getDecl())) |
| 554 | return SVal::MakeSymbolValue(getSymbolManager(), VR, |
| 555 | VR->getRValueType(getContext())); |
| 556 | |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 557 | if (MRMgr.onStack(R) || MRMgr.onHeap(R)) { |
| 558 | // All stack variables are considered to have undefined values |
| 559 | // upon creation. All heap allocated blocks are considered to |
| 560 | // have undefined values as well unless they are explicitly bound |
| 561 | // to specific values. |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 562 | return UndefinedVal(); |
Ted Kremenek | 869fb4a | 2008-12-24 07:46:32 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // All other values are symbolic. |
| 566 | return SVal::MakeSymbolValue(getSymbolManager(), R, |
| 567 | cast<TypedRegion>(R)->getRValueType(getContext())); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 568 | |
| 569 | // FIXME: consider default values for elements and fields. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 572 | SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){ |
| 573 | |
| 574 | Store store = St->getStore(); |
| 575 | GRStateRef state(St, StateMgr); |
| 576 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 577 | // FIXME: Verify we want getRValueType instead of getLValueType. |
| 578 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 579 | assert(T->isStructureType()); |
| 580 | |
| 581 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 582 | RecordDecl* RD = RT->getDecl(); |
| 583 | assert(RD->isDefinition()); |
| 584 | |
| 585 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 586 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 587 | std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 589 | for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(), |
| 590 | FieldEnd = Fields.rend(); |
| 591 | Field != FieldEnd; ++Field) { |
| 592 | FieldRegion* FR = MRMgr.getFieldRegion(*Field, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 593 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 594 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 595 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 596 | SVal FieldValue; |
| 597 | if (data) |
| 598 | FieldValue = *data; |
| 599 | else if (state.contains<RegionKills>(FR)) |
| 600 | FieldValue = UnknownVal(); |
| 601 | else { |
| 602 | if (MRMgr.onStack(FR) || MRMgr.onHeap(FR)) |
| 603 | FieldValue = UndefinedVal(); |
| 604 | else |
| 605 | FieldValue = SVal::MakeSymbolValue(getSymbolManager(), FR, |
| 606 | FR->getRValueType(getContext())); |
| 607 | } |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 608 | |
| 609 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 610 | } |
| 611 | |
| 612 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 613 | } |
| 614 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 615 | const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) { |
| 616 | // Currently we don't bind value to symbolic location. But if the logic is |
| 617 | // made clear, we might change this decision. |
| 618 | if (isa<loc::SymbolVal>(L)) |
| 619 | return St; |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 620 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 621 | // If we get here, the location should be a region. |
| 622 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 623 | assert(R); |
| 624 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 625 | // Check if the region is a struct region. |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 626 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 627 | // FIXME: Verify we want getRValueType(). |
| 628 | if (TR->getRValueType(getContext())->isStructureType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 629 | return BindStruct(St, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 630 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 631 | Store store = St->getStore(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 632 | RegionBindingsTy B = GetRegionBindings(store); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 633 | |
| 634 | if (V.isUnknown()) { |
| 635 | // Remove the binding. |
| 636 | store = RBFactory.Remove(B, R).getRoot(); |
| 637 | |
| 638 | // Add the region to the killset. |
| 639 | GRStateRef state(St, StateMgr); |
| 640 | St = state.add<RegionKills>(R); |
| 641 | } |
| 642 | else |
| 643 | store = RBFactory.Add(B, R, V).getRoot(); |
| 644 | |
| 645 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 648 | Store RegionStoreManager::Remove(Store store, Loc L) { |
| 649 | RegionBindingsTy B = GetRegionBindings(store); |
| 650 | |
| 651 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 652 | assert(R); |
| 653 | |
| 654 | return RBFactory.Remove(B, R).getRoot(); |
| 655 | } |
| 656 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 657 | const GRState* RegionStoreManager::BindDecl(const GRState* St, |
| 658 | const VarDecl* VD, SVal InitVal) { |
| 659 | // All static variables are treated as symbolic values. |
| 660 | if (VD->hasGlobalStorage()) |
| 661 | return St; |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 662 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 663 | // Process local variables. |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 664 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 665 | QualType T = VD->getType(); |
| 666 | |
| 667 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 668 | |
| 669 | if (Loc::IsLocType(T) || T->isIntegerType()) |
| 670 | return Bind(St, Loc::MakeVal(VR), InitVal); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 671 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 672 | else if (T->isArrayType()) |
| 673 | return BindArray(St, VR, InitVal); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 674 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 675 | else if (T->isStructureType()) |
| 676 | return BindStruct(St, VR, InitVal); |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 677 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 678 | // Other types of variable are not supported yet. |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 679 | return St; |
| 680 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 681 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 682 | // FIXME: this method should be merged into Bind(). |
| 683 | const GRState* |
| 684 | RegionStoreManager::BindCompoundLiteral(const GRState* St, |
| 685 | const CompoundLiteralExpr* CL, SVal V) { |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 686 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 687 | return Bind(St, loc::MemRegionVal(R), V); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 690 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 691 | const MemRegion* R, SVal Extent) { |
| 692 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 693 | return state.set<RegionExtents>(R, Extent); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 697 | void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) { |
| 698 | for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 699 | LSymbols.insert(*SI); |
| 700 | } |
| 701 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 702 | Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 703 | const LiveVariables& Live, |
| 704 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 705 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 706 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 707 | Store store = state->getStore(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 708 | RegionBindingsTy B = GetRegionBindings(store); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 709 | |
| 710 | // Lazily constructed backmap from MemRegions to SubRegions. |
| 711 | typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy; |
| 712 | typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy; |
| 713 | |
| 714 | // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have |
| 715 | // the ability to reuse memory. This way we can keep TmpAlloc around as |
| 716 | // an instance variable of RegionStoreManager (avoiding repeated malloc |
| 717 | // overhead). |
| 718 | llvm::BumpPtrAllocator TmpAlloc; |
| 719 | |
| 720 | // Factory objects. |
| 721 | SubRegionsMapTy::Factory SubRegMapF(TmpAlloc); |
| 722 | SubRegionsTy::Factory SubRegF(TmpAlloc); |
| 723 | |
| 724 | // The backmap from regions to subregions. |
| 725 | SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap(); |
| 726 | |
| 727 | // Do a pass over the regions in the store. For VarRegions we check if |
| 728 | // the variable is still live and if so add it to the list of live roots. |
| 729 | // For other regions we populate our region backmap. |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 730 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 731 | const MemRegion* R = I.getKey(); |
| 732 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 733 | if (Live.isLive(Loc, VR->getDecl())) |
| 734 | RegionRoots.push_back(VR); // This is a live "root". |
| 735 | } |
| 736 | else { |
| 737 | // Get the super region for R. |
| 738 | const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion(); |
| 739 | // Get the current set of subregions for SuperR. |
| 740 | const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR); |
| 741 | SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet(); |
| 742 | // Add R to the subregions of SuperR. |
| 743 | SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R)); |
| 744 | |
| 745 | // Finally, check if SuperR is a VarRegion. We need to do this |
| 746 | // to also mark SuperR as a root (as it may not have a value directly |
| 747 | // bound to it in the store). |
| 748 | if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) { |
| 749 | if (Live.isLive(Loc, VR->getDecl())) |
| 750 | RegionRoots.push_back(VR); // This is a live "root". |
| 751 | } |
| 752 | } |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 753 | } |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 754 | |
| 755 | // Process the worklist of RegionRoots. This performs a "mark-and-sweep" |
| 756 | // of the store. We want to find all live symbols and dead regions. |
| 757 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 758 | |
| 759 | while (!RegionRoots.empty()) { |
| 760 | // Dequeue the next region on the worklist. |
| 761 | const MemRegion* R = RegionRoots.back(); |
| 762 | RegionRoots.pop_back(); |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 763 | |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 764 | // Check if we have already processed this region. |
| 765 | if (Marked.count(R)) continue; |
| 766 | |
| 767 | // Mark this region as processed. This is needed for termination in case |
| 768 | // a region is referenced more than once. |
| 769 | Marked.insert(R); |
| 770 | |
| 771 | // Mark the symbol for any live SymbolicRegion as "live". This means we |
| 772 | // should continue to track that symbol. |
| 773 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 774 | LSymbols.insert(SymR->getSymbol()); |
| 775 | |
| 776 | // Get the data binding for R (if any). |
| 777 | RegionBindingsTy::data_type* Xptr = B.lookup(R); |
| 778 | if (Xptr) { |
| 779 | SVal X = *Xptr; |
| 780 | UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols. |
| 781 | |
| 782 | // If X is a region, then add it the RegionRoots. |
| 783 | if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X)) |
| 784 | RegionRoots.push_back(RegionX->getRegion()); |
| 785 | } |
| 786 | |
| 787 | // Get the subregions of R. These are RegionRoots as well since they |
| 788 | // represent values that are also bound to R. |
| 789 | const SubRegionsTy* SRptr = SubRegMap.lookup(R); |
| 790 | if (!SRptr) continue; |
| 791 | SubRegionsTy SR = *SRptr; |
| 792 | |
| 793 | for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I) |
| 794 | RegionRoots.push_back(*I); |
| 795 | } |
| 796 | |
| 797 | // We have now scanned the store, marking reachable regions and symbols |
| 798 | // as live. We now remove all the regions that are dead from the store |
| 799 | // as well as update DSymbols with the set symbols that are now dead. |
| 800 | |
| 801 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 802 | const MemRegion* R = I.getKey(); |
| 803 | |
| 804 | // If this region live? Is so, none of its symbols are dead. |
| 805 | if (Marked.count(R)) |
| 806 | continue; |
| 807 | |
| 808 | // Remove this dead region from the store. |
Zhongxing Xu | 9c9ca08 | 2008-12-16 02:36:30 +0000 | [diff] [blame] | 809 | store = Remove(store, Loc::MakeVal(R)); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 810 | |
| 811 | // Mark all non-live symbols that this region references as dead. |
| 812 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 813 | SymbolRef Sym = SymR->getSymbol(); |
Ted Kremenek | c48ea6e | 2008-12-04 02:08:27 +0000 | [diff] [blame] | 814 | if (!LSymbols.count(Sym)) DSymbols.insert(Sym); |
| 815 | } |
| 816 | |
| 817 | SVal X = I.getData(); |
| 818 | SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 819 | for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); } |
| 820 | } |
| 821 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 822 | return store; |
| 823 | } |
| 824 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 825 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 826 | const char* nl, const char *sep) { |
| 827 | llvm::raw_os_ostream OS(Out); |
| 828 | RegionBindingsTy B = GetRegionBindings(store); |
| 829 | OS << "Store:" << nl; |
| 830 | |
| 831 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 832 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 833 | I.getData().print(OS); OS << nl; |
| 834 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 835 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 836 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 837 | const GRState* RegionStoreManager::BindArray(const GRState* St, |
| 838 | const TypedRegion* R, SVal Init) { |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 839 | |
| 840 | // FIXME: Verify we should use getLValueType or getRValueType. |
Zhongxing Xu | 2ef9372 | 2008-12-14 03:14:52 +0000 | [diff] [blame] | 841 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 842 | assert(T->isArrayType()); |
| 843 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 844 | // When we are binding the whole array, it always has default value 0. |
| 845 | GRStateRef state(St, StateMgr); |
Zhongxing Xu | 1302016 | 2008-12-24 07:29:24 +0000 | [diff] [blame] | 846 | St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0, |
| 847 | false)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 848 | |
| 849 | Store store = St->getStore(); |
| 850 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 851 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 852 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 853 | llvm::APSInt Size(CAT->getSize(), false); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 854 | llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 855 | |
| 856 | // Check if the init expr is a StringLiteral. |
| 857 | if (isa<loc::MemRegionVal>(Init)) { |
| 858 | const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion(); |
| 859 | const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral(); |
| 860 | const char* str = S->getStrData(); |
| 861 | unsigned len = S->getByteLength(); |
| 862 | unsigned j = 0; |
| 863 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 864 | // Copy bytes from the string literal into the target array. Trailing bytes |
| 865 | // in the array that are not covered by the string literal are initialized |
| 866 | // to zero. |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 867 | for (; i < Size; ++i, ++j) { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 868 | if (j >= len) |
| 869 | break; |
| 870 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 871 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
| 872 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 873 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 874 | SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); |
| 875 | St = Bind(St, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 878 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 881 | |
| 882 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 883 | |
| 884 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 885 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 886 | for (; i < Size; ++i, ++VI) { |
| 887 | // The init list might be shorter than the array decl. |
| 888 | if (VI == VE) |
| 889 | break; |
| 890 | |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 891 | SVal Idx = NonLoc::MakeVal(getBasicVals(), i); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 892 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 893 | |
| 894 | if (CAT->getElementType()->isStructureType()) |
| 895 | St = BindStruct(St, ER, *VI); |
| 896 | else |
| 897 | St = Bind(St, Loc::MakeVal(ER), *VI); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 900 | return StateMgr.MakeStateWithStore(St, store); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 903 | const GRState* |
| 904 | RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){ |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 905 | // FIXME: Verify that we should use getRValueType or getLValueType. |
| 906 | QualType T = R->getRValueType(getContext()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 907 | assert(T->isStructureType()); |
| 908 | |
| 909 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 910 | RecordDecl* RD = RT->getDecl(); |
| 911 | assert(RD->isDefinition()); |
| 912 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 913 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 914 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 915 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 916 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 917 | for (; FI != FE; ++FI, ++VI) { |
| 918 | |
| 919 | // There may be fewer values than fields only when we are initializing a |
| 920 | // struct decl. In this case, mark the region as having default value. |
| 921 | if (VI == VE) { |
Zhongxing Xu | 1302016 | 2008-12-24 07:29:24 +0000 | [diff] [blame] | 922 | GRStateRef state(St, StateMgr); |
| 923 | St = state.set<RegionDefaultValue>(R, NonLoc::MakeVal(getBasicVals(), 0, |
| 924 | false)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 928 | QualType FTy = (*FI)->getType(); |
| 929 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 930 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 931 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) |
| 932 | St = Bind(St, Loc::MakeVal(FR), *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 933 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 934 | else if (FTy->isArrayType()) |
| 935 | St = BindArray(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 936 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 937 | else if (FTy->isStructureType()) |
| 938 | St = BindStruct(St, FR, *VI); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 941 | return St; |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 944 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 945 | const MemRegion* View, |
| 946 | const MemRegion* Base) { |
| 947 | GRStateRef state(St, StateMgr); |
| 948 | |
| 949 | // First, retrieve the region view of the base region. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 950 | const RegionViews* d = state.get<RegionViewMap>(Base); |
| 951 | RegionViews L = d ? *d : RVFactory.GetEmptyList(); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 952 | |
| 953 | // Now add View to the region view. |
| 954 | L = RVFactory.Add(View, L); |
| 955 | |
| 956 | // Create a new state with the new region view. |
Ted Kremenek | 50dc1b3 | 2008-12-24 01:05:03 +0000 | [diff] [blame] | 957 | return state.set<RegionViewMap>(Base, L); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 958 | } |