2002-10-07 18:38:01 +00:00
|
|
|
//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-07 18:38:01 +00:00
|
|
|
//
|
|
|
|
// This file defines several printers for various different types of graphs used
|
|
|
|
// by the LLVM infrastructure. It uses the generic graph interface to convert
|
|
|
|
// the graph into a .dot graph. These graphs can then be processed with the
|
|
|
|
// "dot" tool to convert them to postscript or some other suitable format.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2002-10-07 18:38:01 +00:00
|
|
|
#include "llvm/Pass.h"
|
2003-10-22 16:02:58 +00:00
|
|
|
#include "llvm/Value.h"
|
2002-11-04 02:55:30 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2006-11-17 10:05:07 +00:00
|
|
|
#include <iostream>
|
2002-10-07 18:38:01 +00:00
|
|
|
#include <fstream>
|
2004-04-12 05:38:01 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-10-07 18:38:01 +00:00
|
|
|
template<typename GraphType>
|
|
|
|
static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
|
|
|
|
const GraphType >) {
|
|
|
|
std::string Filename = GraphName + ".dot";
|
|
|
|
O << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream F(Filename.c_str());
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-10-07 18:38:01 +00:00
|
|
|
if (F.good())
|
|
|
|
WriteGraph(F, GT);
|
|
|
|
else
|
|
|
|
O << " error opening file for writing!";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-04 02:55:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Call Graph Printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-04-12 05:38:01 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(CallGraph *F) {
|
|
|
|
return "Call Graph";
|
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-04-12 05:38:01 +00:00
|
|
|
static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
|
|
|
|
if (Node->getFunction())
|
|
|
|
return ((Value*)Node->getFunction())->getName();
|
|
|
|
else
|
|
|
|
return "Indirect call node";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2002-11-04 02:55:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2004-09-20 04:48:05 +00:00
|
|
|
struct CallGraphPrinter : public ModulePass {
|
|
|
|
virtual bool runOnModule(Module &M) {
|
2002-11-04 02:55:30 +00:00
|
|
|
WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(std::ostream &OS) const {}
|
2006-08-28 01:02:49 +00:00
|
|
|
void print(std::ostream &OS, const llvm::Module*) const {}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-11-04 02:55:30 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-08-27 22:30:17 +00:00
|
|
|
RegisterPass<CallGraphPrinter> P2("print-callgraph",
|
|
|
|
"Print Call Graph to 'dot' file");
|
2006-05-24 17:04:05 +00:00
|
|
|
}
|