CFGPrinter.h revision 6c507926532d609b1225c29878b37c562e4277f2
1//===-- CFGPrinter.h - CFG printer external interface -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines external functions that can be called to explicitly
11// instantiate the CFG printer.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_CFGPRINTER_H
16#define LLVM_ANALYSIS_CFGPRINTER_H
17
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/Support/CFG.h"
22#include "llvm/Support/GraphWriter.h"
23
24namespace llvm {
25template<>
26struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
27
28  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
29
30  static std::string getGraphName(const Function *F) {
31    return "CFG for '" + F->getNameStr() + "' function";
32  }
33
34  static std::string getSimpleNodeLabel(const BasicBlock *Node,
35                                  const Function *Graph) {
36    if (!Node->getName().empty())
37      return Node->getNameStr();
38
39    std::string Str;
40    raw_string_ostream OS(Str);
41
42    WriteAsOperand(OS, Node, false);
43    return OS.str();
44  }
45
46  static std::string getCompleteNodeLabel(const BasicBlock *Node,
47                                          const Function *Graph) {
48    std::string Str;
49    raw_string_ostream OS(Str);
50
51    if (Node->getName().empty()) {
52      WriteAsOperand(OS, Node, false);
53      OS << ":";
54    }
55
56    OS << *Node;
57    std::string OutStr = OS.str();
58    if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
59
60    // Process string output to make it nicer...
61    for (unsigned i = 0; i != OutStr.length(); ++i)
62      if (OutStr[i] == '\n') {                            // Left justify
63        OutStr[i] = '\\';
64        OutStr.insert(OutStr.begin()+i+1, 'l');
65      } else if (OutStr[i] == ';') {                      // Delete comments!
66        unsigned Idx = OutStr.find('\n', i+1);            // Find end of line
67        OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
68        --i;
69      }
70
71    return OutStr;
72  }
73
74  std::string getNodeLabel(const BasicBlock *Node,
75                           const Function *Graph) {
76    if (isSimple())
77      return getSimpleNodeLabel(Node, Graph);
78    else
79      return getCompleteNodeLabel(Node, Graph);
80  }
81
82  static std::string getEdgeSourceLabel(const BasicBlock *Node,
83                                        succ_const_iterator I) {
84    // Label source of conditional branches with "T" or "F"
85    if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
86      if (BI->isConditional())
87        return (I == succ_begin(Node)) ? "T" : "F";
88
89    // Label source of switch edges with the associated value.
90    if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
91      unsigned SuccNo = I.getSuccessorIndex();
92
93      if (SuccNo == 0) return "def";
94
95      std::string Str;
96      raw_string_ostream OS(Str);
97      OS << SI->getCaseValue(SuccNo)->getValue();
98      return OS.str();
99    }
100    return "";
101  }
102};
103} // End llvm namespace
104
105namespace llvm {
106  class FunctionPass;
107  FunctionPass *createCFGPrinterPass ();
108  FunctionPass *createCFGOnlyPrinterPass ();
109} // End llvm namespace
110
111#endif
112