blob: 584173a33fbcc4020ef5b59a62d4433f2ac332f0 [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"
19#include "llvm/ADT/OwningPtr.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/Support/Allocator.h"
24
25namespace clang {
26
27class Decl;
28class Stmt;
29class CFG;
30class CFGBlock;
31class LiveVariables;
32class ParentMap;
33class ImplicitParamDecl;
34class LocationContextManager;
35class StackFrameContext;
Ted Kremenekd064fdc2010-03-23 00:13:23 +000036
Zhongxing Xuc6238d22010-07-19 01:31:21 +000037namespace idx { class TranslationUnit; }
38
Ted Kremenek326be562010-01-25 05:19:37 +000039/// AnalysisContext contains the context data for the function or method under
40/// analysis.
41class AnalysisContext {
42 const Decl *D;
43
Zhongxing Xuc6238d22010-07-19 01:31:21 +000044 // TranslationUnit is NULL if we don't have multiple translation units.
45 const idx::TranslationUnit *TU;
46
Ted Kremenek326be562010-01-25 05:19:37 +000047 // AnalysisContext owns the following data.
48 CFG *cfg;
Ted Kremenekd064fdc2010-03-23 00:13:23 +000049 bool builtCFG;
Ted Kremenek326be562010-01-25 05:19:37 +000050 LiveVariables *liveness;
51 ParentMap *PM;
52 llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars;
53 llvm::BumpPtrAllocator A;
54 bool AddEHEdges;
55public:
Zhongxing Xuc6238d22010-07-19 01:31:21 +000056 AnalysisContext(const Decl *d, const idx::TranslationUnit *tu,
57 bool addehedges = false)
58 : D(d), TU(tu), cfg(0), builtCFG(false), liveness(0), PM(0),
Ted Kremenekd064fdc2010-03-23 00:13:23 +000059 ReferencedBlockVars(0), AddEHEdges(addehedges) {}
Ted Kremenek326be562010-01-25 05:19:37 +000060
61 ~AnalysisContext();
62
63 ASTContext &getASTContext() { return D->getASTContext(); }
Zhongxing Xuc6238d22010-07-19 01:31:21 +000064 const Decl *getDecl() const { return D; }
65
66 const idx::TranslationUnit *getTranslationUnit() const { return TU; }
67
Ted Kremenek326be562010-01-25 05:19:37 +000068 /// getAddEHEdges - Return true iff we are adding exceptional edges from
69 /// callExprs. If this is false, then try/catch statements and blocks
70 /// reachable from them can appear to be dead in the CFG, analysis passes must
71 /// cope with that.
72 bool getAddEHEdges() const { return AddEHEdges; }
73 Stmt *getBody();
74 CFG *getCFG();
75 ParentMap &getParentMap();
76 LiveVariables *getLiveVariables();
77
78 typedef const VarDecl * const * referenced_decls_iterator;
79
80 std::pair<referenced_decls_iterator, referenced_decls_iterator>
81 getReferencedBlockVars(const BlockDecl *BD);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000082
Ted Kremenek326be562010-01-25 05:19:37 +000083 /// Return the ImplicitParamDecl* associated with 'self' if this
84 /// AnalysisContext wraps an ObjCMethodDecl. Returns NULL otherwise.
85 const ImplicitParamDecl *getSelfDecl() const;
86};
87
88class AnalysisContextManager {
89 typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap;
90 ContextMap Contexts;
91public:
92 ~AnalysisContextManager();
93
Zhongxing Xuc6238d22010-07-19 01:31:21 +000094 AnalysisContext *getContext(const Decl *D,const idx::TranslationUnit *TU = 0);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000095
Ted Kremenek326be562010-01-25 05:19:37 +000096 // Discard all previously created AnalysisContexts.
97 void clear();
98};
99
100class LocationContext : public llvm::FoldingSetNode {
101public:
102 enum ContextKind { StackFrame, Scope, Block };
103
104private:
105 ContextKind Kind;
106 AnalysisContext *Ctx;
107 const LocationContext *Parent;
108
109protected:
110 LocationContext(ContextKind k, AnalysisContext *ctx,
111 const LocationContext *parent)
112 : Kind(k), Ctx(ctx), Parent(parent) {}
113
114public:
115 virtual ~LocationContext();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000116
Ted Kremenek326be562010-01-25 05:19:37 +0000117 ContextKind getKind() const { return Kind; }
118
119 AnalysisContext *getAnalysisContext() const { return Ctx; }
120
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000121 const idx::TranslationUnit *getTranslationUnit() const {
122 return Ctx->getTranslationUnit();
123 }
124
Ted Kremenek326be562010-01-25 05:19:37 +0000125 const LocationContext *getParent() const { return Parent; }
126
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000127 bool isParentOf(const LocationContext *LC) const;
128
Ted Kremenek326be562010-01-25 05:19:37 +0000129 const Decl *getDecl() const { return getAnalysisContext()->getDecl(); }
130
131 CFG *getCFG() const { return getAnalysisContext()->getCFG(); }
132
133 LiveVariables *getLiveVariables() const {
134 return getAnalysisContext()->getLiveVariables();
135 }
136
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000137 ParentMap &getParentMap() const {
Ted Kremenek326be562010-01-25 05:19:37 +0000138 return getAnalysisContext()->getParentMap();
139 }
140
141 const ImplicitParamDecl *getSelfDecl() const {
142 return Ctx->getSelfDecl();
143 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000144
Ted Kremenek326be562010-01-25 05:19:37 +0000145 const StackFrameContext *getCurrentStackFrame() const;
146 const StackFrameContext *
147 getStackFrameForDeclContext(const DeclContext *DC) const;
148
149 virtual void Profile(llvm::FoldingSetNodeID &ID) = 0;
150
151 static bool classof(const LocationContext*) { return true; }
152
153public:
154 static void ProfileCommon(llvm::FoldingSetNodeID &ID,
155 ContextKind ck,
156 AnalysisContext *ctx,
157 const LocationContext *parent,
158 const void* data);
159};
160
161class StackFrameContext : public LocationContext {
162 // The callsite where this stack frame is established.
163 const Stmt *CallSite;
164
165 // The parent block of the callsite.
166 const CFGBlock *Block;
167
168 // The index of the callsite in the CFGBlock.
169 unsigned Index;
170
171 friend class LocationContextManager;
172 StackFrameContext(AnalysisContext *ctx, const LocationContext *parent,
173 const Stmt *s, const CFGBlock *blk, unsigned idx)
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000174 : LocationContext(StackFrame, ctx, parent), CallSite(s), Block(blk),
Ted Kremenek326be562010-01-25 05:19:37 +0000175 Index(idx) {}
176
177public:
178 ~StackFrameContext() {}
179
180 const Stmt *getCallSite() const { return CallSite; }
181
182 const CFGBlock *getCallSiteBlock() const { return Block; }
183
184 unsigned getIndex() const { return Index; }
185
186 void Profile(llvm::FoldingSetNodeID &ID);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000187
Ted Kremenek326be562010-01-25 05:19:37 +0000188 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000189 const LocationContext *parent, const Stmt *s,
Ted Kremenek326be562010-01-25 05:19:37 +0000190 const CFGBlock *blk, unsigned idx) {
191 ProfileCommon(ID, StackFrame, ctx, parent, s);
192 ID.AddPointer(blk);
193 ID.AddInteger(idx);
194 }
195
196 static bool classof(const LocationContext* Ctx) {
197 return Ctx->getKind() == StackFrame;
198 }
199};
200
201class ScopeContext : public LocationContext {
202 const Stmt *Enter;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000203
Ted Kremenek326be562010-01-25 05:19:37 +0000204 friend class LocationContextManager;
205 ScopeContext(AnalysisContext *ctx, const LocationContext *parent,
206 const Stmt *s)
207 : LocationContext(Scope, ctx, parent), Enter(s) {}
208
209public:
210 ~ScopeContext() {}
211
212 void Profile(llvm::FoldingSetNodeID &ID);
213
214 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
215 const LocationContext *parent, const Stmt *s) {
216 ProfileCommon(ID, Scope, ctx, parent, s);
217 }
218
219 static bool classof(const LocationContext* Ctx) {
220 return Ctx->getKind() == Scope;
221 }
222};
223
224class BlockInvocationContext : public LocationContext {
225 // FIXME: Add back context-sensivity (we don't want libAnalysis to know
226 // about MemRegion).
227 const BlockDecl *BD;
228
229 friend class LocationContextManager;
230
231 BlockInvocationContext(AnalysisContext *ctx, const LocationContext *parent,
232 const BlockDecl *bd)
233 : LocationContext(Block, ctx, parent), BD(bd) {}
234
235public:
236 ~BlockInvocationContext() {}
237
238 const BlockDecl *getBlockDecl() const { return BD; }
239
240 void Profile(llvm::FoldingSetNodeID &ID);
241
242 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
243 const LocationContext *parent, const BlockDecl *bd) {
244 ProfileCommon(ID, Block, ctx, parent, bd);
245 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000246
Ted Kremenek326be562010-01-25 05:19:37 +0000247 static bool classof(const LocationContext* Ctx) {
248 return Ctx->getKind() == Block;
249 }
250};
251
252class LocationContextManager {
253 llvm::FoldingSet<LocationContext> Contexts;
254public:
255 ~LocationContextManager();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000256
Ted Kremenek326be562010-01-25 05:19:37 +0000257 const StackFrameContext *getStackFrame(AnalysisContext *ctx,
258 const LocationContext *parent,
259 const Stmt *s, const CFGBlock *blk,
260 unsigned idx);
261
262 const ScopeContext *getScope(AnalysisContext *ctx,
263 const LocationContext *parent,
264 const Stmt *s);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000265
Ted Kremenek326be562010-01-25 05:19:37 +0000266 /// Discard all previously created LocationContext objects.
267 void clear();
268private:
269 template <typename LOC, typename DATA>
270 const LOC *getLocationContext(AnalysisContext *ctx,
271 const LocationContext *parent,
272 const DATA *d);
273};
274
275} // end clang namespace
276#endif