blob: 188a6f8dccfff2caa9ef9f30d05b8eaab9b89018 [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"
Tom Caredb34ab72010-08-23 19:51:57 +000021#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000022#include "clang/Analysis/AnalysisContext.h"
23#include "clang/Analysis/CFG.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000024#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000025#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000026#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000027
28using namespace clang;
29
Ted Kremenek58f5ec72009-10-20 21:39:41 +000030void AnalysisContextManager::clear() {
31 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
32 delete I->second;
33 Contexts.clear();
34}
35
Zhongxing Xu97ab3942009-07-30 01:17:21 +000036Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000037 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000038 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000039 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000040 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000041 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
42 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000043 else if (const FunctionTemplateDecl *FunTmpl
44 = dyn_cast_or_null<FunctionTemplateDecl>(D))
45 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000046
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000047 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000048}
49
Ted Kremenek82cd37c2009-08-21 23:25:54 +000050const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
51 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
52 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek82cd37c2009-08-21 23:25:54 +000054 return NULL;
55}
56
Zhongxing Xu97ab3942009-07-30 01:17:21 +000057CFG *AnalysisContext::getCFG() {
Ted Kremenek9b823e82010-08-03 00:09:51 +000058 if (UseUnoptimizedCFG)
59 return getUnoptimizedCFG();
60
Ted Kremenekd064fdc2010-03-23 00:13:23 +000061 if (!builtCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000062 CFG::BuildOptions B;
63 B.AddEHEdges = AddEHEdges;
64 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000065 // Even when the cfg is not successfully built, we don't
66 // want to try building it again.
67 builtCFG = true;
68 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000069 return cfg;
70}
71
Ted Kremenekad5a8942010-08-02 23:46:59 +000072CFG *AnalysisContext::getUnoptimizedCFG() {
73 if (!builtCompleteCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000074 CFG::BuildOptions B;
75 B.PruneTriviallyFalseEdges = false;
76 B.AddEHEdges = AddEHEdges;
77 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekad5a8942010-08-02 23:46:59 +000078 // Even when the cfg is not successfully built, we don't
79 // want to try building it again.
80 builtCompleteCFG = true;
81 }
82 return completeCFG;
83}
84
Zhongxing Xu97ab3942009-07-30 01:17:21 +000085ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000086 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000087 PM = new ParentMap(getBody());
88 return *PM;
89}
90
Tom Caredb34ab72010-08-23 19:51:57 +000091PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +000092 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +000093 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +000094 return PCA;
95}
96
Zhongxing Xu97ab3942009-07-30 01:17:21 +000097LiveVariables *AnalysisContext::getLiveVariables() {
98 if (!liveness) {
99 CFG *c = getCFG();
100 if (!c)
101 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000103 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000104 liveness->runOnCFG(*c);
105 liveness->runOnAllBlocks(*c, 0, true);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000108 return liveness;
109}
110
Tom Careec49bf42010-08-27 22:30:10 +0000111LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
112 if (!relaxedLiveness) {
113 CFG *c = getCFG();
114 if (!c)
115 return 0;
116
117 relaxedLiveness = new LiveVariables(*this, false);
118 relaxedLiveness->runOnCFG(*c);
119 relaxedLiveness->runOnAllBlocks(*c, 0, true);
120 }
121
122 return relaxedLiveness;
123}
124
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000125AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000126 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000127 AnalysisContext *&AC = Contexts[D];
128 if (!AC)
Ted Kremenek9b823e82010-08-03 00:09:51 +0000129 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenek23760022009-08-21 23:58:43 +0000131 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000132}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000133
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000134//===----------------------------------------------------------------------===//
135// FoldingSet profiling.
136//===----------------------------------------------------------------------===//
137
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000138void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
139 ContextKind ck,
140 AnalysisContext *ctx,
141 const LocationContext *parent,
142 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000143 ID.AddInteger(ck);
144 ID.AddPointer(ctx);
145 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000146 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000147}
148
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000149void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000150 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000151}
152
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000153void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
154 Profile(ID, getAnalysisContext(), getParent(), Enter);
155}
156
157void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000158 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000159}
160
161//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000162// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000163//===----------------------------------------------------------------------===//
164
Ted Kremenek0ee41242009-12-04 01:28:56 +0000165template <typename LOC, typename DATA>
166const LOC*
167LocationContextManager::getLocationContext(AnalysisContext *ctx,
168 const LocationContext *parent,
169 const DATA *d) {
170 llvm::FoldingSetNodeID ID;
171 LOC::Profile(ID, ctx, parent, d);
172 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000173
Ted Kremenek0ee41242009-12-04 01:28:56 +0000174 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000175
Ted Kremenek0ee41242009-12-04 01:28:56 +0000176 if (!L) {
177 L = new LOC(ctx, parent, d);
178 Contexts.InsertNode(L, InsertPos);
179 }
180 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000181}
182
Ted Kremenek0ee41242009-12-04 01:28:56 +0000183const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000184LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000185 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000186 const Stmt *s, const CFGBlock *blk,
187 unsigned idx) {
188 llvm::FoldingSetNodeID ID;
189 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
190 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000191 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000192 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
193 if (!L) {
194 L = new StackFrameContext(ctx, parent, s, blk, idx);
195 Contexts.InsertNode(L, InsertPos);
196 }
197 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000198}
199
Ted Kremenek0ee41242009-12-04 01:28:56 +0000200const ScopeContext *
201LocationContextManager::getScope(AnalysisContext *ctx,
202 const LocationContext *parent,
203 const Stmt *s) {
204 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
205}
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000207//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000208// LocationContext methods.
209//===----------------------------------------------------------------------===//
210
211const StackFrameContext *LocationContext::getCurrentStackFrame() const {
212 const LocationContext *LC = this;
213 while (LC) {
214 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
215 return SFC;
216 LC = LC->getParent();
217 }
218 return NULL;
219}
220
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000221const StackFrameContext *
222LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
223 const LocationContext *LC = this;
224 while (LC) {
225 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
226 if (cast<DeclContext>(SFC->getDecl()) == DC)
227 return SFC;
228 }
229 LC = LC->getParent();
230 }
231 return NULL;
232}
233
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000234bool LocationContext::isParentOf(const LocationContext *LC) const {
235 do {
236 const LocationContext *Parent = LC->getParent();
237 if (Parent == this)
238 return true;
239 else
240 LC = Parent;
241 } while (LC);
242
243 return false;
244}
245
Ted Kremenek67d12872009-12-07 22:05:27 +0000246//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000247// Lazily generated map to query the external variables referenced by a Block.
248//===----------------------------------------------------------------------===//
249
250namespace {
251class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
252 BumpVector<const VarDecl*> &BEVals;
253 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000254 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000255 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000256public:
257 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
258 BumpVectorContext &bc)
259 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000260
261 bool IsTrackedDecl(const VarDecl *VD) {
262 const DeclContext *DC = VD->getDeclContext();
263 return IgnoredContexts.count(DC) == 0;
264 }
265
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000266 void VisitStmt(Stmt *S) {
267 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
268 if (Stmt *child = *I)
269 Visit(child);
270 }
Ted Kremenek85248732010-02-06 00:30:00 +0000271
272 void VisitDeclRefExpr(const DeclRefExpr *DR) {
273 // Non-local variables are also directly modified.
274 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
275 if (!VD->hasLocalStorage()) {
276 unsigned &flag = Visited[VD];
277 if (!flag) {
278 flag = 1;
279 BEVals.push_back(VD, BC);
280 }
281 }
282 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000283
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000284 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000285 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
286 unsigned &flag = Visited[VD];
287 if (!flag) {
288 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000289 if (IsTrackedDecl(VD))
290 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000291 }
292 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000293 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000294
295 void VisitBlockExpr(BlockExpr *BR) {
296 // Blocks containing blocks can transitively capture more variables.
297 IgnoredContexts.insert(BR->getBlockDecl());
298 Visit(BR->getBlockDecl()->getBody());
299 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000300};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000301} // end anonymous namespace
302
303typedef BumpVector<const VarDecl*> DeclVec;
304
305static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
306 void *&Vec,
307 llvm::BumpPtrAllocator &A) {
308 if (Vec)
309 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000310
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000311 BumpVectorContext BC(A);
312 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
313 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000314
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000315 // Find the referenced variables.
316 FindBlockDeclRefExprsVals F(*BV, BC);
317 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000318
319 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000320 return BV;
321}
322
323std::pair<AnalysisContext::referenced_decls_iterator,
324 AnalysisContext::referenced_decls_iterator>
325AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
326 if (!ReferencedBlockVars)
327 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000328
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000329 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
330 return std::make_pair(V->begin(), V->end());
331}
332
333//===----------------------------------------------------------------------===//
334// Cleanup.
335//===----------------------------------------------------------------------===//
336
337AnalysisContext::~AnalysisContext() {
338 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000339 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000340 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000341 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000342 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000343 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000344 delete ReferencedBlockVars;
345}
346
347AnalysisContextManager::~AnalysisContextManager() {
348 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
349 delete I->second;
350}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000351
352LocationContext::~LocationContext() {}
353
354LocationContextManager::~LocationContextManager() {
355 clear();
356}
357
358void LocationContextManager::clear() {
359 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000360 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000361 LocationContext *LC = &*I;
362 ++I;
363 delete LC;
364 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000365
Ted Kremenek0ee41242009-12-04 01:28:56 +0000366 Contexts.clear();
367}
368