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