aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongxing Xu <xuzhongxing@gmail.com>2009-07-23 13:39:38 +0000
committerZhongxing Xu <xuzhongxing@gmail.com>2009-07-23 13:39:38 +0000
commit56a5d8087735438fcedea4ce3e22eb07d1d27e75 (patch)
tree4013d58f771ae7926414f4c134bc8ffa00ff49a7
parent16a705f26d069a0c9e391bf064bcd1bc3496ca83 (diff)
Add two nodes to the call graph:
- Root is the main function or 0. - ExternalCallingNode has edges to all external functions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76876 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Analysis/CallGraph.h15
-rw-r--r--lib/Analysis/CallGraph.cpp12
-rw-r--r--tools/wpa/clang-wpa.cpp2
3 files changed, 23 insertions, 6 deletions
diff --git a/include/clang/Analysis/CallGraph.h b/include/clang/Analysis/CallGraph.h
index 5725816f24..8c437c5261 100644
--- a/include/clang/Analysis/CallGraph.h
+++ b/include/clang/Analysis/CallGraph.h
@@ -63,11 +63,14 @@ class CallGraph {
/// CallerCtx maps a caller to its ASTContext.
llvm::DenseMap<CallGraphNode *, ASTContext *> CallerCtx;
- /// Entry node of the call graph.
- // FIXME: find the entry of the graph.
- CallGraphNode *Entry;
+ /// Root node is the 'main' function or 0.
+ CallGraphNode *Root;
+
+ /// ExternalCallingNode has edges to all external functions.
+ CallGraphNode *ExternalCallingNode;
public:
+ CallGraph();
~CallGraph();
typedef FunctionMapTy::iterator iterator;
@@ -78,7 +81,9 @@ public:
const_iterator begin() const { return FunctionMap.begin(); }
const_iterator end() const { return FunctionMap.end(); }
- CallGraphNode *getEntry() { return Entry; }
+ CallGraphNode *getRoot() { return Root; }
+
+ CallGraphNode *getExternalCallingNode() { return ExternalCallingNode; }
void addTU(ASTUnit &AST);
@@ -106,7 +111,7 @@ template <> struct GraphTraits<clang::CallGraph> {
typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
static NodeType *getEntryNode(GraphType *CG) {
- return CG->getEntry();
+ return CG->getExternalCallingNode();
}
static ChildIteratorType child_begin(NodeType *N) {
diff --git a/lib/Analysis/CallGraph.cpp b/lib/Analysis/CallGraph.cpp
index bfc2c0d219..07c2b35349 100644
--- a/lib/Analysis/CallGraph.cpp
+++ b/lib/Analysis/CallGraph.cpp
@@ -58,6 +58,10 @@ void CGBuilder::VisitCallExpr(CallExpr *CE) {
}
}
+CallGraph::CallGraph() : Root(0) {
+ ExternalCallingNode = getOrInsertFunction(Entity());
+}
+
CallGraph::~CallGraph() {
if (!FunctionMap.empty()) {
for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
@@ -80,6 +84,14 @@ void CallGraph::addTU(ASTUnit &AST) {
Entity Ent = Entity::get(FD, Prog);
CallGraphNode *Node = getOrInsertFunction(Ent);
CallerCtx[Node] = &Ctx;
+
+ // If this function has external linkage, anything could call it.
+ if (FD->isGlobal())
+ ExternalCallingNode->addCallee(idx::ASTLocation(), Node);
+
+ // Set root node to 'main' function.
+ if (FD->getNameAsString() == "main")
+ Root = Node;
CGBuilder builder(*this, FD, Ent, Node);
builder.Visit(FD->getBody());
diff --git a/tools/wpa/clang-wpa.cpp b/tools/wpa/clang-wpa.cpp
index dff8cb0b52..e7515ecd24 100644
--- a/tools/wpa/clang-wpa.cpp
+++ b/tools/wpa/clang-wpa.cpp
@@ -65,5 +65,5 @@ int main(int argc, char **argv) {
for (unsigned i = 0, e = TUnits.size(); i != e; ++i)
CG->addTU(*(TUnits[i]->AST));
- CG->dump();
+ CG->ViewCallGraph();
}