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

Zhongxing Xu xuzhongxing at gmail.com
Thu Jul 23 02:05:18 PDT 2009


Author: zhongxingxu
Date: Thu Jul 23 04:04:23 2009
New Revision: 76874

URL: http://llvm.org/viewvc/llvm-project?rev=76874&view=rev
Log:
Add template specializations to view the call graph in dot format.
 - change the DenseMap used in callgraph to std::map, since DenseMap cannot
   be used with mapped_iterator and friends.

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

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

==============================================================================
--- cfe/trunk/include/clang/Analysis/CallGraph.h (original)
+++ cfe/trunk/include/clang/Analysis/CallGraph.h Thu Jul 23 04:04:23 2009
@@ -19,7 +19,10 @@
 #include "clang/Index/Program.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/GraphTraits.h"
+#include "llvm/ADT/STLExtras.h"
 #include <vector>
+#include <map>
 
 namespace clang {
 
@@ -45,14 +48,14 @@
 
   bool hasCallee() const { return begin() != end(); }
 
-  std::string getName() { return F.getPrintableName(); }
+  std::string getName() const { return F.getPrintableName(); }
 };
 
 class CallGraph {
   /// Program manages all Entities.
   idx::Program Prog;
 
-  typedef llvm::DenseMap<idx::Entity, CallGraphNode *> FunctionMapTy;
+  typedef std::map<idx::Entity, CallGraphNode *> FunctionMapTy;
 
   /// FunctionMap owns all CallGraphNodes.
   FunctionMapTy FunctionMap;
@@ -60,6 +63,10 @@
   /// 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;
+
 public:
   ~CallGraph();
 
@@ -71,6 +78,8 @@
   const_iterator begin() const { return FunctionMap.begin(); }
   const_iterator end()   const { return FunctionMap.end();   }
 
+  CallGraphNode *getEntry() { return Entry; }
+
   void addTU(ASTUnit &AST);
 
   idx::Program &getProgram() { return Prog; }
@@ -79,8 +88,51 @@
 
   void print(llvm::raw_ostream &os);
   void dump();
+
+  void ViewCallGraph() const;
+};
+
+} // end clang namespace
+
+namespace llvm {
+
+template <> struct GraphTraits<clang::CallGraph> {
+  typedef clang::CallGraph GraphType;
+  typedef clang::CallGraphNode NodeType;
+
+  typedef std::pair<clang::idx::ASTLocation, NodeType*> CGNPairTy;
+  typedef std::pointer_to_unary_function<CGNPairTy, NodeType*> CGNDerefFun;
+
+  typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
+
+  static NodeType *getEntryNode(GraphType *CG) {
+    return CG->getEntry();
+  }
+
+  static ChildIteratorType child_begin(NodeType *N) {
+    return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
+  }
+  static ChildIteratorType child_end(NodeType *N) {
+    return map_iterator(N->end(), CGNDerefFun(CGNDeref));
+  }
+
+  typedef std::pair<clang::idx::Entity, NodeType*> PairTy;
+  typedef std::pointer_to_unary_function<PairTy, NodeType*> DerefFun;
+
+  typedef mapped_iterator<GraphType::const_iterator, DerefFun> nodes_iterator;
+
+  static nodes_iterator nodes_begin(const GraphType &CG) {
+    return map_iterator(CG.begin(), DerefFun(CGDeref));
+  }
+  static nodes_iterator nodes_end(const GraphType &CG) {
+    return map_iterator(CG.end(), DerefFun(CGDeref));
+  }
+
+  static NodeType *CGNDeref(CGNPairTy P) { return P.second; }
+
+  static NodeType *CGDeref(PairTy P) { return P.second; }
 };
 
-}
+} // end llvm namespace
 
 #endif

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

==============================================================================
--- cfe/trunk/lib/Analysis/CallGraph.cpp (original)
+++ cfe/trunk/lib/Analysis/CallGraph.cpp Thu Jul 23 04:04:23 2009
@@ -16,6 +16,8 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/StmtVisitor.h"
 
+#include "llvm/Support/GraphWriter.h"
+
 using namespace clang;
 using namespace idx;
 
@@ -111,3 +113,22 @@
 void CallGraph::dump() {
   print(llvm::errs());
 }
+
+void CallGraph::ViewCallGraph() const {
+  llvm::ViewGraph(*this, "CallGraph");
+}
+
+namespace llvm {
+
+template <> 
+struct DOTGraphTraits<CallGraph> : public DefaultDOTGraphTraits {
+
+  static std::string getNodeLabel(const CallGraphNode *Node, 
+                                  const CallGraph &CG, bool ShortNames) {
+    return Node->getName();
+    
+  }
+
+};
+
+}





More information about the cfe-commits mailing list