23 #include "llvm/ADT/PostOrderIterator.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/Statistic.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/Compiler.h" 28 #include "llvm/Support/DOTGraphTraits.h" 29 #include "llvm/Support/GraphWriter.h" 30 #include "llvm/Support/raw_ostream.h" 35 using namespace clang;
37 #define DEBUG_TYPE "CallGraph" 39 STATISTIC(NumObjCCallEdges,
"Number of Objective-C method call edges");
40 STATISTIC(NumBlockCallEdges,
"Number of block call edges");
53 void VisitStmt(
Stmt *S) { VisitChildren(S); }
61 if (
BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
63 return Block->getBlockDecl();
69 void addCalledDecl(
Decl *D) {
77 if (
Decl *D = getDeclFromCall(CE))
121 D = IDecl->lookupPrivateMethod(Sel);
123 D = IDecl->lookupPrivateClassMethod(Sel);
131 void VisitChildren(
Stmt *S) {
134 this->Visit(SubStmt);
141 if (
BlockDecl *BD = dyn_cast<BlockDecl>(D))
142 addNodeForDecl(BD,
true);
144 for (
auto *I : D->
decls())
145 if (
auto *DC = dyn_cast<DeclContext>(I))
146 addNodesForBlocks(DC);
150 Root = getOrInsertNode(
nullptr);
160 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
163 if (FD->isDependentContext())
167 if (II && II->
getName().startswith(
"__inline"))
174 void CallGraph::addNodeForDecl(
Decl* D,
bool IsGlobal) {
181 CGBuilder builder(
this, Node);
186 if (
auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
188 builder.Visit(init->getInit());
194 FunctionMapTy::const_iterator I = FunctionMap.find(F);
195 if (I == FunctionMap.end())
return nullptr;
196 return I->second.get();
200 if (F && !isa<ObjCMethodDecl>(F))
203 std::unique_ptr<CallGraphNode> &
Node = FunctionMap[F];
207 Node = std::make_unique<CallGraphNode>(F);
210 Root->addCallee(Node.get());
215 OS <<
" --- Call graph Dump --- \n";
219 llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(
this);
220 for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
221 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
232 CE = N->
end(); CI != CE; ++CI) {
233 assert(*CI != Root &&
"No one can call the root node.");
247 llvm::ViewGraph(
this,
"CallGraph");
251 if (
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
252 return ND->printQualifiedName(os);
272 return ND->getNameAsString();
The AST-based call graph.
FunctionDecl * getDefinition()
Get the definition for this declaration.
Represents a function declaration or definition.
Smart pointer class that efficiently represents Objective-C method names.
Selector getSelector() const
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Stmt - This represents one statement.
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
const T * get() const
Retrieve the stored node as type T.
Decl - This represents one declaration (or definition), e.g.
FunctionDecl * getOperatorNew() const
Represents a call to a C++ constructor.
DOTGraphTraits(bool isSimple=false)
Represents a C++ constructor within a class.
SmallVectorImpl< CallRecord >::const_iterator const_iterator
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
void addCallee(CallGraphNode *N)
void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx, QualType Ty)
FunctionTemplateDecl * getDependentCallOperator() const
Retrieve the function template call operator associated with this lambda expression.
One of these records is kept for each identifier that is lexed.
DOTGraphTraits(bool isSimple=false)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void addNodesForBlocks(DeclContext *D)
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Represents an ObjC class declaration.
A default argument (C++ [dcl.fct.default]).
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
const Expr * getExpr() const
Get the initialization expression that will be used.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
Represents a block literal declaration, which is like an unnamed FunctionDecl.
This represents one expression.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
An expression that sends a message to the given Objective-C object or class.
void print(raw_ostream &os) const
CXXMethodDecl * getCallOperator() const
Retrieve the function call operator associated with this lambda expression.
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
bool VisitFunctionDecl(FunctionDecl *FD)
Part of recursive declaration visitation.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Represents a static or instance method of a struct/union/class.
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
void print(raw_ostream &os) const
StringRef getName() const
Return the actual identifier string.
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
CallGraphNode * getRoot() const
\ brief Get the virtual root of the graph, all the functions available externally are represented as ...
const Expr * getExpr() const
iterator begin()
Iterators through all the callees/children of the node.
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Represents a C++ base or member initializer.
A use of a default initializer in a constructor or in aggregate initialization.
bool LE(InterpState &S, CodePtr OpPC)
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
This represents a decl that may have a name.
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Declaration of a template function.