blob: 538d559c16cd3bee72bd096d95df1fcf92cf40e9 [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"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Analysis/CFG.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000023#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000024#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000025#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000026
27using namespace clang;
28
Ted Kremenek58f5ec72009-10-20 21:39:41 +000029void AnalysisContextManager::clear() {
30 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
31 delete I->second;
32 Contexts.clear();
33}
34
Zhongxing Xu97ab3942009-07-30 01:17:21 +000035Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000036 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000037 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000038 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000039 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000040 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
41 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000042 else if (const FunctionTemplateDecl *FunTmpl
43 = dyn_cast_or_null<FunctionTemplateDecl>(D))
44 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000045
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000046 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000047}
48
Ted Kremenek82cd37c2009-08-21 23:25:54 +000049const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
50 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
51 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek82cd37c2009-08-21 23:25:54 +000053 return NULL;
54}
55
Zhongxing Xu97ab3942009-07-30 01:17:21 +000056CFG *AnalysisContext::getCFG() {
Ted Kremenekd064fdc2010-03-23 00:13:23 +000057 if (!builtCFG) {
Mike Stump4c45aa12010-01-21 15:20:48 +000058 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), AddEHEdges);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000059 // Even when the cfg is not successfully built, we don't
60 // want to try building it again.
61 builtCFG = true;
62 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000063 return cfg;
64}
65
66ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000067 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000068 PM = new ParentMap(getBody());
69 return *PM;
70}
71
72LiveVariables *AnalysisContext::getLiveVariables() {
73 if (!liveness) {
74 CFG *c = getCFG();
75 if (!c)
76 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenekb1a7b652009-11-26 02:31:33 +000078 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000079 liveness->runOnCFG(*c);
80 liveness->runOnAllBlocks(*c, 0, true);
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Zhongxing Xu97ab3942009-07-30 01:17:21 +000083 return liveness;
84}
85
Zhongxing Xuc6238d22010-07-19 01:31:21 +000086AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
87 const idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +000088 AnalysisContext *&AC = Contexts[D];
89 if (!AC)
Zhongxing Xuc6238d22010-07-19 01:31:21 +000090 AC = new AnalysisContext(D, TU);
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenek23760022009-08-21 23:58:43 +000092 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +000093}
Zhongxing Xu18c7c062009-08-03 07:23:22 +000094
Ted Kremenekdc0d9092009-12-04 00:50:10 +000095//===----------------------------------------------------------------------===//
96// FoldingSet profiling.
97//===----------------------------------------------------------------------===//
98
Ted Kremenekdc0d9092009-12-04 00:50:10 +000099void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
100 ContextKind ck,
101 AnalysisContext *ctx,
102 const LocationContext *parent,
103 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000104 ID.AddInteger(ck);
105 ID.AddPointer(ctx);
106 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000107 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000108}
109
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000110void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000111 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000112}
113
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000114void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
115 Profile(ID, getAnalysisContext(), getParent(), Enter);
116}
117
118void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000119 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000120}
121
122//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000123// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000124//===----------------------------------------------------------------------===//
125
Ted Kremenek0ee41242009-12-04 01:28:56 +0000126template <typename LOC, typename DATA>
127const LOC*
128LocationContextManager::getLocationContext(AnalysisContext *ctx,
129 const LocationContext *parent,
130 const DATA *d) {
131 llvm::FoldingSetNodeID ID;
132 LOC::Profile(ID, ctx, parent, d);
133 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000134
Ted Kremenek0ee41242009-12-04 01:28:56 +0000135 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000136
Ted Kremenek0ee41242009-12-04 01:28:56 +0000137 if (!L) {
138 L = new LOC(ctx, parent, d);
139 Contexts.InsertNode(L, InsertPos);
140 }
141 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000142}
143
Ted Kremenek0ee41242009-12-04 01:28:56 +0000144const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000145LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000146 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000147 const Stmt *s, const CFGBlock *blk,
148 unsigned idx) {
149 llvm::FoldingSetNodeID ID;
150 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
151 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000152 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000153 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
154 if (!L) {
155 L = new StackFrameContext(ctx, parent, s, blk, idx);
156 Contexts.InsertNode(L, InsertPos);
157 }
158 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000159}
160
Ted Kremenek0ee41242009-12-04 01:28:56 +0000161const ScopeContext *
162LocationContextManager::getScope(AnalysisContext *ctx,
163 const LocationContext *parent,
164 const Stmt *s) {
165 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
166}
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000168//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000169// LocationContext methods.
170//===----------------------------------------------------------------------===//
171
172const StackFrameContext *LocationContext::getCurrentStackFrame() const {
173 const LocationContext *LC = this;
174 while (LC) {
175 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
176 return SFC;
177 LC = LC->getParent();
178 }
179 return NULL;
180}
181
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000182const StackFrameContext *
183LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
184 const LocationContext *LC = this;
185 while (LC) {
186 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
187 if (cast<DeclContext>(SFC->getDecl()) == DC)
188 return SFC;
189 }
190 LC = LC->getParent();
191 }
192 return NULL;
193}
194
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000195bool LocationContext::isParentOf(const LocationContext *LC) const {
196 do {
197 const LocationContext *Parent = LC->getParent();
198 if (Parent == this)
199 return true;
200 else
201 LC = Parent;
202 } while (LC);
203
204 return false;
205}
206
Ted Kremenek67d12872009-12-07 22:05:27 +0000207//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000208// Lazily generated map to query the external variables referenced by a Block.
209//===----------------------------------------------------------------------===//
210
211namespace {
212class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
213 BumpVector<const VarDecl*> &BEVals;
214 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000215 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000216 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000217public:
218 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
219 BumpVectorContext &bc)
220 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000221
222 bool IsTrackedDecl(const VarDecl *VD) {
223 const DeclContext *DC = VD->getDeclContext();
224 return IgnoredContexts.count(DC) == 0;
225 }
226
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000227 void VisitStmt(Stmt *S) {
228 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
229 if (Stmt *child = *I)
230 Visit(child);
231 }
Ted Kremenek85248732010-02-06 00:30:00 +0000232
233 void VisitDeclRefExpr(const DeclRefExpr *DR) {
234 // Non-local variables are also directly modified.
235 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
236 if (!VD->hasLocalStorage()) {
237 unsigned &flag = Visited[VD];
238 if (!flag) {
239 flag = 1;
240 BEVals.push_back(VD, BC);
241 }
242 }
243 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000244
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000245 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000246 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
247 unsigned &flag = Visited[VD];
248 if (!flag) {
249 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000250 if (IsTrackedDecl(VD))
251 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000252 }
253 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000254 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000255
256 void VisitBlockExpr(BlockExpr *BR) {
257 // Blocks containing blocks can transitively capture more variables.
258 IgnoredContexts.insert(BR->getBlockDecl());
259 Visit(BR->getBlockDecl()->getBody());
260 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000261};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000262} // end anonymous namespace
263
264typedef BumpVector<const VarDecl*> DeclVec;
265
266static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
267 void *&Vec,
268 llvm::BumpPtrAllocator &A) {
269 if (Vec)
270 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000271
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000272 BumpVectorContext BC(A);
273 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
274 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000275
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000276 // Find the referenced variables.
277 FindBlockDeclRefExprsVals F(*BV, BC);
278 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000279
280 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000281 return BV;
282}
283
284std::pair<AnalysisContext::referenced_decls_iterator,
285 AnalysisContext::referenced_decls_iterator>
286AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
287 if (!ReferencedBlockVars)
288 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000289
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000290 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
291 return std::make_pair(V->begin(), V->end());
292}
293
294//===----------------------------------------------------------------------===//
295// Cleanup.
296//===----------------------------------------------------------------------===//
297
298AnalysisContext::~AnalysisContext() {
299 delete cfg;
300 delete liveness;
301 delete PM;
302 delete ReferencedBlockVars;
303}
304
305AnalysisContextManager::~AnalysisContextManager() {
306 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
307 delete I->second;
308}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000309
310LocationContext::~LocationContext() {}
311
312LocationContextManager::~LocationContextManager() {
313 clear();
314}
315
316void LocationContextManager::clear() {
317 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000318 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000319 LocationContext *LC = &*I;
320 ++I;
321 delete LC;
322 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323
Ted Kremenek0ee41242009-12-04 01:28:56 +0000324 Contexts.clear();
325}
326