blob: ccd5088f2ec757ab135b3cf8d15501aa9c8e0685 [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;
Ted Kremenek85248732010-02-06 00:30:00 +0000197 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000198public:
199 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
200 BumpVectorContext &bc)
201 : BEVals(bevals), BC(bc) {}
202
203 void VisitStmt(Stmt *S) {
204 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
205 if (Stmt *child = *I)
206 Visit(child);
207 }
Ted Kremenek85248732010-02-06 00:30:00 +0000208
209 void VisitDeclRefExpr(const DeclRefExpr *DR) {
210 // Non-local variables are also directly modified.
211 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
212 if (!VD->hasLocalStorage()) {
213 unsigned &flag = Visited[VD];
214 if (!flag) {
215 flag = 1;
216 BEVals.push_back(VD, BC);
217 }
218 }
219 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000220
221 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000222 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
223 unsigned &flag = Visited[VD];
224 if (!flag) {
225 flag = 1;
226 BEVals.push_back(VD, BC);
227 }
228 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000229 }
230};
231} // end anonymous namespace
232
233typedef BumpVector<const VarDecl*> DeclVec;
234
235static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
236 void *&Vec,
237 llvm::BumpPtrAllocator &A) {
238 if (Vec)
239 return (DeclVec*) Vec;
240
241 BumpVectorContext BC(A);
242 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
243 new (BV) DeclVec(BC, 10);
244
245 // Find the referenced variables.
246 FindBlockDeclRefExprsVals F(*BV, BC);
247 F.Visit(BD->getBody());
248
249 Vec = BV;
250 return BV;
251}
252
253std::pair<AnalysisContext::referenced_decls_iterator,
254 AnalysisContext::referenced_decls_iterator>
255AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
256 if (!ReferencedBlockVars)
257 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
258
259 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
260 return std::make_pair(V->begin(), V->end());
261}
262
263//===----------------------------------------------------------------------===//
264// Cleanup.
265//===----------------------------------------------------------------------===//
266
267AnalysisContext::~AnalysisContext() {
268 delete cfg;
269 delete liveness;
270 delete PM;
271 delete ReferencedBlockVars;
272}
273
274AnalysisContextManager::~AnalysisContextManager() {
275 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
276 delete I->second;
277}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000278
279LocationContext::~LocationContext() {}
280
281LocationContextManager::~LocationContextManager() {
282 clear();
283}
284
285void LocationContextManager::clear() {
286 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
287 E = Contexts.end(); I != E; ) {
288 LocationContext *LC = &*I;
289 ++I;
290 delete LC;
291 }
292
293 Contexts.clear();
294}
295