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 /// \brief Default traits class for extracting a graph from an analysis pass.
24 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
25 template <typename AnalysisT, typename GraphT = AnalysisT *>
26 struct DefaultAnalysisGraphTraits {
27 static GraphT getGraph(AnalysisT *A) { return A; }
31 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
32 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
33 class DOTGraphTraitsViewer : public FunctionPass {
35 DOTGraphTraitsViewer(StringRef GraphName, char &ID)
36 : FunctionPass(ID), Name(GraphName) {}
38 virtual bool runOnFunction(Function &F) {
39 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
40 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
41 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
43 ViewGraph(Graph, Name, IsSimple, Title);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addRequired<AnalysisT>();
58 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
59 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
60 class DOTGraphTraitsPrinter : public FunctionPass {
62 DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
63 : FunctionPass(ID), Name(GraphName) {}
65 virtual bool runOnFunction(Function &F) {
66 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
67 std::string Filename = Name + "." + F.getName().str() + ".dot";
68 std::string ErrorInfo;
70 errs() << "Writing '" << Filename << "'...";
72 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
73 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
74 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
76 if (ErrorInfo.empty())
77 WriteGraph(File, Graph, IsSimple, Title);
79 errs() << " error opening file for writing!";
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequired<AnalysisT>();
95 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
96 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
97 class DOTGraphTraitsModuleViewer : public ModulePass {
99 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
100 : ModulePass(ID), Name(GraphName) {}
102 virtual bool runOnModule(Module &M) {
103 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
104 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
106 ViewGraph(Graph, Name, IsSimple, Title);
111 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
112 AU.setPreservesAll();
113 AU.addRequired<AnalysisT>();
121 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
122 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
123 class DOTGraphTraitsModulePrinter : public ModulePass {
125 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
126 : ModulePass(ID), Name(GraphName) {}
128 virtual bool runOnModule(Module &M) {
129 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
130 std::string Filename = Name + ".dot";
131 std::string ErrorInfo;
133 errs() << "Writing '" << Filename << "'...";
135 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
136 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
138 if (ErrorInfo.empty())
139 WriteGraph(File, Graph, IsSimple, Title);
141 errs() << " error opening file for writing!";
147 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.setPreservesAll();
149 AU.addRequired<AnalysisT>();
156 } // end namespace llvm