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 /// @brief Return true if this function should be processed.
41 /// An implementation of this class my override this function to indicate that
42 /// only certain functions should be viewed.
43 virtual bool processFunction(Function &F) {
47 bool runOnFunction(Function &F) override {
48 if (!processFunction(F))
51 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
52 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
53 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
55 ViewGraph(Graph, Name, IsSimple, Title);
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.addRequired<AnalysisT>();
70 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
71 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
72 class DOTGraphTraitsPrinter : public FunctionPass {
74 DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
75 : FunctionPass(ID), Name(GraphName) {}
77 /// @brief Return true if this function should be processed.
79 /// An implementation of this class my override this function to indicate that
80 /// only certain functions should be printed.
81 virtual bool processFunction(Function &F) {
85 bool runOnFunction(Function &F) override {
86 if (!processFunction(F))
89 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
90 std::string Filename = Name + "." + F.getName().str() + ".dot";
93 errs() << "Writing '" << Filename << "'...";
95 raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
96 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
97 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
100 WriteGraph(File, Graph, IsSimple, Title);
102 errs() << " error opening file for writing!";
108 void getAnalysisUsage(AnalysisUsage &AU) const override {
109 AU.setPreservesAll();
110 AU.addRequired<AnalysisT>();
118 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
119 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
120 class DOTGraphTraitsModuleViewer : public ModulePass {
122 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
123 : ModulePass(ID), Name(GraphName) {}
125 bool runOnModule(Module &M) override {
126 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
127 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
129 ViewGraph(Graph, Name, IsSimple, Title);
134 void getAnalysisUsage(AnalysisUsage &AU) const override {
135 AU.setPreservesAll();
136 AU.addRequired<AnalysisT>();
144 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
145 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
146 class DOTGraphTraitsModulePrinter : public ModulePass {
148 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
149 : ModulePass(ID), Name(GraphName) {}
151 bool runOnModule(Module &M) override {
152 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
153 std::string Filename = Name + ".dot";
156 errs() << "Writing '" << Filename << "'...";
158 raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
159 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
162 WriteGraph(File, Graph, IsSimple, Title);
164 errs() << " error opening file for writing!";
170 void getAnalysisUsage(AnalysisUsage &AU) const override {
171 AU.setPreservesAll();
172 AU.addRequired<AnalysisT>();
179 } // end namespace llvm