LLVM API Documentation

CallPrinter.cpp
Go to the documentation of this file.
00001 //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
00011 // containing the call graph of a module.
00012 //
00013 // There is also a pass available to directly call dotty ('-view-callgraph').
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Analysis/CallGraph.h"
00018 #include "llvm/Analysis/CallPrinter.h"
00019 #include "llvm/Analysis/DOTGraphTraitsPass.h"
00020 
00021 using namespace llvm;
00022 
00023 namespace llvm {
00024 
00025 template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
00026   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
00027 
00028   static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
00029 
00030   std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
00031     if (Function *Func = Node->getFunction())
00032       return Func->getName();
00033 
00034     return "external node";
00035   }
00036 };
00037 
00038 struct AnalysisCallGraphWrapperPassTraits {
00039   static CallGraph *getGraph(CallGraphWrapperPass *P) {
00040     return &P->getCallGraph();
00041   }
00042 };
00043 
00044 } // end llvm namespace
00045 
00046 namespace {
00047 
00048 struct CallGraphViewer
00049     : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
00050                                         AnalysisCallGraphWrapperPassTraits> {
00051   static char ID;
00052 
00053   CallGraphViewer()
00054       : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
00055                                    AnalysisCallGraphWrapperPassTraits>(
00056             "callgraph", ID) {
00057     initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
00058   }
00059 };
00060 
00061 struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<
00062                               CallGraphWrapperPass, true, CallGraph *,
00063                               AnalysisCallGraphWrapperPassTraits> {
00064   static char ID;
00065 
00066   CallGraphPrinter()
00067       : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
00068                                     AnalysisCallGraphWrapperPassTraits>(
00069             "callgraph", ID) {
00070     initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
00071   }
00072 };
00073 
00074 } // end anonymous namespace
00075 
00076 char CallGraphViewer::ID = 0;
00077 INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
00078                 false)
00079 
00080 char CallGraphPrinter::ID = 0;
00081 INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
00082                 "Print call graph to 'dot' file", false, false)
00083 
00084 // Create methods available outside of this file, to use them
00085 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
00086 // the link time optimization.
00087 
00088 ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
00089 
00090 ModulePass *llvm::createCallGraphPrinterPass() {
00091   return new CallGraphPrinter();
00092 }