blob: f6bf4497ba664576dc3df85a21fec869488bb1c1 [file] [log] [blame]
Ted Kremenek9e240492008-10-04 05:50:14 +00001//== 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
19using namespace clang;
20
21
22MemRegion::~MemRegion() {}
23
24void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
25 ID.AddInteger((unsigned)getKind());
26}
27
Zhongxing Xue9f4e542008-10-25 14:13:41 +000028void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
29 const StringLiteral* Str,
30 const MemRegion* superRegion) {
31 ID.AddInteger((unsigned) StringRegionKind);
32 ID.AddPointer(Str);
33 ID.AddPointer(superRegion);
34}
35
Ted Kremenek7090ae12008-11-02 00:34:33 +000036void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
37 const Expr* Ex, unsigned cnt) {
38 ID.AddInteger((unsigned) AllocaRegionKind);
39 ID.AddPointer(Ex);
40 ID.AddInteger(cnt);
41}
42
43void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
44 ProfileRegion(ID, Ex, Cnt);
45}
46
Zhongxing Xu817c67d2008-11-03 04:12:24 +000047QualType AnonPointeeRegion::getType(ASTContext& C) const {
48 QualType T = C.getCanonicalType(Pointer->getType());
49 PointerType* PTy = cast<PointerType>(T.getTypePtr());
50
51 QualType PointeeTy = C.getCanonicalType(PTy->getPointeeType());
52 return PointeeTy;
Ted Kremenek9e240492008-10-04 05:50:14 +000053}
54
Zhongxing Xu17892752008-10-08 02:50:44 +000055void AnonPointeeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Zhongxing Xu817c67d2008-11-03 04:12:24 +000056 const VarDecl* VD,
Zhongxing Xu17892752008-10-08 02:50:44 +000057 const MemRegion* superRegion) {
58 ID.AddInteger((unsigned) AnonPointeeRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +000059 ID.AddPointer(VD);
60 ID.AddPointer(superRegion);
61}
62
Ted Kremenek329d6fd2008-10-27 20:57:58 +000063void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
64 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
65}
66
67void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
68 const CompoundLiteralExpr* CL,
69 const MemRegion* superRegion) {
70 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
71 ID.AddPointer(CL);
72 ID.AddPointer(superRegion);
73}
74
Ted Kremenek9e240492008-10-04 05:50:14 +000075void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
76 const MemRegion* superRegion, Kind k) {
77 ID.AddInteger((unsigned) k);
78 ID.AddPointer(D);
79 ID.AddPointer(superRegion);
80}
81
82void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
83 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
84}
85
Ted Kremenek993f1c72008-10-17 20:28:54 +000086void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolID sym) {
87 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
88 ID.AddInteger(sym.getNumber());
89}
90
91void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
92 SymbolicRegion::ProfileRegion(ID, sym);
93}
94
Zhongxing Xu511191c2008-10-21 05:27:10 +000095void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx,
96 const MemRegion* superRegion) {
97 ID.AddInteger(MemRegion::ElementRegionKind);
98 ID.AddPointer(superRegion);
99 Idx.Profile(ID);
100}
101
102void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
103 ElementRegion::ProfileRegion(ID, Index, superRegion);
104}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000105
106QualType ElementRegion::getType(ASTContext& C) const {
107 QualType T = cast<TypedRegion>(superRegion)->getType(C);
108 ArrayType* AT = cast<ArrayType>(T.getTypePtr());
109 return AT->getElementType();
110}
111
Ted Kremenek9e240492008-10-04 05:50:14 +0000112//===----------------------------------------------------------------------===//
113// Region pretty-printing.
114//===----------------------------------------------------------------------===//
115
116std::string MemRegion::getString() const {
117 std::string s;
118 llvm::raw_string_ostream os(s);
119 print(os);
120 return os.str();
121}
122
123void MemRegion::print(llvm::raw_ostream& os) const {
124 os << "<Unknown Region>";
125}
126
Ted Kremenek7090ae12008-11-02 00:34:33 +0000127void AllocaRegion::print(llvm::raw_ostream& os) const {
128 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
129}
130
Ted Kremenek9e240492008-10-04 05:50:14 +0000131void VarRegion::print(llvm::raw_ostream& os) const {
132 os << cast<VarDecl>(D)->getName();
133}
134
Ted Kremenek993f1c72008-10-17 20:28:54 +0000135void SymbolicRegion::print(llvm::raw_ostream& os) const {
136 os << "$" << sym.getNumber();
137}
138
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000139void FieldRegion::print(llvm::raw_ostream& os) const {
140 superRegion->print(os);
141 os << "->" << getDecl()->getName();
142}
143
Zhongxing Xub21ff772008-10-24 06:30:07 +0000144void ElementRegion::print(llvm::raw_ostream& os) const {
145 superRegion->print(os);
146 os << '['; Index.print(os); os << ']';
147}
148
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000149void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
150 // FIXME: More elaborate pretty-printing.
151 os << "{ " << (void*) CL << " }";
152}
153
Zhongxing Xucc128b32008-11-10 13:05:26 +0000154void StringRegion::print(llvm::raw_ostream& os) const {
155 os << "\"" << Str->getStrData() << "\"";
156}
157
Ted Kremenek9e240492008-10-04 05:50:14 +0000158//===----------------------------------------------------------------------===//
159// MemRegionManager methods.
160//===----------------------------------------------------------------------===//
161
162MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
163
164 if (!region) {
165 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
166 new (region) MemSpaceRegion();
167 }
168
169 return region;
170}
171
172MemSpaceRegion* MemRegionManager::getStackRegion() {
173 return LazyAllocate(stack);
174}
175
176MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
177 return LazyAllocate(globals);
178}
179
180MemSpaceRegion* MemRegionManager::getHeapRegion() {
181 return LazyAllocate(heap);
182}
183
Zhongxing Xu17892752008-10-08 02:50:44 +0000184MemSpaceRegion* MemRegionManager::getUnknownRegion() {
185 return LazyAllocate(unknown);
186}
187
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000188StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
189 llvm::FoldingSetNodeID ID;
190 MemSpaceRegion* GlobalsR = getGlobalsRegion();
191
192 StringRegion::ProfileRegion(ID, Str, GlobalsR);
193
194 void* InsertPos;
195 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
196 StringRegion* R = cast_or_null<StringRegion>(data);
197
198 if (!R) {
199 R = (StringRegion*) A.Allocate<StringRegion>();
200 new (R) StringRegion(Str, GlobalsR);
201 Regions.InsertNode(R, InsertPos);
202 }
203
204 return R;
205}
206
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000207VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
208
209 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
210 : getGlobalsRegion();
211
Ted Kremenek9e240492008-10-04 05:50:14 +0000212 llvm::FoldingSetNodeID ID;
213 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
214
215 void* InsertPos;
216 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
217 VarRegion* R = cast_or_null<VarRegion>(data);
218
219 if (!R) {
220 R = (VarRegion*) A.Allocate<VarRegion>();
221 new (R) VarRegion(d, superRegion);
222 Regions.InsertNode(R, InsertPos);
223 }
224
225 return R;
226}
227
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000228CompoundLiteralRegion*
229MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
230 // Is this compound literal allocated on the stack or is part of the
231 // global constant pool?
232 const MemRegion* superRegion = CL->isFileScope() ?
233 getGlobalsRegion() : getStackRegion();
234
235 // Profile the compound literal.
236 llvm::FoldingSetNodeID ID;
237 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
238
239 void* InsertPos;
240 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
241 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
242
243 if (!R) {
244 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
245 new (R) CompoundLiteralRegion(CL, superRegion);
246 Regions.InsertNode(R, InsertPos);
247 }
248
249 return R;
250}
251
Zhongxing Xu511191c2008-10-21 05:27:10 +0000252ElementRegion* MemRegionManager::getElementRegion(SVal Idx,
253 const MemRegion* superRegion){
254 llvm::FoldingSetNodeID ID;
255 ElementRegion::ProfileRegion(ID, Idx, superRegion);
256
257 void* InsertPos;
258 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
259 ElementRegion* R = cast_or_null<ElementRegion>(data);
260
261 if (!R) {
262 R = (ElementRegion*) A.Allocate<ElementRegion>();
263 new (R) ElementRegion(Idx, superRegion);
264 Regions.InsertNode(R, InsertPos);
265 }
266
267 return R;
268}
269
Ted Kremenek993f1c72008-10-17 20:28:54 +0000270/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
271SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) {
272
273 llvm::FoldingSetNodeID ID;
274 SymbolicRegion::ProfileRegion(ID, sym);
275
276 void* InsertPos;
277 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
278 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
279
280 if (!R) {
281 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
282 new (R) SymbolicRegion(sym);
283 Regions.InsertNode(R, InsertPos);
284 }
285
286 return R;
287}
288
Ted Kremenek9e240492008-10-04 05:50:14 +0000289FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000290 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000291 llvm::FoldingSetNodeID ID;
292 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
293
294 void* InsertPos;
295 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
296 FieldRegion* R = cast_or_null<FieldRegion>(data);
297
298 if (!R) {
299 R = (FieldRegion*) A.Allocate<FieldRegion>();
300 new (R) FieldRegion(d, superRegion);
301 Regions.InsertNode(R, InsertPos);
302 }
303
304 return R;
305}
306
Ted Kremenek993f1c72008-10-17 20:28:54 +0000307ObjCIvarRegion*
308MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
309 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000310 llvm::FoldingSetNodeID ID;
311 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
312
313 void* InsertPos;
314 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
315 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
316
317 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000318 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
319 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000320 Regions.InsertNode(R, InsertPos);
321 }
322
323 return R;
324}
325
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000326ObjCObjectRegion*
327MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
328 const MemRegion* superRegion) {
329 llvm::FoldingSetNodeID ID;
330 DeclRegion::ProfileRegion(ID, d, superRegion,
331 MemRegion::ObjCObjectRegionKind);
332
333 void* InsertPos;
334 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
335 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
336
337 if (!R) {
338 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
339 new (R) ObjCObjectRegion(d, superRegion);
340 Regions.InsertNode(R, InsertPos);
341 }
342
343 return R;
344}
345
346
Zhongxing Xu17892752008-10-08 02:50:44 +0000347AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const VarDecl* d) {
348 llvm::FoldingSetNodeID ID;
Zhongxing Xu17892752008-10-08 02:50:44 +0000349 MemRegion* superRegion = getUnknownRegion();
350
Zhongxing Xu817c67d2008-11-03 04:12:24 +0000351 AnonPointeeRegion::ProfileRegion(ID, d, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000352
353 void* InsertPos;
354 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
355 AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data);
356
357 if (!R) {
358 R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>();
Zhongxing Xu817c67d2008-11-03 04:12:24 +0000359 new (R) AnonPointeeRegion(d, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000360 Regions.InsertNode(R, InsertPos);
361 }
362
363 return R;
364}
365
Ted Kremenek7090ae12008-11-02 00:34:33 +0000366AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
367 llvm::FoldingSetNodeID ID;
368 AllocaRegion::ProfileRegion(ID, E, cnt);
369
370 void* InsertPos;
371 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
372 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
373
374 if (!R) {
375 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
376 new (R) AllocaRegion(E, cnt, getStackRegion());
377 Regions.InsertNode(R, InsertPos);
378 }
379
380 return R;
381}
382
Ted Kremenek9e240492008-10-04 05:50:14 +0000383bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000384
385 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000386 const SubRegion* SR = dyn_cast<SubRegion>(R);
387
Ted Kremenek993f1c72008-10-17 20:28:54 +0000388 if (!SR)
389 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000390
Ted Kremenek9e240492008-10-04 05:50:14 +0000391 MemSpaceRegion* S = getStackRegion();
392
Ted Kremenek993f1c72008-10-17 20:28:54 +0000393 while (SR) {
394 R = SR->getSuperRegion();
395 if (R == S)
396 return true;
397
398 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000399 }
400
401 return false;
402}