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();
70 void addCalledDecl(
Decl *D) {
78 if (
Decl *D = getDeclFromCall(CE))
91 D = IDecl->lookupPrivateMethod(Sel);
93 D = IDecl->lookupPrivateClassMethod(Sel);
101 void VisitChildren(
Stmt *S) {
104 this->Visit(SubStmt);
111 if (
BlockDecl *BD = dyn_cast<BlockDecl>(D))
112 addNodeForDecl(BD,
true);
114 for (
auto *I : D->
decls())
115 if (
auto *DC = dyn_cast<DeclContext>(I))
116 addNodesForBlocks(DC);
120 Root = getOrInsertNode(
nullptr);
130 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
133 if (FD->isDependentContext())
137 if (II && II->
getName().startswith(
"__inline"))
144 void CallGraph::addNodeForDecl(
Decl* D,
bool IsGlobal) {
151 CGBuilder builder(
this, Node);
157 FunctionMapTy::const_iterator I = FunctionMap.find(F);
158 if (I == FunctionMap.end())
return nullptr;
159 return I->second.get();
163 if (F && !isa<ObjCMethodDecl>(F))
166 std::unique_ptr<CallGraphNode> &
Node = FunctionMap[F];
170 Node = llvm::make_unique<CallGraphNode>(F);
173 Root->addCallee(Node.get());
178 OS <<
" --- Call graph Dump --- \n";
182 llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(
this);
183 for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
184 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
195 CE = N->
end(); CI != CE; ++CI) {
196 assert(*CI != Root &&
"No one can call the root node.");
210 llvm::ViewGraph(
this,
"CallGraph");
214 if (
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
215 return ND->printName(os);
235 return ND->getNameAsString();
The AST-based call graph.
An instance of this class is created to represent 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...
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
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.
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)
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)
Represents an ObjC class declaration.
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Expr - This represents one expression.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
const Expr * getCallee() const
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
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
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.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return 0.
CallGraphNode * getRoot() const
\ brief Get the virtual root of the graph, all the functions available externally are represented as ...
iterator begin()
Iterators through all the callees/children of the node.
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
NamedDecl - This represents a decl with a name.
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.