Ted Kremenek | b15eba4 | 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 | b287b21 | 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 | b15eba4 | 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 | 73507bd | 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 | ea9ca50 | 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 | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 60 | void TypedViewRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T, |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 61 | const MemRegion* superRegion) { |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 62 | ID.AddInteger((unsigned) TypedViewRegionKind); |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 63 | ID.Add(T); |
| 64 | ID.AddPointer(superRegion); |
| 65 | } |
| 66 | |
Ted Kremenek | 6bc91b9 | 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 | b15eba4 | 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 | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 90 | void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) { |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 91 | ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); |
Ted Kremenek | e8993f1 | 2008-12-05 02:39:38 +0000 | [diff] [blame] | 92 | ID.Add(sym); |
Ted Kremenek | 38a4b4b | 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 | |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 99 | void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, |
| 100 | QualType ElementType, SVal Idx, |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 101 | const MemRegion* superRegion) { |
| 102 | ID.AddInteger(MemRegion::ElementRegionKind); |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 103 | ID.Add(ElementType); |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 104 | ID.AddPointer(superRegion); |
| 105 | Idx.Profile(ID); |
| 106 | } |
| 107 | |
| 108 | void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 109 | ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion); |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 110 | } |
Zhongxing Xu | 1a563da | 2008-10-27 13:17:02 +0000 | [diff] [blame] | 111 | |
Zhongxing Xu | ef3fb4c | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 112 | void CodeTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const void* data, |
| 113 | QualType t) { |
| 114 | ID.AddInteger(MemRegion::CodeTextRegionKind); |
| 115 | ID.AddPointer(data); |
| 116 | ID.Add(t); |
| 117 | } |
| 118 | |
| 119 | void CodeTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { |
| 120 | CodeTextRegion::ProfileRegion(ID, Data, LocationType); |
| 121 | } |
| 122 | |
Zhongxing Xu | 93b8066 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// |
| 124 | // getLValueType() and getRValueType() |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
Ted Kremenek | f5da325 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 127 | QualType StringRegion::getRValueType(ASTContext& C) const { |
| 128 | return Str->getType(); |
| 129 | } |
| 130 | |
| 131 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 132 | // Region pretty-printing. |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | std::string MemRegion::getString() const { |
| 136 | std::string s; |
| 137 | llvm::raw_string_ostream os(s); |
| 138 | print(os); |
| 139 | return os.str(); |
| 140 | } |
| 141 | |
| 142 | void MemRegion::print(llvm::raw_ostream& os) const { |
| 143 | os << "<Unknown Region>"; |
| 144 | } |
| 145 | |
Ted Kremenek | ea9ca50 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 146 | void AllocaRegion::print(llvm::raw_ostream& os) const { |
| 147 | os << "alloca{" << (void*) Ex << ',' << Cnt << '}'; |
| 148 | } |
| 149 | |
Ted Kremenek | 398efb5 | 2009-04-21 19:56:58 +0000 | [diff] [blame] | 150 | void CodeTextRegion::print(llvm::raw_ostream& os) const { |
| 151 | os << "code{"; |
| 152 | if (isDeclared()) |
Ted Kremenek | b895ed2 | 2009-04-29 15:37:24 +0000 | [diff] [blame] | 153 | os << getDecl()->getDeclName().getAsString(); |
Ted Kremenek | 398efb5 | 2009-04-21 19:56:58 +0000 | [diff] [blame] | 154 | else |
| 155 | os << '$' << getSymbol(); |
| 156 | |
| 157 | os << '}'; |
| 158 | } |
| 159 | |
Ted Kremenek | 3a4beb5 | 2009-04-21 18:09:22 +0000 | [diff] [blame] | 160 | void CompoundLiteralRegion::print(llvm::raw_ostream& os) const { |
| 161 | // FIXME: More elaborate pretty-printing. |
| 162 | os << "{ " << (void*) CL << " }"; |
| 163 | } |
| 164 | |
| 165 | void ElementRegion::print(llvm::raw_ostream& os) const { |
| 166 | superRegion->print(os); |
| 167 | os << '['; Index.print(os); os << ']'; |
| 168 | } |
| 169 | |
| 170 | void FieldRegion::print(llvm::raw_ostream& os) const { |
| 171 | superRegion->print(os); |
| 172 | os << "->" << getDecl()->getNameAsString(); |
| 173 | } |
| 174 | |
| 175 | void StringRegion::print(llvm::raw_ostream& os) const { |
| 176 | Str->printPretty(os); |
| 177 | } |
| 178 | |
| 179 | void SymbolicRegion::print(llvm::raw_ostream& os) const { |
| 180 | os << "SymRegion-" << sym; |
| 181 | } |
| 182 | |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 183 | void TypedViewRegion::print(llvm::raw_ostream& os) const { |
Ted Kremenek | d71d13a | 2009-03-11 21:57:34 +0000 | [diff] [blame] | 184 | os << "typed_view{" << LValueType.getAsString() << ','; |
Ted Kremenek | 542c34d | 2008-12-17 19:25:50 +0000 | [diff] [blame] | 185 | getSuperRegion()->print(os); |
| 186 | os << '}'; |
| 187 | } |
| 188 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 189 | void VarRegion::print(llvm::raw_ostream& os) const { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 190 | os << cast<VarDecl>(D)->getNameAsString(); |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | // MemRegionManager methods. |
| 195 | //===----------------------------------------------------------------------===// |
| 196 | |
| 197 | MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) { |
| 198 | |
| 199 | if (!region) { |
| 200 | region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>(); |
| 201 | new (region) MemSpaceRegion(); |
| 202 | } |
| 203 | |
| 204 | return region; |
| 205 | } |
| 206 | |
| 207 | MemSpaceRegion* MemRegionManager::getStackRegion() { |
| 208 | return LazyAllocate(stack); |
| 209 | } |
| 210 | |
| 211 | MemSpaceRegion* MemRegionManager::getGlobalsRegion() { |
| 212 | return LazyAllocate(globals); |
| 213 | } |
| 214 | |
| 215 | MemSpaceRegion* MemRegionManager::getHeapRegion() { |
| 216 | return LazyAllocate(heap); |
| 217 | } |
| 218 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 219 | MemSpaceRegion* MemRegionManager::getUnknownRegion() { |
| 220 | return LazyAllocate(unknown); |
| 221 | } |
| 222 | |
Zhongxing Xu | ef3fb4c | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 223 | MemSpaceRegion* MemRegionManager::getCodeRegion() { |
| 224 | return LazyAllocate(code); |
| 225 | } |
| 226 | |
Zhongxing Xu | 5ea4ad0 | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 227 | bool MemRegionManager::onStack(const MemRegion* R) { |
| 228 | while (const SubRegion* SR = dyn_cast<SubRegion>(R)) |
| 229 | R = SR->getSuperRegion(); |
| 230 | |
| 231 | return (R != 0) && (R == stack); |
| 232 | } |
| 233 | |
| 234 | bool MemRegionManager::onHeap(const MemRegion* R) { |
| 235 | while (const SubRegion* SR = dyn_cast<SubRegion>(R)) |
| 236 | R = SR->getSuperRegion(); |
| 237 | |
| 238 | return (R != 0) && (R == heap); |
| 239 | } |
| 240 | |
Zhongxing Xu | 73507bd | 2008-10-25 14:13:41 +0000 | [diff] [blame] | 241 | StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) { |
| 242 | llvm::FoldingSetNodeID ID; |
| 243 | MemSpaceRegion* GlobalsR = getGlobalsRegion(); |
| 244 | |
| 245 | StringRegion::ProfileRegion(ID, Str, GlobalsR); |
| 246 | |
| 247 | void* InsertPos; |
| 248 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 249 | StringRegion* R = cast_or_null<StringRegion>(data); |
| 250 | |
| 251 | if (!R) { |
| 252 | R = (StringRegion*) A.Allocate<StringRegion>(); |
| 253 | new (R) StringRegion(Str, GlobalsR); |
| 254 | Regions.InsertNode(R, InsertPos); |
| 255 | } |
| 256 | |
| 257 | return R; |
| 258 | } |
| 259 | |
Ted Kremenek | 81329ab | 2008-10-27 21:01:26 +0000 | [diff] [blame] | 260 | VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) { |
| 261 | |
| 262 | const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion() |
| 263 | : getGlobalsRegion(); |
| 264 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 265 | llvm::FoldingSetNodeID ID; |
| 266 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind); |
| 267 | |
| 268 | void* InsertPos; |
| 269 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 270 | VarRegion* R = cast_or_null<VarRegion>(data); |
| 271 | |
| 272 | if (!R) { |
| 273 | R = (VarRegion*) A.Allocate<VarRegion>(); |
| 274 | new (R) VarRegion(d, superRegion); |
| 275 | Regions.InsertNode(R, InsertPos); |
| 276 | } |
| 277 | |
| 278 | return R; |
| 279 | } |
| 280 | |
Ted Kremenek | 6bc91b9 | 2008-10-27 20:57:58 +0000 | [diff] [blame] | 281 | CompoundLiteralRegion* |
| 282 | MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) { |
| 283 | // Is this compound literal allocated on the stack or is part of the |
| 284 | // global constant pool? |
| 285 | const MemRegion* superRegion = CL->isFileScope() ? |
| 286 | getGlobalsRegion() : getStackRegion(); |
| 287 | |
| 288 | // Profile the compound literal. |
| 289 | llvm::FoldingSetNodeID ID; |
| 290 | CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); |
| 291 | |
| 292 | void* InsertPos; |
| 293 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 294 | CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data); |
| 295 | |
| 296 | if (!R) { |
| 297 | R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>(); |
| 298 | new (R) CompoundLiteralRegion(CL, superRegion); |
| 299 | Regions.InsertNode(R, InsertPos); |
| 300 | } |
| 301 | |
| 302 | return R; |
| 303 | } |
| 304 | |
Ted Kremenek | 2c0de35 | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 305 | ElementRegion* |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 306 | MemRegionManager::getElementRegion(QualType elementType, SVal Idx, |
| 307 | const TypedRegion* superRegion){ |
Ted Kremenek | 2c0de35 | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 308 | |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 309 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 310 | ElementRegion::ProfileRegion(ID, elementType, Idx, superRegion); |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 311 | |
| 312 | void* InsertPos; |
| 313 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 314 | ElementRegion* R = cast_or_null<ElementRegion>(data); |
| 315 | |
| 316 | if (!R) { |
| 317 | R = (ElementRegion*) A.Allocate<ElementRegion>(); |
Ted Kremenek | af81ece | 2009-05-04 06:18:28 +0000 | [diff] [blame^] | 318 | new (R) ElementRegion(elementType, Idx, superRegion); |
Zhongxing Xu | 5496973 | 2008-10-21 05:27:10 +0000 | [diff] [blame] | 319 | Regions.InsertNode(R, InsertPos); |
| 320 | } |
| 321 | |
| 322 | return R; |
| 323 | } |
| 324 | |
Zhongxing Xu | ef3fb4c | 2009-04-10 08:45:10 +0000 | [diff] [blame] | 325 | CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd, |
| 326 | QualType t) { |
| 327 | llvm::FoldingSetNodeID ID; |
| 328 | CodeTextRegion::ProfileRegion(ID, fd, t); |
| 329 | void* InsertPos; |
| 330 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 331 | CodeTextRegion* R = cast_or_null<CodeTextRegion>(data); |
| 332 | |
| 333 | if (!R) { |
| 334 | R = (CodeTextRegion*) A.Allocate<CodeTextRegion>(); |
| 335 | new (R) CodeTextRegion(fd, t, getCodeRegion()); |
| 336 | Regions.InsertNode(R, InsertPos); |
| 337 | } |
| 338 | |
| 339 | return R; |
| 340 | } |
| 341 | |
| 342 | CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) { |
| 343 | llvm::FoldingSetNodeID ID; |
| 344 | CodeTextRegion::ProfileRegion(ID, sym, t); |
| 345 | void* InsertPos; |
| 346 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 347 | CodeTextRegion* R = cast_or_null<CodeTextRegion>(data); |
| 348 | |
| 349 | if (!R) { |
| 350 | R = (CodeTextRegion*) A.Allocate<CodeTextRegion>(); |
| 351 | new (R) CodeTextRegion(sym, t, getCodeRegion()); |
| 352 | Regions.InsertNode(R, InsertPos); |
| 353 | } |
| 354 | |
| 355 | return R; |
| 356 | } |
| 357 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 358 | /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 359 | SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) { |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 360 | llvm::FoldingSetNodeID ID; |
| 361 | SymbolicRegion::ProfileRegion(ID, sym); |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 362 | void* InsertPos; |
| 363 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 364 | SymbolicRegion* R = cast_or_null<SymbolicRegion>(data); |
| 365 | |
| 366 | if (!R) { |
| 367 | R = (SymbolicRegion*) A.Allocate<SymbolicRegion>(); |
Zhongxing Xu | 93b8066 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 368 | // SymbolicRegion's storage class is usually unknown. |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 369 | new (R) SymbolicRegion(sym, getUnknownRegion()); |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 370 | Regions.InsertNode(R, InsertPos); |
| 371 | } |
| 372 | |
| 373 | return R; |
| 374 | } |
| 375 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 376 | FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d, |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 377 | const MemRegion* superRegion) { |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 378 | llvm::FoldingSetNodeID ID; |
| 379 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind); |
| 380 | |
| 381 | void* InsertPos; |
| 382 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 383 | FieldRegion* R = cast_or_null<FieldRegion>(data); |
| 384 | |
| 385 | if (!R) { |
| 386 | R = (FieldRegion*) A.Allocate<FieldRegion>(); |
| 387 | new (R) FieldRegion(d, superRegion); |
| 388 | Regions.InsertNode(R, InsertPos); |
| 389 | } |
| 390 | |
| 391 | return R; |
| 392 | } |
| 393 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 394 | ObjCIvarRegion* |
| 395 | MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d, |
| 396 | const MemRegion* superRegion) { |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 397 | llvm::FoldingSetNodeID ID; |
| 398 | DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind); |
| 399 | |
| 400 | void* InsertPos; |
| 401 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 402 | ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data); |
| 403 | |
| 404 | if (!R) { |
Zhongxing Xu | 7a861e8 | 2008-10-06 03:03:33 +0000 | [diff] [blame] | 405 | R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>(); |
| 406 | new (R) ObjCIvarRegion(d, superRegion); |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 407 | Regions.InsertNode(R, InsertPos); |
| 408 | } |
| 409 | |
| 410 | return R; |
| 411 | } |
| 412 | |
Ted Kremenek | 2539c11 | 2008-10-24 20:30:08 +0000 | [diff] [blame] | 413 | ObjCObjectRegion* |
| 414 | MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d, |
| 415 | const MemRegion* superRegion) { |
| 416 | llvm::FoldingSetNodeID ID; |
| 417 | DeclRegion::ProfileRegion(ID, d, superRegion, |
| 418 | MemRegion::ObjCObjectRegionKind); |
| 419 | |
| 420 | void* InsertPos; |
| 421 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 422 | ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data); |
| 423 | |
| 424 | if (!R) { |
| 425 | R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>(); |
| 426 | new (R) ObjCObjectRegion(d, superRegion); |
| 427 | Regions.InsertNode(R, InsertPos); |
| 428 | } |
| 429 | |
| 430 | return R; |
| 431 | } |
| 432 | |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 433 | TypedViewRegion* |
| 434 | MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) { |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 435 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 436 | TypedViewRegion::ProfileRegion(ID, t, superRegion); |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 437 | |
| 438 | void* InsertPos; |
| 439 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 440 | TypedViewRegion* R = cast_or_null<TypedViewRegion>(data); |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 441 | |
| 442 | if (!R) { |
Ted Kremenek | 6a9b535 | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 443 | R = (TypedViewRegion*) A.Allocate<TypedViewRegion>(); |
| 444 | new (R) TypedViewRegion(t, superRegion); |
Zhongxing Xu | 8fbe7ae | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 445 | Regions.InsertNode(R, InsertPos); |
| 446 | } |
| 447 | |
| 448 | return R; |
| 449 | } |
Ted Kremenek | 2539c11 | 2008-10-24 20:30:08 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | ea9ca50 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 451 | AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) { |
| 452 | llvm::FoldingSetNodeID ID; |
| 453 | AllocaRegion::ProfileRegion(ID, E, cnt); |
| 454 | |
| 455 | void* InsertPos; |
| 456 | MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); |
| 457 | AllocaRegion* R = cast_or_null<AllocaRegion>(data); |
| 458 | |
| 459 | if (!R) { |
| 460 | R = (AllocaRegion*) A.Allocate<AllocaRegion>(); |
| 461 | new (R) AllocaRegion(E, cnt, getStackRegion()); |
| 462 | Regions.InsertNode(R, InsertPos); |
| 463 | } |
| 464 | |
| 465 | return R; |
| 466 | } |
| 467 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 468 | bool MemRegionManager::hasStackStorage(const MemRegion* R) { |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 469 | |
| 470 | // Only subregions can have stack storage. |
Ted Kremenek | ea9ca50 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 471 | const SubRegion* SR = dyn_cast<SubRegion>(R); |
| 472 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 473 | if (!SR) |
| 474 | return false; |
Ted Kremenek | ea9ca50 | 2008-11-02 00:34:33 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 476 | MemSpaceRegion* S = getStackRegion(); |
| 477 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 478 | while (SR) { |
| 479 | R = SR->getSuperRegion(); |
| 480 | if (R == S) |
| 481 | return true; |
| 482 | |
| 483 | SR = dyn_cast<SubRegion>(R); |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 484 | } |
Ted Kremenek | 8765ebc | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 486 | return false; |
| 487 | } |
Ted Kremenek | 8765ebc | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 488 | |
| 489 | |
| 490 | //===----------------------------------------------------------------------===// |
| 491 | // View handling. |
| 492 | //===----------------------------------------------------------------------===// |
| 493 | |
| 494 | const MemRegion *TypedViewRegion::removeViews() const { |
| 495 | const SubRegion *SR = this; |
| 496 | const MemRegion *R = SR; |
| 497 | while (SR && isa<TypedViewRegion>(SR)) { |
| 498 | R = SR->getSuperRegion(); |
| 499 | SR = dyn_cast<SubRegion>(R); |
| 500 | } |
| 501 | return R; |
| 502 | } |