1 //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
11 // containing the call graph of a module.
13 // There is also a pass available to directly call dotty ('-view-callgraph').
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/Analysis/CallPrinter.h"
19 #include "llvm/Analysis/DOTGraphTraitsPass.h"
26 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
27 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
29 static std::string getGraphName(CallGraph *Graph) {
33 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
34 if (Function *Func = Node->getFunction())
35 return Func->getName();
37 return "external node";
41 } // end llvm namespace
45 struct CallGraphViewer
46 : public DOTGraphTraitsModuleViewer<CallGraph, true> {
50 : DOTGraphTraitsModuleViewer<CallGraph, true>("callgraph", ID) {
51 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
55 struct CallGraphPrinter
56 : public DOTGraphTraitsModulePrinter<CallGraph, true> {
60 : DOTGraphTraitsModulePrinter<CallGraph, true>("callgraph", ID) {
61 initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
65 } // end anonymous namespace
67 char CallGraphViewer::ID = 0;
68 INITIALIZE_PASS(CallGraphViewer, "view-callgraph",
72 char CallGraphPrinter::ID = 0;
73 INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
74 "Print call graph to 'dot' file",
77 // Create methods available outside of this file, to use them
78 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
79 // the link time optimization.
81 ModulePass *llvm::createCallGraphViewerPass() {
82 return new CallGraphViewer();
85 ModulePass *llvm::createCallGraphPrinterPass() {
86 return new CallGraphPrinter();