Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1 | //== MemRegion.cpp - Abstract memory regions for static analysis --*- 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 MemRegion and its subclasses. MemRegion defines a |
| 11 | // partially-typed abstraction of memory useful for path-sensitive dataflow |
| 12 | // analyses. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/MemRegion.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/AnalysisContext.h" |
| 18 | #include "clang/Analysis/Support/BumpVector.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bcd7f9f | 2009-12-04 00:05:57 +0000 | [diff] [blame] | 25 | // MemRegion Construction. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | template<typename RegionTy> struct MemRegionManagerTrait; |
| 29 | |
| 30 | template <typename RegionTy, typename A1> |
| 31 | RegionTy* MemRegionManager::getRegion(const A1 a1) { |
| 32 | |
| 33 | const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion = |
| 34 | MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1); |
| 35 | |
| 36 | llvm::FoldingSetNodeID ID; |
| 37 | RegionTy::ProfileRegion(ID, a1, superRegion); |
| 38 | void* InsertPos; |
| 39 | RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, |
| 40 | InsertPos)); |
| 41 | |
| 42 | if (!R) { |
| 43 | R = (RegionTy*) A.Allocate<RegionTy>(); |
| 44 | new (R) RegionTy(a1, superRegion); |
| 45 | Regions.InsertNode(R, InsertPos); |
| 46 | } |
| 47 | |
| 48 | return R; |
| 49 | } |
| 50 | |
| 51 | template <typename RegionTy, typename A1> |
| 52 | RegionTy* MemRegionManager::getSubRegion(const A1 a1, |
| 53 | const MemRegion *superRegion) { |
| 54 | llvm::FoldingSetNodeID ID; |
| 55 | RegionTy::ProfileRegion(ID, a1, superRegion); |
| 56 | void* InsertPos; |
| 57 | RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, |
| 58 | InsertPos)); |
| 59 | |
| 60 | if (!R) { |
| 61 | R = (RegionTy*) A.Allocate<RegionTy>(); |
| 62 | new (R) RegionTy(a1, superRegion); |
| 63 | Regions.InsertNode(R, InsertPos); |
| 64 | } |
| 65 | |
| 66 | return R; |
| 67 | } |
| 68 | |
| 69 | template <typename RegionTy, typename A1, typename A2> |
| 70 | RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) { |
| 71 | |
| 72 | const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion = |
| 73 | MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2); |
| 74 | |
| 75 | llvm::FoldingSetNodeID ID; |
| 76 | RegionTy::ProfileRegion(ID, a1, a2, superRegion); |
| 77 | void* InsertPos; |
| 78 | RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, |
| 79 | InsertPos)); |
| 80 | |
| 81 | if (!R) { |
| 82 | R = (RegionTy*) A.Allocate<RegionTy>(); |
| 83 | new (R) RegionTy(a1, a2, superRegion); |
| 84 | Regions.InsertNode(R, InsertPos); |
| 85 | } |
| 86 | |
| 87 | return R; |
| 88 | } |
| 89 | |
| 90 | template <typename RegionTy, typename A1, typename A2> |
| 91 | RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, |
| 92 | const MemRegion *superRegion) { |
| 93 | |
| 94 | llvm::FoldingSetNodeID ID; |
| 95 | RegionTy::ProfileRegion(ID, a1, a2, superRegion); |
| 96 | void* InsertPos; |
| 97 | RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, |
| 98 | InsertPos)); |
| 99 | |
| 100 | if (!R) { |
| 101 | R = (RegionTy*) A.Allocate<RegionTy>(); |
| 102 | new (R) RegionTy(a1, a2, superRegion); |
| 103 | Regions.InsertNode(R, InsertPos); |
| 104 | } |
| 105 | |
| 106 | return R; |
| 107 | } |
| 108 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 109 | template <typename RegionTy, typename A1, typename A2, typename A3> |
| 110 | RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3, |
| 111 | const MemRegion *superRegion) { |
| 112 | |
| 113 | llvm::FoldingSetNodeID ID; |
| 114 | RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion); |
| 115 | void* InsertPos; |
| 116 | RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, |
| 117 | InsertPos)); |
| 118 | |
| 119 | if (!R) { |
| 120 | R = (RegionTy*) A.Allocate<RegionTy>(); |
| 121 | new (R) RegionTy(a1, a2, a3, superRegion); |
| 122 | Regions.InsertNode(R, InsertPos); |
Ted Kremenek | bcd7f9f | 2009-12-04 00:05:57 +0000 | [diff] [blame] | 123 | } |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 124 | |
| 125 | return R; |
| 126 | } |
Ted Kremenek | bcd7f9f | 2009-12-04 00:05:57 +0000 | [diff] [blame] | 127 | |
| 128 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 129 | // Object destruction. |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 131 | |
| 132 | MemRegion::~MemRegion() {} |
| 133 | |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 134 | MemRegionManager::~MemRegionManager() { |
| 135 | // All regions and their data are BumpPtrAllocated. No need to call |
| 136 | // their destructors. |
| 137 | } |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | // Basic methods. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Zhongxing Xu | 7e5d6ed | 2009-01-08 13:17:14 +0000 | [diff] [blame] | 143 | bool SubRegion::isSubRegionOf(const MemRegion* R) const { |
| 144 | const MemRegion* r = getSuperRegion(); |
| 145 | while (r != 0) { |
| 146 | if (r == R) |
| 147 | return true; |
| 148 | if (const SubRegion* sr = dyn_cast<SubRegion>(r)) |
| 149 | r = sr->getSuperRegion(); |
| 150 | else |
| 151 | break; |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
Ted Kremenek | a43484a | 2009-06-23 00:46:41 +0000 | [diff] [blame] | 156 | MemRegionManager* SubRegion::getMemRegionManager() const { |
| 157 | const SubRegion* r = this; |
| 158 | do { |
| 159 | const MemRegion *superRegion = r->getSuperRegion(); |
| 160 | if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) { |
| 161 | r = sr; |
| 162 | continue; |
| 163 | } |
| 164 | return superRegion->getMemRegionManager(); |
| 165 | } while (1); |
| 166 | } |
| 167 | |
Ted Kremenek | 5348f94 | 2009-12-14 22:15:06 +0000 | [diff] [blame] | 168 | const StackFrameContext *VarRegion::getStackFrame() const { |
| 169 | const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace()); |
| 170 | return SSR ? SSR->getStackFrame() : NULL; |
| 171 | } |
| 172 | |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | // FoldingSet profiling. |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 177 | void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 178 | ID.AddInteger((unsigned)getKind()); |
| 179 | } |
| 180 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 181 | void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
| 182 | ID.AddInteger((unsigned)getKind()); |
| 183 | ID.AddPointer(getStackFrame()); |
| 184 | } |
| 185 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 187 | const StringLiteral* Str, |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 188 | const MemRegion* superRegion) { |
| 189 | ID.AddInteger((unsigned) StringRegionKind); |
| 190 | ID.AddPointer(Str); |
| 191 | ID.AddPointer(superRegion); |
| 192 | } |
| 193 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 194 | void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
Ted Kremenek | 7ae7ad9 | 2009-06-23 00:15:41 +0000 | [diff] [blame] | 195 | const Expr* Ex, unsigned cnt, |
| 196 | const MemRegion *) { |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 197 | ID.AddInteger((unsigned) AllocaRegionKind); |
| 198 | ID.AddPointer(Ex); |
| 199 | ID.AddInteger(cnt); |
| 200 | } |
| 201 | |
| 202 | void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 7ae7ad9 | 2009-06-23 00:15:41 +0000 | [diff] [blame] | 203 | ProfileRegion(ID, Ex, Cnt, superRegion); |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 206 | void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 207 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 208 | } |
| 209 | |
| 210 | void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 211 | const CompoundLiteralExpr* CL, |
| 212 | const MemRegion* superRegion) { |
| 213 | ID.AddInteger((unsigned) CompoundLiteralRegionKind); |
| 214 | ID.AddPointer(CL); |
| 215 | ID.AddPointer(superRegion); |
| 216 | } |
| 217 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 218 | void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
| 219 | const PointerType *PT, |
| 220 | const MemRegion *sRegion) { |
| 221 | ID.AddInteger((unsigned) CXXThisRegionKind); |
| 222 | ID.AddPointer(PT); |
| 223 | ID.AddPointer(sRegion); |
| 224 | } |
| 225 | |
| 226 | void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
| 227 | CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion); |
| 228 | } |
| 229 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 230 | void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D, |
| 231 | const MemRegion* superRegion, Kind k) { |
| 232 | ID.AddInteger((unsigned) k); |
| 233 | ID.AddPointer(D); |
| 234 | ID.AddPointer(superRegion); |
| 235 | } |
| 236 | |
| 237 | void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 238 | DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); |
| 239 | } |
| 240 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 241 | void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 242 | VarRegion::ProfileRegion(ID, getDecl(), superRegion); |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 245 | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym, |
| 246 | const MemRegion *sreg) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 247 | ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); |
Ted Kremenek | 6d0e2d2 | 2008-12-05 02:39:38 +0000 | [diff] [blame] | 248 | ID.Add(sym); |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 249 | ID.AddPointer(sreg); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 253 | SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion()); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 256 | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | QualType ElementType, SVal Idx, |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 258 | const MemRegion* superRegion) { |
| 259 | ID.AddInteger(MemRegion::ElementRegionKind); |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 260 | ID.Add(ElementType); |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 261 | ID.AddPointer(superRegion); |
| 262 | Idx.Profile(ID); |
| 263 | } |
| 264 | |
| 265 | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 266 | ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion); |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 267 | } |
Zhongxing Xu | 27b5706 | 2008-10-27 13:17:02 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 269 | void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 270 | const FunctionDecl *FD, |
| 271 | const MemRegion*) { |
| 272 | ID.AddInteger(MemRegion::FunctionTextRegionKind); |
Ted Kremenek | abd46e1 | 2009-08-28 04:49:15 +0000 | [diff] [blame] | 273 | ID.AddPointer(FD); |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 276 | void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 277 | FunctionTextRegion::ProfileRegion(ID, FD, superRegion); |
| 278 | } |
| 279 | |
| 280 | void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 281 | const BlockDecl *BD, CanQualType, |
| 282 | const AnalysisContext *AC, |
| 283 | const MemRegion*) { |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 284 | ID.AddInteger(MemRegion::BlockTextRegionKind); |
| 285 | ID.AddPointer(BD); |
| 286 | } |
| 287 | |
| 288 | void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 289 | BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion); |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 292 | void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 293 | const BlockTextRegion *BC, |
| 294 | const LocationContext *LC, |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 295 | const MemRegion *sReg) { |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 296 | ID.AddInteger(MemRegion::BlockDataRegionKind); |
| 297 | ID.AddPointer(BC); |
| 298 | ID.AddPointer(LC); |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 299 | ID.AddPointer(sReg); |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 303 | BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion()); |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 306 | void CXXObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 307 | Expr const *Ex, |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 308 | const MemRegion *sReg) { |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 309 | ID.AddPointer(Ex); |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 310 | ID.AddPointer(sReg); |
| 311 | } |
| 312 | |
| 313 | void CXXObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 314 | ProfileRegion(ID, Ex, getSuperRegion()); |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 317 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 318 | // Region pretty-printing. |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 321 | void MemRegion::dump() const { |
| 322 | dumpToStream(llvm::errs()); |
Ted Kremenek | 7f39d29 | 2009-07-02 17:24:10 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 325 | std::string MemRegion::getString() const { |
| 326 | std::string s; |
| 327 | llvm::raw_string_ostream os(s); |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 328 | dumpToStream(os); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 329 | return os.str(); |
| 330 | } |
| 331 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 332 | void MemRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 333 | os << "<Unknown Region>"; |
| 334 | } |
| 335 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 336 | void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 337 | os << "alloca{" << (void*) Ex << ',' << Cnt << '}'; |
| 338 | } |
| 339 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 340 | void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | abd46e1 | 2009-08-28 04:49:15 +0000 | [diff] [blame] | 341 | os << "code{" << getDecl()->getDeclName().getAsString() << '}'; |
Ted Kremenek | 72e0320 | 2009-04-21 19:56:58 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 344 | void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 345 | os << "block_code{" << (void*) this << '}'; |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 348 | void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const { |
| 349 | os << "block_data{" << BC << '}'; |
| 350 | } |
| 351 | |
| 352 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 353 | void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 5639a3e | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 354 | // FIXME: More elaborate pretty-printing. |
| 355 | os << "{ " << (void*) CL << " }"; |
| 356 | } |
| 357 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 358 | void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const { |
| 359 | os << "this"; |
| 360 | } |
| 361 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 362 | void ElementRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 363 | os << "element{" << superRegion << ',' |
| 364 | << Index << ',' << getElementType().getAsString() << '}'; |
Ted Kremenek | 5639a3e | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 367 | void FieldRegion::dumpToStream(llvm::raw_ostream& os) const { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 368 | os << superRegion << "->" << getDecl(); |
Ted Kremenek | 5639a3e | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Ted Kremenek | bcfe03a | 2009-07-19 20:36:24 +0000 | [diff] [blame] | 371 | void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 372 | os << "ivar{" << superRegion << ',' << getDecl() << '}'; |
Ted Kremenek | bcfe03a | 2009-07-19 20:36:24 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | void StringRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | b27ed3d | 2009-07-19 20:38:24 +0000 | [diff] [blame] | 376 | Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions())); |
Ted Kremenek | 5639a3e | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 379 | void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const { |
Ted Kremenek | aef5d22 | 2009-07-13 23:38:57 +0000 | [diff] [blame] | 380 | os << "SymRegion{" << sym << '}'; |
Ted Kremenek | 5639a3e | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Ted Kremenek | 8800ad4 | 2009-07-13 23:31:04 +0000 | [diff] [blame] | 383 | void VarRegion::dumpToStream(llvm::raw_ostream& os) const { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 384 | os << cast<VarDecl>(D); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 387 | void RegionRawOffset::dump() const { |
| 388 | dumpToStream(llvm::errs()); |
| 389 | } |
| 390 | |
| 391 | void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const { |
| 392 | os << "raw_offset{" << getRegion() << ',' << getByteOffset() << '}'; |
| 393 | } |
| 394 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 395 | //===----------------------------------------------------------------------===// |
| 396 | // MemRegionManager methods. |
| 397 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 399 | template <typename REG> |
| 400 | const REG *MemRegionManager::LazyAllocate(REG*& region) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | if (!region) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 402 | region = (REG*) A.Allocate<REG>(); |
| 403 | new (region) REG(this); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | a43484a | 2009-06-23 00:46:41 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 406 | return region; |
| 407 | } |
| 408 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 409 | template <typename REG, typename ARG> |
| 410 | const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) { |
| 411 | if (!region) { |
| 412 | region = (REG*) A.Allocate<REG>(); |
| 413 | new (region) REG(this, a); |
| 414 | } |
| 415 | |
| 416 | return region; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 419 | const StackLocalsSpaceRegion* |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 420 | MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) { |
| 421 | assert(STC); |
Zhongxing Xu | c30470d | 2010-02-17 08:46:50 +0000 | [diff] [blame] | 422 | StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC]; |
| 423 | |
| 424 | if (R) |
| 425 | return R; |
| 426 | |
| 427 | R = A.Allocate<StackLocalsSpaceRegion>(); |
| 428 | new (R) StackLocalsSpaceRegion(this, STC); |
| 429 | return R; |
Ted Kremenek | d05552a | 2009-07-02 18:14:59 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 432 | const StackArgumentsSpaceRegion * |
| 433 | MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) { |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 434 | assert(STC); |
Zhongxing Xu | c30470d | 2010-02-17 08:46:50 +0000 | [diff] [blame] | 435 | StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC]; |
| 436 | |
| 437 | if (R) |
| 438 | return R; |
| 439 | |
| 440 | R = A.Allocate<StackArgumentsSpaceRegion>(); |
| 441 | new (R) StackArgumentsSpaceRegion(this, STC); |
| 442 | return R; |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | const GlobalsSpaceRegion *MemRegionManager::getGlobalsRegion() { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 446 | return LazyAllocate(globals); |
| 447 | } |
| 448 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 449 | const HeapSpaceRegion *MemRegionManager::getHeapRegion() { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 450 | return LazyAllocate(heap); |
| 451 | } |
| 452 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 453 | const MemSpaceRegion *MemRegionManager::getUnknownRegion() { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 454 | return LazyAllocate(unknown); |
| 455 | } |
| 456 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 457 | const MemSpaceRegion *MemRegionManager::getCodeRegion() { |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 458 | return LazyAllocate(code); |
| 459 | } |
| 460 | |
Ted Kremenek | 2501013 | 2009-06-22 23:13:13 +0000 | [diff] [blame] | 461 | //===----------------------------------------------------------------------===// |
| 462 | // Constructing regions. |
| 463 | //===----------------------------------------------------------------------===// |
| 464 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 465 | const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 466 | return getSubRegion<StringRegion>(Str, getGlobalsRegion()); |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 469 | const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D, |
| 470 | const LocationContext *LC) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 471 | const MemRegion *sReg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 473 | if (D->hasLocalStorage()) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 474 | // FIXME: Once we implement scope handling, we will need to properly lookup |
| 475 | // 'D' to the proper LocationContext. |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 476 | const DeclContext *DC = D->getDeclContext(); |
| 477 | const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | 2b87ae4 | 2009-12-11 06:43:27 +0000 | [diff] [blame] | 479 | if (!STC) |
| 480 | sReg = getUnknownRegion(); |
| 481 | else { |
| 482 | sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) |
| 483 | ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC)) |
| 484 | : static_cast<const MemRegion*>(getStackLocalsRegion(STC)); |
| 485 | } |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 486 | } |
| 487 | else { |
| 488 | sReg = getGlobalsRegion(); |
| 489 | } |
| 490 | |
| 491 | return getSubRegion<VarRegion>(D, sReg); |
| 492 | } |
| 493 | |
| 494 | const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D, |
| 495 | const MemRegion *superR) { |
| 496 | return getSubRegion<VarRegion>(D, superR); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 499 | const BlockDataRegion * |
| 500 | MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC, |
| 501 | const LocationContext *LC) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 502 | const MemRegion *sReg = 0; |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 504 | if (LC) { |
| 505 | // FIXME: Once we implement scope handling, we want the parent region |
| 506 | // to be the scope. |
| 507 | const StackFrameContext *STC = LC->getCurrentStackFrame(); |
| 508 | assert(STC); |
| 509 | sReg = getStackLocalsRegion(STC); |
| 510 | } |
| 511 | else { |
| 512 | // We allow 'LC' to be NULL for cases where want BlockDataRegions |
| 513 | // without context-sensitivity. |
| 514 | sReg = getUnknownRegion(); |
| 515 | } |
| 516 | |
| 517 | return getSubRegion<BlockDataRegion>(BC, LC, sReg); |
Ted Kremenek | 0a8112a | 2009-11-25 23:53:07 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 520 | const CompoundLiteralRegion* |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 521 | MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL, |
| 522 | const LocationContext *LC) { |
| 523 | |
| 524 | const MemRegion *sReg = 0; |
| 525 | |
| 526 | if (CL->isFileScope()) |
| 527 | sReg = getGlobalsRegion(); |
| 528 | else { |
| 529 | const StackFrameContext *STC = LC->getCurrentStackFrame(); |
| 530 | assert(STC); |
| 531 | sReg = getStackLocalsRegion(STC); |
| 532 | } |
| 533 | |
| 534 | return getSubRegion<CompoundLiteralRegion>(CL, sReg); |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 537 | const ElementRegion* |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 538 | MemRegionManager::getElementRegion(QualType elementType, SVal Idx, |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 539 | const MemRegion* superRegion, |
| 540 | ASTContext& Ctx){ |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 541 | |
Ted Kremenek | 32f9010 | 2010-05-27 00:29:00 +0000 | [diff] [blame] | 542 | QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType(); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 543 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 544 | llvm::FoldingSetNodeID ID; |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 545 | ElementRegion::ProfileRegion(ID, T, Idx, superRegion); |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 546 | |
| 547 | void* InsertPos; |
| 548 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 549 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 550 | |
| 551 | if (!R) { |
| 552 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 553 | new (R) ElementRegion(T, Idx, superRegion); |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 554 | Regions.InsertNode(R, InsertPos); |
| 555 | } |
| 556 | |
| 557 | return R; |
| 558 | } |
| 559 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 560 | const FunctionTextRegion * |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 561 | MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 562 | return getSubRegion<FunctionTextRegion>(FD, getCodeRegion()); |
Zhongxing Xu | ec13d92 | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 565 | const BlockTextRegion * |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 566 | MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy, |
| 567 | AnalysisContext *AC) { |
| 568 | return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion()); |
Ted Kremenek | eb1c7a0 | 2009-11-25 01:32:22 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 572 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 573 | const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 574 | return getSubRegion<SymbolicRegion>(sym, getUnknownRegion()); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 577 | const FieldRegion* |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 578 | MemRegionManager::getFieldRegion(const FieldDecl* d, |
| 579 | const MemRegion* superRegion){ |
Ted Kremenek | eeea456 | 2009-07-10 16:51:45 +0000 | [diff] [blame] | 580 | return getSubRegion<FieldRegion>(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 583 | const ObjCIvarRegion* |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 584 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 585 | const MemRegion* superRegion) { |
Ted Kremenek | eeea456 | 2009-07-10 16:51:45 +0000 | [diff] [blame] | 586 | return getSubRegion<ObjCIvarRegion>(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 589 | const CXXObjectRegion* |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 590 | MemRegionManager::getCXXObjectRegion(Expr const *E, |
| 591 | LocationContext const *LC) { |
| 592 | const StackFrameContext *SFC = LC->getCurrentStackFrame(); |
| 593 | assert(SFC); |
| 594 | return getSubRegion<CXXObjectRegion>(E, getStackLocalsRegion(SFC)); |
Zhongxing Xu | bb14121 | 2009-12-16 11:27:52 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 597 | const CXXThisRegion* |
| 598 | MemRegionManager::getCXXThisRegion(QualType thisPointerTy, |
| 599 | const LocationContext *LC) { |
| 600 | const StackFrameContext *STC = LC->getCurrentStackFrame(); |
| 601 | assert(STC); |
| 602 | const PointerType *PT = thisPointerTy->getAs<PointerType>(); |
| 603 | assert(PT); |
| 604 | return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC)); |
| 605 | } |
| 606 | |
Ted Kremenek | b48ad64 | 2009-12-04 00:26:31 +0000 | [diff] [blame] | 607 | const AllocaRegion* |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 608 | MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt, |
| 609 | const LocationContext *LC) { |
| 610 | const StackFrameContext *STC = LC->getCurrentStackFrame(); |
| 611 | assert(STC); |
| 612 | return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC)); |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Ted Kremenek | bb7c96f | 2009-06-23 18:17:08 +0000 | [diff] [blame] | 615 | const MemSpaceRegion *MemRegion::getMemorySpace() const { |
| 616 | const MemRegion *R = this; |
Ted Kremenek | ea20cd7 | 2009-06-23 18:05:21 +0000 | [diff] [blame] | 617 | const SubRegion* SR = dyn_cast<SubRegion>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 619 | while (SR) { |
Ted Kremenek | bb7c96f | 2009-06-23 18:17:08 +0000 | [diff] [blame] | 620 | R = SR->getSuperRegion(); |
| 621 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Ted Kremenek | bb7c96f | 2009-06-23 18:17:08 +0000 | [diff] [blame] | 624 | return dyn_cast<MemSpaceRegion>(R); |
| 625 | } |
| 626 | |
| 627 | bool MemRegion::hasStackStorage() const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 628 | return isa<StackSpaceRegion>(getMemorySpace()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 629 | } |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 630 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 631 | bool MemRegion::hasStackNonParametersStorage() const { |
| 632 | return isa<StackLocalsSpaceRegion>(getMemorySpace()); |
Zhongxing Xu | dd198f0 | 2009-06-23 02:51:21 +0000 | [diff] [blame] | 633 | } |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | de0d263 | 2010-01-05 02:18:06 +0000 | [diff] [blame] | 635 | bool MemRegion::hasStackParametersStorage() const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 636 | return isa<StackArgumentsSpaceRegion>(getMemorySpace()); |
Ted Kremenek | dc14726 | 2009-07-02 22:02:15 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Ted Kremenek | 1508636 | 2009-07-02 18:25:09 +0000 | [diff] [blame] | 639 | bool MemRegion::hasGlobalsOrParametersStorage() const { |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 640 | const MemSpaceRegion *MS = getMemorySpace(); |
| 641 | return isa<StackArgumentsSpaceRegion>(MS) || |
| 642 | isa<GlobalsSpaceRegion>(MS); |
Ted Kremenek | 1508636 | 2009-07-02 18:25:09 +0000 | [diff] [blame] | 643 | } |
Ted Kremenek | bb7c96f | 2009-06-23 18:17:08 +0000 | [diff] [blame] | 644 | |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 645 | // getBaseRegion strips away all elements and fields, and get the base region |
| 646 | // of them. |
| 647 | const MemRegion *MemRegion::getBaseRegion() const { |
| 648 | const MemRegion *R = this; |
| 649 | while (true) { |
Ted Kremenek | 68b9a59 | 2010-04-06 22:06:03 +0000 | [diff] [blame] | 650 | switch (R->getKind()) { |
| 651 | case MemRegion::ElementRegionKind: |
| 652 | case MemRegion::FieldRegionKind: |
| 653 | case MemRegion::ObjCIvarRegionKind: |
| 654 | R = cast<SubRegion>(R)->getSuperRegion(); |
| 655 | continue; |
| 656 | default: |
| 657 | break; |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 658 | } |
| 659 | break; |
| 660 | } |
| 661 | return R; |
| 662 | } |
| 663 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
| 665 | // View handling. |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
Zhongxing Xu | 479529e | 2009-11-10 02:17:20 +0000 | [diff] [blame] | 668 | const MemRegion *MemRegion::StripCasts() const { |
Ted Kremenek | 0e3ec3f | 2009-07-29 18:14:27 +0000 | [diff] [blame] | 669 | const MemRegion *R = this; |
| 670 | while (true) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Ted Kremenek | 0e3ec3f | 2009-07-29 18:14:27 +0000 | [diff] [blame] | 672 | // FIXME: generalize. Essentially we want to strip away ElementRegions |
| 673 | // that were layered on a symbolic region because of casts. We only |
| 674 | // want to strip away ElementRegions, however, where the index is 0. |
| 675 | SVal index = ER->getIndex(); |
| 676 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 677 | if (CI->getValue().getSExtValue() == 0) { |
Ted Kremenek | 0e3ec3f | 2009-07-29 18:14:27 +0000 | [diff] [blame] | 678 | R = ER->getSuperRegion(); |
| 679 | continue; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | break; |
| 684 | } |
| 685 | return R; |
| 686 | } |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 687 | |
| 688 | // FIXME: Merge with the implementation of the same method in Store.cpp |
| 689 | static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { |
| 690 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 691 | const RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 692 | if (!D->getDefinition()) |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 693 | return false; |
| 694 | } |
| 695 | |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | RegionRawOffset ElementRegion::getAsRawOffset() const { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 700 | CharUnits offset = CharUnits::Zero(); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 701 | const ElementRegion *ER = this; |
| 702 | const MemRegion *superR = NULL; |
| 703 | ASTContext &C = getContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 705 | // FIXME: Handle multi-dimensional arrays. |
| 706 | |
| 707 | while (ER) { |
| 708 | superR = ER->getSuperRegion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 710 | // FIXME: generalize to symbolic offsets. |
| 711 | SVal index = ER->getIndex(); |
| 712 | if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { |
| 713 | // Update the offset. |
| 714 | int64_t i = CI->getValue().getSExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 716 | if (i != 0) { |
| 717 | QualType elemType = ER->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 719 | // If we are pointing to an incomplete type, go no further. |
| 720 | if (!IsCompleteType(C, elemType)) { |
| 721 | superR = ER; |
| 722 | break; |
| 723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 725 | CharUnits size = C.getTypeSizeInChars(elemType); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 726 | offset += (i * size); |
| 727 | } |
| 728 | |
| 729 | // Go to the next ElementRegion (if any). |
| 730 | ER = dyn_cast<ElementRegion>(superR); |
| 731 | continue; |
| 732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 737 | assert(superR && "super region cannot be NULL"); |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 738 | return RegionRawOffset(superR, offset.getQuantity()); |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 741 | //===----------------------------------------------------------------------===// |
| 742 | // BlockDataRegion |
| 743 | //===----------------------------------------------------------------------===// |
| 744 | |
| 745 | void BlockDataRegion::LazyInitializeReferencedVars() { |
| 746 | if (ReferencedVars) |
| 747 | return; |
| 748 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 749 | AnalysisContext *AC = getCodeRegion()->getAnalysisContext(); |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 750 | AnalysisContext::referenced_decls_iterator I, E; |
| 751 | llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl()); |
| 752 | |
| 753 | if (I == E) { |
| 754 | ReferencedVars = (void*) 0x1; |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | MemRegionManager &MemMgr = *getMemRegionManager(); |
| 759 | llvm::BumpPtrAllocator &A = MemMgr.getAllocator(); |
| 760 | BumpVectorContext BC(A); |
| 761 | |
| 762 | typedef BumpVector<const MemRegion*> VarVec; |
| 763 | VarVec *BV = (VarVec*) A.Allocate<VarVec>(); |
Ted Kremenek | 02b1df6 | 2009-12-01 22:12:34 +0000 | [diff] [blame] | 764 | new (BV) VarVec(BC, E - I); |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 765 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 766 | for ( ; I != E; ++I) { |
| 767 | const VarDecl *VD = *I; |
| 768 | const VarRegion *VR = 0; |
| 769 | |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 770 | if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage()) |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 771 | VR = MemMgr.getVarRegion(VD, this); |
| 772 | else { |
| 773 | if (LC) |
| 774 | VR = MemMgr.getVarRegion(VD, LC); |
| 775 | else { |
| 776 | VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion()); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | assert(VR); |
| 781 | BV->push_back(VR, BC); |
| 782 | } |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 783 | |
| 784 | ReferencedVars = BV; |
| 785 | } |
| 786 | |
| 787 | BlockDataRegion::referenced_vars_iterator |
| 788 | BlockDataRegion::referenced_vars_begin() const { |
| 789 | const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); |
| 790 | |
| 791 | BumpVector<const MemRegion*> *Vec = |
| 792 | static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); |
| 793 | |
Ted Kremenek | 81cef58 | 2009-12-03 08:09:21 +0000 | [diff] [blame] | 794 | return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ? |
| 795 | NULL : Vec->begin()); |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | BlockDataRegion::referenced_vars_iterator |
| 799 | BlockDataRegion::referenced_vars_end() const { |
| 800 | const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); |
| 801 | |
| 802 | BumpVector<const MemRegion*> *Vec = |
| 803 | static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); |
| 804 | |
Ted Kremenek | 81cef58 | 2009-12-03 08:09:21 +0000 | [diff] [blame] | 805 | return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ? |
| 806 | NULL : Vec->end()); |
Ted Kremenek | 4240096 | 2009-11-26 02:34:36 +0000 | [diff] [blame] | 807 | } |