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 | |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | |
| 22 | MemRegion::~MemRegion() {} |
| 23 | |
Zhongxing Xu | 7e5d6ed | 2009-01-08 13:17:14 +0000 | [diff] [blame] | 24 | bool SubRegion::isSubRegionOf(const MemRegion* R) const { |
| 25 | const MemRegion* r = getSuperRegion(); |
| 26 | while (r != 0) { |
| 27 | if (r == R) |
| 28 | return true; |
| 29 | if (const SubRegion* sr = dyn_cast<SubRegion>(r)) |
| 30 | r = sr->getSuperRegion(); |
| 31 | else |
| 32 | break; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 37 | void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 38 | ID.AddInteger((unsigned)getKind()); |
| 39 | } |
| 40 | |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 41 | void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 42 | const StringLiteral* Str, |
| 43 | const MemRegion* superRegion) { |
| 44 | ID.AddInteger((unsigned) StringRegionKind); |
| 45 | ID.AddPointer(Str); |
| 46 | ID.AddPointer(superRegion); |
| 47 | } |
| 48 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 49 | void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 50 | const Expr* Ex, unsigned cnt) { |
| 51 | ID.AddInteger((unsigned) AllocaRegionKind); |
| 52 | ID.AddPointer(Ex); |
| 53 | ID.AddInteger(cnt); |
| 54 | } |
| 55 | |
| 56 | void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 57 | ProfileRegion(ID, Ex, Cnt); |
| 58 | } |
| 59 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 60 | void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T, |
| 61 | const MemRegion* superRegion) { |
| 62 | ID.AddInteger((unsigned) AnonTypedRegionKind); |
| 63 | ID.Add(T); |
| 64 | ID.AddPointer(superRegion); |
| 65 | } |
| 66 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 67 | void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 68 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 69 | } |
| 70 | |
| 71 | void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 72 | const CompoundLiteralExpr* CL, |
| 73 | const MemRegion* superRegion) { |
| 74 | ID.AddInteger((unsigned) CompoundLiteralRegionKind); |
| 75 | ID.AddPointer(CL); |
| 76 | ID.AddPointer(superRegion); |
| 77 | } |
| 78 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 79 | void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D, |
| 80 | const MemRegion* superRegion, Kind k) { |
| 81 | ID.AddInteger((unsigned) k); |
| 82 | ID.AddPointer(D); |
| 83 | ID.AddPointer(superRegion); |
| 84 | } |
| 85 | |
| 86 | void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 87 | DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); |
| 88 | } |
| 89 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 90 | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 91 | ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); |
Ted Kremenek | 6d0e2d2 | 2008-12-05 02:39:38 +0000 | [diff] [blame] | 92 | ID.Add(sym); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 96 | SymbolicRegion::ProfileRegion(ID, sym); |
| 97 | } |
| 98 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 99 | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx, |
| 100 | const MemRegion* superRegion) { |
| 101 | ID.AddInteger(MemRegion::ElementRegionKind); |
| 102 | ID.AddPointer(superRegion); |
| 103 | Idx.Profile(ID); |
| 104 | } |
| 105 | |
| 106 | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 107 | ElementRegion::ProfileRegion(ID, Index, superRegion); |
| 108 | } |
Zhongxing Xu | 27b5706 | 2008-10-27 13:17:02 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 110 | QualType ElementRegion::getRValueType(ASTContext& C) const { |
Ted Kremenek | 8318304 | 2009-01-24 06:11:36 +0000 | [diff] [blame^] | 111 | // Strip off typedefs from the ArrayRegion's RvalueType. |
| 112 | QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType(); |
Zhongxing Xu | c496f14 | 2009-01-23 10:19:29 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 8318304 | 2009-01-24 06:11:36 +0000 | [diff] [blame^] | 114 | if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr())) |
Zhongxing Xu | 56af977 | 2008-11-13 07:30:58 +0000 | [diff] [blame] | 115 | return AT->getElementType(); |
Ted Kremenek | 8318304 | 2009-01-24 06:11:36 +0000 | [diff] [blame^] | 116 | |
| 117 | PointerType* PtrT = cast<PointerType>(T.getTypePtr()); |
| 118 | return C.getCanonicalType(PtrT->getPointeeType()); |
Zhongxing Xu | 27b5706 | 2008-10-27 13:17:02 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 122 | // getLValueType() and getRValueType() |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | |
| 125 | QualType StringRegion::getRValueType(ASTContext& C) const { |
| 126 | return Str->getType(); |
| 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 130 | // Region pretty-printing. |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
| 133 | std::string MemRegion::getString() const { |
| 134 | std::string s; |
| 135 | llvm::raw_string_ostream os(s); |
| 136 | print(os); |
| 137 | return os.str(); |
| 138 | } |
| 139 | |
| 140 | void MemRegion::print(llvm::raw_ostream& os) const { |
| 141 | os << "<Unknown Region>"; |
| 142 | } |
| 143 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 144 | void AllocaRegion::print(llvm::raw_ostream& os) const { |
| 145 | os << "alloca{" << (void*) Ex << ',' << Cnt << '}'; |
| 146 | } |
| 147 | |
Ted Kremenek | 500d2ee | 2008-12-17 19:25:50 +0000 | [diff] [blame] | 148 | void AnonTypedRegion::print(llvm::raw_ostream& os) const { |
| 149 | os << "anon_type{" << T.getAsString() << ','; |
| 150 | getSuperRegion()->print(os); |
| 151 | os << '}'; |
| 152 | } |
| 153 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 154 | void VarRegion::print(llvm::raw_ostream& os) const { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 155 | os << cast<VarDecl>(D)->getNameAsString(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 158 | void SymbolicRegion::print(llvm::raw_ostream& os) const { |
Ted Kremenek | 562731e | 2008-12-05 02:45:20 +0000 | [diff] [blame] | 159 | os << "SymRegion-"; |
| 160 | sym.print(os); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | 4bd1eef | 2008-10-17 21:05:44 +0000 | [diff] [blame] | 163 | void FieldRegion::print(llvm::raw_ostream& os) const { |
| 164 | superRegion->print(os); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 165 | os << "->" << getDecl()->getNameAsString(); |
Ted Kremenek | 4bd1eef | 2008-10-17 21:05:44 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Zhongxing Xu | b21ff77 | 2008-10-24 06:30:07 +0000 | [diff] [blame] | 168 | void ElementRegion::print(llvm::raw_ostream& os) const { |
| 169 | superRegion->print(os); |
| 170 | os << '['; Index.print(os); os << ']'; |
| 171 | } |
| 172 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 173 | void CompoundLiteralRegion::print(llvm::raw_ostream& os) const { |
| 174 | // FIXME: More elaborate pretty-printing. |
| 175 | os << "{ " << (void*) CL << " }"; |
| 176 | } |
| 177 | |
Zhongxing Xu | cc128b3 | 2008-11-10 13:05:26 +0000 | [diff] [blame] | 178 | void StringRegion::print(llvm::raw_ostream& os) const { |
Ted Kremenek | e2916d6 | 2009-01-16 19:26:50 +0000 | [diff] [blame] | 179 | Str->printPretty(os); |
Zhongxing Xu | cc128b3 | 2008-11-10 13:05:26 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
| 183 | // MemRegionManager methods. |
| 184 | //===----------------------------------------------------------------------===// |
| 185 | |
| 186 | MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { |
| 187 | |
| 188 | if (!region) { |
| 189 | region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); |
| 190 | new (region) MemSpaceRegion(); |
| 191 | } |
| 192 | |
| 193 | return region; |
| 194 | } |
| 195 | |
| 196 | MemSpaceRegion* MemRegionManager::getStackRegion() { |
| 197 | return LazyAllocate(stack); |
| 198 | } |
| 199 | |
| 200 | MemSpaceRegion* MemRegionManager::getGlobalsRegion() { |
| 201 | return LazyAllocate(globals); |
| 202 | } |
| 203 | |
| 204 | MemSpaceRegion* MemRegionManager::getHeapRegion() { |
| 205 | return LazyAllocate(heap); |
| 206 | } |
| 207 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 208 | MemSpaceRegion* MemRegionManager::getUnknownRegion() { |
| 209 | return LazyAllocate(unknown); |
| 210 | } |
| 211 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 212 | bool MemRegionManager::onStack(const MemRegion* R) { |
| 213 | while (const SubRegion* SR = dyn_cast<SubRegion>(R)) |
| 214 | R = SR->getSuperRegion(); |
| 215 | |
| 216 | return (R != 0) && (R == stack); |
| 217 | } |
| 218 | |
| 219 | bool MemRegionManager::onHeap(const MemRegion* R) { |
| 220 | while (const SubRegion* SR = dyn_cast<SubRegion>(R)) |
| 221 | R = SR->getSuperRegion(); |
| 222 | |
| 223 | return (R != 0) && (R == heap); |
| 224 | } |
| 225 | |
Zhongxing Xu | e9f4e54 | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 226 | StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) { |
| 227 | llvm::FoldingSetNodeID ID; |
| 228 | MemSpaceRegion* GlobalsR = getGlobalsRegion(); |
| 229 | |
| 230 | StringRegion::ProfileRegion(ID, Str, GlobalsR); |
| 231 | |
| 232 | void* InsertPos; |
| 233 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 234 | StringRegion* R = cast_or_null<StringRegion>(data); |
| 235 | |
| 236 | if (!R) { |
| 237 | R = (StringRegion*) A.Allocate<StringRegion>(); |
| 238 | new (R) StringRegion(Str, GlobalsR); |
| 239 | Regions.InsertNode(R, InsertPos); |
| 240 | } |
| 241 | |
| 242 | return R; |
| 243 | } |
| 244 | |
Ted Kremenek | 9a1f03a | 2008-10-27 21:01:26 +0000 | [diff] [blame] | 245 | VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) { |
| 246 | |
| 247 | const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion() |
| 248 | : getGlobalsRegion(); |
| 249 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 250 | llvm::FoldingSetNodeID ID; |
| 251 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); |
| 252 | |
| 253 | void* InsertPos; |
| 254 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 255 | VarRegion* R = cast_or_null<VarRegion>(data); |
| 256 | |
| 257 | if (!R) { |
| 258 | R = (VarRegion*) A.Allocate<VarRegion>(); |
| 259 | new (R) VarRegion(d, superRegion); |
| 260 | Regions.InsertNode(R, InsertPos); |
| 261 | } |
| 262 | |
| 263 | return R; |
| 264 | } |
| 265 | |
Ted Kremenek | 329d6fd | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 266 | CompoundLiteralRegion* |
| 267 | MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) { |
| 268 | // Is this compound literal allocated on the stack or is part of the |
| 269 | // global constant pool? |
| 270 | const MemRegion* superRegion = CL->isFileScope() ? |
| 271 | getGlobalsRegion() : getStackRegion(); |
| 272 | |
| 273 | // Profile the compound literal. |
| 274 | llvm::FoldingSetNodeID ID; |
| 275 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 276 | |
| 277 | void* InsertPos; |
| 278 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 279 | CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data); |
| 280 | |
| 281 | if (!R) { |
| 282 | R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>(); |
| 283 | new (R) CompoundLiteralRegion(CL, superRegion); |
| 284 | Regions.InsertNode(R, InsertPos); |
| 285 | } |
| 286 | |
| 287 | return R; |
| 288 | } |
| 289 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 290 | ElementRegion* |
| 291 | MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){ |
| 292 | |
Zhongxing Xu | 511191c | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 293 | llvm::FoldingSetNodeID ID; |
| 294 | ElementRegion::ProfileRegion(ID, Idx, superRegion); |
| 295 | |
| 296 | void* InsertPos; |
| 297 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 298 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 299 | |
| 300 | if (!R) { |
| 301 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
| 302 | new (R) ElementRegion(Idx, superRegion); |
| 303 | Regions.InsertNode(R, InsertPos); |
| 304 | } |
| 305 | |
| 306 | return R; |
| 307 | } |
| 308 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 309 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 310 | SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 311 | |
| 312 | llvm::FoldingSetNodeID ID; |
| 313 | SymbolicRegion::ProfileRegion(ID, sym); |
| 314 | |
| 315 | void* InsertPos; |
| 316 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 317 | SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); |
| 318 | |
| 319 | if (!R) { |
| 320 | R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); |
| 321 | new (R) SymbolicRegion(sym); |
| 322 | Regions.InsertNode(R, InsertPos); |
| 323 | } |
| 324 | |
| 325 | return R; |
| 326 | } |
| 327 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 328 | FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 329 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 330 | llvm::FoldingSetNodeID ID; |
| 331 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); |
| 332 | |
| 333 | void* InsertPos; |
| 334 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 335 | FieldRegion* R = cast_or_null<FieldRegion>(data); |
| 336 | |
| 337 | if (!R) { |
| 338 | R = (FieldRegion*) A.Allocate<FieldRegion>(); |
| 339 | new (R) FieldRegion(d, superRegion); |
| 340 | Regions.InsertNode(R, InsertPos); |
| 341 | } |
| 342 | |
| 343 | return R; |
| 344 | } |
| 345 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 346 | ObjCIvarRegion* |
| 347 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 348 | const MemRegion* superRegion) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 349 | llvm::FoldingSetNodeID ID; |
| 350 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); |
| 351 | |
| 352 | void* InsertPos; |
| 353 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 354 | ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); |
| 355 | |
| 356 | if (!R) { |
Zhongxing Xu | 722c288 | 2008-10-06 03:03:33 +0000 | [diff] [blame] | 357 | R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); |
| 358 | new (R) ObjCIvarRegion(d, superRegion); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 359 | Regions.InsertNode(R, InsertPos); |
| 360 | } |
| 361 | |
| 362 | return R; |
| 363 | } |
| 364 | |
Ted Kremenek | a7f1b9e | 2008-10-24 20:30:08 +0000 | [diff] [blame] | 365 | ObjCObjectRegion* |
| 366 | MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d, |
| 367 | const MemRegion* superRegion) { |
| 368 | llvm::FoldingSetNodeID ID; |
| 369 | DeclRegion::ProfileRegion(ID, d, superRegion, |
| 370 | MemRegion::ObjCObjectRegionKind); |
| 371 | |
| 372 | void* InsertPos; |
| 373 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 374 | ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data); |
| 375 | |
| 376 | if (!R) { |
| 377 | R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>(); |
| 378 | new (R) ObjCObjectRegion(d, superRegion); |
| 379 | Regions.InsertNode(R, InsertPos); |
| 380 | } |
| 381 | |
| 382 | return R; |
| 383 | } |
| 384 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 385 | AnonTypedRegion* |
| 386 | MemRegionManager::getAnonTypedRegion(QualType t, const MemRegion* superRegion) { |
| 387 | llvm::FoldingSetNodeID ID; |
| 388 | AnonTypedRegion::ProfileRegion(ID, t, superRegion); |
| 389 | |
| 390 | void* InsertPos; |
| 391 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 392 | AnonTypedRegion* R = cast_or_null<AnonTypedRegion>(data); |
| 393 | |
| 394 | if (!R) { |
| 395 | R = (AnonTypedRegion*) A.Allocate<AnonTypedRegion>(); |
| 396 | new (R) AnonTypedRegion(t, superRegion); |
| 397 | Regions.InsertNode(R, InsertPos); |
| 398 | } |
| 399 | |
| 400 | return R; |
| 401 | } |
Ted Kremenek | a7f1b9e | 2008-10-24 20:30:08 +0000 | [diff] [blame] | 402 | |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 403 | AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { |
| 404 | llvm::FoldingSetNodeID ID; |
| 405 | AllocaRegion::ProfileRegion(ID, E, cnt); |
| 406 | |
| 407 | void* InsertPos; |
| 408 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 409 | AllocaRegion* R = cast_or_null<AllocaRegion>(data); |
| 410 | |
| 411 | if (!R) { |
| 412 | R = (AllocaRegion*) A.Allocate<AllocaRegion>(); |
| 413 | new (R) AllocaRegion(E, cnt, getStackRegion()); |
| 414 | Regions.InsertNode(R, InsertPos); |
| 415 | } |
| 416 | |
| 417 | return R; |
| 418 | } |
| 419 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 420 | bool MemRegionManager::hasStackStorage(const MemRegion* R) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 421 | |
| 422 | // Only subregions can have stack storage. |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 423 | const SubRegion* SR = dyn_cast<SubRegion>(R); |
| 424 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 425 | if (!SR) |
| 426 | return false; |
Ted Kremenek | 7090ae1 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 428 | MemSpaceRegion* S = getStackRegion(); |
| 429 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 430 | while (SR) { |
| 431 | R = SR->getSuperRegion(); |
| 432 | if (R == S) |
| 433 | return true; |
| 434 | |
| 435 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return false; |
| 439 | } |