blob: 34c937f0d4d52b6db556871a36eeb8df567fcdd7 [file] [log] [blame]
Chris Lattnerac859db2002-10-07 18:38:01 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
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 Lattnerac859db2002-10-07 18:38:01 +00009//
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 Lattner3b395372003-10-22 16:02:58 +000019#include "llvm/Value.h"
Chris Lattnerf7482542002-11-04 02:55:30 +000020#include "llvm/Analysis/CallGraph.h"
Chris Lattnerac859db2002-10-07 18:38:01 +000021#include <fstream>
22
Chris Lattnerac859db2002-10-07 18:38:01 +000023template<typename GraphType>
24static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
25 const GraphType &GT) {
26 std::string Filename = GraphName + ".dot";
27 O << "Writing '" << Filename << "'...";
28 std::ofstream F(Filename.c_str());
29
30 if (F.good())
31 WriteGraph(F, GT);
32 else
33 O << " error opening file for writing!";
34 O << "\n";
35}
36
37
Chris Lattnerf7482542002-11-04 02:55:30 +000038//===----------------------------------------------------------------------===//
39// Call Graph Printer
40//===----------------------------------------------------------------------===//
41
42template<>
43struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
44 static std::string getGraphName(CallGraph *F) {
45 return "Call Graph";
46 }
47
48 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
49 if (Node->getFunction())
Chris Lattner3b395372003-10-22 16:02:58 +000050 return ((Value*)Node->getFunction())->getName();
Chris Lattnerf7482542002-11-04 02:55:30 +000051 else
52 return "Indirect call node";
53 }
54};
55
56
57namespace {
58 struct CallGraphPrinter : public Pass {
59 virtual bool run(Module &M) {
60 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
61 return false;
62 }
63
64 void print(std::ostream &OS) const {}
65
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<CallGraph>();
68 AU.setPreservesAll();
69 }
70 };
71
72 RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
73 "Print Call Graph to 'dot' file");
74};