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);
158 if (
const Expr *e = dyn_cast<Expr>(stmt))
159 stmt = e->IgnoreParens();
160 (void) (*forcedBlkExprs)[
stmt];
165 assert(forcedBlkExprs);
166 if (
const Expr *e = dyn_cast<Expr>(stmt))
167 stmt = e->IgnoreParens();
168 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
169 forcedBlkExprs->find(stmt);
170 assert(itr != forcedBlkExprs->end());
207 if (!builtCompleteCFG) {
214 builtCompleteCFG =
true;
222 return completeCFG.get();
227 return cfgStmtMap.get();
231 return cfgStmtMap.get();
257 for (
const auto *I :
C->inits()) {
258 PM->addStmt(I->getInit());
263 if (builtCompleteCFG)
276 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
291 const CFGBlock *Blk,
unsigned Idx) {
292 return getLocationContextManager().
getStackFrame(
this, Parent, S, Blk, Idx);
298 const void *ContextData) {
305 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
306 return Manager->getLocationContextManager();
320 ID.AddPointer(parent);
340 template <
typename LOC,
typename DATA>
345 llvm::FoldingSetNodeID
ID;
346 LOC::Profile(ID, ctx, parent, d);
349 LOC *L = cast_or_null<LOC>(
Contexts.FindNodeOrInsertPos(ID, InsertPos));
352 L =
new LOC(ctx, parent, d);
362 const CFGBlock *blk,
unsigned idx) {
363 llvm::FoldingSetNodeID
ID;
367 cast_or_null<StackFrameContext>(
Contexts.FindNodeOrInsertPos(ID, InsertPos));
379 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
386 const void *ContextData) {
387 llvm::FoldingSetNodeID
ID;
391 cast_or_null<BlockInvocationContext>(
Contexts.FindNodeOrInsertPos(ID,
437 switch (LCtx->getKind()) {
439 OS << Indent <<
'#' << Frame++ <<
' ';
440 cast<StackFrameContext>(LCtx)->
getDecl()->
print(OS, PP);
444 OS << Indent <<
" (scope)\n";
447 OS << Indent <<
" (block context: "
448 << cast<BlockInvocationContext>(LCtx)->getContextData()
464 class FindBlockDeclRefExprsVals :
public StmtVisitor<FindBlockDeclRefExprsVals>{
467 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
468 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
472 : BEVals(bevals), BC(bc) {}
474 void VisitStmt(
Stmt *
S) {
475 for (
Stmt *Child : S->children())
483 if (!VD->hasLocalStorage()) {
484 if (Visited.insert(VD).second)
485 BEVals.push_back(VD, BC);
499 Expr *Semantic = *it;
501 Semantic = OVE->getSourceExpr();
512 llvm::BumpPtrAllocator &A) {
521 for (
const auto &CI : BD->
captures()) {
526 FindBlockDeclRefExprsVals F(*BV, BC);
533 llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
535 if (!ReferencedBlockVars)
536 ReferencedBlockVars =
new llvm::DenseMap<const BlockDecl*,void*>();
540 return llvm::make_range(V->begin(), V->end());
543 ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(
const void *tag) {
544 if (!ManagedAnalyses)
557 delete forcedBlkExprs;
558 delete ReferencedBlockVars;
560 if (ManagedAnalyses) {
562 llvm::DeleteContainerSeconds(*M);
568 llvm::DeleteContainerSeconds(Contexts);
578 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
579 E = Contexts.end(); I != E; ) {
Defines the clang::ASTContext interface.
StringRef getName() const
BumpVector< const VarDecl * > DeclVec
ASTContext & getASTContext() const
void Profile(llvm::FoldingSetNodeID &ID) override
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.
Defines the C++ template declaration subclasses.
bool AddStaticInitBranches
Represents a C++ constructor within a class.
Describes how types, statements, expressions, and declarations should be printed. ...
const ImplicitParamDecl * getSelfDecl() const
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 * getAnalysisDeclContext() const
Stmt * getBody() const override
bool synthesizeBodies() const
~AnalysisDeclContextManager()
void setParent(const Stmt *S, const Stmt *Parent)
const LangOptions & getLangOpts() const
llvm::DenseMap< const void *, ManagedAnalysis * > ManagedAnalysisMap
CFGReverseBlockReachabilityAnalysis * getCFGReachablityAnalysis()
semantics_iterator semantics_end()
bool isParentOf(const LocationContext *LC) const
const Decl * getDecl() const
void clear()
Discard all previously created LocationContext objects.
ForcedBlkExprs ** forcedBlkExprs
Stmt * getBody() const
Get the body of the Declaration.
ID
Defines the set of possible language-specific address spaces.
AnalysisDeclContext * getContext(const Decl *D)
void Profile(llvm::FoldingSetNodeID &ID) override
bool inTopFrame() const override
Return true if the current LocationContext has no caller context.
const CFGBlock * getBlockForRegisteredExpression(const Stmt *stmt)
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
Stmt * getParent(Stmt *) const
~LocationContextManager()
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
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)
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()
bool isBodyAutosynthesizedFromModelFile() const
Checks if the body of the Decl is generated by the BodyFarm from a model file.
semantics_iterator semantics_begin()
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)
PseudoConstantAnalysis * getPseudoConstantAnalysis()
const BlockInvocationContext * getBlockInvocationContext(AnalysisDeclContext *ctx, const LocationContext *parent, const BlockDecl *BD, const void *ContextData)
void dumpCFG(bool ShowColors)
void registerForcedBlockExpression(const Stmt *stmt)
A reference to a declared variable, function, enum, etc. [C99 6.5.1p2].
synthetic_stmt_iterator synthetic_stmt_begin() const
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)