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/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"
26 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
27 static std::string getGraphName(const Function *F) {
28 return "CFG for '" + F->getNameStr() + "' function";
31 static std::string getNodeLabel(const BasicBlock *Node,
32 const Function *Graph,
34 if (ShortNames && !Node->getName().empty())
35 return Node->getNameStr() + ":";
38 raw_string_ostream OS(Str);
41 WriteAsOperand(OS, Node, false);
45 if (Node->getName().empty()) {
46 WriteAsOperand(OS, Node, false);
51 std::string OutStr = OS.str();
52 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
54 // Process string output to make it nicer...
55 for (unsigned i = 0; i != OutStr.length(); ++i)
56 if (OutStr[i] == '\n') { // Left justify
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);
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";
77 } // End llvm namespace
81 FunctionPass *createCFGPrinterPass ();
82 FunctionPass *createCFGOnlyPrinterPass ();
83 } // End llvm namespace