Home | History | Annotate | Download | only in Analysis
      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_DOT_GRAPHTRAITS_PASS_H
     15 #define LLVM_ANALYSIS_DOT_GRAPHTRAITS_PASS_H
     16 
     17 #include "llvm/Pass.h"
     18 #include "llvm/Analysis/CFGPrinter.h"
     19 
     20 namespace llvm {
     21 template <class Analysis, bool Simple>
     22 struct DOTGraphTraitsViewer : public FunctionPass {
     23   std::string Name;
     24 
     25   DOTGraphTraitsViewer(std::string GraphName, char &ID) : FunctionPass(ID) {
     26     Name = GraphName;
     27   }
     28 
     29   virtual bool runOnFunction(Function &F) {
     30     Analysis *Graph;
     31     std::string Title, GraphName;
     32     Graph = &getAnalysis<Analysis>();
     33     GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
     34     Title = GraphName + " for '" + F.getNameStr() + "' function";
     35     ViewGraph(Graph, Name, Simple, Title);
     36 
     37     return false;
     38   }
     39 
     40   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     41     AU.setPreservesAll();
     42     AU.addRequired<Analysis>();
     43   }
     44 };
     45 
     46 template <class Analysis, bool Simple>
     47 struct DOTGraphTraitsPrinter : public FunctionPass {
     48 
     49   std::string Name;
     50 
     51   DOTGraphTraitsPrinter(std::string GraphName, char &ID)
     52     : FunctionPass(ID) {
     53     Name = GraphName;
     54   }
     55 
     56   virtual bool runOnFunction(Function &F) {
     57     Analysis *Graph;
     58     std::string Filename = Name + "." + F.getNameStr() + ".dot";
     59     errs() << "Writing '" << Filename << "'...";
     60 
     61     std::string ErrorInfo;
     62     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
     63     Graph = &getAnalysis<Analysis>();
     64 
     65     std::string Title, GraphName;
     66     GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
     67     Title = GraphName + " for '" + F.getNameStr() + "' function";
     68 
     69     if (ErrorInfo.empty())
     70       WriteGraph(File, Graph, Simple, Title);
     71     else
     72       errs() << "  error opening file for writing!";
     73     errs() << "\n";
     74     return false;
     75   }
     76 
     77   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     78     AU.setPreservesAll();
     79     AU.addRequired<Analysis>();
     80   }
     81 };
     82 }
     83 #endif
     84