37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/FoldingSet.h"
39#include "llvm/ADT/SmallPtrSet.h"
40#include "llvm/ADT/iterator_range.h"
41#include "llvm/Support/Allocator.h"
42#include "llvm/Support/Compiler.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/SaveAndRestore.h"
45#include "llvm/Support/raw_ostream.h"
56 : ADCMgr(ADCMgr), D(D), cfgBuildOptions(Options) {
57 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
62 : ADCMgr(ADCMgr), D(D) {
63 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
67 ASTContext &ASTCtx,
bool useUnoptimizedCFG,
bool addImplicitDtors,
68 bool addInitializers,
bool addTemporaryDtors,
bool addLifetime,
70 bool addStaticInitBranch,
bool addCXXNewAllocator,
71 bool addRichCXXConstructors,
bool markElidedCXXConstructors,
72 bool addVirtualBaseBranches, std::unique_ptr<CodeInjector> injector)
73 : Injector(
std::move(injector)), FunctionBodyFarm(ASTCtx, Injector.get()),
75 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
76 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
77 cfgBuildOptions.AddInitializers = addInitializers;
78 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
79 cfgBuildOptions.AddLifetime = addLifetime;
80 cfgBuildOptions.AddLoopExit = addLoopExit;
81 cfgBuildOptions.AddScopes = addScopes;
82 cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
83 cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
84 cfgBuildOptions.AddRichCXXConstructors = addRichCXXConstructors;
85 cfgBuildOptions.MarkElidedCXXConstructors = markElidedCXXConstructors;
86 cfgBuildOptions.AddVirtualBaseBranches = addVirtualBaseBranches;
92 IsAutosynthesized =
false;
93 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
94 Stmt *Body = FD->getBody();
95 if (
auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body))
96 Body = CoroBody->getBody();
97 if (ADCMgr && ADCMgr->synthesizeBodies()) {
98 Stmt *SynthesizedBody = ADCMgr->getBodyFarm().getBody(FD);
99 if (SynthesizedBody) {
100 Body = SynthesizedBody;
101 IsAutosynthesized =
true;
106 else if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
107 Stmt *Body = MD->getBody();
108 if (ADCMgr && ADCMgr->synthesizeBodies()) {
109 Stmt *SynthesizedBody = ADCMgr->getBodyFarm().getBody(MD);
110 if (SynthesizedBody) {
111 Body = SynthesizedBody;
112 IsAutosynthesized =
true;
116 }
else if (
const auto *BD = dyn_cast<BlockDecl>(D))
117 return BD->getBody();
118 else if (
const auto *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
119 return FunTmpl->getTemplatedDecl()->getBody();
120 else if (
const auto *VD = dyn_cast_or_null<VarDecl>(D)) {
121 if (VD->isFileVarDecl()) {
122 return const_cast<Stmt *
>(dyn_cast_or_null<Stmt>(VD->getInit()));
126 llvm_unreachable(
"unknown code decl");
148 return isa_and_nonnull<ImplicitParamDecl>(VD) && VD->
getName() ==
"self";
152 if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D))
153 return MD->getSelfDecl();
154 if (
const auto *BD = dyn_cast<BlockDecl>(D)) {
156 for (
const auto &I : BD->captures()) {
157 const VarDecl *VD = I.getVariable();
159 return dyn_cast<ImplicitParamDecl>(VD);
163 auto *CXXMethod = dyn_cast<CXXMethodDecl>(D);
171 for (
const auto &LC : parent->
captures()) {
172 if (!LC.capturesVariable())
177 return dyn_cast<ImplicitParamDecl>(VD);
187 if (
const auto *e = dyn_cast<Expr>(
stmt))
188 stmt = e->IgnoreParens();
189 (void) (*forcedBlkExprs)[
stmt];
194 assert(forcedBlkExprs);
195 if (
const auto *e = dyn_cast<Expr>(
stmt))
196 stmt = e->IgnoreParens();
197 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
198 forcedBlkExprs->find(
stmt);
199 assert(itr != forcedBlkExprs->end());
217 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
236 if (!builtCompleteCFG) {
237 SaveAndRestore NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
false);
242 builtCompleteCFG =
true;
250 return completeCFG.get();
284 if (
const auto *
C = dyn_cast<CXXConstructorDecl>(
getDecl())) {
285 for (
const auto *I :
C->inits()) {
286 PM->addStmt(I->getInit());
291 if (builtCompleteCFG)
298 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
305 std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D];
307 AC = std::make_unique<AnalysisDeclContext>(
this, D, cfgBuildOptions);
315 const CFGBlock *Blk,
unsigned BlockCount,
unsigned Index) {
322 const auto *ND = dyn_cast<NamespaceDecl>(DC);
332 return ND->isStdNamespace();
337 llvm::raw_string_ostream OS(Str);
340 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
341 OS << FD->getQualifiedNameAsString();
347 for (
const auto &P : FD->parameters()) {
348 if (P != *FD->param_begin())
363 }
else if (
const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
366 OS << (OMD->isInstanceMethod() ?
'-' :
'+') <<
'[';
368 if (
const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
369 OS << OID->getName();
370 }
else if (
const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
371 OS << OID->getName();
372 }
else if (
const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
373 if (OC->IsClassExtension()) {
374 OS << OC->getClassInterface()->getName();
376 OS << OC->getIdentifier()->getNameStart() <<
'('
377 << OC->getIdentifier()->getNameStart() <<
')';
379 }
else if (
const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
380 OS << OCD->getClassInterface()->getName() <<
'(' << OCD->getName() <<
')';
382 OS <<
' ' << OMD->getSelector().getAsString() <<
']';
391 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
392 return ADCMgr->getLocationContextManager();
406 ID.AddPointer(parent);
421 const Expr *E,
const CFGBlock *Blk,
unsigned BlockCount,
unsigned StmtIdx) {
422 llvm::FoldingSetNodeID ID;
426 cast_or_null<StackFrame>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
428 L =
new StackFrame(Ctx, Parent,
Data, E, Blk, BlockCount, StmtIdx, ++NewID);
429 Contexts.InsertNode(L, InsertPos);
441 if (
const auto *SF = dyn_cast<StackFrame>(LC))
467 Out <<
SM.getExpansionLineNumber(Loc);
481 for (
const LocationContext *LCtx =
this; LCtx; LCtx = LCtx->getParent()) {
482 switch (LCtx->getKind()) {
484 Out <<
"\t#" << Frame <<
' ';
486 if (
const auto *D = dyn_cast<NamedDecl>(LCtx->getDecl()))
489 Out <<
"Calling anonymous code";
501 unsigned int Space,
bool IsDot,
503 printMoreInfoPerContext)
const {
512 for (
const LocationContext *LCtx =
this; LCtx; LCtx = LCtx->getParent()) {
514 <<
"{ \"lctx_id\": " << LCtx->getID() <<
", \"location_context\": \"";
515 switch (LCtx->getKind()) {
517 Out <<
'#' << Frame <<
" Call\", \"calling\": \"";
519 if (
const auto *D = dyn_cast<NamedDecl>(LCtx->getDecl()))
520 Out << D->getQualifiedNameAsString();
522 Out <<
"anonymous code";
524 Out <<
"\", \"location\": ";
531 Out <<
", \"items\": ";
535 printMoreInfoPerContext(LCtx);
538 if (LCtx->getParent())
552class FindBlockDeclRefExprsVals :
public StmtVisitor<FindBlockDeclRefExprsVals>{
561 : BEVals(bevals), BC(bc) {}
563 void VisitStmt(
Stmt *S) {
569 void VisitDeclRefExpr(DeclRefExpr *DR) {
571 if (
const auto *VD = dyn_cast<VarDecl>(DR->
getDecl())) {
572 if (!VD->hasLocalStorage()) {
573 if (Visited.insert(VD).second)
579 void VisitBlockExpr(BlockExpr *BR) {
585 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
588 Expr *Semantic = *it;
589 if (
auto *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
590 Semantic = OVE->getSourceExpr();
602 llvm::BumpPtrAllocator &A) {
611 for (
const auto &CI : BD->
captures()) {
616 FindBlockDeclRefExprsVals F(*BV, BC);
623llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
625 if (!ReferencedBlockVars)
626 ReferencedBlockVars =
new llvm::DenseMap<const BlockDecl*,void*>();
630 return llvm::make_range(
V->begin(),
V->end());
633std::unique_ptr<ManagedAnalysis> &AnalysisDeclContext::getAnalysisImpl(
const void *tag) {
634 if (!ManagedAnalyses)
647 delete forcedBlkExprs;
648 delete ReferencedBlockVars;
659 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
660 E = Contexts.end(); I != E; ) {
Defines the clang::ASTContext interface.
static DeclVec * LazyInitializeReferencedDecls(const BlockDecl *BD, void *&Vec, llvm::BumpPtrAllocator &A)
static bool isSelfDecl(const VarDecl *VD)
Returns true if.
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.
static void printLocation(raw_ostream &Out, const SourceManager &SM, SourceLocation Loc)
llvm::DenseMap< const void *, std::unique_ptr< ManagedAnalysis > > ManagedAnalysisMap
BumpVector< const VarDecl * > DeclVec
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the LambdaCapture class.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
__device__ __2f16 float c
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
const LangOptions & getLangOpts() const
AnalysisDeclContextManager(ASTContext &ASTCtx, bool useUnoptimizedCFG=false, bool addImplicitDtors=false, bool addInitializers=false, bool addTemporaryDtors=false, bool addLifetime=false, bool addLoopExit=false, bool addScopes=false, bool synthesizeBodies=false, bool addStaticInitBranches=false, bool addCXXNewAllocator=true, bool addRichCXXConstructors=true, bool markElidedCXXConstructors=true, bool addVirtualBaseBranches=true, std::unique_ptr< CodeInjector > injector=nullptr)
friend class AnalysisDeclContext
bool synthesizeBodies() const
void clear()
Discard all previously created AnalysisDeclContexts.
AnalysisDeclContext * getContext(const Decl *D)
AnalysisDeclContext contains the context data for the function, method or block under analysis.
static std::string getFunctionName(const Decl *D)
void registerForcedBlockExpression(const Stmt *stmt)
const CFGBlock * getBlockForRegisteredExpression(const Stmt *stmt)
ParentMap & getParentMap()
const Decl * getDecl() const
const CFGStmtMap * getCFGStmtMap()
static bool isInStdNamespace(const Decl *D)
CFGReverseBlockReachabilityAnalysis * getCFGReachablityAnalysis()
const ImplicitParamDecl * getSelfDecl() const
ASTContext & getASTContext() const
llvm::iterator_range< referenced_decls_iterator > getReferencedBlockVars(const BlockDecl *BD)
bool isBodyAutosynthesized() const
CFG * getUnoptimizedCFG()
AnalysisDeclContext(AnalysisDeclContextManager *Mgr, const Decl *D)
void dumpCFG(bool ShowColors)
const StackFrame * getStackFrame(LocationContext const *ParentLC, const void *Data, const Expr *E, const CFGBlock *Blk, unsigned BlockCount, unsigned Index)
Obtain a context of the call stack using its parent context.
CFG::BuildOptions & getCFGBuildOptions()
bool isBodyAutosynthesizedFromModelFile() const
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
ArrayRef< Capture > captures() const
const BlockDecl * getBlockDecl() const
void push_back(const_reference Elt, BumpVectorContext &C)
Represents a single basic block in a source-level CFG.
llvm::DenseMap< const Stmt *, const CFGBlock * > ForcedBlkExprs
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
static std::unique_ptr< CFG > buildCFG(const Decl *D, Stmt *AST, ASTContext *C, const BuildOptions &BO)
Builds a CFG from an AST.
synthetic_stmt_iterator synthetic_stmt_end() const
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
synthetic_stmt_iterator synthetic_stmt_begin() const
Iterates over synthetic DeclStmts in the CFG.
llvm::DenseMap< const DeclStmt *, const DeclStmt * >::const_iterator synthetic_stmt_iterator
Represents a C++ struct/union/class.
bool isLambda() const
Determine whether this class describes a lambda function object.
capture_const_range captures() const
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
DeclContext * getEnclosingNamespaceContext()
Retrieve the nearest enclosing namespace context.
Decl - This represents one declaration (or definition), e.g.
This represents one expression.
Represents a function declaration or definition.
~LocationContextManager()
const StackFrame * getStackFrame(AnalysisDeclContext *ADC, const LocationContext *ParentLC, const void *Data, const Expr *E, const CFGBlock *Block, unsigned BlockCount, unsigned StmtIdx)
Obtain a context of the call stack using its parent context.
void clear()
Discard all previously created LocationContext objects.
bool isParentOf(const LocationContext *LC) const
LocationContext(ContextKind k, AnalysisDeclContext *ctx, const LocationContext *parent, int64_t ID)
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
static void ProfileCommon(llvm::FoldingSetNodeID &ID, ContextKind ck, AnalysisDeclContext *ctx, const LocationContext *parent, const void *data)
const LocationContext * getParent() const
It might return null.
const StackFrame * getStackFrame() const
LLVM_DUMP_METHOD void dumpStack(raw_ostream &Out) const
Prints out the call stack.
LLVM_DUMP_METHOD void dump() const
virtual bool inTopFrame() const
virtual ~LocationContext()
void printJson(raw_ostream &Out, const char *NL="\n", unsigned int Space=0, bool IsDot=false, std::function< void(const LocationContext *)> printMoreInfoPerContext=[](const LocationContext *) {}) const
Prints out the call stack in json format.
virtual ~ManagedAnalysis()
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
ObjCMethodDecl - Represents an instance or class method declaration.
void setParent(const Stmt *S, const Stmt *Parent)
Manually sets the parent of S to Parent.
Stmt * getParent(Stmt *) const
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
unsigned getLine() const
Return the presumed line number of this location.
semantics_iterator semantics_end()
semantics_iterator semantics_begin()
Expr *const * semantics_iterator
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
void print(raw_ostream &OS, const SourceManager &SM) const
This class handles loading and caching of source files into memory.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
It represents a stack frame of the call stack (based on CallEvent).
bool inTopFrame() const override
void Profile(llvm::FoldingSetNodeID &ID) override
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
SourceLocation getBeginLoc() const LLVM_READONLY
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc, const SourceManager &SM, bool AddBraces=true)
U cast(CodeGen::Address addr)
int const char * function
Describes how types, statements, expressions, and declarations should be printed.
unsigned TerseOutput
Provide a 'terse' output.