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"
19 #include "llvm/Support/FileSystem.h"
23 /// \brief Default traits class for extracting a graph from an analysis pass.
25 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
26 template <typename AnalysisT, typename GraphT = AnalysisT *>
27 struct DefaultAnalysisGraphTraits {
28 static GraphT getGraph(AnalysisT *A) { return A; }
32 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
33 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
34 class DOTGraphTraitsViewer : public FunctionPass {
36 DOTGraphTraitsViewer(StringRef GraphName, char &ID)
37 : FunctionPass(ID), Name(GraphName) {}
39 bool runOnFunction(Function &F) override {
40 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
41 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
42 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
44 ViewGraph(Graph, Name, IsSimple, Title);
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.addRequired<AnalysisT>();
59 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
60 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
61 class DOTGraphTraitsPrinter : public FunctionPass {
63 DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
64 : FunctionPass(ID), Name(GraphName) {}
66 bool runOnFunction(Function &F) override {
67 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
68 std::string Filename = Name + "." + F.getName().str() + ".dot";
71 errs() << "Writing '" << Filename << "'...";
73 raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
74 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
75 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
78 WriteGraph(File, Graph, IsSimple, Title);
80 errs() << " error opening file for writing!";
86 void getAnalysisUsage(AnalysisUsage &AU) const override {
88 AU.addRequired<AnalysisT>();
96 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
97 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
98 class DOTGraphTraitsModuleViewer : public ModulePass {
100 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
101 : ModulePass(ID), Name(GraphName) {}
103 bool runOnModule(Module &M) override {
104 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
105 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
107 ViewGraph(Graph, Name, IsSimple, Title);
112 void getAnalysisUsage(AnalysisUsage &AU) const override {
113 AU.setPreservesAll();
114 AU.addRequired<AnalysisT>();
122 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
123 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
124 class DOTGraphTraitsModulePrinter : public ModulePass {
126 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
127 : ModulePass(ID), Name(GraphName) {}
129 bool runOnModule(Module &M) override {
130 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
131 std::string Filename = Name + ".dot";
134 errs() << "Writing '" << Filename << "'...";
136 raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
137 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
140 WriteGraph(File, Graph, IsSimple, Title);
142 errs() << " error opening file for writing!";
148 void getAnalysisUsage(AnalysisUsage &AU) const override {
149 AU.setPreservesAll();
150 AU.addRequired<AnalysisT>();
157 } // end namespace llvm