1 // AnalysisDeclContext.h - Analysis context for Path Sens analysis -*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines AnalysisDeclContext, a class that manages the analysis
11 // context data for path sensitive analysis.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_ANALYSIS_ANALYSISDECLCONTEXT_H
16 #define LLVM_CLANG_ANALYSIS_ANALYSISDECLCONTEXT_H
18 #include "clang/AST/DeclBase.h"
19 #include "clang/Analysis/BodyFarm.h"
20 #include "clang/Analysis/CFG.h"
21 #include "clang/Analysis/CodeInjector.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/Support/Allocator.h"
33 class AnalysisDeclContextManager;
36 class BlockInvocationContext;
37 class CFGReverseBlockReachabilityAnalysis;
39 class ImplicitParamDecl;
40 class LocationContext;
41 class LocationContextManager;
43 class StackFrameContext;
47 /// The base class of a hierarchy of objects representing analyses tied
48 /// to AnalysisDeclContext.
49 class ManagedAnalysis {
51 ManagedAnalysis() = default;
54 virtual ~ManagedAnalysis();
56 // Subclasses need to implement:
58 // static const void *getTag();
60 // Which returns a fixed pointer address to distinguish classes of
61 // analysis objects. They also need to implement:
63 // static [Derived*] create(AnalysisDeclContext &Ctx);
65 // which creates the analysis object given an AnalysisDeclContext.
68 /// AnalysisDeclContext contains the context data for the function or method
70 class AnalysisDeclContext {
71 /// Backpoint to the AnalysisManager object that created this
72 /// AnalysisDeclContext. This may be null.
73 AnalysisDeclContextManager *Manager;
77 std::unique_ptr<CFG> cfg, completeCFG;
78 std::unique_ptr<CFGStmtMap> cfgStmtMap;
80 CFG::BuildOptions cfgBuildOptions;
81 CFG::BuildOptions::ForcedBlkExprs *forcedBlkExprs = nullptr;
83 bool builtCFG = false;
84 bool builtCompleteCFG = false;
85 std::unique_ptr<ParentMap> PM;
86 std::unique_ptr<CFGReverseBlockReachabilityAnalysis> CFA;
88 llvm::BumpPtrAllocator A;
90 llvm::DenseMap<const BlockDecl *,void *> *ReferencedBlockVars = nullptr;
92 void *ManagedAnalyses = nullptr;
95 AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
98 AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
100 const CFG::BuildOptions &BuildOptions);
102 ~AnalysisDeclContext();
104 ASTContext &getASTContext() const { return D->getASTContext(); }
105 const Decl *getDecl() const { return D; }
107 /// Return the AnalysisDeclContextManager (if any) that created
108 /// this AnalysisDeclContext.
109 AnalysisDeclContextManager *getManager() const {
113 /// Return the build options used to construct the CFG.
114 CFG::BuildOptions &getCFGBuildOptions() {
115 return cfgBuildOptions;
118 const CFG::BuildOptions &getCFGBuildOptions() const {
119 return cfgBuildOptions;
122 /// getAddEHEdges - Return true iff we are adding exceptional edges from
123 /// callExprs. If this is false, then try/catch statements and blocks
124 /// reachable from them can appear to be dead in the CFG, analysis passes must
126 bool getAddEHEdges() const { return cfgBuildOptions.AddEHEdges; }
127 bool getUseUnoptimizedCFG() const {
128 return !cfgBuildOptions.PruneTriviallyFalseEdges;
130 bool getAddImplicitDtors() const { return cfgBuildOptions.AddImplicitDtors; }
131 bool getAddInitializers() const { return cfgBuildOptions.AddInitializers; }
133 void registerForcedBlockExpression(const Stmt *stmt);
134 const CFGBlock *getBlockForRegisteredExpression(const Stmt *stmt);
136 /// Get the body of the Declaration.
137 Stmt *getBody() const;
139 /// Get the body of the Declaration.
140 /// \param[out] IsAutosynthesized Specifies if the body is auto-generated
142 Stmt *getBody(bool &IsAutosynthesized) const;
144 /// Checks if the body of the Decl is generated by the BodyFarm.
146 /// Note, the lookup is not free. We are going to call getBody behind
149 bool isBodyAutosynthesized() const;
151 /// Checks if the body of the Decl is generated by the BodyFarm from a
154 /// Note, the lookup is not free. We are going to call getBody behind
157 bool isBodyAutosynthesizedFromModelFile() const;
161 CFGStmtMap *getCFGStmtMap();
163 CFGReverseBlockReachabilityAnalysis *getCFGReachablityAnalysis();
165 /// Return a version of the CFG without any edges pruned.
166 CFG *getUnoptimizedCFG();
168 void dumpCFG(bool ShowColors);
170 /// Returns true if we have built a CFG for this analysis context.
171 /// Note that this doesn't correspond to whether or not a valid CFG exists, it
172 /// corresponds to whether we *attempted* to build one.
173 bool isCFGBuilt() const { return builtCFG; }
175 ParentMap &getParentMap();
177 using referenced_decls_iterator = const VarDecl * const *;
179 llvm::iterator_range<referenced_decls_iterator>
180 getReferencedBlockVars(const BlockDecl *BD);
182 /// Return the ImplicitParamDecl* associated with 'self' if this
183 /// AnalysisDeclContext wraps an ObjCMethodDecl. Returns NULL otherwise.
184 const ImplicitParamDecl *getSelfDecl() const;
186 const StackFrameContext *getStackFrame(LocationContext const *Parent,
191 const BlockInvocationContext *
192 getBlockInvocationContext(const LocationContext *parent,
194 const void *ContextData);
196 /// Return the specified analysis object, lazily running the analysis if
197 /// necessary. Return NULL if the analysis could not run.
198 template <typename T>
200 const void *tag = T::getTag();
201 ManagedAnalysis *&data = getAnalysisImpl(tag);
203 data = T::create(*this);
205 return static_cast<T *>(data);
208 /// Returns true if the root namespace of the given declaration is the 'std'
210 static bool isInStdNamespace(const Decl *D);
213 ManagedAnalysis *&getAnalysisImpl(const void* tag);
215 LocationContextManager &getLocationContextManager();
218 class LocationContext : public llvm::FoldingSetNode {
220 enum ContextKind { StackFrame, Scope, Block };
225 // AnalysisDeclContext can't be const since some methods may modify its
227 AnalysisDeclContext *Ctx;
229 const LocationContext *Parent;
233 LocationContext(ContextKind k, AnalysisDeclContext *ctx,
234 const LocationContext *parent,
236 : Kind(k), Ctx(ctx), Parent(parent), ID(ID) {}
239 virtual ~LocationContext();
241 ContextKind getKind() const { return Kind; }
243 int64_t getID() const {
247 AnalysisDeclContext *getAnalysisDeclContext() const { return Ctx; }
249 const LocationContext *getParent() const { return Parent; }
251 bool isParentOf(const LocationContext *LC) const;
253 const Decl *getDecl() const { return getAnalysisDeclContext()->getDecl(); }
255 CFG *getCFG() const { return getAnalysisDeclContext()->getCFG(); }
257 template <typename T>
258 T *getAnalysis() const {
259 return getAnalysisDeclContext()->getAnalysis<T>();
262 ParentMap &getParentMap() const {
263 return getAnalysisDeclContext()->getParentMap();
266 const ImplicitParamDecl *getSelfDecl() const {
267 return Ctx->getSelfDecl();
270 const StackFrameContext *getStackFrame() const;
272 /// Return true if the current LocationContext has no caller context.
273 virtual bool inTopFrame() const;
275 virtual void Profile(llvm::FoldingSetNodeID &ID) = 0;
278 raw_ostream &OS, StringRef Indent = {}, const char *NL = "\n",
279 const char *Sep = "",
280 std::function<void(const LocationContext *)> printMoreInfoPerContext =
281 [](const LocationContext *) {}) const;
282 void dumpStack() const;
285 static void ProfileCommon(llvm::FoldingSetNodeID &ID,
287 AnalysisDeclContext *ctx,
288 const LocationContext *parent,
292 class StackFrameContext : public LocationContext {
293 friend class LocationContextManager;
295 // The callsite where this stack frame is established.
296 const Stmt *CallSite;
298 // The parent block of the callsite.
299 const CFGBlock *Block;
301 // The index of the callsite in the CFGBlock.
304 StackFrameContext(AnalysisDeclContext *ctx, const LocationContext *parent,
305 const Stmt *s, const CFGBlock *blk,
308 : LocationContext(StackFrame, ctx, parent, ID), CallSite(s),
309 Block(blk), Index(idx) {}
312 ~StackFrameContext() override = default;
314 const Stmt *getCallSite() const { return CallSite; }
316 const CFGBlock *getCallSiteBlock() const { return Block; }
318 /// Return true if the current LocationContext has no caller context.
319 bool inTopFrame() const override { return getParent() == nullptr; }
321 unsigned getIndex() const { return Index; }
323 void Profile(llvm::FoldingSetNodeID &ID) override;
325 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx,
326 const LocationContext *parent, const Stmt *s,
327 const CFGBlock *blk, unsigned idx) {
328 ProfileCommon(ID, StackFrame, ctx, parent, s);
333 static bool classof(const LocationContext *Ctx) {
334 return Ctx->getKind() == StackFrame;
338 class ScopeContext : public LocationContext {
339 friend class LocationContextManager;
343 ScopeContext(AnalysisDeclContext *ctx, const LocationContext *parent,
344 const Stmt *s, int64_t ID)
345 : LocationContext(Scope, ctx, parent, ID), Enter(s) {}
348 ~ScopeContext() override = default;
350 void Profile(llvm::FoldingSetNodeID &ID) override;
352 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx,
353 const LocationContext *parent, const Stmt *s) {
354 ProfileCommon(ID, Scope, ctx, parent, s);
357 static bool classof(const LocationContext *Ctx) {
358 return Ctx->getKind() == Scope;
362 class BlockInvocationContext : public LocationContext {
363 friend class LocationContextManager;
367 // FIXME: Come up with a more type-safe way to model context-sensitivity.
368 const void *ContextData;
370 BlockInvocationContext(AnalysisDeclContext *ctx,
371 const LocationContext *parent, const BlockDecl *bd,
372 const void *contextData, int64_t ID)
373 : LocationContext(Block, ctx, parent, ID), BD(bd),
374 ContextData(contextData) {}
377 ~BlockInvocationContext() override = default;
379 const BlockDecl *getBlockDecl() const { return BD; }
381 const void *getContextData() const { return ContextData; }
383 void Profile(llvm::FoldingSetNodeID &ID) override;
385 static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx,
386 const LocationContext *parent, const BlockDecl *bd,
387 const void *contextData) {
388 ProfileCommon(ID, Block, ctx, parent, bd);
389 ID.AddPointer(contextData);
392 static bool classof(const LocationContext *Ctx) {
393 return Ctx->getKind() == Block;
397 class LocationContextManager {
398 llvm::FoldingSet<LocationContext> Contexts;
400 /// ID used for generating a new location context.
404 ~LocationContextManager();
406 const StackFrameContext *getStackFrame(AnalysisDeclContext *ctx,
407 const LocationContext *parent,
409 const CFGBlock *blk, unsigned idx);
411 const ScopeContext *getScope(AnalysisDeclContext *ctx,
412 const LocationContext *parent,
415 const BlockInvocationContext *
416 getBlockInvocationContext(AnalysisDeclContext *ctx,
417 const LocationContext *parent,
419 const void *ContextData);
421 /// Discard all previously created LocationContext objects.
424 template <typename LOC, typename DATA>
425 const LOC *getLocationContext(AnalysisDeclContext *ctx,
426 const LocationContext *parent,
430 class AnalysisDeclContextManager {
432 llvm::DenseMap<const Decl *, std::unique_ptr<AnalysisDeclContext>>;
435 LocationContextManager LocContexts;
436 CFG::BuildOptions cfgBuildOptions;
438 /// Pointer to an interface that can provide function bodies for
439 /// declarations from external source.
440 std::unique_ptr<CodeInjector> Injector;
442 /// A factory for creating and caching implementations for common
443 /// methods during the analysis.
444 BodyFarm FunctionBodyFarm;
446 /// Flag to indicate whether or not bodies should be synthesized
447 /// for well-known functions.
448 bool SynthesizeBodies;
451 AnalysisDeclContextManager(ASTContext &ASTCtx, bool useUnoptimizedCFG = false,
452 bool addImplicitDtors = false,
453 bool addInitializers = false,
454 bool addTemporaryDtors = false,
455 bool addLifetime = false,
456 bool addLoopExit = false,
457 bool addScopes = false,
458 bool synthesizeBodies = false,
459 bool addStaticInitBranches = false,
460 bool addCXXNewAllocator = true,
461 bool addRichCXXConstructors = true,
462 bool markElidedCXXConstructors = true,
463 CodeInjector *injector = nullptr);
465 AnalysisDeclContext *getContext(const Decl *D);
467 bool getUseUnoptimizedCFG() const {
468 return !cfgBuildOptions.PruneTriviallyFalseEdges;
471 CFG::BuildOptions &getCFGBuildOptions() {
472 return cfgBuildOptions;
475 /// Return true if faux bodies should be synthesized for well-known
477 bool synthesizeBodies() const { return SynthesizeBodies; }
479 const StackFrameContext *getStackFrame(AnalysisDeclContext *Ctx,
480 LocationContext const *Parent,
484 return LocContexts.getStackFrame(Ctx, Parent, S, Blk, Idx);
487 // Get the top level stack frame.
488 const StackFrameContext *getStackFrame(const Decl *D) {
489 return LocContexts.getStackFrame(getContext(D), nullptr, nullptr, nullptr,
493 // Get a stack frame with parent.
494 StackFrameContext const *getStackFrame(const Decl *D,
495 LocationContext const *Parent,
499 return LocContexts.getStackFrame(getContext(D), Parent, S, Blk, Idx);
502 /// Get a reference to {@code BodyFarm} instance.
503 BodyFarm &getBodyFarm();
505 /// Discard all previously created AnalysisDeclContexts.
509 friend class AnalysisDeclContext;
511 LocationContextManager &getLocationContextManager() {
518 #endif // LLVM_CLANG_ANALYSIS_ANALYSISDECLCONTEXT_H