1 //===-- CFGPrinter.h - CFG printer external interface -----------*- C++ -*-===//
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 external functions that can be called to explicitly
11 // instantiate the CFG printer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_CFGPRINTER_H
16 #define LLVM_ANALYSIS_CFGPRINTER_H
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Support/GraphWriter.h"
26 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
28 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
30 static std::string getGraphName(const Function *F) {
31 return "CFG for '" + F->getName().str() + "' function";
34 static std::string getSimpleNodeLabel(const BasicBlock *Node,
36 if (!Node->getName().empty())
37 return Node->getName().str();
40 raw_string_ostream OS(Str);
42 Node->printAsOperand(OS, false);
46 static std::string getCompleteNodeLabel(const BasicBlock *Node,
48 enum { MaxColumns = 80 };
50 raw_string_ostream OS(Str);
52 if (Node->getName().empty()) {
53 Node->printAsOperand(OS, false);
58 std::string OutStr = OS.str();
59 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
61 // Process string output to make it nicer...
63 unsigned LastSpace = 0;
64 for (unsigned i = 0; i != OutStr.length(); ++i) {
65 if (OutStr[i] == '\n') { // Left justify
67 OutStr.insert(OutStr.begin()+i+1, 'l');
70 } else if (OutStr[i] == ';') { // Delete comments!
71 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
72 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
74 } else if (ColNum == MaxColumns) { // Wrap lines.
76 OutStr.insert(LastSpace, "\\l...");
77 ColNum = i - LastSpace;
79 i += 3; // The loop will advance 'i' again.
81 // Else keep trying to find a space.
91 std::string getNodeLabel(const BasicBlock *Node,
92 const Function *Graph) {
94 return getSimpleNodeLabel(Node, Graph);
96 return getCompleteNodeLabel(Node, Graph);
99 static std::string getEdgeSourceLabel(const BasicBlock *Node,
100 succ_const_iterator I) {
101 // Label source of conditional branches with "T" or "F"
102 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
103 if (BI->isConditional())
104 return (I == succ_begin(Node)) ? "T" : "F";
106 // Label source of switch edges with the associated value.
107 if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
108 unsigned SuccNo = I.getSuccessorIndex();
110 if (SuccNo == 0) return "def";
113 raw_string_ostream OS(Str);
114 SwitchInst::ConstCaseIt Case =
115 SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
116 OS << Case.getCaseValue()->getValue();
122 } // End llvm namespace
126 FunctionPass *createCFGPrinterPass ();
127 FunctionPass *createCFGOnlyPrinterPass ();
128 } // End llvm namespace