1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
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 a '-dot-cfg' analysis pass, which emits the
11 // cfg.<fnname>.dot file for each function in the program, with a graph of the
12 // CFG for that function.
14 // The other main feature of this file is that it implements the
15 // Function::viewCFG method, which is useful for debugging passes which operate
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Analysis/CFGPrinter.h"
22 #include "llvm/Pass.h"
26 struct CFGViewer
: public FunctionPass
{
27 static char ID
; // Pass identifcation, replacement for typeid
28 CFGViewer() : FunctionPass(ID
) {
29 initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
32 virtual bool runOnFunction(Function
&F
) {
37 void print(raw_ostream
&OS
, const Module
* = 0) const {}
39 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
45 char CFGViewer::ID
= 0;
46 INITIALIZE_PASS(CFGViewer
, "view-cfg", "View CFG of function", false, true)
49 struct CFGOnlyViewer
: public FunctionPass
{
50 static char ID
; // Pass identifcation, replacement for typeid
51 CFGOnlyViewer() : FunctionPass(ID
) {
52 initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
55 virtual bool runOnFunction(Function
&F
) {
60 void print(raw_ostream
&OS
, const Module
* = 0) const {}
62 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
68 char CFGOnlyViewer::ID
= 0;
69 INITIALIZE_PASS(CFGOnlyViewer
, "view-cfg-only",
70 "View CFG of function (with no function bodies)", false, true)
73 struct CFGPrinter
: public FunctionPass
{
74 static char ID
; // Pass identification, replacement for typeid
75 CFGPrinter() : FunctionPass(ID
) {
76 initializeCFGPrinterPass(*PassRegistry::getPassRegistry());
79 virtual bool runOnFunction(Function
&F
) {
80 std::string Filename
= "cfg." + F
.getNameStr() + ".dot";
81 errs() << "Writing '" << Filename
<< "'...";
83 std::string ErrorInfo
;
84 raw_fd_ostream
File(Filename
.c_str(), ErrorInfo
);
86 if (ErrorInfo
.empty())
87 WriteGraph(File
, (const Function
*)&F
);
89 errs() << " error opening file for writing!";
94 void print(raw_ostream
&OS
, const Module
* = 0) const {}
96 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
102 char CFGPrinter::ID
= 0;
103 INITIALIZE_PASS(CFGPrinter
, "dot-cfg", "Print CFG of function to 'dot' file",
107 struct CFGOnlyPrinter
: public FunctionPass
{
108 static char ID
; // Pass identification, replacement for typeid
109 CFGOnlyPrinter() : FunctionPass(ID
) {
110 initializeCFGOnlyPrinterPass(*PassRegistry::getPassRegistry());
113 virtual bool runOnFunction(Function
&F
) {
114 std::string Filename
= "cfg." + F
.getNameStr() + ".dot";
115 errs() << "Writing '" << Filename
<< "'...";
117 std::string ErrorInfo
;
118 raw_fd_ostream
File(Filename
.c_str(), ErrorInfo
);
120 if (ErrorInfo
.empty())
121 WriteGraph(File
, (const Function
*)&F
, true);
123 errs() << " error opening file for writing!";
127 void print(raw_ostream
&OS
, const Module
* = 0) const {}
129 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
130 AU
.setPreservesAll();
135 char CFGOnlyPrinter::ID
= 0;
136 INITIALIZE_PASS(CFGOnlyPrinter
, "dot-cfg-only",
137 "Print CFG of function to 'dot' file (with no function bodies)",
140 /// viewCFG - This function is meant for use from the debugger. You can just
141 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
142 /// program, displaying the CFG of the current function. This depends on there
143 /// being a 'dot' and 'gv' program in your path.
145 void Function::viewCFG() const {
146 ViewGraph(this, "cfg" + getNameStr());
149 /// viewCFGOnly - This function is meant for use from the debugger. It works
150 /// just like viewCFG, but it does not include the contents of basic blocks
151 /// into the nodes, just the label. If you are only interested in the CFG t
152 /// his can make the graph smaller.
154 void Function::viewCFGOnly() const {
155 ViewGraph(this, "cfg" + getNameStr(), true);
158 FunctionPass
*llvm::createCFGPrinterPass () {
159 return new CFGPrinter();
162 FunctionPass
*llvm::createCFGOnlyPrinterPass () {
163 return new CFGOnlyPrinter();