23#include "llvm/ADT/PostOrderIterator.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/DOTGraphTraits.h"
28#include "llvm/Support/GraphWriter.h"
29#include "llvm/Support/raw_ostream.h"
36#define DEBUG_TYPE "CallGraph"
38STATISTIC(NumObjCCallEdges,
"Number of Objective-C method call edges");
39STATISTIC(NumBlockCallEdges,
"Number of block call edges");
52 void VisitStmt(Stmt *S) { VisitChildren(S); }
54 Decl *getDeclFromCall(CallExpr *CE) {
60 if (BlockExpr *
Block = dyn_cast<BlockExpr>(CEE)) {
62 return Block->getBlockDecl();
68 void addCalledDecl(Decl *D, Expr *CallExpr) {
69 if (G->includeCalleeInGraph(D)) {
70 CallGraphNode *CalleeNode = G->getOrInsertNode(D);
71 CallerNode->addCallee({CalleeNode, CallExpr});
75 void VisitCallExpr(CallExpr *CE) {
76 if (Decl *D = getDeclFromCall(CE))
82 if (FunctionTemplateDecl *FTD =
LE->getDependentCallOperator())
83 for (FunctionDecl *FD : FTD->specializations())
84 G->VisitFunctionDecl(FD);
85 else if (CXXMethodDecl *MD =
LE->getCallOperator())
86 G->VisitFunctionDecl(MD);
89 void VisitCXXNewExpr(CXXNewExpr *E) {
95 void VisitCXXConstructExpr(CXXConstructExpr *E) {
98 addCalledDecl(Def, E);
103 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
108 void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
113 void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
120 D = IDecl->lookupPrivateMethod(Sel);
122 D = IDecl->lookupPrivateClassMethod(Sel);
124 addCalledDecl(D, ME);
130 void VisitChildren(Stmt *S) {
133 this->Visit(SubStmt);
140 if (
BlockDecl *BD = dyn_cast<BlockDecl>(D))
141 addNodeForDecl(BD,
true);
143 for (
auto *I : D->
decls())
144 if (
auto *DC = dyn_cast<DeclContext>(I))
166 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
169 if (FD->isDependentContext())
173 if (II && II->
getName().starts_with(
"__inline"))
180void CallGraph::addNodeForDecl(
Decl* D,
bool IsGlobal) {
187 CGBuilder builder(
this, Node);
192 if (
auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
194 builder.Visit(init->getInit());
200 FunctionMapTy::const_iterator I = FunctionMap.find(F);
201 if (I == FunctionMap.end())
return nullptr;
202 return I->second.get();
209 std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
213 Node = std::make_unique<CallGraphNode>(F);
216 Root->addCallee({Node.get(),
nullptr});
221 OS <<
" --- Call graph Dump --- \n";
225 llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(
this);
235 CE = N->end(); CI != CE; ++CI) {
236 assert(CI->Callee != Root &&
"No one can call the root node.");
237 CI->Callee->print(OS);
250 llvm::ViewGraph(
this,
"CallGraph");
254 if (
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
255 return ND->printQualifiedName(os);
275 return ND->getNameAsString();
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Represents a block literal declaration, which is like an unnamed FunctionDecl.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Represents a C++ base or member initializer.
Expr * getExpr()
Get the initialization expression that will be used.
FunctionDecl * getOperatorNew() const
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
void print(raw_ostream &os) const
SmallVectorImpl< CallRecord >::const_iterator const_iterator
The AST-based call graph.
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
void addNodesForBlocks(DeclContext *D)
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
void print(raw_ostream &os) const
friend class CallGraphNode
static bool includeCalleeInGraph(const Decl *D)
Determine if a declaration should be included in the graph for the purposes of being a callee.
CallGraphNode * getRoot() const
Get the virtual root of the graph, all the functions available externally are represented as callees ...
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
bool ShouldVisitTemplateInstantiations
bool ShouldVisitImplicitCode
bool ShouldWalkTypesOfTypeLocs
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Represents a function declaration or definition.
FunctionDecl * getDefinition()
Get the definition for this declaration.
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
This represents a decl that may have a name.
Selector getSelector() const
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
bool LE(InterpState &S, CodePtr OpPC)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Diagnostic wrappers for TextAPI types for error reporting.
DOTGraphTraits(bool isSimple=false)
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)