1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/GraphWriter.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Value.h"
20 #include "llvm/Analysis/CallGraph.h"
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());
34 O << " error opening file for writing!";
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
45 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
46 static std::string getGraphName(CallGraph *F) {
50 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
51 if (Node->getFunction())
52 return ((Value*)Node->getFunction())->getName();
54 return "Indirect call node";
61 struct CallGraphPrinter : public ModulePass {
62 virtual bool runOnModule(Module &M) {
63 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
67 void print(std::ostream &OS) const {}
68 void print(std::ostream &OS, const llvm::Module*) const {}
70 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
71 AU.addRequired<CallGraph>();
76 RegisterPass<CallGraphPrinter> P2("print-callgraph",
77 "Print Call Graph to 'dot' file");