[cfe-commits] r76876 - in /cfe/trunk: include/clang/Analysis/CallGraph.h lib/Analysis/CallGraph.cpp tools/wpa/clang-wpa.cpp

Zhongxing Xu xuzhongxing at gmail.com
Thu Jul 23 06:40:15 PDT 2009


Author: zhongxingxu
Date: Thu Jul 23 08:39:38 2009
New Revision: 76876

URL: http://llvm.org/viewvc/llvm-project?rev=76876&view=rev
Log:
Add two nodes to the call graph:
 - Root is the main function or 0.
 - ExternalCallingNode has edges to all external functions.

Modified:
    cfe/trunk/include/clang/Analysis/CallGraph.h
    cfe/trunk/lib/Analysis/CallGraph.cpp
    cfe/trunk/tools/wpa/clang-wpa.cpp

Modified: cfe/trunk/include/clang/Analysis/CallGraph.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/CallGraph.h?rev=76876&r1=76875&r2=76876&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/CallGraph.h (original)
+++ cfe/trunk/include/clang/Analysis/CallGraph.h Thu Jul 23 08:39:38 2009
@@ -63,11 +63,14 @@
   /// 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 @@
   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 @@
   typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
 
   static NodeType *getEntryNode(GraphType *CG) {
-    return CG->getEntry();
+    return CG->getExternalCallingNode();
   }
 
   static ChildIteratorType child_begin(NodeType *N) {

Modified: cfe/trunk/lib/Analysis/CallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CallGraph.cpp?rev=76876&r1=76875&r2=76876&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CallGraph.cpp (original)
+++ cfe/trunk/lib/Analysis/CallGraph.cpp Thu Jul 23 08:39:38 2009
@@ -58,6 +58,10 @@
   }
 }
 
+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 @@
         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());

Modified: cfe/trunk/tools/wpa/clang-wpa.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/wpa/clang-wpa.cpp?rev=76876&r1=76875&r2=76876&view=diff

==============================================================================
--- cfe/trunk/tools/wpa/clang-wpa.cpp (original)
+++ cfe/trunk/tools/wpa/clang-wpa.cpp Thu Jul 23 08:39:38 2009
@@ -65,5 +65,5 @@
   for (unsigned i = 0, e = TUnits.size(); i != e; ++i)
     CG->addTU(*(TUnits[i]->AST));
 
-  CG->dump();
+  CG->ViewCallGraph();
 }





More information about the cfe-commits mailing list