Chris Lattner | ac859db | 2002-10-07 18:38:01 +0000 | [diff] [blame] | 1 | //===- GraphPrinters.cpp - DOT printers for various graph types -----------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ac859db | 2002-10-07 18:38:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines several printers for various different types of graphs used |
| 11 | // by the LLVM infrastructure. It uses the generic graph interface to convert |
| 12 | // the graph into a .dot graph. These graphs can then be processed with the |
| 13 | // "dot" tool to convert them to postscript or some other suitable format. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "Support/GraphWriter.h" |
| 18 | #include "llvm/Pass.h" |
Chris Lattner | 3b39537 | 2003-10-22 16:02:58 +0000 | [diff] [blame] | 19 | #include "llvm/Value.h" |
Chris Lattner | f748254 | 2002-11-04 02:55:30 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | ac859db | 2002-10-07 18:38:01 +0000 | [diff] [blame] | 21 | #include <fstream> |
Chris Lattner | 27829ec | 2004-04-12 05:38:01 +0000 | [diff] [blame^] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | ac859db | 2002-10-07 18:38:01 +0000 | [diff] [blame] | 24 | template<typename GraphType> |
| 25 | static void WriteGraphToFile(std::ostream &O, const std::string &GraphName, |
| 26 | const GraphType >) { |
| 27 | std::string Filename = GraphName + ".dot"; |
| 28 | O << "Writing '" << Filename << "'..."; |
| 29 | std::ofstream F(Filename.c_str()); |
| 30 | |
| 31 | if (F.good()) |
| 32 | WriteGraph(F, GT); |
| 33 | else |
| 34 | O << " error opening file for writing!"; |
| 35 | O << "\n"; |
| 36 | } |
| 37 | |
| 38 | |
Chris Lattner | f748254 | 2002-11-04 02:55:30 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // Call Graph Printer |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 27829ec | 2004-04-12 05:38:01 +0000 | [diff] [blame^] | 43 | namespace llvm { |
| 44 | template<> |
| 45 | struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits { |
| 46 | static std::string getGraphName(CallGraph *F) { |
| 47 | return "Call Graph"; |
| 48 | } |
| 49 | |
| 50 | static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) { |
| 51 | if (Node->getFunction()) |
| 52 | return ((Value*)Node->getFunction())->getName(); |
| 53 | else |
| 54 | return "Indirect call node"; |
| 55 | } |
| 56 | }; |
| 57 | } |
Chris Lattner | f748254 | 2002-11-04 02:55:30 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | namespace { |
| 61 | struct CallGraphPrinter : public Pass { |
| 62 | virtual bool run(Module &M) { |
| 63 | WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>()); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | void print(std::ostream &OS) const {} |
| 68 | |
| 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 70 | AU.addRequired<CallGraph>(); |
| 71 | AU.setPreservesAll(); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | RegisterAnalysis<CallGraphPrinter> P2("print-callgraph", |
| 76 | "Print Call Graph to 'dot' file"); |
| 77 | }; |