blob: 06d8aec3910e293da8db353262c97e725e6b19c7 [file] [log] [blame]
Zhongxing Xu97ab3942009-07-30 01:17:21 +00001//== AnalysisContext.cpp - Analysis context for Path Sens 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 AnalysisContext, a class that manages the analysis context
11// data for path sensitive analysis.
12//
13//===----------------------------------------------------------------------===//
14
Zhongxing Xu97ab3942009-07-30 01:17:21 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclObjC.h"
Mike Stumpfa6ef182010-01-13 02:59:54 +000017#include "clang/AST/DeclTemplate.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000018#include "clang/AST/ParentMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000019#include "clang/AST/StmtVisitor.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Analysis/CFG.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000023#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000024#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000025#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000026
27using namespace clang;
28
Ted Kremenek58f5ec72009-10-20 21:39:41 +000029void AnalysisContextManager::clear() {
30 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
31 delete I->second;
32 Contexts.clear();
33}
34
Zhongxing Xu97ab3942009-07-30 01:17:21 +000035Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000036 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000037 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000038 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000039 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000040 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
41 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000042 else if (const FunctionTemplateDecl *FunTmpl
43 = dyn_cast_or_null<FunctionTemplateDecl>(D))
44 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000045
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000046 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000047}
48
Ted Kremenek82cd37c2009-08-21 23:25:54 +000049const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
50 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
51 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek82cd37c2009-08-21 23:25:54 +000053 return NULL;
54}
55
Zhongxing Xu97ab3942009-07-30 01:17:21 +000056CFG *AnalysisContext::getCFG() {
Ted Kremenekd064fdc2010-03-23 00:13:23 +000057 if (!builtCFG) {
Mike Stump4c45aa12010-01-21 15:20:48 +000058 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), AddEHEdges);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000059 // Even when the cfg is not successfully built, we don't
60 // want to try building it again.
61 builtCFG = true;
62 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000063 return cfg;
64}
65
66ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000067 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000068 PM = new ParentMap(getBody());
69 return *PM;
70}
71
72LiveVariables *AnalysisContext::getLiveVariables() {
73 if (!liveness) {
74 CFG *c = getCFG();
75 if (!c)
76 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenekb1a7b652009-11-26 02:31:33 +000078 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000079 liveness->runOnCFG(*c);
80 liveness->runOnAllBlocks(*c, 0, true);
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Zhongxing Xu97ab3942009-07-30 01:17:21 +000083 return liveness;
84}
85
Ted Kremenek23760022009-08-21 23:58:43 +000086AnalysisContext *AnalysisContextManager::getContext(const Decl *D) {
87 AnalysisContext *&AC = Contexts[D];
88 if (!AC)
89 AC = new AnalysisContext(D);
Mike Stump1eb44332009-09-09 15:08:12 +000090
Ted Kremenek23760022009-08-21 23:58:43 +000091 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +000092}
Zhongxing Xu18c7c062009-08-03 07:23:22 +000093
Ted Kremenekdc0d9092009-12-04 00:50:10 +000094//===----------------------------------------------------------------------===//
95// FoldingSet profiling.
96//===----------------------------------------------------------------------===//
97
Ted Kremenekdc0d9092009-12-04 00:50:10 +000098void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
99 ContextKind ck,
100 AnalysisContext *ctx,
101 const LocationContext *parent,
102 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000103 ID.AddInteger(ck);
104 ID.AddPointer(ctx);
105 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000106 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000107}
108
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000109void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000110 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000111}
112
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000113void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
114 Profile(ID, getAnalysisContext(), getParent(), Enter);
115}
116
117void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000118 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000119}
120
121//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000122// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000123//===----------------------------------------------------------------------===//
124
Ted Kremenek0ee41242009-12-04 01:28:56 +0000125template <typename LOC, typename DATA>
126const LOC*
127LocationContextManager::getLocationContext(AnalysisContext *ctx,
128 const LocationContext *parent,
129 const DATA *d) {
130 llvm::FoldingSetNodeID ID;
131 LOC::Profile(ID, ctx, parent, d);
132 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000133
Ted Kremenek0ee41242009-12-04 01:28:56 +0000134 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000135
Ted Kremenek0ee41242009-12-04 01:28:56 +0000136 if (!L) {
137 L = new LOC(ctx, parent, d);
138 Contexts.InsertNode(L, InsertPos);
139 }
140 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000141}
142
Ted Kremenek0ee41242009-12-04 01:28:56 +0000143const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000144LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000145 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000146 const Stmt *s, const CFGBlock *blk,
147 unsigned idx) {
148 llvm::FoldingSetNodeID ID;
149 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
150 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000151 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000152 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
153 if (!L) {
154 L = new StackFrameContext(ctx, parent, s, blk, idx);
155 Contexts.InsertNode(L, InsertPos);
156 }
157 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000158}
159
Ted Kremenek0ee41242009-12-04 01:28:56 +0000160const ScopeContext *
161LocationContextManager::getScope(AnalysisContext *ctx,
162 const LocationContext *parent,
163 const Stmt *s) {
164 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
165}
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000167//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000168// LocationContext methods.
169//===----------------------------------------------------------------------===//
170
171const StackFrameContext *LocationContext::getCurrentStackFrame() const {
172 const LocationContext *LC = this;
173 while (LC) {
174 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
175 return SFC;
176 LC = LC->getParent();
177 }
178 return NULL;
179}
180
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000181const StackFrameContext *
182LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
183 const LocationContext *LC = this;
184 while (LC) {
185 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
186 if (cast<DeclContext>(SFC->getDecl()) == DC)
187 return SFC;
188 }
189 LC = LC->getParent();
190 }
191 return NULL;
192}
193
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000194bool LocationContext::isParentOf(const LocationContext *LC) const {
195 do {
196 const LocationContext *Parent = LC->getParent();
197 if (Parent == this)
198 return true;
199 else
200 LC = Parent;
201 } while (LC);
202
203 return false;
204}
205
Ted Kremenek67d12872009-12-07 22:05:27 +0000206//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000207// Lazily generated map to query the external variables referenced by a Block.
208//===----------------------------------------------------------------------===//
209
210namespace {
211class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
212 BumpVector<const VarDecl*> &BEVals;
213 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000214 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000215 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000216public:
217 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
218 BumpVectorContext &bc)
219 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000220
221 bool IsTrackedDecl(const VarDecl *VD) {
222 const DeclContext *DC = VD->getDeclContext();
223 return IgnoredContexts.count(DC) == 0;
224 }
225
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000226 void VisitStmt(Stmt *S) {
227 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
228 if (Stmt *child = *I)
229 Visit(child);
230 }
Ted Kremenek85248732010-02-06 00:30:00 +0000231
232 void VisitDeclRefExpr(const DeclRefExpr *DR) {
233 // Non-local variables are also directly modified.
234 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
235 if (!VD->hasLocalStorage()) {
236 unsigned &flag = Visited[VD];
237 if (!flag) {
238 flag = 1;
239 BEVals.push_back(VD, BC);
240 }
241 }
242 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000243
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000244 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000245 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
246 unsigned &flag = Visited[VD];
247 if (!flag) {
248 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000249 if (IsTrackedDecl(VD))
250 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000251 }
252 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000253 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000254
255 void VisitBlockExpr(BlockExpr *BR) {
256 // Blocks containing blocks can transitively capture more variables.
257 IgnoredContexts.insert(BR->getBlockDecl());
258 Visit(BR->getBlockDecl()->getBody());
259 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000260};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000261} // end anonymous namespace
262
263typedef BumpVector<const VarDecl*> DeclVec;
264
265static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
266 void *&Vec,
267 llvm::BumpPtrAllocator &A) {
268 if (Vec)
269 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000270
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000271 BumpVectorContext BC(A);
272 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
273 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000274
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000275 // Find the referenced variables.
276 FindBlockDeclRefExprsVals F(*BV, BC);
277 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000278
279 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000280 return BV;
281}
282
283std::pair<AnalysisContext::referenced_decls_iterator,
284 AnalysisContext::referenced_decls_iterator>
285AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
286 if (!ReferencedBlockVars)
287 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000288
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000289 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
290 return std::make_pair(V->begin(), V->end());
291}
292
293//===----------------------------------------------------------------------===//
294// Cleanup.
295//===----------------------------------------------------------------------===//
296
297AnalysisContext::~AnalysisContext() {
298 delete cfg;
299 delete liveness;
300 delete PM;
301 delete ReferencedBlockVars;
302}
303
304AnalysisContextManager::~AnalysisContextManager() {
305 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
306 delete I->second;
307}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000308
309LocationContext::~LocationContext() {}
310
311LocationContextManager::~LocationContextManager() {
312 clear();
313}
314
315void LocationContextManager::clear() {
316 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000317 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000318 LocationContext *LC = &*I;
319 ++I;
320 delete LC;
321 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000322
Ted Kremenek0ee41242009-12-04 01:28:56 +0000323 Contexts.clear();
324}
325