1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Templates to create dotty viewer and printer passes for GraphTraits graphs. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H 15 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H 16 17 #include "llvm/Analysis/CFGPrinter.h" 18 #include "llvm/Pass.h" 19 20 namespace llvm { 21 22 template <class Analysis, bool Simple> 23 class DOTGraphTraitsViewer : public FunctionPass { 24 public: 25 DOTGraphTraitsViewer(StringRef GraphName, char &ID) 26 : FunctionPass(ID), Name(GraphName) {} 27 28 virtual bool runOnFunction(Function &F) { 29 Analysis *Graph = &getAnalysis<Analysis>(); 30 std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph); 31 std::string Title = GraphName + " for '" + F.getName().str() + "' function"; 32 33 ViewGraph(Graph, Name, Simple, Title); 34 35 return false; 36 } 37 38 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 39 AU.setPreservesAll(); 40 AU.addRequired<Analysis>(); 41 } 42 43 private: 44 std::string Name; 45 }; 46 47 template <class Analysis, bool Simple> 48 class DOTGraphTraitsPrinter : public FunctionPass { 49 public: 50 DOTGraphTraitsPrinter(StringRef GraphName, char &ID) 51 : FunctionPass(ID), Name(GraphName) {} 52 53 virtual bool runOnFunction(Function &F) { 54 Analysis *Graph = &getAnalysis<Analysis>(); 55 std::string Filename = Name + "." + F.getName().str() + ".dot"; 56 std::string ErrorInfo; 57 58 errs() << "Writing '" << Filename << "'..."; 59 60 raw_fd_ostream File(Filename.c_str(), ErrorInfo); 61 std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph); 62 std::string Title = GraphName + " for '" + F.getName().str() + "' function"; 63 64 if (ErrorInfo.empty()) 65 WriteGraph(File, Graph, Simple, Title); 66 else 67 errs() << " error opening file for writing!"; 68 errs() << "\n"; 69 70 return false; 71 } 72 73 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 74 AU.setPreservesAll(); 75 AU.addRequired<Analysis>(); 76 } 77 78 private: 79 std::string Name; 80 }; 81 82 template <class Analysis, bool Simple> 83 class DOTGraphTraitsModuleViewer : public ModulePass { 84 public: 85 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID) 86 : ModulePass(ID), Name(GraphName) {} 87 88 virtual bool runOnModule(Module &M) { 89 Analysis *Graph = &getAnalysis<Analysis>(); 90 std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph); 91 92 ViewGraph(Graph, Name, Simple, Title); 93 94 return false; 95 } 96 97 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 98 AU.setPreservesAll(); 99 AU.addRequired<Analysis>(); 100 } 101 102 private: 103 std::string Name; 104 }; 105 106 template <class Analysis, bool Simple> 107 class DOTGraphTraitsModulePrinter : public ModulePass { 108 public: 109 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID) 110 : ModulePass(ID), Name(GraphName) {} 111 112 virtual bool runOnModule(Module &M) { 113 Analysis *Graph = &getAnalysis<Analysis>(); 114 std::string Filename = Name + ".dot"; 115 std::string ErrorInfo; 116 117 errs() << "Writing '" << Filename << "'..."; 118 119 raw_fd_ostream File(Filename.c_str(), ErrorInfo); 120 std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph); 121 122 if (ErrorInfo.empty()) 123 WriteGraph(File, Graph, Simple, Title); 124 else 125 errs() << " error opening file for writing!"; 126 errs() << "\n"; 127 128 return false; 129 } 130 131 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 132 AU.setPreservesAll(); 133 AU.addRequired<Analysis>(); 134 } 135 136 private: 137 std::string Name; 138 }; 139 140 } // end namespace llvm 141 142 #endif 143