29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/SaveAndRestore.h"
32 #include "llvm/Support/raw_ostream.h"
34 using namespace clang;
43 cfgBuildOptions(buildOptions),
44 forcedBlkExprs(nullptr),
46 builtCompleteCFG(
false),
47 ReferencedBlockVars(nullptr),
48 ManagedAnalyses(nullptr)
57 forcedBlkExprs(nullptr),
59 builtCompleteCFG(
false),
60 ReferencedBlockVars(nullptr),
61 ManagedAnalyses(nullptr)
67 bool addImplicitDtors,
69 bool addTemporaryDtors,
70 bool synthesizeBodies,
71 bool addStaticInitBranch,
72 bool addCXXNewAllocator,
74 : Injector(injector), SynthesizeBodies(synthesizeBodies)
85 llvm::DeleteContainerSeconds(
Contexts);
94 IsAutosynthesized =
false;
95 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
96 Stmt *Body = FD->getBody();
100 IsAutosynthesized =
true;
104 else if (
const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
105 Stmt *Body = MD->getBody();
109 IsAutosynthesized =
true;
112 }
else if (
const BlockDecl *BD = dyn_cast<BlockDecl>(D))
113 return BD->getBody();
115 = dyn_cast_or_null<FunctionTemplateDecl>(D))
116 return FunTmpl->getTemplatedDecl()->getBody();
118 llvm_unreachable(
"unknown code decl");
135 return Tmp && Body->getLocStart().isValid();
141 return MD->getSelfDecl();
142 if (
const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
144 for (
const auto &
I : BD->captures()) {
145 const VarDecl *VD =
I.getVariable();
147 return dyn_cast<ImplicitParamDecl>(VD);
160 if (!LC.capturesVariable())
163 VarDecl *VD = LC.getCapturedVar();
165 return dyn_cast<ImplicitParamDecl>(VD);
175 if (
const Expr *e = dyn_cast<Expr>(stmt))
176 stmt = e->IgnoreParens();
177 (void) (*forcedBlkExprs)[
stmt];
182 assert(forcedBlkExprs);
183 if (
const Expr *e = dyn_cast<Expr>(stmt))
184 stmt = e->IgnoreParens();
185 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
186 forcedBlkExprs->find(stmt);
187 assert(itr != forcedBlkExprs->end());
224 if (!builtCompleteCFG) {
231 builtCompleteCFG =
true;
239 return completeCFG.get();
244 return cfgStmtMap.get();
248 return cfgStmtMap.get();
274 for (
const auto *
I :
C->inits()) {
275 PM->addStmt(
I->getInit());
280 if (builtCompleteCFG)
293 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
308 const CFGBlock *Blk,
unsigned Idx) {
309 return getLocationContextManager().
getStackFrame(
this, Parent, S, Blk, Idx);
315 const void *ContextData) {
322 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
323 return Manager->getLocationContextManager();
337 ID.AddPointer(parent);
357 template <
typename LOC,
typename DATA>
362 llvm::FoldingSetNodeID
ID;
366 LOC *L = cast_or_null<LOC>(
Contexts.FindNodeOrInsertPos(ID, InsertPos));
369 L =
new LOC(ctx, parent, d);
379 const CFGBlock *blk,
unsigned idx) {
380 llvm::FoldingSetNodeID
ID;
384 cast_or_null<StackFrameContext>(
Contexts.FindNodeOrInsertPos(ID, InsertPos));
396 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
403 const void *ContextData) {
404 llvm::FoldingSetNodeID
ID;
408 cast_or_null<BlockInvocationContext>(
Contexts.FindNodeOrInsertPos(ID,
454 switch (LCtx->getKind()) {
456 OS << Indent <<
'#' << Frame++ <<
' ';
457 cast<StackFrameContext>(LCtx)->
getDecl()->
print(OS, PP);
461 OS << Indent <<
" (scope)\n";
464 OS << Indent <<
" (block context: "
465 << cast<BlockInvocationContext>(LCtx)->getContextData()
481 class FindBlockDeclRefExprsVals :
public StmtVisitor<FindBlockDeclRefExprsVals>{
484 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
485 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
489 : BEVals(bevals), BC(bc) {}
491 void VisitStmt(
Stmt *
S) {
492 for (
Stmt *Child : S->children())
500 if (!VD->hasLocalStorage()) {
501 if (Visited.insert(VD).second)
502 BEVals.push_back(VD, BC);
516 Expr *Semantic = *it;
518 Semantic = OVE->getSourceExpr();
529 llvm::BumpPtrAllocator &A) {
538 for (
const auto &CI : BD->
captures()) {
543 FindBlockDeclRefExprsVals F(*BV, BC);
550 llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
552 if (!ReferencedBlockVars)
553 ReferencedBlockVars =
new llvm::DenseMap<const BlockDecl*,void*>();
557 return llvm::make_range(V->begin(), V->end());
560 ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(
const void *tag) {
561 if (!ManagedAnalyses)
574 delete forcedBlkExprs;
575 delete ReferencedBlockVars;
577 if (ManagedAnalyses) {
579 llvm::DeleteContainerSeconds(*M);
585 llvm::DeleteContainerSeconds(Contexts);
596 E = Contexts.end();
I !=
E; ) {
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
The base class of a hierarchy of objects representing analyses tied to AnalysisDeclContext.
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
BumpVector< const VarDecl * > DeclVec
ASTContext & getASTContext() const
void Profile(llvm::FoldingSetNodeID &ID) override
capture_const_range captures() const
CFGStmtMap * getCFGStmtMap()
Expr *const * semantics_iterator
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
virtual bool inTopFrame() const
Return true if the current LocationContext has no caller context.
Decl - This represents one declaration (or definition), e.g.
Defines the C++ template declaration subclasses.
Describes the capture of a variable or of this, or of a C++1y init-capture.
bool AddStaticInitBranches
Represents a C++ constructor within a class.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
ObjCMethodDecl - Represents an instance or class method declaration.
Describes how types, statements, expressions, and declarations should be printed. ...
const ImplicitParamDecl * getSelfDecl() const
Return the ImplicitParamDecl* associated with 'self' if this AnalysisDeclContext wraps an ObjCMethodD...
unsigned TerseOutput
Provide a 'terse' output.
bool isBodyAutosynthesized() const
Checks if the body of the Decl is generated by the BodyFarm.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
synthetic_stmt_iterator synthetic_stmt_end() const
AnalysisDeclContext contains the context data for the function or method under analysis.
AnalysisDeclContext * getAnalysisDeclContext() const
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
bool synthesizeBodies() const
Return true if faux bodies should be synthesized for well-known functions.
~AnalysisDeclContextManager()
void setParent(const Stmt *S, const Stmt *Parent)
Manually sets the parent of S to Parent.
const LangOptions & getLangOpts() const
llvm::DenseMap< const void *, ManagedAnalysis * > ManagedAnalysisMap
CFGReverseBlockReachabilityAnalysis * getCFGReachablityAnalysis()
semantics_iterator semantics_end()
bool isParentOf(const LocationContext *LC) const
Scope - A scope is a transient data structure that is used while parsing the program.
const Decl * getDecl() const
void clear()
Discard all previously created LocationContext objects.
detail::InMemoryDirectory::const_iterator I
ForcedBlkExprs ** forcedBlkExprs
CFGBlock - Represents a single basic block in a source-level CFG.
Stmt * getBody() const
Get the body of the Declaration.
ID
Defines the set of possible language-specific address spaces.
AnalysisDeclContext * getContext(const Decl *D)
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Expr - This represents one expression.
CFG - Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt...
void Profile(llvm::FoldingSetNodeID &ID) override
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
bool inTopFrame() const override
Return true if the current LocationContext has no caller context.
const CFGBlock * getBlockForRegisteredExpression(const Stmt *stmt)
DeclContext * getParent()
getParent - Returns the containing DeclContext.
AnalysisDeclContextManager(bool useUnoptimizedCFG=false, bool addImplicitDtors=false, bool addInitializers=false, bool addTemporaryDtors=false, bool synthesizeBodies=false, bool addStaticInitBranches=false, bool addCXXNewAllocator=true, CodeInjector *injector=nullptr)
const ScopeContext * getScope(AnalysisDeclContext *ctx, const LocationContext *parent, const Stmt *s)
const StackFrameContext * getStackFrame(LocationContext const *Parent, const Stmt *S, const CFGBlock *Blk, unsigned Idx)
const StackFrameContext * getStackFrame(AnalysisDeclContext *ctx, const LocationContext *parent, const Stmt *s, const CFGBlock *blk, unsigned idx)
CFG * getUnoptimizedCFG()
Return a version of the CFG without any edges pruned.
llvm::DenseMap< const DeclStmt *, const DeclStmt * >::const_iterator synthetic_stmt_iterator
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Stmt * getParent(Stmt *) const
~LocationContextManager()
const TemplateArgument * iterator
const StackFrameContext * getCurrentStackFrame() const
friend class AnalysisDeclContext
static std::unique_ptr< CFG > buildCFG(const Decl *D, Stmt *AST, ASTContext *C, const BuildOptions &BO)
buildCFG - Builds a CFG from an AST.
ASTContext & getASTContext() const LLVM_READONLY
bool PruneTriviallyFalseEdges
Represents a static or instance method of a struct/union/class.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx)
const Decl * getDecl() const
virtual ~ManagedAnalysis()
const BlockDecl * getBlockDecl() const
void push_back(const_reference Elt, BumpVectorContext &C)
const LocationContext * getParent() const
CFG::BuildOptions & getCFGBuildOptions()
Return the build options used to construct the CFG.
static CFGStmtMap * Build(CFG *C, ParentMap *PM)
Returns a new CFGMap for the given CFG.
CodeInjector is an interface which is responsible for injecting AST of function definitions that may ...
llvm::iterator_range< referenced_decls_iterator > getReferencedBlockVars(const BlockDecl *BD)
static DeclVec * LazyInitializeReferencedDecls(const BlockDecl *BD, void *&Vec, llvm::BumpPtrAllocator &A)
void Profile(llvm::FoldingSetNodeID &ID) override
virtual ~LocationContext()
detail::InMemoryDirectory::const_iterator E
bool isBodyAutosynthesizedFromModelFile() const
Checks if the body of the Decl is generated by the BodyFarm from a model file.
semantics_iterator semantics_begin()
bool isLambda() const
Determine whether this class describes a lambda function object.
SmallVector< Context, 8 > Contexts
llvm::DenseMap< const Stmt *, const CFGBlock * > ForcedBlkExprs
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM)
Add each synthetic statement in the CFG to the parent map, using the source statement's parent...
PseudoConstantAnalysis * getPseudoConstantAnalysis()
const BlockInvocationContext * getBlockInvocationContext(AnalysisDeclContext *ctx, const LocationContext *parent, const BlockDecl *BD, const void *ContextData)
Represents a C++ struct/union/class.
void dumpCFG(bool ShowColors)
void registerForcedBlockExpression(const Stmt *stmt)
A reference to a declared variable, function, enum, etc.
synthetic_stmt_iterator synthetic_stmt_begin() const
Iterates over synthetic DeclStmts in the CFG.
const BlockInvocationContext * getBlockInvocationContext(const LocationContext *parent, const BlockDecl *BD, const void *ContextData)
ParentMap & getParentMap()
void clear()
Discard all previously created AnalysisDeclContexts.
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
Declaration of a template function.
static BodyFarm & getBodyFarm(ASTContext &C, CodeInjector *injector=nullptr)
AnalysisDeclContext(AnalysisDeclContextManager *Mgr, const Decl *D)
static void ProfileCommon(llvm::FoldingSetNodeID &ID, ContextKind ck, AnalysisDeclContext *ctx, const LocationContext *parent, const void *data)