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