]> granicus.if.org Git - clang/commitdiff
Add support for viewing the module graph via Graphviz, for debugging
authorDouglas Gregor <dgregor@apple.com>
Tue, 11 Oct 2011 19:27:55 +0000 (19:27 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 11 Oct 2011 19:27:55 +0000 (19:27 +0000)
purposes.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141697 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Serialization/ModuleManager.h
lib/Serialization/ModuleManager.cpp

index 65a5d5e2ec2039a31ad964a948d7c0dab108f523..f86915a79cd00289d8bf8a68e353706aa960b5f6 100644 (file)
@@ -146,6 +146,9 @@ public:
   void visitDepthFirst(bool (*Visitor)(Module &M, bool Preorder, 
                                        void *UserData), 
                        void *UserData);
+  
+  /// \brief View the graphviz representation of the module graph.
+  void viewGraph();
 };
 
 } } // end namespace clang::serialization
index a9fe64d42bdba3ba602cbcb818c5abde67a0b40e..c4b1f7199bedd196bae42ed9c1059e555902d72a 100644 (file)
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/system_error.h"
 
+#ifndef NDEBUG
+#include "llvm/Support/GraphWriter.h"
+#endif
+
 using namespace clang;
 using namespace serialization;
 
@@ -202,3 +206,48 @@ void ModuleManager::visitDepthFirst(bool (*Visitor)(Module &M, bool Preorder,
       return;
   }
 }
+
+#ifndef NDEBUG
+namespace llvm {
+  template<>
+  struct GraphTraits<ModuleManager> {
+    typedef Module NodeType;
+    typedef llvm::SetVector<Module *>::const_iterator ChildIteratorType;
+    typedef ModuleManager::ModuleConstIterator nodes_iterator;
+    
+    static ChildIteratorType child_begin(NodeType *Node) {
+      return Node->Imports.begin();
+    }
+
+    static ChildIteratorType child_end(NodeType *Node) {
+      return Node->Imports.end();
+    }
+    
+    static nodes_iterator nodes_begin(const ModuleManager &Manager) {
+      return Manager.begin();
+    }
+    
+    static nodes_iterator nodes_end(const ModuleManager &Manager) {
+      return Manager.end();
+    }
+  };
+  
+  template<>
+  struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
+    explicit DOTGraphTraits(bool IsSimple = false)
+      : DefaultDOTGraphTraits(IsSimple) { }
+    
+    static bool renderGraphFromBottomUp() {
+      return true;
+    }
+
+    std::string getNodeLabel(Module *M, const ModuleManager&) {
+      return llvm::sys::path::stem(M->FileName);
+    }
+  };
+}
+
+void ModuleManager::viewGraph() {
+  llvm::ViewGraph(*this, "Modules");
+}
+#endif