CFGPrinter.h revision 641c6f983eb4e40ab23732ab46ffa85bab7f0ba9
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  static std::string getGraphName(const Function *F) {
28    return "CFG for '" + F->getNameStr() + "' function";
29  }
30
31  static std::string getNodeLabel(const BasicBlock *Node,
32                                  const Function *Graph,
33                                  bool ShortNames) {
34    if (ShortNames && !Node->getName().empty())
35      return Node->getNameStr() + ":";
36
37    std::string Str;
38    raw_string_ostream OS(Str);
39
40    if (ShortNames) {
41      WriteAsOperand(OS, Node, false);
42      return OS.str();
43    }
44
45    if (Node->getName().empty()) {
46      WriteAsOperand(OS, Node, false);
47      OS << ":";
48    }
49
50    OS << *Node;
51    std::string OutStr = OS.str();
52    if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
53
54    // Process string output to make it nicer...
55    for (unsigned i = 0; i != OutStr.length(); ++i)
56      if (OutStr[i] == '\n') {                            // Left justify
57        OutStr[i] = '\\';
58        OutStr.insert(OutStr.begin()+i+1, 'l');
59      } else if (OutStr[i] == ';') {                      // Delete comments!
60        unsigned Idx = OutStr.find('\n', i+1);            // Find end of line
61        OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
62        --i;
63      }
64
65    return OutStr;
66  }
67
68  static std::string getEdgeSourceLabel(const BasicBlock *Node,
69                                        succ_const_iterator I) {
70    // Label source of conditional branches with "T" or "F"
71    if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
72      if (BI->isConditional())
73        return (I == succ_begin(Node)) ? "T" : "F";
74
75    // Label source of switch edges with the associated value.
76    if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
77      unsigned SuccNo = I.getSuccessorIndex();
78
79      if (SuccNo == 0) return "def";
80
81      std::string Str;
82      raw_string_ostream OS(Str);
83      OS << SI->getCaseValue(SuccNo)->getValue();
84      return OS.str();
85    }
86    return "";
87  }
88};
89} // End llvm namespace
90
91namespace llvm {
92  class FunctionPass;
93  FunctionPass *createCFGPrinterPass ();
94  FunctionPass *createCFGOnlyPrinterPass ();
95} // End llvm namespace
96
97#endif
98