blob: ef0a50f98ee62da7a0d03b1e19597c3fde841e1c [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) {
Ted Kremenekad5a8942010-08-02 23:46:59 +000058 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), true, 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
Ted Kremenekad5a8942010-08-02 23:46:59 +000066CFG *AnalysisContext::getUnoptimizedCFG() {
67 if (!builtCompleteCFG) {
68 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(),
69 false, AddEHEdges);
70 // Even when the cfg is not successfully built, we don't
71 // want to try building it again.
72 builtCompleteCFG = true;
73 }
74 return completeCFG;
75}
76
Zhongxing Xu97ab3942009-07-30 01:17:21 +000077ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000078 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000079 PM = new ParentMap(getBody());
80 return *PM;
81}
82
83LiveVariables *AnalysisContext::getLiveVariables() {
84 if (!liveness) {
85 CFG *c = getCFG();
86 if (!c)
87 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenekb1a7b652009-11-26 02:31:33 +000089 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000090 liveness->runOnCFG(*c);
91 liveness->runOnAllBlocks(*c, 0, true);
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Zhongxing Xu97ab3942009-07-30 01:17:21 +000094 return liveness;
95}
96
Zhongxing Xuc6238d22010-07-19 01:31:21 +000097AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000098 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +000099 AnalysisContext *&AC = Contexts[D];
100 if (!AC)
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000101 AC = new AnalysisContext(D, TU);
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek23760022009-08-21 23:58:43 +0000103 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000104}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000105
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000106//===----------------------------------------------------------------------===//
107// FoldingSet profiling.
108//===----------------------------------------------------------------------===//
109
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000110void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
111 ContextKind ck,
112 AnalysisContext *ctx,
113 const LocationContext *parent,
114 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000115 ID.AddInteger(ck);
116 ID.AddPointer(ctx);
117 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000118 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000119}
120
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000121void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000122 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000123}
124
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000125void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
126 Profile(ID, getAnalysisContext(), getParent(), Enter);
127}
128
129void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000130 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000131}
132
133//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000134// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000135//===----------------------------------------------------------------------===//
136
Ted Kremenek0ee41242009-12-04 01:28:56 +0000137template <typename LOC, typename DATA>
138const LOC*
139LocationContextManager::getLocationContext(AnalysisContext *ctx,
140 const LocationContext *parent,
141 const DATA *d) {
142 llvm::FoldingSetNodeID ID;
143 LOC::Profile(ID, ctx, parent, d);
144 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000145
Ted Kremenek0ee41242009-12-04 01:28:56 +0000146 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000147
Ted Kremenek0ee41242009-12-04 01:28:56 +0000148 if (!L) {
149 L = new LOC(ctx, parent, d);
150 Contexts.InsertNode(L, InsertPos);
151 }
152 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000153}
154
Ted Kremenek0ee41242009-12-04 01:28:56 +0000155const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000156LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000157 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000158 const Stmt *s, const CFGBlock *blk,
159 unsigned idx) {
160 llvm::FoldingSetNodeID ID;
161 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
162 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000163 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000164 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
165 if (!L) {
166 L = new StackFrameContext(ctx, parent, s, blk, idx);
167 Contexts.InsertNode(L, InsertPos);
168 }
169 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000170}
171
Ted Kremenek0ee41242009-12-04 01:28:56 +0000172const ScopeContext *
173LocationContextManager::getScope(AnalysisContext *ctx,
174 const LocationContext *parent,
175 const Stmt *s) {
176 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
177}
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000179//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000180// LocationContext methods.
181//===----------------------------------------------------------------------===//
182
183const StackFrameContext *LocationContext::getCurrentStackFrame() const {
184 const LocationContext *LC = this;
185 while (LC) {
186 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
187 return SFC;
188 LC = LC->getParent();
189 }
190 return NULL;
191}
192
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000193const StackFrameContext *
194LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
195 const LocationContext *LC = this;
196 while (LC) {
197 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
198 if (cast<DeclContext>(SFC->getDecl()) == DC)
199 return SFC;
200 }
201 LC = LC->getParent();
202 }
203 return NULL;
204}
205
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000206bool LocationContext::isParentOf(const LocationContext *LC) const {
207 do {
208 const LocationContext *Parent = LC->getParent();
209 if (Parent == this)
210 return true;
211 else
212 LC = Parent;
213 } while (LC);
214
215 return false;
216}
217
Ted Kremenek67d12872009-12-07 22:05:27 +0000218//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000219// Lazily generated map to query the external variables referenced by a Block.
220//===----------------------------------------------------------------------===//
221
222namespace {
223class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
224 BumpVector<const VarDecl*> &BEVals;
225 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000226 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000227 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000228public:
229 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
230 BumpVectorContext &bc)
231 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000232
233 bool IsTrackedDecl(const VarDecl *VD) {
234 const DeclContext *DC = VD->getDeclContext();
235 return IgnoredContexts.count(DC) == 0;
236 }
237
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000238 void VisitStmt(Stmt *S) {
239 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
240 if (Stmt *child = *I)
241 Visit(child);
242 }
Ted Kremenek85248732010-02-06 00:30:00 +0000243
244 void VisitDeclRefExpr(const DeclRefExpr *DR) {
245 // Non-local variables are also directly modified.
246 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
247 if (!VD->hasLocalStorage()) {
248 unsigned &flag = Visited[VD];
249 if (!flag) {
250 flag = 1;
251 BEVals.push_back(VD, BC);
252 }
253 }
254 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000255
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000256 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000257 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
258 unsigned &flag = Visited[VD];
259 if (!flag) {
260 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000261 if (IsTrackedDecl(VD))
262 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000263 }
264 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000265 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000266
267 void VisitBlockExpr(BlockExpr *BR) {
268 // Blocks containing blocks can transitively capture more variables.
269 IgnoredContexts.insert(BR->getBlockDecl());
270 Visit(BR->getBlockDecl()->getBody());
271 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000272};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000273} // end anonymous namespace
274
275typedef BumpVector<const VarDecl*> DeclVec;
276
277static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
278 void *&Vec,
279 llvm::BumpPtrAllocator &A) {
280 if (Vec)
281 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000282
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000283 BumpVectorContext BC(A);
284 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
285 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000286
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000287 // Find the referenced variables.
288 FindBlockDeclRefExprsVals F(*BV, BC);
289 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000290
291 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000292 return BV;
293}
294
295std::pair<AnalysisContext::referenced_decls_iterator,
296 AnalysisContext::referenced_decls_iterator>
297AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
298 if (!ReferencedBlockVars)
299 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000300
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000301 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
302 return std::make_pair(V->begin(), V->end());
303}
304
305//===----------------------------------------------------------------------===//
306// Cleanup.
307//===----------------------------------------------------------------------===//
308
309AnalysisContext::~AnalysisContext() {
310 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000311 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000312 delete liveness;
313 delete PM;
314 delete ReferencedBlockVars;
315}
316
317AnalysisContextManager::~AnalysisContextManager() {
318 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
319 delete I->second;
320}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000321
322LocationContext::~LocationContext() {}
323
324LocationContextManager::~LocationContextManager() {
325 clear();
326}
327
328void LocationContextManager::clear() {
329 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000330 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000331 LocationContext *LC = &*I;
332 ++I;
333 delete LC;
334 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000335
Ted Kremenek0ee41242009-12-04 01:28:56 +0000336 Contexts.clear();
337}
338