blob: d9933e85cb9103eebdf9cda50e06e208d0066eab [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
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000189bool LocationContext::isParentOf(const LocationContext *LC) const {
190 do {
191 const LocationContext *Parent = LC->getParent();
192 if (Parent == this)
193 return true;
194 else
195 LC = Parent;
196 } while (LC);
197
198 return false;
199}
200
Ted Kremenek67d12872009-12-07 22:05:27 +0000201//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000202// Lazily generated map to query the external variables referenced by a Block.
203//===----------------------------------------------------------------------===//
204
205namespace {
206class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
207 BumpVector<const VarDecl*> &BEVals;
208 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000209 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000210public:
211 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
212 BumpVectorContext &bc)
213 : BEVals(bevals), BC(bc) {}
214
215 void VisitStmt(Stmt *S) {
216 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
217 if (Stmt *child = *I)
218 Visit(child);
219 }
Ted Kremenek85248732010-02-06 00:30:00 +0000220
221 void VisitDeclRefExpr(const DeclRefExpr *DR) {
222 // Non-local variables are also directly modified.
223 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
224 if (!VD->hasLocalStorage()) {
225 unsigned &flag = Visited[VD];
226 if (!flag) {
227 flag = 1;
228 BEVals.push_back(VD, BC);
229 }
230 }
231 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000232
233 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000234 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
235 unsigned &flag = Visited[VD];
236 if (!flag) {
237 flag = 1;
238 BEVals.push_back(VD, BC);
239 }
240 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000241 }
242};
243} // end anonymous namespace
244
245typedef BumpVector<const VarDecl*> DeclVec;
246
247static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
248 void *&Vec,
249 llvm::BumpPtrAllocator &A) {
250 if (Vec)
251 return (DeclVec*) Vec;
252
253 BumpVectorContext BC(A);
254 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
255 new (BV) DeclVec(BC, 10);
256
257 // Find the referenced variables.
258 FindBlockDeclRefExprsVals F(*BV, BC);
259 F.Visit(BD->getBody());
260
261 Vec = BV;
262 return BV;
263}
264
265std::pair<AnalysisContext::referenced_decls_iterator,
266 AnalysisContext::referenced_decls_iterator>
267AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
268 if (!ReferencedBlockVars)
269 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
270
271 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
272 return std::make_pair(V->begin(), V->end());
273}
274
275//===----------------------------------------------------------------------===//
276// Cleanup.
277//===----------------------------------------------------------------------===//
278
279AnalysisContext::~AnalysisContext() {
280 delete cfg;
281 delete liveness;
282 delete PM;
283 delete ReferencedBlockVars;
284}
285
286AnalysisContextManager::~AnalysisContextManager() {
287 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
288 delete I->second;
289}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000290
291LocationContext::~LocationContext() {}
292
293LocationContextManager::~LocationContextManager() {
294 clear();
295}
296
297void LocationContextManager::clear() {
298 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
299 E = Contexts.end(); I != E; ) {
300 LocationContext *LC = &*I;
301 ++I;
302 delete LC;
303 }
304
305 Contexts.clear();
306}
307