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