blob: 5640c4a461e0012fc368b34d4e591f5c3a339e33 [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() {
Mike Stump1eb44332009-09-09 15:08:12 +000057 if (!cfg)
Mike Stump4c45aa12010-01-21 15:20:48 +000058 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), AddEHEdges);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000059 return cfg;
60}
61
62ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000063 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000064 PM = new ParentMap(getBody());
65 return *PM;
66}
67
68LiveVariables *AnalysisContext::getLiveVariables() {
69 if (!liveness) {
70 CFG *c = getCFG();
71 if (!c)
72 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Ted Kremenekb1a7b652009-11-26 02:31:33 +000074 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000075 liveness->runOnCFG(*c);
76 liveness->runOnAllBlocks(*c, 0, true);
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Zhongxing Xu97ab3942009-07-30 01:17:21 +000079 return liveness;
80}
81
Ted Kremenek23760022009-08-21 23:58:43 +000082AnalysisContext *AnalysisContextManager::getContext(const Decl *D) {
83 AnalysisContext *&AC = Contexts[D];
84 if (!AC)
85 AC = new AnalysisContext(D);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenek23760022009-08-21 23:58:43 +000087 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +000088}
Zhongxing Xu18c7c062009-08-03 07:23:22 +000089
Ted Kremenekdc0d9092009-12-04 00:50:10 +000090//===----------------------------------------------------------------------===//
91// FoldingSet profiling.
92//===----------------------------------------------------------------------===//
93
Ted Kremenekdc0d9092009-12-04 00:50:10 +000094void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
95 ContextKind ck,
96 AnalysisContext *ctx,
97 const LocationContext *parent,
98 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +000099 ID.AddInteger(ck);
100 ID.AddPointer(ctx);
101 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000102 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000103}
104
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000105void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000106 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000107}
108
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000109void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
110 Profile(ID, getAnalysisContext(), getParent(), Enter);
111}
112
113void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000114 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000115}
116
117//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000118// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000119//===----------------------------------------------------------------------===//
120
Ted Kremenek0ee41242009-12-04 01:28:56 +0000121template <typename LOC, typename DATA>
122const LOC*
123LocationContextManager::getLocationContext(AnalysisContext *ctx,
124 const LocationContext *parent,
125 const DATA *d) {
126 llvm::FoldingSetNodeID ID;
127 LOC::Profile(ID, ctx, parent, d);
128 void *InsertPos;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000129
Ted Kremenek0ee41242009-12-04 01:28:56 +0000130 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
131
132 if (!L) {
133 L = new LOC(ctx, parent, d);
134 Contexts.InsertNode(L, InsertPos);
135 }
136 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000137}
138
Ted Kremenek0ee41242009-12-04 01:28:56 +0000139const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000140LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000141 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000142 const Stmt *s, const CFGBlock *blk,
143 unsigned idx) {
144 llvm::FoldingSetNodeID ID;
145 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
146 void *InsertPos;
147 StackFrameContext *L =
148 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
149 if (!L) {
150 L = new StackFrameContext(ctx, parent, s, blk, idx);
151 Contexts.InsertNode(L, InsertPos);
152 }
153 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000154}
155
Ted Kremenek0ee41242009-12-04 01:28:56 +0000156const ScopeContext *
157LocationContextManager::getScope(AnalysisContext *ctx,
158 const LocationContext *parent,
159 const Stmt *s) {
160 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
161}
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000163//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000164// LocationContext methods.
165//===----------------------------------------------------------------------===//
166
167const StackFrameContext *LocationContext::getCurrentStackFrame() const {
168 const LocationContext *LC = this;
169 while (LC) {
170 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
171 return SFC;
172 LC = LC->getParent();
173 }
174 return NULL;
175}
176
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000177const StackFrameContext *
178LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
179 const LocationContext *LC = this;
180 while (LC) {
181 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
182 if (cast<DeclContext>(SFC->getDecl()) == DC)
183 return SFC;
184 }
185 LC = LC->getParent();
186 }
187 return NULL;
188}
189
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000190bool LocationContext::isParentOf(const LocationContext *LC) const {
191 do {
192 const LocationContext *Parent = LC->getParent();
193 if (Parent == this)
194 return true;
195 else
196 LC = Parent;
197 } while (LC);
198
199 return false;
200}
201
Ted Kremenek67d12872009-12-07 22:05:27 +0000202//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000203// Lazily generated map to query the external variables referenced by a Block.
204//===----------------------------------------------------------------------===//
205
206namespace {
207class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
208 BumpVector<const VarDecl*> &BEVals;
209 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000210 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000211 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000212public:
213 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
214 BumpVectorContext &bc)
215 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000216
217 bool IsTrackedDecl(const VarDecl *VD) {
218 const DeclContext *DC = VD->getDeclContext();
219 return IgnoredContexts.count(DC) == 0;
220 }
221
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000222 void VisitStmt(Stmt *S) {
223 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
224 if (Stmt *child = *I)
225 Visit(child);
226 }
Ted Kremenek85248732010-02-06 00:30:00 +0000227
228 void VisitDeclRefExpr(const DeclRefExpr *DR) {
229 // Non-local variables are also directly modified.
230 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
231 if (!VD->hasLocalStorage()) {
232 unsigned &flag = Visited[VD];
233 if (!flag) {
234 flag = 1;
235 BEVals.push_back(VD, BC);
236 }
237 }
238 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000239
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000240 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000241 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
242 unsigned &flag = Visited[VD];
243 if (!flag) {
244 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000245 if (IsTrackedDecl(VD))
246 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000247 }
248 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000249 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000250
251 void VisitBlockExpr(BlockExpr *BR) {
252 // Blocks containing blocks can transitively capture more variables.
253 IgnoredContexts.insert(BR->getBlockDecl());
254 Visit(BR->getBlockDecl()->getBody());
255 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000256};
257} // end anonymous namespace
258
259typedef BumpVector<const VarDecl*> DeclVec;
260
261static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
262 void *&Vec,
263 llvm::BumpPtrAllocator &A) {
264 if (Vec)
265 return (DeclVec*) Vec;
266
267 BumpVectorContext BC(A);
268 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
269 new (BV) DeclVec(BC, 10);
270
271 // Find the referenced variables.
272 FindBlockDeclRefExprsVals F(*BV, BC);
273 F.Visit(BD->getBody());
274
275 Vec = BV;
276 return BV;
277}
278
279std::pair<AnalysisContext::referenced_decls_iterator,
280 AnalysisContext::referenced_decls_iterator>
281AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
282 if (!ReferencedBlockVars)
283 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
284
285 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
286 return std::make_pair(V->begin(), V->end());
287}
288
289//===----------------------------------------------------------------------===//
290// Cleanup.
291//===----------------------------------------------------------------------===//
292
293AnalysisContext::~AnalysisContext() {
294 delete cfg;
295 delete liveness;
296 delete PM;
297 delete ReferencedBlockVars;
298}
299
300AnalysisContextManager::~AnalysisContextManager() {
301 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
302 delete I->second;
303}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000304
305LocationContext::~LocationContext() {}
306
307LocationContextManager::~LocationContextManager() {
308 clear();
309}
310
311void LocationContextManager::clear() {
312 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
313 E = Contexts.end(); I != E; ) {
314 LocationContext *LC = &*I;
315 ++I;
316 delete LC;
317 }
318
319 Contexts.clear();
320}
321