LLVM API Documentation

DOTGraphTraitsPass.h
Go to the documentation of this file.
00001 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
00015 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
00016 
00017 #include "llvm/Analysis/CFGPrinter.h"
00018 #include "llvm/Pass.h"
00019 
00020 namespace llvm {
00021 
00022 template <class Analysis, bool Simple>
00023 class DOTGraphTraitsViewer : public FunctionPass {
00024 public:
00025   DOTGraphTraitsViewer(StringRef GraphName, char &ID)
00026     : FunctionPass(ID), Name(GraphName) {}
00027 
00028   virtual bool runOnFunction(Function &F) {
00029     Analysis *Graph = &getAnalysis<Analysis>();
00030     std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
00031     std::string Title = GraphName + " for '" + F.getName().str() + "' function";
00032 
00033     ViewGraph(Graph, Name, Simple, Title);
00034 
00035     return false;
00036   }
00037 
00038   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00039     AU.setPreservesAll();
00040     AU.addRequired<Analysis>();
00041   }
00042 
00043 private:
00044   std::string Name;
00045 };
00046 
00047 template <class Analysis, bool Simple>
00048 class DOTGraphTraitsPrinter : public FunctionPass {
00049 public:
00050   DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
00051     : FunctionPass(ID), Name(GraphName) {}
00052 
00053   virtual bool runOnFunction(Function &F) {
00054     Analysis *Graph = &getAnalysis<Analysis>();
00055     std::string Filename = Name + "." + F.getName().str() + ".dot";
00056     std::string ErrorInfo;
00057 
00058     errs() << "Writing '" << Filename << "'...";
00059 
00060     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
00061     std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
00062     std::string Title = GraphName + " for '" + F.getName().str() + "' function";
00063 
00064     if (ErrorInfo.empty())
00065       WriteGraph(File, Graph, Simple, Title);
00066     else
00067       errs() << "  error opening file for writing!";
00068     errs() << "\n";
00069 
00070     return false;
00071   }
00072 
00073   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00074     AU.setPreservesAll();
00075     AU.addRequired<Analysis>();
00076   }
00077 
00078 private:
00079   std::string Name;
00080 };
00081 
00082 template <class Analysis, bool Simple>
00083 class DOTGraphTraitsModuleViewer : public ModulePass {
00084 public:
00085   DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
00086     : ModulePass(ID), Name(GraphName) {}
00087 
00088   virtual bool runOnModule(Module &M) {
00089     Analysis *Graph = &getAnalysis<Analysis>();
00090     std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
00091 
00092     ViewGraph(Graph, Name, Simple, Title);
00093 
00094     return false;
00095   }
00096 
00097   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00098     AU.setPreservesAll();
00099     AU.addRequired<Analysis>();
00100   }
00101 
00102 private:
00103   std::string Name;
00104 };
00105 
00106 template <class Analysis, bool Simple>
00107 class DOTGraphTraitsModulePrinter : public ModulePass {
00108 public:
00109   DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
00110     : ModulePass(ID), Name(GraphName) {}
00111 
00112   virtual bool runOnModule(Module &M) {
00113     Analysis *Graph = &getAnalysis<Analysis>();
00114     std::string Filename = Name + ".dot";
00115     std::string ErrorInfo;
00116 
00117     errs() << "Writing '" << Filename << "'...";
00118 
00119     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
00120     std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
00121 
00122     if (ErrorInfo.empty())
00123       WriteGraph(File, Graph, Simple, Title);
00124     else
00125       errs() << "  error opening file for writing!";
00126     errs() << "\n";
00127 
00128     return false;
00129   }
00130 
00131   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00132     AU.setPreservesAll();
00133     AU.addRequired<Analysis>();
00134   }
00135 
00136 private:
00137   std::string Name;
00138 };
00139 
00140 } // end namespace llvm
00141 
00142 #endif