24 #include "llvm/ADT/PostOrderIterator.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/DOTGraphTraits.h"
30 #include "llvm/Support/GraphWriter.h"
31 #include "llvm/Support/raw_ostream.h"
36 using namespace clang;
38 #define DEBUG_TYPE "CallGraph"
40 STATISTIC(NumObjCCallEdges,
"Number of Objective-C method call edges");
41 STATISTIC(NumBlockCallEdges,
"Number of block call edges");
54 void VisitStmt(
Stmt *S) { VisitChildren(S); }
62 if (
BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
64 return Block->getBlockDecl();
78 if (
Decl *D = getDeclFromCall(CE))
100 addCalledDecl(Def, E);
103 const auto *ConstructedType = Ctor->
getParent();
104 if (ConstructedType->hasUserDeclaredDestructor()) {
107 addCalledDecl(Def, E);
130 D = IDecl->lookupPrivateMethod(Sel);
132 D = IDecl->lookupPrivateClassMethod(Sel);
134 addCalledDecl(D, ME);
140 void VisitIfStmt(
IfStmt *If) {
142 if (std::optional<Stmt *> ActiveStmt =
145 this->Visit(*ActiveStmt);
150 StmtVisitor::VisitIfStmt(If);
153 void VisitChildren(
Stmt *S) {
154 for (
Stmt *SubStmt : S->children())
156 this->Visit(SubStmt);
163 if (
BlockDecl *BD = dyn_cast<BlockDecl>(D))
164 addNodeForDecl(BD,
true);
166 for (
auto *I : D->
decls())
167 if (
auto *DC = dyn_cast<DeclContext>(I))
186 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
189 if (FD->isDependentContext())
193 if (II && II->
getName().starts_with(
"__inline"))
200 void CallGraph::addNodeForDecl(
Decl* D,
bool IsGlobal) {
207 CGBuilder builder(
this,
Node);
212 if (
auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
214 builder.Visit(init->getInit());
220 FunctionMapTy::const_iterator I = FunctionMap.find(F);
221 if (I == FunctionMap.end())
return nullptr;
222 return I->second.get();
226 if (F && !isa<ObjCMethodDecl>(F))
229 std::unique_ptr<CallGraphNode> &
Node = FunctionMap[F];
233 Node = std::make_unique<CallGraphNode>(F);
241 OS <<
" --- Call graph Dump --- \n";
245 llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(
this);
246 for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
247 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
258 CE = N->
end(); CI != CE; ++CI) {
259 assert(CI->Callee != Root &&
"No one can call the root node.");
260 CI->Callee->print(OS);
273 llvm::ViewGraph(
this,
"CallGraph");
277 if (
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
278 return ND->printQualifiedName(os);
297 if (
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(
Node->getDecl()))
298 return ND->getNameAsString();
Defines the clang::ASTContext interface.
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.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Represents a call to a C++ constructor.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Represents a C++ constructor within a class.
Represents a C++ base or member initializer.
A default argument (C++ [dcl.fct.default]).
A use of a default initializer in a constructor or in aggregate initialization.
Expr * getExpr()
Get the initialization expression that will be used.
Represents a C++ destructor within a class.
Represents a static or instance method of a struct/union/class.
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
FunctionDecl * getOperatorNew() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
iterator begin()
Iterators through all the callees/children of the node.
void print(raw_ostream &os) const
SmallVectorImpl< CallRecord >::const_iterator const_iterator
void addCallee(CallRecord Call)
The AST-based call graph.
ASTContext & getASTContext()
CallGraphNode * getRoot() const
Get the virtual root of the graph, all the functions available externally are represented as callees ...
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
bool VisitFunctionDecl(FunctionDecl *FD)
Part of recursive declaration visitation.
void addNodesForBlocks(DeclContext *D)
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
void print(raw_ostream &os) const
static bool includeCalleeInGraph(const Decl *D)
Determine if a declaration should be included in the graph for the purposes of being a callee.
bool shouldSkipConstantExpressions() const
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.
const T * get() const
Retrieve the stored node as type T.
This represents one expression.
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.
Declaration of a template function.
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
IfStmt - This represents an if/then/else.
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
This represents a decl that may have a name.
Represents an ObjC class declaration.
An expression that sends a message to the given Objective-C object or class.
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.
Smart pointer class that efficiently represents Objective-C method names.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
bool LE(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.
@ If
'if' clause, allowed on all the Compute Constructs, Data Constructs, Executable Constructs,...
Diagnostic wrappers for TextAPI types for error reporting.
DOTGraphTraits(bool isSimple=false)
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)