2012-03-08 08:42:23 +08:00
|
|
|
//== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the AST-based CallGraph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Analysis/CallGraph.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2012-12-22 01:27:01 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2012-12-21 09:19:15 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-03-08 08:42:23 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "CallGraph"
|
|
|
|
|
2012-12-22 01:27:04 +08:00
|
|
|
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
|
2012-12-21 09:19:15 +08:00
|
|
|
STATISTIC(NumBlockCallEdges, "Number of block call edges");
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
namespace {
|
|
|
|
/// A helper class, which walks the AST and locates all the call sites in the
|
|
|
|
/// given function body.
|
|
|
|
class CGBuilder : public StmtVisitor<CGBuilder> {
|
|
|
|
CallGraph *G;
|
|
|
|
CallGraphNode *CallerNode;
|
|
|
|
|
|
|
|
public:
|
2012-06-07 01:32:50 +08:00
|
|
|
CGBuilder(CallGraph *g, CallGraphNode *N)
|
|
|
|
: G(g), CallerNode(N) {}
|
2012-03-08 08:42:23 +08:00
|
|
|
|
|
|
|
void VisitStmt(Stmt *S) { VisitChildren(S); }
|
|
|
|
|
2012-12-21 09:19:15 +08:00
|
|
|
Decl *getDeclFromCall(CallExpr *CE) {
|
2012-03-08 08:42:23 +08:00
|
|
|
if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
|
2012-12-21 09:19:15 +08:00
|
|
|
return CalleeDecl;
|
|
|
|
|
|
|
|
// Simple detection of a call through a block.
|
|
|
|
Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
|
|
|
|
if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
|
|
|
|
NumBlockCallEdges++;
|
|
|
|
return Block->getBlockDecl();
|
|
|
|
}
|
|
|
|
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-12-21 09:19:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void addCalledDecl(Decl *D) {
|
|
|
|
if (G->includeInGraph(D)) {
|
|
|
|
CallGraphNode *CalleeNode = G->getOrInsertNode(D);
|
|
|
|
CallerNode->addCallee(CalleeNode, G);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCallExpr(CallExpr *CE) {
|
|
|
|
if (Decl *D = getDeclFromCall(CE))
|
|
|
|
addCalledDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds may-call edges for the ObjC message sends.
|
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
|
|
|
|
if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
|
|
|
|
Selector Sel = ME->getSelector();
|
|
|
|
|
2012-12-22 01:27:04 +08:00
|
|
|
// Find the callee definition within the same translation unit.
|
2014-05-20 12:30:07 +08:00
|
|
|
Decl *D = nullptr;
|
2012-12-21 09:19:15 +08:00
|
|
|
if (ME->isInstanceMessage())
|
|
|
|
D = IDecl->lookupPrivateMethod(Sel);
|
|
|
|
else
|
|
|
|
D = IDecl->lookupPrivateClassMethod(Sel);
|
|
|
|
if (D) {
|
|
|
|
addCalledDecl(D);
|
|
|
|
NumObjCCallEdges++;
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
2012-12-21 09:19:15 +08:00
|
|
|
}
|
2012-03-09 04:28:59 +08:00
|
|
|
}
|
2012-03-08 08:42:23 +08:00
|
|
|
|
|
|
|
void VisitChildren(Stmt *S) {
|
2015-07-03 05:03:14 +08:00
|
|
|
for (Stmt *SubStmt : S->children())
|
|
|
|
if (SubStmt)
|
|
|
|
this->Visit(SubStmt);
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
};
|
2012-03-14 03:32:08 +08:00
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-12-21 09:19:15 +08:00
|
|
|
void CallGraph::addNodesForBlocks(DeclContext *D) {
|
|
|
|
if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
|
|
|
addNodeForDecl(BD, true);
|
|
|
|
|
2014-03-08 03:56:05 +08:00
|
|
|
for (auto *I : D->decls())
|
|
|
|
if (auto *DC = dyn_cast<DeclContext>(I))
|
2012-12-21 09:19:15 +08:00
|
|
|
addNodesForBlocks(DC);
|
|
|
|
}
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
CallGraph::CallGraph() {
|
2014-05-20 12:30:07 +08:00
|
|
|
Root = getOrInsertNode(nullptr);
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CallGraph::~CallGraph() {
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(FunctionMap);
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-13 06:36:48 +08:00
|
|
|
bool CallGraph::includeInGraph(const Decl *D) {
|
2012-12-21 09:19:15 +08:00
|
|
|
assert(D);
|
2014-12-17 08:34:07 +08:00
|
|
|
if (!D->hasBody())
|
2012-12-21 09:19:15 +08:00
|
|
|
return false;
|
|
|
|
|
2012-04-13 06:36:48 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// We skip function template definitions, as their semantics is
|
|
|
|
// only determined when they are instantiated.
|
2014-12-17 08:34:07 +08:00
|
|
|
if (FD->isDependentContext())
|
2012-04-13 06:36:48 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (II && II->getName().startswith("__inline"))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
|
2012-03-09 07:16:26 +08:00
|
|
|
assert(D);
|
2012-03-08 08:42:23 +08:00
|
|
|
|
2012-04-13 06:36:48 +08:00
|
|
|
// Allocate a new node, mark it as root, and process it's calls.
|
|
|
|
CallGraphNode *Node = getOrInsertNode(D);
|
2012-03-08 08:42:23 +08:00
|
|
|
|
|
|
|
// Process all the calls by this function as well.
|
2012-06-07 01:32:50 +08:00
|
|
|
CGBuilder builder(this, Node);
|
2012-04-05 12:03:23 +08:00
|
|
|
if (Stmt *Body = D->getBody())
|
|
|
|
builder.Visit(Body);
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 05:13:53 +08:00
|
|
|
CallGraphNode *CallGraph::getNode(const Decl *F) const {
|
2012-03-15 04:21:25 +08:00
|
|
|
FunctionMapTy::const_iterator I = FunctionMap.find(F);
|
2014-05-20 12:30:07 +08:00
|
|
|
if (I == FunctionMap.end()) return nullptr;
|
2012-03-15 04:21:25 +08:00
|
|
|
return I->second;
|
2012-03-10 05:13:53 +08:00
|
|
|
}
|
|
|
|
|
2012-04-13 06:36:48 +08:00
|
|
|
CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
|
2014-12-17 08:34:07 +08:00
|
|
|
if (F && !isa<ObjCMethodDecl>(F))
|
|
|
|
F = F->getCanonicalDecl();
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
CallGraphNode *&Node = FunctionMap[F];
|
|
|
|
if (Node)
|
|
|
|
return Node;
|
|
|
|
|
|
|
|
Node = new CallGraphNode(F);
|
2012-12-22 01:27:01 +08:00
|
|
|
// Make Root node a parent of all functions to make sure all are reachable.
|
2014-05-20 12:30:07 +08:00
|
|
|
if (F)
|
2012-12-22 01:27:01 +08:00
|
|
|
Root->addCallee(Node, this);
|
2012-03-08 08:42:23 +08:00
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraph::print(raw_ostream &OS) const {
|
|
|
|
OS << " --- Call graph Dump --- \n";
|
2012-12-22 01:27:01 +08:00
|
|
|
|
|
|
|
// We are going to print the graph in reverse post order, partially, to make
|
|
|
|
// sure the output is deterministic.
|
|
|
|
llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
|
|
|
|
for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
|
|
|
|
I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
|
|
|
|
const CallGraphNode *N = *I;
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
OS << " Function: ";
|
2012-12-22 01:27:01 +08:00
|
|
|
if (N == Root)
|
2012-03-08 08:42:23 +08:00
|
|
|
OS << "< root >";
|
|
|
|
else
|
2012-12-22 01:27:01 +08:00
|
|
|
N->print(OS);
|
|
|
|
|
2012-03-08 08:42:23 +08:00
|
|
|
OS << " calls: ";
|
2012-12-22 01:27:01 +08:00
|
|
|
for (CallGraphNode::const_iterator CI = N->begin(),
|
|
|
|
CE = N->end(); CI != CE; ++CI) {
|
2012-03-08 08:42:23 +08:00
|
|
|
assert(*CI != Root && "No one can call the root node.");
|
|
|
|
(*CI)->print(OS);
|
|
|
|
OS << " ";
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void CallGraph::dump() const {
|
2012-03-08 08:42:23 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraph::viewGraph() const {
|
|
|
|
llvm::ViewGraph(this, "CallGraph");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraphNode::print(raw_ostream &os) const {
|
2012-12-21 09:19:15 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
|
|
|
|
return ND->printName(os);
|
|
|
|
os << "< >";
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void CallGraphNode::dump() const {
|
2012-03-08 08:42:23 +08:00
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
|
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
|
|
|
static std::string getNodeLabel(const CallGraphNode *Node,
|
|
|
|
const CallGraph *CG) {
|
|
|
|
if (CG->getRoot() == Node) {
|
|
|
|
return "< root >";
|
|
|
|
}
|
2012-12-21 09:19:15 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
|
|
|
|
return ND->getNameAsString();
|
|
|
|
else
|
|
|
|
return "< >";
|
2012-03-08 08:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|