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 {
28 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
30 static std::string getGraphName(const Function *F) {
31 return "CFG for '" + F->getNameStr() + "' function";
34 static std::string getNodeLabel(const BasicBlock *Node,
35 const Function *Graph,
37 if (ShortNames && !Node->getName().empty())
38 return Node->getNameStr();
41 raw_string_ostream OS(Str);
44 WriteAsOperand(OS, Node, false);
48 if (Node->getName().empty()) {
49 WriteAsOperand(OS, Node, false);
54 std::string OutStr = OS.str();
55 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
57 // Process string output to make it nicer...
58 for (unsigned i = 0; i != OutStr.length(); ++i)
59 if (OutStr[i] == '\n') { // Left justify
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);
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";
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();
82 if (SuccNo == 0) return "def";
85 raw_string_ostream OS(Str);
86 OS << SI->getCaseValue(SuccNo)->getValue();
92 } // End llvm namespace
96 FunctionPass *createCFGPrinterPass ();
97 FunctionPass *createCFGOnlyPrinterPass ();
98 } // End llvm namespace