blob: e3d2c8df353d85f70b6b48bf329d191fbaf25089 [file] [log] [blame]
Ted Kremenek326be562010-01-25 05:19:37 +00001//=== AnalysisContext.h - 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
15#ifndef LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H
16#define LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H
17
18#include "clang/AST/Decl.h"
Ted Kremenek892697d2010-12-16 07:46:53 +000019#include "clang/AST/Expr.h"
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000020#include "clang/Analysis/CFG.h"
Ted Kremenek326be562010-01-25 05:19:37 +000021#include "llvm/ADT/OwningPtr.h"
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +000022#include "llvm/ADT/IntrusiveRefCntPtr.h"
Ted Kremenek326be562010-01-25 05:19:37 +000023#include "llvm/ADT/FoldingSet.h"
24#include "llvm/ADT/PointerUnion.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/Support/Allocator.h"
27
28namespace clang {
29
30class Decl;
31class Stmt;
Ted Kremenekaf13d5b2011-03-19 01:00:33 +000032class CFGReverseBlockReachabilityAnalysis;
Ted Kremenek283a3582011-02-23 01:51:53 +000033class CFGStmtMap;
Ted Kremenek326be562010-01-25 05:19:37 +000034class LiveVariables;
35class ParentMap;
Tom Caredb34ab72010-08-23 19:51:57 +000036class PseudoConstantAnalysis;
Ted Kremenek326be562010-01-25 05:19:37 +000037class ImplicitParamDecl;
38class LocationContextManager;
39class StackFrameContext;
Ted Kremenekd064fdc2010-03-23 00:13:23 +000040
Zhongxing Xuc6238d22010-07-19 01:31:21 +000041namespace idx { class TranslationUnit; }
42
Ted Kremenek326be562010-01-25 05:19:37 +000043/// AnalysisContext contains the context data for the function or method under
44/// analysis.
45class AnalysisContext {
46 const Decl *D;
47
Zhongxing Xuc6238d22010-07-19 01:31:21 +000048 // TranslationUnit is NULL if we don't have multiple translation units.
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000049 idx::TranslationUnit *TU;
Zhongxing Xuc6238d22010-07-19 01:31:21 +000050
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000051 llvm::OwningPtr<CFG> cfg, completeCFG;
52 llvm::OwningPtr<CFGStmtMap> cfgStmtMap;
53
54 CFG::BuildOptions cfgBuildOptions;
55 CFG::BuildOptions::ForcedBlkExprs *forcedBlkExprs;
56
Ted Kremenekad5a8942010-08-02 23:46:59 +000057 bool builtCFG, builtCompleteCFG;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000058
59 llvm::OwningPtr<LiveVariables> liveness;
60 llvm::OwningPtr<LiveVariables> relaxedLiveness;
61 llvm::OwningPtr<ParentMap> PM;
62 llvm::OwningPtr<PseudoConstantAnalysis> PCA;
Ted Kremenekaf13d5b2011-03-19 01:00:33 +000063 llvm::OwningPtr<CFGReverseBlockReachabilityAnalysis> CFA;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000064
Ted Kremenek326be562010-01-25 05:19:37 +000065 llvm::BumpPtrAllocator A;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000066
67 // FIXME: remove.
68 llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars;
69
Ted Kremenek326be562010-01-25 05:19:37 +000070public:
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +000071 AnalysisContext(const Decl *d, idx::TranslationUnit *tu);
72
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000073 AnalysisContext(const Decl *d, idx::TranslationUnit *tu,
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +000074 const CFG::BuildOptions &buildOptions);
Ted Kremenek326be562010-01-25 05:19:37 +000075
76 ~AnalysisContext();
77
78 ASTContext &getASTContext() { return D->getASTContext(); }
Zhongxing Xuc6238d22010-07-19 01:31:21 +000079 const Decl *getDecl() const { return D; }
80
Zhongxing Xu2ce43c82010-07-22 13:52:13 +000081 idx::TranslationUnit *getTranslationUnit() const { return TU; }
Zhongxing Xuc6238d22010-07-19 01:31:21 +000082
Ted Kremenek74fb1a42011-07-19 14:18:43 +000083 /// Return the build options used to construct the CFG.
84 CFG::BuildOptions &getCFGBuildOptions() {
85 return cfgBuildOptions;
86 }
87
88 const CFG::BuildOptions &getCFGBuildOptions() const {
89 return cfgBuildOptions;
90 }
91
Ted Kremenek326be562010-01-25 05:19:37 +000092 /// getAddEHEdges - Return true iff we are adding exceptional edges from
93 /// callExprs. If this is false, then try/catch statements and blocks
94 /// reachable from them can appear to be dead in the CFG, analysis passes must
95 /// cope with that.
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000096 bool getAddEHEdges() const { return cfgBuildOptions.AddEHEdges; }
97 bool getUseUnoptimizedCFG() const {
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +000098 return !cfgBuildOptions.PruneTriviallyFalseEdges;
Ted Kremenekb8ad5ee2011-03-10 01:14:05 +000099 }
100 bool getAddImplicitDtors() const { return cfgBuildOptions.AddImplicitDtors; }
101 bool getAddInitializers() const { return cfgBuildOptions.AddInitializers; }
Ted Kremenek9b823e82010-08-03 00:09:51 +0000102
Ted Kremenek0d28d362011-03-10 03:50:34 +0000103 void registerForcedBlockExpression(const Stmt *stmt);
104 const CFGBlock *getBlockForRegisteredExpression(const Stmt *stmt);
105
Ted Kremenek326be562010-01-25 05:19:37 +0000106 Stmt *getBody();
107 CFG *getCFG();
Ted Kremenek283a3582011-02-23 01:51:53 +0000108
109 CFGStmtMap *getCFGStmtMap();
Anders Carlsson04eeba42011-01-16 22:05:23 +0000110
Ted Kremenekaf13d5b2011-03-19 01:00:33 +0000111 CFGReverseBlockReachabilityAnalysis *getCFGReachablityAnalysis();
Ted Kremenek42461ee2011-02-23 01:51:59 +0000112
Ted Kremenekad5a8942010-08-02 23:46:59 +0000113 /// Return a version of the CFG without any edges pruned.
114 CFG *getUnoptimizedCFG();
115
Anders Carlsson04eeba42011-01-16 22:05:23 +0000116 void dumpCFG();
117
Chandler Carruth5d989942011-07-06 16:21:37 +0000118 /// \brief Returns true if we have built a CFG for this analysis context.
119 /// Note that this doesn't correspond to whether or not a valid CFG exists, it
120 /// corresponds to whether we *attempted* to build one.
121 bool isCFGBuilt() const { return builtCFG; }
122
Ted Kremenek326be562010-01-25 05:19:37 +0000123 ParentMap &getParentMap();
Tom Caredb34ab72010-08-23 19:51:57 +0000124 PseudoConstantAnalysis *getPseudoConstantAnalysis();
Ted Kremenek326be562010-01-25 05:19:37 +0000125 LiveVariables *getLiveVariables();
Tom Careec49bf42010-08-27 22:30:10 +0000126 LiveVariables *getRelaxedLiveVariables();
Ted Kremenek326be562010-01-25 05:19:37 +0000127
128 typedef const VarDecl * const * referenced_decls_iterator;
129
130 std::pair<referenced_decls_iterator, referenced_decls_iterator>
131 getReferencedBlockVars(const BlockDecl *BD);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000132
Ted Kremenek326be562010-01-25 05:19:37 +0000133 /// Return the ImplicitParamDecl* associated with 'self' if this
134 /// AnalysisContext wraps an ObjCMethodDecl. Returns NULL otherwise.
135 const ImplicitParamDecl *getSelfDecl() const;
136};
137
138class AnalysisContextManager {
139 typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap;
140 ContextMap Contexts;
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +0000141 CFG::BuildOptions cfgBuildOptions;
Ted Kremenek326be562010-01-25 05:19:37 +0000142public:
Marcin Swiderski9121ba22010-09-30 07:41:24 +0000143 AnalysisContextManager(bool useUnoptimizedCFG = false,
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +0000144 bool addImplicitDtors = false,
145 bool addInitializers = false);
Ted Kremenek9b823e82010-08-03 00:09:51 +0000146
Ted Kremenek326be562010-01-25 05:19:37 +0000147 ~AnalysisContextManager();
148
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000149 AnalysisContext *getContext(const Decl *D, idx::TranslationUnit *TU = 0);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000150
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +0000151 bool getUseUnoptimizedCFG() const {
152 return !cfgBuildOptions.PruneTriviallyFalseEdges;
153 }
154
155 CFG::BuildOptions &getCFGBuildOptions() {
156 return cfgBuildOptions;
157 }
Ted Kremenek9b823e82010-08-03 00:09:51 +0000158
Ted Kremenekbc5cb8a2011-07-21 05:22:47 +0000159 /// Discard all previously created AnalysisContexts.
Ted Kremenek326be562010-01-25 05:19:37 +0000160 void clear();
161};
162
163class LocationContext : public llvm::FoldingSetNode {
164public:
165 enum ContextKind { StackFrame, Scope, Block };
166
167private:
168 ContextKind Kind;
Zhongxing Xua02d8932010-07-20 02:14:22 +0000169
170 // AnalysisContext can't be const since some methods may modify its member.
Ted Kremenek326be562010-01-25 05:19:37 +0000171 AnalysisContext *Ctx;
Zhongxing Xua02d8932010-07-20 02:14:22 +0000172
Ted Kremenek326be562010-01-25 05:19:37 +0000173 const LocationContext *Parent;
174
175protected:
176 LocationContext(ContextKind k, AnalysisContext *ctx,
177 const LocationContext *parent)
178 : Kind(k), Ctx(ctx), Parent(parent) {}
179
180public:
181 virtual ~LocationContext();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000182
Ted Kremenek326be562010-01-25 05:19:37 +0000183 ContextKind getKind() const { return Kind; }
184
185 AnalysisContext *getAnalysisContext() const { return Ctx; }
186
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000187 idx::TranslationUnit *getTranslationUnit() const {
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000188 return Ctx->getTranslationUnit();
189 }
190
Ted Kremenek326be562010-01-25 05:19:37 +0000191 const LocationContext *getParent() const { return Parent; }
192
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000193 bool isParentOf(const LocationContext *LC) const;
194
Ted Kremenek326be562010-01-25 05:19:37 +0000195 const Decl *getDecl() const { return getAnalysisContext()->getDecl(); }
196
197 CFG *getCFG() const { return getAnalysisContext()->getCFG(); }
198
199 LiveVariables *getLiveVariables() const {
200 return getAnalysisContext()->getLiveVariables();
201 }
202
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000203 ParentMap &getParentMap() const {
Ted Kremenek326be562010-01-25 05:19:37 +0000204 return getAnalysisContext()->getParentMap();
205 }
206
207 const ImplicitParamDecl *getSelfDecl() const {
208 return Ctx->getSelfDecl();
209 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000210
Ted Kremenek326be562010-01-25 05:19:37 +0000211 const StackFrameContext *getCurrentStackFrame() const;
212 const StackFrameContext *
213 getStackFrameForDeclContext(const DeclContext *DC) const;
214
215 virtual void Profile(llvm::FoldingSetNodeID &ID) = 0;
216
217 static bool classof(const LocationContext*) { return true; }
218
219public:
220 static void ProfileCommon(llvm::FoldingSetNodeID &ID,
221 ContextKind ck,
222 AnalysisContext *ctx,
223 const LocationContext *parent,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000224 const void *data);
Ted Kremenek326be562010-01-25 05:19:37 +0000225};
226
227class StackFrameContext : public LocationContext {
Zhongxing Xu26c9cb52011-01-10 11:28:29 +0000228 // The callsite where this stack frame is established.
Ted Kremenek892697d2010-12-16 07:46:53 +0000229 const Stmt *CallSite;
Ted Kremenek326be562010-01-25 05:19:37 +0000230
231 // The parent block of the callsite.
232 const CFGBlock *Block;
233
234 // The index of the callsite in the CFGBlock.
235 unsigned Index;
236
237 friend class LocationContextManager;
238 StackFrameContext(AnalysisContext *ctx, const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000239 const Stmt *s, const CFGBlock *blk,
Zhongxing Xud7064342010-11-24 13:08:51 +0000240 unsigned idx)
Ted Kremenek892697d2010-12-16 07:46:53 +0000241 : LocationContext(StackFrame, ctx, parent), CallSite(s),
Zhongxing Xud7064342010-11-24 13:08:51 +0000242 Block(blk), Index(idx) {}
Ted Kremenek326be562010-01-25 05:19:37 +0000243
244public:
245 ~StackFrameContext() {}
246
Ted Kremenek892697d2010-12-16 07:46:53 +0000247 const Stmt *getCallSite() const { return CallSite; }
Zhongxing Xud7064342010-11-24 13:08:51 +0000248
Ted Kremenek326be562010-01-25 05:19:37 +0000249 const CFGBlock *getCallSiteBlock() const { return Block; }
250
251 unsigned getIndex() const { return Index; }
252
253 void Profile(llvm::FoldingSetNodeID &ID);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000254
Ted Kremenek326be562010-01-25 05:19:37 +0000255 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000256 const LocationContext *parent, const Stmt *s,
Ted Kremenek892697d2010-12-16 07:46:53 +0000257 const CFGBlock *blk, unsigned idx) {
Ted Kremenek326be562010-01-25 05:19:37 +0000258 ProfileCommon(ID, StackFrame, ctx, parent, s);
259 ID.AddPointer(blk);
260 ID.AddInteger(idx);
261 }
262
Ted Kremenek9c378f72011-08-12 23:37:29 +0000263 static bool classof(const LocationContext *Ctx) {
Ted Kremenek326be562010-01-25 05:19:37 +0000264 return Ctx->getKind() == StackFrame;
265 }
266};
267
268class ScopeContext : public LocationContext {
269 const Stmt *Enter;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000270
Ted Kremenek326be562010-01-25 05:19:37 +0000271 friend class LocationContextManager;
272 ScopeContext(AnalysisContext *ctx, const LocationContext *parent,
273 const Stmt *s)
274 : LocationContext(Scope, ctx, parent), Enter(s) {}
275
276public:
277 ~ScopeContext() {}
278
279 void Profile(llvm::FoldingSetNodeID &ID);
280
281 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
282 const LocationContext *parent, const Stmt *s) {
283 ProfileCommon(ID, Scope, ctx, parent, s);
284 }
285
Ted Kremenek9c378f72011-08-12 23:37:29 +0000286 static bool classof(const LocationContext *Ctx) {
Ted Kremenek326be562010-01-25 05:19:37 +0000287 return Ctx->getKind() == Scope;
288 }
289};
290
291class BlockInvocationContext : public LocationContext {
292 // FIXME: Add back context-sensivity (we don't want libAnalysis to know
293 // about MemRegion).
294 const BlockDecl *BD;
295
296 friend class LocationContextManager;
297
298 BlockInvocationContext(AnalysisContext *ctx, const LocationContext *parent,
299 const BlockDecl *bd)
300 : LocationContext(Block, ctx, parent), BD(bd) {}
301
302public:
303 ~BlockInvocationContext() {}
304
305 const BlockDecl *getBlockDecl() const { return BD; }
306
307 void Profile(llvm::FoldingSetNodeID &ID);
308
309 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
310 const LocationContext *parent, const BlockDecl *bd) {
311 ProfileCommon(ID, Block, ctx, parent, bd);
312 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000313
Ted Kremenek9c378f72011-08-12 23:37:29 +0000314 static bool classof(const LocationContext *Ctx) {
Ted Kremenek326be562010-01-25 05:19:37 +0000315 return Ctx->getKind() == Block;
316 }
317};
318
319class LocationContextManager {
320 llvm::FoldingSet<LocationContext> Contexts;
321public:
322 ~LocationContextManager();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323
Ted Kremenek326be562010-01-25 05:19:37 +0000324 const StackFrameContext *getStackFrame(AnalysisContext *ctx,
325 const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000326 const Stmt *s,
Zhongxing Xud7064342010-11-24 13:08:51 +0000327 const CFGBlock *blk, unsigned idx);
Ted Kremenek326be562010-01-25 05:19:37 +0000328
329 const ScopeContext *getScope(AnalysisContext *ctx,
330 const LocationContext *parent,
331 const Stmt *s);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000332
Ted Kremenek326be562010-01-25 05:19:37 +0000333 /// Discard all previously created LocationContext objects.
334 void clear();
335private:
336 template <typename LOC, typename DATA>
337 const LOC *getLocationContext(AnalysisContext *ctx,
338 const LocationContext *parent,
339 const DATA *d);
340};
341
342} // end clang namespace
343#endif