1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
17 #include "llvm/Analysis/CFGPrinter.h"
18 #include "llvm/Pass.h"
22 template <class Analysis, bool Simple>
23 class DOTGraphTraitsViewer : public FunctionPass {
25 DOTGraphTraitsViewer(StringRef GraphName, char &ID)
26 : FunctionPass(ID), Name(GraphName) {}
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";
33 ViewGraph(Graph, Name, Simple, Title);
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<Analysis>();
47 template <class Analysis, bool Simple>
48 class DOTGraphTraitsPrinter : public FunctionPass {
50 DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
51 : FunctionPass(ID), Name(GraphName) {}
53 virtual bool runOnFunction(Function &F) {
54 Analysis *Graph = &getAnalysis<Analysis>();
55 std::string Filename = Name + "." + F.getName().str() + ".dot";
56 std::string ErrorInfo;
58 errs() << "Writing '" << Filename << "'...";
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";
64 if (ErrorInfo.empty())
65 WriteGraph(File, Graph, Simple, Title);
67 errs() << " error opening file for writing!";
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.addRequired<Analysis>();
82 template <class Analysis, bool Simple>
83 class DOTGraphTraitsModuleViewer : public ModulePass {
85 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
86 : ModulePass(ID), Name(GraphName) {}
88 virtual bool runOnModule(Module &M) {
89 Analysis *Graph = &getAnalysis<Analysis>();
90 std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
92 ViewGraph(Graph, Name, Simple, Title);
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.addRequired<Analysis>();
106 template <class Analysis, bool Simple>
107 class DOTGraphTraitsModulePrinter : public ModulePass {
109 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
110 : ModulePass(ID), Name(GraphName) {}
112 virtual bool runOnModule(Module &M) {
113 Analysis *Graph = &getAnalysis<Analysis>();
114 std::string Filename = Name + ".dot";
115 std::string ErrorInfo;
117 errs() << "Writing '" << Filename << "'...";
119 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
120 std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
122 if (ErrorInfo.empty())
123 WriteGraph(File, Graph, Simple, Title);
125 errs() << " error opening file for writing!";
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
132 AU.setPreservesAll();
133 AU.addRequired<Analysis>();
140 } // end namespace llvm