blob: 62097ef20dd9965919d07bde70ee74f781c63b00 [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 Kremenek42461ee2011-02-23 01:51:59 +000022#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000023#include "clang/Analysis/AnalysisContext.h"
24#include "clang/Analysis/CFG.h"
Ted Kremenek283a3582011-02-23 01:51:53 +000025#include "clang/Analysis/CFGStmtMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000026#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000027#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000028#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000029
30using namespace clang;
31
Ted Kremenek58f5ec72009-10-20 21:39:41 +000032void AnalysisContextManager::clear() {
33 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
34 delete I->second;
35 Contexts.clear();
36}
37
Zhongxing Xu97ab3942009-07-30 01:17:21 +000038Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000039 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000040 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000041 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000042 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000043 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
44 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000045 else if (const FunctionTemplateDecl *FunTmpl
46 = dyn_cast_or_null<FunctionTemplateDecl>(D))
47 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000048
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000049 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000050}
51
Ted Kremenek82cd37c2009-08-21 23:25:54 +000052const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
53 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
54 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenek82cd37c2009-08-21 23:25:54 +000056 return NULL;
57}
58
Zhongxing Xu97ab3942009-07-30 01:17:21 +000059CFG *AnalysisContext::getCFG() {
Ted Kremenek9b823e82010-08-03 00:09:51 +000060 if (UseUnoptimizedCFG)
61 return getUnoptimizedCFG();
62
Ted Kremenekd064fdc2010-03-23 00:13:23 +000063 if (!builtCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000064 CFG::BuildOptions B;
65 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000066 B.AddImplicitDtors = AddImplicitDtors;
67 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000068 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000069 // Even when the cfg is not successfully built, we don't
70 // want to try building it again.
71 builtCFG = true;
72 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000073 return cfg;
74}
75
Ted Kremenekad5a8942010-08-02 23:46:59 +000076CFG *AnalysisContext::getUnoptimizedCFG() {
77 if (!builtCompleteCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000078 CFG::BuildOptions B;
79 B.PruneTriviallyFalseEdges = false;
80 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000081 B.AddImplicitDtors = AddImplicitDtors;
82 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000083 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekad5a8942010-08-02 23:46:59 +000084 // Even when the cfg is not successfully built, we don't
85 // want to try building it again.
86 builtCompleteCFG = true;
87 }
88 return completeCFG;
89}
90
Ted Kremenek283a3582011-02-23 01:51:53 +000091CFGStmtMap *AnalysisContext::getCFGStmtMap() {
92 if (cfgStmtMap)
93 return cfgStmtMap;
94
95 if (CFG *c = getCFG()) {
96 cfgStmtMap = CFGStmtMap::Build(c, &getParentMap());
97 return cfgStmtMap;
98 }
99
100 return 0;
101}
Ted Kremenek42461ee2011-02-23 01:51:59 +0000102
103CFGReachabilityAnalysis *AnalysisContext::getCFGReachablityAnalysis() {
104 if (CFA)
105 return CFA;
Ted Kremenek283a3582011-02-23 01:51:53 +0000106
Ted Kremenek42461ee2011-02-23 01:51:59 +0000107 if (CFG *c = getCFG()) {
108 CFA = new CFGReachabilityAnalysis(*c);
109 return CFA;
110 }
111
112 return 0;
113}
Ted Kremenek283a3582011-02-23 01:51:53 +0000114
Anders Carlsson04eeba42011-01-16 22:05:23 +0000115void AnalysisContext::dumpCFG() {
116 getCFG()->dump(getASTContext().getLangOptions());
117}
118
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000119ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +0000120 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000121 PM = new ParentMap(getBody());
122 return *PM;
123}
124
Tom Caredb34ab72010-08-23 19:51:57 +0000125PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +0000126 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +0000127 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +0000128 return PCA;
129}
130
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000131LiveVariables *AnalysisContext::getLiveVariables() {
132 if (!liveness) {
133 CFG *c = getCFG();
134 if (!c)
135 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000137 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000138 liveness->runOnCFG(*c);
139 liveness->runOnAllBlocks(*c, 0, true);
140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000142 return liveness;
143}
144
Tom Careec49bf42010-08-27 22:30:10 +0000145LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
146 if (!relaxedLiveness) {
147 CFG *c = getCFG();
148 if (!c)
149 return 0;
150
151 relaxedLiveness = new LiveVariables(*this, false);
152 relaxedLiveness->runOnCFG(*c);
153 relaxedLiveness->runOnAllBlocks(*c, 0, true);
154 }
155
156 return relaxedLiveness;
157}
158
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000159AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000160 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000161 AnalysisContext *&AC = Contexts[D];
162 if (!AC)
Marcin Swiderski9121ba22010-09-30 07:41:24 +0000163 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG, false,
164 AddImplicitDtors, AddInitializers);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek23760022009-08-21 23:58:43 +0000166 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000167}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000168
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000169//===----------------------------------------------------------------------===//
170// FoldingSet profiling.
171//===----------------------------------------------------------------------===//
172
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000173void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
174 ContextKind ck,
175 AnalysisContext *ctx,
176 const LocationContext *parent,
177 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000178 ID.AddInteger(ck);
179 ID.AddPointer(ctx);
180 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000181 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000182}
183
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000184void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000185 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000186}
187
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000188void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
189 Profile(ID, getAnalysisContext(), getParent(), Enter);
190}
191
192void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000193 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000194}
195
196//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000197// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000198//===----------------------------------------------------------------------===//
199
Ted Kremenek0ee41242009-12-04 01:28:56 +0000200template <typename LOC, typename DATA>
201const LOC*
202LocationContextManager::getLocationContext(AnalysisContext *ctx,
203 const LocationContext *parent,
204 const DATA *d) {
205 llvm::FoldingSetNodeID ID;
206 LOC::Profile(ID, ctx, parent, d);
207 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000208
Ted Kremenek0ee41242009-12-04 01:28:56 +0000209 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000210
Ted Kremenek0ee41242009-12-04 01:28:56 +0000211 if (!L) {
212 L = new LOC(ctx, parent, d);
213 Contexts.InsertNode(L, InsertPos);
214 }
215 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000216}
217
Ted Kremenek0ee41242009-12-04 01:28:56 +0000218const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000219LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000220 const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000221 const Stmt *s,
Zhongxing Xud7064342010-11-24 13:08:51 +0000222 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000223 llvm::FoldingSetNodeID ID;
Ted Kremenek892697d2010-12-16 07:46:53 +0000224 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000225 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000226 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000227 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
228 if (!L) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000229 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000230 Contexts.InsertNode(L, InsertPos);
231 }
232 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000233}
234
Ted Kremenek0ee41242009-12-04 01:28:56 +0000235const ScopeContext *
236LocationContextManager::getScope(AnalysisContext *ctx,
237 const LocationContext *parent,
238 const Stmt *s) {
239 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
240}
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000242//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000243// LocationContext methods.
244//===----------------------------------------------------------------------===//
245
246const StackFrameContext *LocationContext::getCurrentStackFrame() const {
247 const LocationContext *LC = this;
248 while (LC) {
249 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
250 return SFC;
251 LC = LC->getParent();
252 }
253 return NULL;
254}
255
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000256const StackFrameContext *
257LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
258 const LocationContext *LC = this;
259 while (LC) {
260 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
261 if (cast<DeclContext>(SFC->getDecl()) == DC)
262 return SFC;
263 }
264 LC = LC->getParent();
265 }
266 return NULL;
267}
268
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000269bool LocationContext::isParentOf(const LocationContext *LC) const {
270 do {
271 const LocationContext *Parent = LC->getParent();
272 if (Parent == this)
273 return true;
274 else
275 LC = Parent;
276 } while (LC);
277
278 return false;
279}
280
Ted Kremenek67d12872009-12-07 22:05:27 +0000281//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000282// Lazily generated map to query the external variables referenced by a Block.
283//===----------------------------------------------------------------------===//
284
285namespace {
286class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
287 BumpVector<const VarDecl*> &BEVals;
288 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000289 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000290 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000291public:
292 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
293 BumpVectorContext &bc)
294 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000295
296 bool IsTrackedDecl(const VarDecl *VD) {
297 const DeclContext *DC = VD->getDeclContext();
298 return IgnoredContexts.count(DC) == 0;
299 }
300
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000301 void VisitStmt(Stmt *S) {
John McCall7502c1d2011-02-13 04:07:26 +0000302 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000303 if (Stmt *child = *I)
304 Visit(child);
305 }
Ted Kremenek85248732010-02-06 00:30:00 +0000306
307 void VisitDeclRefExpr(const DeclRefExpr *DR) {
308 // Non-local variables are also directly modified.
309 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
310 if (!VD->hasLocalStorage()) {
311 unsigned &flag = Visited[VD];
312 if (!flag) {
313 flag = 1;
314 BEVals.push_back(VD, BC);
315 }
316 }
317 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000318
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000319 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000320 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
321 unsigned &flag = Visited[VD];
322 if (!flag) {
323 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000324 if (IsTrackedDecl(VD))
325 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000326 }
327 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000328 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000329
330 void VisitBlockExpr(BlockExpr *BR) {
331 // Blocks containing blocks can transitively capture more variables.
332 IgnoredContexts.insert(BR->getBlockDecl());
333 Visit(BR->getBlockDecl()->getBody());
334 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000335};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000336} // end anonymous namespace
337
338typedef BumpVector<const VarDecl*> DeclVec;
339
340static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
341 void *&Vec,
342 llvm::BumpPtrAllocator &A) {
343 if (Vec)
344 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000345
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000346 BumpVectorContext BC(A);
347 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
348 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000349
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000350 // Find the referenced variables.
351 FindBlockDeclRefExprsVals F(*BV, BC);
352 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000353
354 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000355 return BV;
356}
357
358std::pair<AnalysisContext::referenced_decls_iterator,
359 AnalysisContext::referenced_decls_iterator>
360AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
361 if (!ReferencedBlockVars)
362 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000363
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000364 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
365 return std::make_pair(V->begin(), V->end());
366}
367
368//===----------------------------------------------------------------------===//
369// Cleanup.
370//===----------------------------------------------------------------------===//
371
372AnalysisContext::~AnalysisContext() {
373 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000374 delete completeCFG;
Ted Kremenek283a3582011-02-23 01:51:53 +0000375 delete cfgStmtMap;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000376 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000377 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000378 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000379 delete PCA;
Ted Kremenek42461ee2011-02-23 01:51:59 +0000380 delete CFA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000381 delete ReferencedBlockVars;
382}
383
384AnalysisContextManager::~AnalysisContextManager() {
385 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
386 delete I->second;
387}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000388
389LocationContext::~LocationContext() {}
390
391LocationContextManager::~LocationContextManager() {
392 clear();
393}
394
395void LocationContextManager::clear() {
396 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000397 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000398 LocationContext *LC = &*I;
399 ++I;
400 delete LC;
401 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000402
Ted Kremenek0ee41242009-12-04 01:28:56 +0000403 Contexts.clear();
404}
405