blob: 0c64610bea406921549431730540e51f3fc60a1b [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/Analysis/CFG.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Analysis/AnalysisContext.h"
17#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
Mike Stumpfa6ef182010-01-13 02:59:54 +000020#include "clang/AST/DeclTemplate.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000021#include "clang/AST/ParentMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000022#include "clang/AST/StmtVisitor.h"
23#include "clang/Analysis/Support/BumpVector.h"
Mike Stump87a05f12009-07-31 01:10:29 +000024#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000025
26using namespace clang;
27
Ted Kremenek58f5ec72009-10-20 21:39:41 +000028void AnalysisContextManager::clear() {
29 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
30 delete I->second;
31 Contexts.clear();
32}
33
Zhongxing Xu97ab3942009-07-30 01:17:21 +000034Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000035 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000036 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000037 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000038 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000039 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
40 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000041 else if (const FunctionTemplateDecl *FunTmpl
42 = dyn_cast_or_null<FunctionTemplateDecl>(D))
43 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000044
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000045 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000046}
47
Ted Kremenek82cd37c2009-08-21 23:25:54 +000048const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
49 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
50 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek82cd37c2009-08-21 23:25:54 +000052 return NULL;
53}
54
Zhongxing Xu97ab3942009-07-30 01:17:21 +000055CFG *AnalysisContext::getCFG() {
Mike Stump1eb44332009-09-09 15:08:12 +000056 if (!cfg)
Mike Stump4c45aa12010-01-21 15:20:48 +000057 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), AddEHEdges);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000058 return cfg;
59}
60
61ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000062 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000063 PM = new ParentMap(getBody());
64 return *PM;
65}
66
67LiveVariables *AnalysisContext::getLiveVariables() {
68 if (!liveness) {
69 CFG *c = getCFG();
70 if (!c)
71 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenekb1a7b652009-11-26 02:31:33 +000073 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000074 liveness->runOnCFG(*c);
75 liveness->runOnAllBlocks(*c, 0, true);
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Zhongxing Xu97ab3942009-07-30 01:17:21 +000078 return liveness;
79}
80
Ted Kremenek23760022009-08-21 23:58:43 +000081AnalysisContext *AnalysisContextManager::getContext(const Decl *D) {
82 AnalysisContext *&AC = Contexts[D];
83 if (!AC)
84 AC = new AnalysisContext(D);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremenek23760022009-08-21 23:58:43 +000086 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +000087}
Zhongxing Xu18c7c062009-08-03 07:23:22 +000088
Ted Kremenekdc0d9092009-12-04 00:50:10 +000089//===----------------------------------------------------------------------===//
90// FoldingSet profiling.
91//===----------------------------------------------------------------------===//
92
Ted Kremenekdc0d9092009-12-04 00:50:10 +000093void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
94 ContextKind ck,
95 AnalysisContext *ctx,
96 const LocationContext *parent,
97 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +000098 ID.AddInteger(ck);
99 ID.AddPointer(ctx);
100 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000101 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000102}
103
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000104void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000105 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000106}
107
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000108void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
109 Profile(ID, getAnalysisContext(), getParent(), Enter);
110}
111
112void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000113 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000114}
115
116//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000117// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000118//===----------------------------------------------------------------------===//
119
Ted Kremenek0ee41242009-12-04 01:28:56 +0000120template <typename LOC, typename DATA>
121const LOC*
122LocationContextManager::getLocationContext(AnalysisContext *ctx,
123 const LocationContext *parent,
124 const DATA *d) {
125 llvm::FoldingSetNodeID ID;
126 LOC::Profile(ID, ctx, parent, d);
127 void *InsertPos;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000128
Ted Kremenek0ee41242009-12-04 01:28:56 +0000129 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
130
131 if (!L) {
132 L = new LOC(ctx, parent, d);
133 Contexts.InsertNode(L, InsertPos);
134 }
135 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000136}
137
Ted Kremenek0ee41242009-12-04 01:28:56 +0000138const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000139LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000140 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000141 const Stmt *s, const CFGBlock *blk,
142 unsigned idx) {
143 llvm::FoldingSetNodeID ID;
144 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
145 void *InsertPos;
146 StackFrameContext *L =
147 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
148 if (!L) {
149 L = new StackFrameContext(ctx, parent, s, blk, idx);
150 Contexts.InsertNode(L, InsertPos);
151 }
152 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000153}
154
Ted Kremenek0ee41242009-12-04 01:28:56 +0000155const ScopeContext *
156LocationContextManager::getScope(AnalysisContext *ctx,
157 const LocationContext *parent,
158 const Stmt *s) {
159 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
160}
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000162//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000163// LocationContext methods.
164//===----------------------------------------------------------------------===//
165
166const StackFrameContext *LocationContext::getCurrentStackFrame() const {
167 const LocationContext *LC = this;
168 while (LC) {
169 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
170 return SFC;
171 LC = LC->getParent();
172 }
173 return NULL;
174}
175
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000176const StackFrameContext *
177LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
178 const LocationContext *LC = this;
179 while (LC) {
180 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
181 if (cast<DeclContext>(SFC->getDecl()) == DC)
182 return SFC;
183 }
184 LC = LC->getParent();
185 }
186 return NULL;
187}
188
Ted Kremenek67d12872009-12-07 22:05:27 +0000189//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000190// Lazily generated map to query the external variables referenced by a Block.
191//===----------------------------------------------------------------------===//
192
193namespace {
194class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
195 BumpVector<const VarDecl*> &BEVals;
196 BumpVectorContext &BC;
197public:
198 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
199 BumpVectorContext &bc)
200 : BEVals(bevals), BC(bc) {}
201
202 void VisitStmt(Stmt *S) {
203 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
204 if (Stmt *child = *I)
205 Visit(child);
206 }
207
208 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
209 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
210 BEVals.push_back(VD, BC);
211 }
212};
213} // end anonymous namespace
214
215typedef BumpVector<const VarDecl*> DeclVec;
216
217static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
218 void *&Vec,
219 llvm::BumpPtrAllocator &A) {
220 if (Vec)
221 return (DeclVec*) Vec;
222
223 BumpVectorContext BC(A);
224 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
225 new (BV) DeclVec(BC, 10);
226
227 // Find the referenced variables.
228 FindBlockDeclRefExprsVals F(*BV, BC);
229 F.Visit(BD->getBody());
230
231 Vec = BV;
232 return BV;
233}
234
235std::pair<AnalysisContext::referenced_decls_iterator,
236 AnalysisContext::referenced_decls_iterator>
237AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
238 if (!ReferencedBlockVars)
239 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
240
241 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
242 return std::make_pair(V->begin(), V->end());
243}
244
245//===----------------------------------------------------------------------===//
246// Cleanup.
247//===----------------------------------------------------------------------===//
248
249AnalysisContext::~AnalysisContext() {
250 delete cfg;
251 delete liveness;
252 delete PM;
253 delete ReferencedBlockVars;
254}
255
256AnalysisContextManager::~AnalysisContextManager() {
257 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
258 delete I->second;
259}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000260
261LocationContext::~LocationContext() {}
262
263LocationContextManager::~LocationContextManager() {
264 clear();
265}
266
267void LocationContextManager::clear() {
268 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
269 E = Contexts.end(); I != E; ) {
270 LocationContext *LC = &*I;
271 ++I;
272 delete LC;
273 }
274
275 Contexts.clear();
276}
277