2002-10-07 20:38:01 +02:00
|
|
|
//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02: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 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-07 20:38:01 +02: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-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2002-10-07 20:38:01 +02:00
|
|
|
#include "llvm/Pass.h"
|
2003-10-22 18:02:58 +02:00
|
|
|
#include "llvm/Value.h"
|
2002-11-04 03:55:30 +01:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2002-10-07 20:38:01 +02:00
|
|
|
#include <fstream>
|
2004-04-12 07:38:01 +02:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-10-07 20:38:01 +02: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 02:00:37 +02:00
|
|
|
|
2002-10-07 20:38:01 +02:00
|
|
|
if (F.good())
|
|
|
|
WriteGraph(F, GT);
|
|
|
|
else
|
|
|
|
O << " error opening file for writing!";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-04 03:55:30 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Call Graph Printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-04-12 07:38:01 +02:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(CallGraph *F) {
|
|
|
|
return "Call Graph";
|
|
|
|
}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-04-12 07:38:01 +02: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 03:55:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2004-09-20 06:48:05 +02:00
|
|
|
struct CallGraphPrinter : public ModulePass {
|
|
|
|
virtual bool runOnModule(Module &M) {
|
2002-11-04 03:55:30 +01:00
|
|
|
WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(std::ostream &OS) const {}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2002-11-04 03:55:30 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
|
|
|
|
"Print Call Graph to 'dot' file");
|
|
|
|
};
|